public void op_AdditionTest() { Vector a = new PerceptronLib.Vector(3); Vector b = new PerceptronLib.Vector(a.Dimension); for (int i = 0; i < a.Dimension; i++) { a[i] = i; b[i] = i; } Vector expected = new PerceptronLib.Vector(a.Dimension); for (int i = 0; i < a.Dimension; i++) { expected[i] = 2 * i; } Vector actual; actual = (a + b); Assert.AreEqual(expected, actual); for (int i = 0; i < a.Dimension; i++) { a[i] = i + 1; b[i] = i + 2; } expected = new PerceptronLib.Vector(a.Dimension); for (int i = 0; i < a.Dimension; i++) { expected[i] = 2 * (i + 1) + 1; } actual = (a + b); Assert.AreEqual(expected, actual); }
/// <summary> /// Tworzy listę przykładów uczących na podstawie obrazów wczytanych z dysku /// </summary> private List<LearningExample> createLearningExamples(List<FileInfo> files) { List<LearningExample> examples = new List<LearningExample>(); foreach (FileInfo f in files) { double sum = 0.0F; Bitmap bitmap = (Bitmap)Bitmap.FromFile(f.FullName); examplesWidth = bitmap.Width; examplesHeight = bitmap.Height; //printLine(f.Name + ": " + bitmap.Width + "x" + bitmap.Height); printLine(f.Name + ": " + bitmap.Width + "x" + bitmap.Height); PerceptronLib.Vector v = new PerceptronLib.Vector(bitmap.Width * bitmap.Height); int width = bitmap.Width; int height = bitmap.Height; double max = 0; double min = 255; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int index = i * height + j; //v[index] = (double)bitmap.GetPixel(i, j).R / (256.0F * width * height) * 3000; v[index] = (double)bitmap.GetPixel(i, j).R; sum += v[index]; if (min > v[index]) min = v[index]; if (max < v[index]) max = v[index]; } } double medium = sum / (double)(width * height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int index = i * height + j; v[index] -= medium; } } v.normalizeWeights(); //printLine("Max = " + max + ", Min = " + min + ", Sum = " + sum // + ", medium = " + medium); examples.Add(new LearningExample(v, 0)); } return examples; }
/// <summary> /// Algorytm redukcji składowych głównych /// </summary> internal void reduction(List<LearningExample> exampleList) { try { Dispatcher.Invoke(OnReductionStarted, this, new EventArgs()); List<LearningExample> list = new List<LearningExample>(exampleList); int dimension = exampleList[0].Example.Dimension; List<PerceptronLib.Vector> principalComponents = new List<PerceptronLib.Vector>(outputDimension); for (int i = 0; i < outputDimension; i++) { #if DEBUG printLine("i = " + i); #endif principalComponents.Add(ojLearn(list).Weights); PerceptronLib.Vector w = principalComponents[i]; //printLine("Składowa główna: " + w[0] + ", " + w[1] + ", " + w[2] + ", " + w[3]); //printLine("Składowa główna: długość = " + w.Length); List<LearningExample> nextList = new List<LearningExample>(); foreach (LearningExample ex in list) { PerceptronLib.Vector x = ex.Example; double val = w * w; double activation = w * x; PerceptronLib.Vector nextExVector = new PerceptronLib.Vector(dimension); nextExVector = x - w * (activation / val); nextExVector.normalizeWeights(); LearningExample nextEx = new LearningExample(nextExVector, 0); nextList.Add(nextEx); } list = nextList; } saveImages(principalComponents, examplesWidth, examplesHeight); Dispatcher.Invoke(OnReductionFinished, this, new EventArgs()); } catch (Exception ex) { printLine(ex.Message + " [ " + ex.StackTrace + " ]"); } }
private void compareButton_Click(object sender, RoutedEventArgs e) { if (File.Exists(dataBasePathText.Text) == false) { printLine("Nie istnieje plik bazy danych"); return; } try { openFileDialog.ShowDialog(); if (openFileDialog.FileName != null) { string fn = openFileDialog.FileName; printLine("Porównywanie pliku: " + fn); Bitmap bitmap = (Bitmap)Bitmap.FromFile(fn); if (bitmap.Width == examplesWidth && bitmap.Height == examplesHeight) { printLine("Wymiary OK"); PerceptronLib.Vector v = new PerceptronLib.Vector(bitmap.Width * bitmap.Height); int width = bitmap.Width; int height = bitmap.Height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int index = i * height + j; //v[index] = (double)bitmap.GetPixel(i, j).R / (256.0F * width * height) * 3000; v[index] = (double)bitmap.GetPixel(i, j).R; } } BinaryFormatter formatter = new BinaryFormatter(); FileStream fs = new System.IO.FileStream(dataBasePathText.Text, FileMode.Open, System.IO.FileAccess.Read); EigenFacesDB db = (EigenFacesDB)formatter.Deserialize(fs); printLine("Rozpoznany obraz: " + db.compareFace(v)); } else { printLine("Niezgodne wymiary wejścia"); } } } catch (Exception ex) { printLine(ex.Message + " [ " + ex.StackTrace + " ]"); } }
/// <summary> /// Zapisuje utworzone obrazy na dysku /// </summary> private void saveImages(List<PerceptronLib.Vector> vectors, int width, int height) { try { //if (File.Exists(getDataBaseFileName()) == false) //{ // printLine("Plik bazy danych nie istnieje"); // return; //} // Otwiera plik bazy danych i nadpisuje go FileStream stream = new System.IO.FileStream(getDataBaseFileName(), FileMode.Create, System.IO.FileAccess.Write); EigenFacesDB db = new EigenFacesDB(vectors); BinaryFormatter formatter = new BinaryFormatter(); printLine("Zapisywanie wyników..."); int dimension = examples[0].Example.Dimension; for (int l = 0; l < examples.Count; l++) { LearningExample ex = examples[l]; // Tworzy nowy element bazy danych EigenNode node = new EigenNode("Przykład" + (l + 1)); PerceptronLib.Vector v = new PerceptronLib.Vector(dimension); //printLine("outputDim = " + outputDimension + ", vectors.count = " + vectors.Count); for (int k = 0; k < outputDimension; k++) { PerceptronLib.Vector p = vectors[k]; Bitmap img = new Bitmap(examplesWidth, examplesHeight); if (l == 0) { Bitmap eigenImg = new Bitmap(examplesWidth, examplesHeight); LearningExample eigenEx = new LearningExample(vectors[k], 0); normalizeRange(eigenEx, 256.0F, width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int index = i * height + j; byte color = (byte)(eigenEx.Example[index]); System.Drawing.Color c = System.Drawing.Color.FromArgb(255, color, color, color); eigenImg.SetPixel(i, j, c); } } eigenImg.Save("eigenVector-" + (k + 1) + ".jpg"); } double val = p * p; double activation = p * ex.Example; node.Coordinates.Add(activation); v += p * (activation / val); LearningExample newEx = new LearningExample(v, 0); normalizeRange(newEx, 256.0F, width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int index = i * height + j; byte color = (byte)(newEx.Example[index]); System.Drawing.Color c = System.Drawing.Color.FromArgb(255, color, color, color); img.SetPixel(i, j, c); } } img.Save("output" + (l + 1) + "-" + (k + 1) + ".jpg"); } db.add(node); } //foreach (EigenNode n in nodes) //{ // printLine("n.coordinates: " + n.Coordinates.Count); // db.add(n); //} printLine("Wymiar zapisywanej bazy: " + db.Dimension); try { formatter.Serialize(stream, db); } catch (Exception ex) { printLine("Wujątek: " + ex.Message + " [ " + ex.StackTrace + " ]"); } stream.Close(); } catch (Exception ex) { printLine(ex.Message + " [ " + ex.StackTrace + " ] " + ex.GetType()); } }
public void op_MultiplyTest() { Vector a = new PerceptronLib.Vector(3); Vector b = new PerceptronLib.Vector(3); for (int i = 0; i < 3; i++) { a[i] = i; b[i] = i; } double expected = 5F; double actual; actual = (a * b); Assert.AreEqual(expected, actual); for (int i = 0; i < 3; i++) { a[i] = i + 1; b[i] = i + 2; } expected = 20F; actual = (a * b); Assert.AreEqual(expected, actual); }
/// <summary> /// Zdarzenie przechwytywane w momencie poruszania myszą na canvasie. /// Ma na celu pokacywanie, jak klasyfikowane są odpowiednie punkty na płaszczyźnie /// przez stworzoną sieć. /// </summary> private void canvas_MouseMove(object sender, MouseEventArgs e) { // Sprawdzamy, czy sieć została już utworzona if (isNetworkCreated) { Point p = e.GetPosition(canvas); // Tworzy wektor przeznaczony do przeliczenia PerceptronLib.Vector v = new PerceptronLib.Vector(3); v[0] = 1; v[1] = p.X - 100; v[2] = p.Y - 100; // Tworzy obiekt przykładu LearningExample ex = new LearningExample(v, 0); // Sprawdza, na jaki kolor należy pomalować prostokąt if (network.classify(ex)[1] == 1) { // Koloruje na czerwono classRect.Fill = new SolidColorBrush(Colors.Red); } else { // Koloruje na niebiesko classRect.Fill = new SolidColorBrush(Colors.Blue); } } }
/// <summary> /// Funkcja przeznaczona do dodawania przykładu uczącego do listy /// </summary> private void addLearningExample(Point p, double exClass) { // Tworzy nowy wektor PerceptronLib.Vector v = new PerceptronLib.Vector(3); v[0] = 1.0F; v[1] = p.X; v[2] = p.Y; // Tworzy przykład uczący learningExamples.Add(new LearningExample(v, exClass)); }