public MnistImage [] Read(int batchSize) { var result = new MnistImage [batchSize]; if (start + batchSize < source.Length) { Array.Copy(source, start, result, 0, batchSize); start += batchSize; } else { var firstLength = source.Length - start; Array.Copy(source, start, result, 0, firstLength); Array.Copy(source, 0, result, firstLength, batchSize - firstLength); start = firstLength; } return(result); }
MnistImage [] ExtractImages(Stream input, string file) { using (var gz = new GZipStream(input, CompressionMode.Decompress)) { if (Read32(gz) != 2051) { throw new Exception("Invalid magic number found on the MNIST " + file); } var count = Read32(gz); var rows = Read32(gz); var cols = Read32(gz); var result = new MnistImage [count]; for (int i = 0; i < count; i++) { var size = rows * cols; var data = new byte [size]; gz.Read(data, 0, size); result [i] = new MnistImage(cols, rows, data); } return(result); } }