// ==== UI methods

        private void button1_Click(object sender, EventArgs e) // Load data
        {
            this.pixelFile   = textBox1.Text;
            this.labelFile   = textBox2.Text;
            this.trainImages = MnistDataLoader.LoadData(pixelFile, labelFile);
            listBox1.Items.Add("MNIST images loaded into memory");
        }
Esempio n. 2
0
        public MnistData()
        {
            MnistDataLoader mnistDataLoader = new MnistDataLoader();

            //訓練用データ
            Real[][] x      = new Real[mnistDataLoader.TrainData.Length][];
            Real[]   xLabel = new Real[mnistDataLoader.TrainData.Length];

            for (int i = 0; i < mnistDataLoader.TrainData.Length; i++)
            {
                x[i] = new Real[1 * 28 * 28];

                for (int j = 0; j < mnistDataLoader.TrainData[i].Length; j++)
                {
                    x[i][j] = mnistDataLoader.TrainData[i][j] / 255.0;
                }

                xLabel[i] = mnistDataLoader.TrainLabel[i];
            }

            this.Train = new LabeledDataSet(x, xLabel, new[] { 1, 28, 28 });


            //評価用データ
            Real[][] y      = new Real[mnistDataLoader.TeachData.Length][];
            Real[]   yLabel = new Real[mnistDataLoader.TeachData.Length];

            for (int i = 0; i < mnistDataLoader.TeachData.Length; i++)
            {
                y[i] = new Real[1 * 28 * 28];

                for (int j = 0; j < mnistDataLoader.TeachData[i].Length; j++)
                {
                    y[i][j] = mnistDataLoader.TeachData[i][j] / 255.0;
                }

                yLabel[i] = mnistDataLoader.TeachLabel[i];
            }

            this.Eval = new LabeledDataSet(y, yLabel, new[] { 1, 28, 28 });
        }
Esempio n. 3
0
        public void LoadData(Action <int> progressUpdater)
        {
            if (File.Exists(jsonFilePath))
            {
                var deserialized = JsonConvert.DeserializeObject <NeuralNetwork>(File.ReadAllText(jsonFilePath));
                network = deserialized;
                progressUpdater.Invoke(100);
                return;
            }
            network = new NeuralNetwork(inputLength, 100, 10, 0.3);



            var database  = MnistDataLoader.LoadData(pixelFile, labelFile);
            var toDoubles = database.Select(i => i.pixels.SelectMany(s => s)
                                            .Select(
                                                byt => DigitImage.ConvertGrayScaleByteToDouble(byt))).ToArray();
            //Test2(toDoubles, database);


            var targets = database.Select(i => Convert.ToInt32(i.label)).ToArray();



            var percent = 0;

            for (var index = 0; index < toDoubles.Length; index++)
            {
                var doubleArray   = toDoubles[index];
                var correctAnswer = targets[index];
                var target        = Enumerable.Range(0, 10).Select(x => 0.01).ToArray();
                target[correctAnswer] = 0.99;

                percent = NotifyDataLoadingProgress(index, toDoubles.Length, percent, progressUpdater);
                network.Train(doubleArray.ToArray(), target);
            }
            var networkJson = JsonConvert.SerializeObject(network);

            File.WriteAllText(jsonFilePath, networkJson);
        }