/// <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 + " ]"); } }