Ejemplo n.º 1
0
        private void ResetTraining()
        {
            _patternStorage = null;
            panelTrainingImages.Children.Clear();

            if (_images.Count == 0)
            {
                return;
            }

            #region choose images

            int numImages;
            if (!int.TryParse(txtTrainCount.Text, out numImages))
            {
                MessageBox.Show("Couldn't parse count as an integer", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            numImages = Math.Min(_images.Count, numImages);

            var trainImages = UtilityCore.RandomRange(0, _images.Count, numImages).
                              Select(o => new
            {
                File = _images[o],
                Conv = GetTrainingImage(_images[o], _trainKernel),
            }).
                              ToArray();

            #endregion

            //_patternStorage = new RandomPatternStorage();
            _patternStorage = new Hopfield(IMAGESIZE * IMAGESIZE, 0, 1, .9);

            #region show thumbnails

            // Display thumbnails
            //< !--Run them through a KMeans, then sort in 1D-- >
            //< !--Show full resolution over the canvas on mouseover-- >
            //< !--Show full resolution under the canvas on click-- >

            foreach (var trainImage in trainImages)
            {
                double widthHeight    = Math.Sqrt(trainImage.Conv.Length);  // they should be square
                int    widthHeightInt = widthHeight.ToInt_Round();
                if (!Math1D.IsNearValue(widthHeight, widthHeightInt))
                {
                    throw new ApplicationException("Expected square images");
                }

                double[] imageConv = trainImage.Conv;
                imageConv = _patternStorage.Convert_Local_External(imageConv);

                BitmapSource source;
                //if (isColor)
                //{
                //    source = UtilityWPF.GetBitmap_RGB(example, width, height);
                //}
                //else
                //{
                source = UtilityWPF.GetBitmap(imageConv, widthHeightInt, widthHeightInt);
                //}

                Image image = new Image()
                {
                    Source = source,
                    Width  = source.PixelWidth,     // if this isn't set, the image will take up all of the width, and be huge
                    Height = source.PixelHeight,
                    Margin = new Thickness(8),
                };

                panelTrainingImages.Children.Add(image);
            }

            #endregion

            _patternStorage.AddItems(trainImages.Select(o => o.Conv).ToArray());
        }
        private void ResetTraining()
        {
            _patternStorage = null;
            panelTrainingImages.Children.Clear();

            if (_images.Count == 0)
            {
                return;
            }

            #region choose images

            int numImages;
            if (!int.TryParse(txtTrainCount.Text, out numImages))
            {
                MessageBox.Show("Couldn't parse count as an integer", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            numImages = Math.Min(_images.Count, numImages);

            var trainImages = UtilityCore.RandomRange(0, _images.Count, numImages).
                Select(o => new
                {
                    File = _images[o],
                    Conv = GetTrainingImage(_images[o], _trainKernel),
                }).
                ToArray();

            #endregion

            //_patternStorage = new RandomPatternStorage();
            _patternStorage = new Hopfield(IMAGESIZE * IMAGESIZE, 0, 1, .9);

            #region show thumbnails

            // Display thumbnails
            //< !--Run them through a KMeans, then sort in 1D-- >
            //< !--Show full resolution over the canvas on mouseover-- >
            //< !--Show full resolution under the canvas on click-- >

            foreach (var trainImage in trainImages)
            {
                double widthHeight = Math.Sqrt(trainImage.Conv.Length);     // they should be square
                int widthHeightInt = widthHeight.ToInt_Round();
                if (!Math1D.IsNearValue(widthHeight, widthHeightInt))
                {
                    throw new ApplicationException("Expected square images");
                }

                double[] imageConv = trainImage.Conv;
                imageConv = _patternStorage.Convert_Local_External(imageConv);

                BitmapSource source;
                //if (isColor)
                //{
                //    source = UtilityWPF.GetBitmap_RGB(example, width, height);
                //}
                //else
                //{
                source = UtilityWPF.GetBitmap(imageConv, widthHeightInt, widthHeightInt);
                //}

                Image image = new Image()
                {
                    Source = source,
                    Width = source.PixelWidth,      // if this isn't set, the image will take up all of the width, and be huge
                    Height = source.PixelHeight,
                    Margin = new Thickness(8),
                };

                panelTrainingImages.Children.Add(image);
            }

            #endregion

            _patternStorage.AddItems(trainImages.Select(o => o.Conv).ToArray());
        }