Beispiel #1
0
        public void TestNetwork(MNistImage[] images, CancellationToken cancellation)
        {
            Debug.AssertNotNull(images);
            Debug.AssertNotNull(cancellation);

            if (_statRefresher != null && _statRefresher.Started) return;

            _cancelTrainToken = cancellation;

            var normalImages = new NormalizedImage[images.Length];
            for (var i = 0; i < images.Length; i++)
            {
                normalImages[i] = new NormalizedImage(images[i]);
                normalImages[i].PadCanvas(Network.InputImageWidth, Network.InputImageHeight);
            }

            _testInfoBuffer.Clear();
            _imageProcessed = 0;
            _errors.Clear();
            _totalMSE = 0.0;

            _statRefresher = new ActionRepeater(PrintTestingInfo, 1000.0);
            _statRefresher.Start(false);

            TestProcess(normalImages);

            _statRefresher.Stop(true);
            History = txtStatistics.Text;
        }
        public static MNistImage[] GetImageSet(string labelsPath, string imagesPath)
        {
            Debug.Assert(labelsPath != null && imagesPath != null);

            var imagesBytes = File.ReadAllBytes(imagesPath);
            if (ConvertFromBigEndian(imagesBytes, 0) != Idx3MagicNumber)
                throw new FormatException("Неверный формат файла изображений");

            var imagesCount = ConvertFromBigEndian(imagesBytes, sizeof (int));
            var imageHeight = ConvertFromBigEndian(imagesBytes, sizeof (int)*2);
            var imageWidth = ConvertFromBigEndian(imagesBytes, sizeof (int)*3);
            var imageLength = imageWidth*imageHeight;
            if (imagesCount != (imagesBytes.Length - Idx3DataOffset)/imageLength)
                throw new FormatException("Файл изображений поврежден");

            var labelsBytes = File.ReadAllBytes(labelsPath);
            if (ConvertFromBigEndian(labelsBytes, 0) != Idx1MagicNumber)
                throw new FormatException("Неверный формат файла подписей");

            var labelsCount = ConvertFromBigEndian(labelsBytes, sizeof (int));
            if (labelsCount != labelsBytes.Length - Idx1DataOffset)
                throw new FormatException("Файл подписей поврежден");

            if (imagesCount != labelsCount)
                throw new Exception("Количество изображений не совпадает с количеством подписей");

            // получаем все изображения
            var images = new MNistImage[imagesCount];
            for (var i = 0; i < images.Length; i++)
            {
                var label = (int) labelsBytes[Idx1DataOffset + i];
                Debug.Assert(label < 10, label.ToString());

                var imageBytes = new byte[imageWidth][];
                for (var r = 0; r < imageHeight; r++)
                {
                    imageBytes[r] = new byte[imageHeight];
                    Buffer.BlockCopy(imagesBytes, Idx3DataOffset + i*imageLength + r*imageWidth,
                                     imageBytes[r], 0, imageWidth);
                }

                images[i] = new MNistImage(label, imageBytes);
            }

            return images;
        }
Beispiel #3
0
        public void TrainNetwork(MNistImage[] images, CancellationToken cancellation)
        {
            Debug.AssertNotNull(images);
            Debug.Assert(cancellation != null);
            if (_statRefresher != null && _statRefresher.Started) return;

            _cancelTrainToken = cancellation;

            var normalImages = new NormalizedImage[images.Length];
            for (var i = 0; i < images.Length; i++)
                normalImages[i] = new NormalizedImage(images[i]);

            _statRefresher = new ActionRepeater(PrintTrainingInfo, 1000.0);
            _statRefresher.Start(false);

            TrainProcess(normalImages);

            _statRefresher.Stop(true);
            History = txtStatistics.Text;
        }