Esempio n. 1
0
 public static MnistDataset Read(string path)
 {
     return(new MnistDataset()
     {
         TrainingLabels = MnistLabels.Read(Path.Combine(path, "train-labels-idx1-ubyte.gz")),
         TrainingImages = MnistImages.Read(Path.Combine(path, "train-images-idx3-ubyte.gz")),
         TestLabels = MnistLabels.Read(Path.Combine(path, "t10k-labels-idx1-ubyte.gz")),
         TestImages = MnistImages.Read(Path.Combine(path, "t10k-images-idx3-ubyte.gz")),
     });
 }
Esempio n. 2
0
        public static MnistImages Read(string path)
        {
            Stream stream = File.OpenRead(path);

            if (path.EndsWith(".gz"))
            {
                stream = new GZipInputStream(stream);
            }

            using (var reader = new BinaryReader(stream))
            {
                var magic = reader.ReadInt32BigEndian();
                if (magic != 0x803)
                {
                    throw new InvalidOperationException();
                }

                var count   = reader.ReadInt32BigEndian();
                var rows    = reader.ReadInt32BigEndian();
                var columns = reader.ReadInt32BigEndian();
                var result  = new MnistImages()
                {
                    Count  = count,
                    Images = new NDArray <byte>(new Shape(count, rows, columns))
                };

                for (var i = 0; i < count; i++)
                {
                    for (var r = 0; r < rows; r++)
                    {
                        for (var c = 0; c < columns; c++)
                        {
                            var b = reader.ReadByte();
                            result.Images[i, r, c] = b;
                        }
                    }
                }

                return(result);
            }
        }