//here is where the magic starts private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Image File|*.jpg;*.bmp;*.jpeg"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { clearItem(); Bitmap img = new Bitmap(ofd.FileName); pictureBrowsed.Image = img; img = mainForm.Preprocess(img); ImageToArray imageToArray = new ImageToArray(); double[] input; imageToArray.Convert(img, out input); var pcares = pca.Transform(input); dn.Compute(pcares); var index = dn.GetWinner(); var similarIndex = classMap[index]; foreach (var item in similarIndex) { showItem(item); } } }
static void Learning() { for (int i = 0; i < iterationsBetweenDrawing; i++) { learning.Run(Inputs[rnd.Next(Inputs.Length)]); } map = new MapElement[task.NetworkWidth, task.NetworkHeight]; int number = 0; for (int x = 0; x < task.NetworkWidth; x++) { for (int y = 0; y < task.NetworkHeight; y++) { var neuron = network.Layers[0].Neurons[x * task.NetworkHeight + y]; map[x, y] = new MapElement { X = (float)neuron.Weights[0], Y = (float)neuron.Weights[1], Id = number++ }; } } foreach (var e in Inputs) { network.Compute(e); var winner = network.GetWinner(); map[winner / task.NetworkHeight, winner % task.NetworkHeight].IsActive = true; } }
/// <summary> /// Runs learning iteration. /// </summary> /// /// <param name="input">Input vector.</param> /// /// <returns>Returns learning error - summary absolute difference between neurons' /// weights and appropriate inputs. The difference is measured according to the neurons /// distance to the winner neuron.</returns> /// /// <remarks><para>The method runs one learning iterations - finds winner neuron (the neuron /// which has weights with values closest to the specified input vector) and updates its weight /// (as well as weights of neighbor neurons) in the way to decrease difference with the specified /// input vector.</para></remarks> /// public double Run(double[] input) { double error = 0.0; // compute the network network.Compute(input); int winner = network.GetWinner( ); // get layer of the network Layer layer = network.Layers[0]; // walk through all neurons of the layer for (int j = 0; j < layer.Neurons.Length; j++) { Neuron neuron = layer.Neurons[j]; // update factor double factor = Math.Exp(-distance[Math.Abs(j - winner)] / squaredRadius2); // update weights of the neuron for (int i = 0; i < neuron.Weights.Length; i++) { // calculate the error double e = (input[i] - neuron.Weights[i]) * factor; error += Math.Abs(e); // update weight neuron.Weights[i] += e * learningRate; } } return(error); }
/// <summary> /// Implementation of the divergence operation. /// </summary> protected override void diverge() { //convert all the modalities in a single vector double[] realSignal; double[] predictedSignal; getConcatenatedModalities(out realSignal, out predictedSignal); //Generate the prediction double[] output = network.Compute(realSignal); Neuron winner = network.Layers[0].Neurons[network.GetWinner()]; double[] networkPrediction = new double[InputCount]; for (int i = 0; i < InputCount; i++) { networkPrediction[i] = winner.Weights[i]; } //Distribute it over the Signal setConcatenatedModalities(null, networkPrediction); //Proceed to learning if (!learningLocked && learningRate > 0) { teacher.Run(realSignal); } //todo : copy the neuron activities to MNNode output }
private void button1_Click(object sender, EventArgs e) { listView1.Items.Clear(); imageList1.Images.Clear(); openFileDialog1.Multiselect = false; openFileDialog1.Filter = "Images Files (*.jpg, *jpeg, *.png) | *.jpg; *.jpeg; *.png"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { Bitmap img1 = new Bitmap(openFileDialog1.FileName); pictureBox1.Image = img1; } Bitmap img = new Bitmap(pictureBox1.Image); img = Data.getInstance().preprocessing(img); double[] input = new double[10 * 10]; ImageToArray converter = new ImageToArray(0, 1); converter.Convert(img, out input); double[] input_from_pca = pca.Transform(input); som_network.Compute(input_from_pca); double winner = som_network.GetWinner(); for (int i = 0; i < Data.getInstance().images.Count; i++) { Bitmap img2 = Data.getInstance().preprocessing(Data.getInstance().images[i]); double[] input2 = new double[10 * 10]; ImageToArray converter2 = new ImageToArray(0, 1); converter2.Convert(img2, out input2); double[] input_from_pca2 = pca.Transform(input2); som_network.Compute(input_from_pca2); double winner2 = som_network.GetWinner(); if (winner == winner2) { imageList1.Images.Add(Data.getInstance().images[i]); listView1.Items.Add(Data.getInstance().filenames[i], imageList1.Images.Count - 1); } } MessageBox.Show("Find Simillar Item Success"); }
/// <summary> /// Runs learning iteration. /// </summary> /// /// <param name="input">Input vector.</param> /// /// <returns>Returns learning error - summary absolute difference between neurons' weights /// and appropriate inputs. The difference is measured according to the neurons /// distance to the winner neuron.</returns> /// /// <remarks><para>The method runs one learning iterations - finds winner neuron (the neuron /// which has weights with values closest to the specified input vector) and updates its weight /// (as well as weights of neighbor neurons) in the way to decrease difference with the specified /// input vector.</para></remarks> /// public double Run(double[] input) { double error = 0.0; // compute the network network.Compute(input); int winner = network.GetWinner( ); // get layer of the network Layer layer = network.Layers[0]; // check learning radius if (learningRadius == 0) { Neuron neuron = layer.Neurons[winner]; // update weight of the winner only for (int i = 0; i < neuron.Weights.Length; i++) { // calculate the error double e = input[i] - neuron.Weights[i]; error += Math.Abs(e); // update weights neuron.Weights[i] += e * learningRate; } } else { // winner's X and Y int wx = winner % width; int wy = winner / width; // walk through all neurons of the layer for (int j = 0; j < layer.Neurons.Length; j++) { Neuron neuron = layer.Neurons[j]; int dx = (j % width) - wx; int dy = (j / width) - wy; // update factor ( Gaussian based ) double factor = Math.Exp(-(double)(dx * dx + dy * dy) / squaredRadius2); // update weight of the neuron for (int i = 0; i < neuron.Weights.Length; i++) { // calculate the error double e = (input[i] - neuron.Weights[i]) * factor; error += Math.Abs(e); // update weight neuron.Weights[i] += e * learningRate; } } } return(error); }
static void MakeSpace() { var Colors = new[] { Color.Orange, Color.LightGray, Color.LightGreen, Color.LightBlue, Color.LightYellow, Color.LightCoral, Color.LightCyan }; space = new int[pointsPanel.ClientSize.Width, pointsPanel.ClientSize.Height]; spaceBitmap = new Bitmap(pointsPanel.ClientSize.Width, pointsPanel.ClientSize.Height); for (int x = 0; x < spaceBitmap.Width; x++) { for (int y = 0; y < spaceBitmap.Height; y++) { network.Compute(new double[] { (double)x / spaceBitmap.Width, (double)y / spaceBitmap.Height }); var winner = network.GetWinner(); space[x, y] = winner; var n = map.Cast <MapElement>().Where(z => z.Id == winner).First(); if (n.IsActive) { spaceBitmap.SetPixel(x, y, GetColor(n.MapX, n.MapY)); } } } }
private void btnCluster_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(pictureBox1.Image); image = Data.getInstance().preprocessing(image); pictureBox1.Image = image; double[] input = new double[100 * 100]; ImageToArray converter = new ImageToArray(0, 1); converter.Convert(image, out input); double[] input_from_pca = pca.Transform(input); som_network.Compute(input_from_pca); double winner = som_network.GetWinner(); MessageBox.Show("Winner : " + winner); for (int i = 0; i < Data.getInstance().images.Count; i++) { Bitmap image2 = Data.getInstance().preprocessing(Data.getInstance().images[i]); double[] input2 = new double[100 * 100]; ImageToArray converter2 = new ImageToArray(0, 1); converter2.Convert(image2, out input2); double[] input_from_pca2 = pca.Transform(input2); som_network.Compute(input_from_pca2); double check_winner = som_network.GetWinner(); if (winner == check_winner) { listBox1.Items.Add(Data.getInstance().file_names[i] + " - " + check_winner); } } }
/// <summary> /// Find a similar image of the image inputted into the method /// </summary> /// <param name="inputFilePath">full path to the image file</param> /// <returns>List of bitmap similar to the input, empty bitmap list if no image is similar</returns> public List <KeyValuePair <string, Bitmap> > FindSimilar(string inputFilePath) { //Train SOM TrainClusteringNetwork(); // Get List of Categories var categories = _allData.Keys.ToList(); var listOfImagePaths = new List <string>(); // Get all image paths and associate it with it's name, put it in listOfImagePaths categories.ForEach(category => listOfImagePaths.AddRange(Directory.GetFiles("pictures/" + category))); // Create another list of pair of preprocessed image and it's name var preprocessedImageArray = listOfImagePaths.Select(data => { double[] imageArray; _imageToArray.Convert(PreprocessImage(new Bitmap(data)), out imageArray); return(new KeyValuePair <string, double[]>(data, imageArray)); }).ToList(); int chosenImageCluster = 0, i = 0; var pca = ComputePca(preprocessedImageArray.Select(data => data.Value).ToList()); // Compute every image, and get its cluster. Save it into List of keyvaluepair (key = pathName, value = cluster) var clusterImageDictionary = preprocessedImageArray.Select(pair => { _clusteringNetwork.Compute(pca[i]); i++; var cluster = _clusteringNetwork.GetWinner(); if (pair.Key.Equals(inputFilePath)) { chosenImageCluster = cluster; } return(new KeyValuePair <string, int>(pair.Key, cluster)); }); // return list of bitmap that belong to the same cluster to the inputFilePath return (clusterImageDictionary.Where(data => data.Value == chosenImageCluster && data.Key != inputFilePath) .Select(data => new KeyValuePair <string, Bitmap>(data.Key, new Bitmap(data.Key))).ToList()); }
// Update map private void UpdateMap(DistanceNetwork network) { // get first layer Layer layer = network[0]; // lock Monitor.Enter(this); // run through all neurons for (int i = 0, n = layer.NeuronsCount; i < n; i++) { var neuron = layer[i]; var x = i % this.networkSize; var y = i / this.networkSize; this.map[y, x, 0] = (int)neuron[0]; this.map[y, x, 1] = (int)neuron[1]; this.map[y, x, 2] = 0; } // collect active neurons for (var i = 0; i < pointsCount; i++) { network.Compute(this.trainingSet[i]); var w = network.GetWinner( ); this.map[w / this.networkSize, w % this.networkSize, 2] = 1; } // unlock Monitor.Exit(this); // this.mapPanel.Invalidate( ); }
// Update map private void UpdateMap(DistanceNetwork network) { // get first layer Layer layer = network.Layers[0]; // lock Monitor.Enter(this); // run through all neurons for (int i = 0; i < layer.Neurons.Length; i++) { Neuron neuron = layer.Neurons[i]; int x = i % networkSize; int y = i / networkSize; map[y, x, 0] = (int)neuron.Weights[0]; map[y, x, 1] = (int)neuron.Weights[1]; map[y, x, 2] = 0; } // collect active neurons for (int i = 0; i < pointsCount; i++) { network.Compute(trainingSet[i]); int w = network.GetWinner(); map[w / networkSize, w % networkSize, 2] = 1; } // unlock Monitor.Exit(this); // mapPanel.Invalidate(); }
public Browse() { InitializeComponent(); if (listView1.Items.Count == 0) { MessageBox.Show("Add some art first"); } else { OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true; List <double[]> PCAInput = new List <double[]>(); if (dialog.ShowDialog() == DialogResult.OK) { for (int i = 0; i < dialog.FileNames.Length; i++) { Bitmap image = new Bitmap(dialog.FileNames.ElementAt(i)); image = preprocessImage(image); double[] output; ImageToArray converter = new ImageToArray(); converter.Convert(image, out output); PCAInput.Add(output); pca = new PrincipalComponentAnalysis(PCAInput.ToArray()); pca.Compute(); } } dn = new DistanceNetwork(pca.Result.GetLength(0), 4); som = new SOMLearning(dn); double[][] inputSOM = new double[pca.Result.GetLength(0)][]; for (int i = 0; i < pca.Result.GetLength(1); i++) { inputSOM[i] = new double[pca.Result.GetLength(1)]; for (int j = 0; j < pca.Result.GetLength(1); j++) { inputSOM[i][j] = pca.Result[i, j]; } } //training som int epoch = 10000; double minError = 0.0000001; for (int i = 0; i < epoch; i++) { double error = som.RunEpoch(inputSOM); if (error < minError) { break; } } //clustering for (int i = 0; i < pca.Result.GetLength(0); i++) { dn.Compute(inputSOM[i].ToArray()); int winner = dn.GetWinner(); ListViewGroup group; if (listView1.Groups[winner.ToString()] == null) { //bkin group baru group = new ListViewGroup(winner.ToString(), "" + winner); } else { //masukin ke group lama group = listView1.Groups[winner.ToString()]; } listView1.Groups.Add(group); //listView1.Items.Add(new ListViewItem(dialog.SafeFileNames[i], i, group)); //imageList1.Images.Add(new Bitmap(dialog.FileNames.ElementAt(i))); } } }