protected internal override void onStart() { base.onStart(); // set weights between input and rbf layer using kmeans KMeansClustering kmeans = new KMeansClustering(TrainingSet); kmeans.NumberOfClusters = neuralNetwork.getLayerAt(1).NeuronsCount; // set number of clusters as number of rbf neurons kmeans.doClustering(); // get clusters (centroids) Cluster[] clusters = kmeans.Clusters; // assign each rbf neuron to one cluster // and use centroid vectors to initialize neuron's input weights Layer rbfLayer = neuralNetwork.getLayerAt(1); int i = 0; foreach (Neuron neuron in rbfLayer.Neurons) { KVector centroid = clusters[i].Centroid; double[] weightValues = centroid.Values; int c = 0; foreach (Connection conn in neuron.InputConnections) { conn.Weight.Value = weightValues[c]; c++; } i++; } // get cluster centroids as list List <KVector> centroids = new List <KVector>(); foreach (Cluster cluster in clusters) { centroids.Add(cluster.Centroid); } // use KNN to calculate sigma param - gausssian function width for each neuron KNearestNeighbour knn = new KNearestNeighbour(); knn.DataSet = centroids; int n = 0; foreach (KVector centroid in centroids) { // calculate and set sigma for each neuron in rbf layer KVector[] nearestNeighbours = knn.getKNearestNeighbours(centroid, k); double sigma = calculateSigma(centroid, nearestNeighbours); // calculate in method Neuron neuron = rbfLayer.getNeuronAt(n); ((Gaussian)neuron.TransferFunction).Sigma = sigma; i++; } }
private void classifyBtn_Click(object sender, EventArgs e) { Bitmap currentImage = new Bitmap(pictureBox1.Image); if (ClassifierType == "KNN") { KNearestNeighbour knnClassifier = new KNearestNeighbour(10, Main.trainingImagesFeatures, Main.trainingLabels); pictureBox2.Image = pictureBox1.Image; Bitmap drawnImage = new Bitmap(scaledImage((Bitmap)pictureBox1.Image)); pictureBox2.Image = drawnImage; //byte[] imageFeatures = bitmapToBuffer(drawnImage); // int classIndex = knnClassifier.classifySorting(K, imageFeatures); // MessageBox.Show(classIndex.ToString()); } }
private void button1_Click(object sender, EventArgs e) { dataGridView1.Rows.Clear(); dataGridView1.Refresh(); int index = 0; string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Output.txt"); StreamWriter file = new StreamWriter(@path); KNearestNeighbour knnClassifier = new KNearestNeighbour(10, Main.trainingImagesFeatures, Main.trainingLabels); this.confusionMatrix = new int[10, 10]; var watch = Stopwatch.StartNew(); if (ClassifierType == "KNN") { while (index != NumberOfImages) { int classIndex = knnClassifier.classify(K, Mode.testImagesFeatures[index]); this.confusionMatrix[Mode.testLabels[index], classIndex]++; string[] output = { index.ToString(), Mode.testLabels[index].ToString(), classIndex.ToString() }; this.dataGridView1.Rows.Add(output); string outputFile = "Image# " + index.ToString() + "\t Predection: " + classIndex.ToString() + "\t Label: " + Mode.testLabels[index] + "\n"; file.WriteLine(outputFile); index++; } watch.Stop(); file.WriteLine(watch.ElapsedMilliseconds.ToString()); Process proc = Process.GetCurrentProcess(); file.WriteLine((proc.PrivateMemorySize64 / 1024).ToString()); } else if (ClassifierType == "KNN Modified") { while (index != NumberOfImages) { int classIndex = knnClassifier.classifyModified(Mode.testImagesFeatures[index]); this.confusionMatrix[Mode.testLabels[index], classIndex]++; string[] output = { index.ToString(), Mode.testLabels[index].ToString(), classIndex.ToString() }; this.dataGridView1.Rows.Add(output); string outputFile = "Image# " + index.ToString() + "\t Predection: " + classIndex.ToString() + "\t Label: " + Mode.testLabels[index] + "\n"; file.WriteLine(outputFile); index++; } watch.Stop(); file.WriteLine(watch.ElapsedMilliseconds.ToString()); Process proc = Process.GetCurrentProcess(); file.WriteLine((proc.PrivateMemorySize64 / 1024).ToString()); } file.Close(); ConfusionMatrixForm cmf = new ConfusionMatrixForm(); cmf.Show(); cmf.setConfusionMatrix(this.confusionMatrix, watch.ElapsedMilliseconds.ToString()); }