public float Distance(SparseVectorF x, float[] y) { return(x.EuclideanDistance(y, 0)); }
public float Distance(SparseVectorF x, float[] y) { return(x.ManhattanDistance(y, 0)); }
public (List <string> classes, KMeans kmeans, OneVsAllSupportVectorMachine svm) FinishLearning( int vectorLength, CancellationToken cancellationToken) { // count classes List <string> classes = new List <string>(this.features.Select(x => x.truth).ToLookup(x => x).Select(x => x.Key)); if (classes.Count < 2) { throw new ArgumentException(); } classes.Sort(); // count vectors int numberOfVectors = this.features.Sum(x => x.features.Count); // copy vectors Dictionary <IVector <float>, float> vectors = new Dictionary <IVector <float>, float>(numberOfVectors); for (int i = 0, ii = this.features.Count; i < ii; i++) { FeatureDetectors.Features f = this.features[i].features; for (int j = 0, jj = f.Count, len = f.Length, off = 0; j < jj; j++, off += len) { ////DenseVectorF vector = new DenseVectorF(len, f.X, off); SparseVectorF vector = SparseVectorF.FromDense(len, f.X, off); vectors[vector] = vectors.TryGetValue(vector, out float weight) ? weight + 1.0f : 1.0f; } } cancellationToken.ThrowIfCancellationRequested(); // learn k-means KMeans kmeans = KMeans.Learn( vectorLength, KMeansSeeding.Random, 2, default(EuclideanDistance), vectors.Keys.ToList(), vectors.Values.ToList(), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); // learn svm Dictionary <string, int> classesLookup = classes.ToDictionary((x, i) => x, (x, i) => i); SequentualMinimalOptimization smo = new SequentualMinimalOptimization(new ChiSquare()) { Algorithm = SMOAlgorithm.LibSVM, Tolerance = 0.01f, }; List <float[]> svmx = new List <float[]>(this.features.Count); List <int> svmy = new List <int>(this.features.Count); for (int i = 0, ii = this.features.Count; i < ii; i++) { (FeatureDetectors.Features features, string truth) = this.features[i]; svmx.Add(PointsOfInterestClassifier.PrepareVector(kmeans, features, cancellationToken)); svmy.Add(classesLookup[truth]); } cancellationToken.ThrowIfCancellationRequested(); OneVsAllSupportVectorMachine svm = OneVsAllSupportVectorMachine.Learn( smo, classes.Count, svmx, svmy, null, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); return(classes, kmeans, svm); }
public void XXXTest() { #if false const int Count = 10000000; Stopwatch stopwatch = new Stopwatch(); RandomGenerator random = new RandomGenerator(); int length = 128; float[] dense1 = random.Generate(length); float[] dense2 = new float[length]; for (int i = 0; i < 16; i++) { dense2[(int)random.Generate(0, length)] = random.Generate(); } SparseVectorF sparse = SparseVectorF.FromDense(length, dense2, 0); stopwatch.Restart(); for (int i = 0; i < Count; i++) { Math32f.EuclideanDistance(length, dense1, 0, dense2, 0); } stopwatch.Stop(); Console.WriteLine("{0:F4} ms", stopwatch.ElapsedMilliseconds /* / Count*/); stopwatch.Restart(); for (int i = 0; i < Count; i++) { sparse.EuclideanDistance(dense1, 0); } stopwatch.Stop(); Console.WriteLine("{0:F4} ms", stopwatch.ElapsedMilliseconds /* / Count*/); #else const int Count = 5; Stopwatch stopwatch = new Stopwatch(); ////Image image = new Image(2000, 3000, 8, 200, 200); ////image.Randomize(); ////foreach ((Image image, _, _) in Image.FromFile(@"C:\DNN\dnn\test.jpg")) ////foreach ((Image image, _, _) in Image.FromFile(@"C:\DNN\dnn\363978.tif")) foreach ((Image image, _, _) in Imaging.Image.FromFile(@"L:\FormXtra\Receipts\Concur\Full\jpg\Work\08240EB488FE324E83C7C54042D9813C.jpg")) { stopwatch.Restart(); Image xxx = image.ConvertTo(null, 8); for (int i = 0; i < Count; i++) { xxx.Erode(xxx, StructuringElement.Square(2), 1, BorderType.BorderRepl, 0); xxx.ScaleByDownsampling2(xxx); ////image.Binarize(null, 0, 0, 0, 0, true, 0, 0); ////IntegralImage.FromImage(image); ////long power = image.Power(); ////Histogram hyst = image.GrayHistogram(); /*Image workImage = image * .ConvertTo(null, 8) * .Scale(null, 100.0 / image.HorizontalResolution, 100.0 / image.VerticalResolution, ScalingOptions.None) * ////.Binarize(null) * .Convert8To1(null, 128) * .CleanOverscan(0.5f, 0.5f) * .Deskew(null) * .Despeckle(null); * * ISet<ConnectedComponent> components = workImage.FindConnectedComponents(8); * workImage.RemoveConnectedComponents(components);*/ /*workImage = workImage.Scale(100.0 / image.HorizontalResolution, 100.0 / image.VerticalResolution, Imaging.ScalingOptions.None); * workImage = workImage.Binarize(); * ////workImage = workImage.Convert8To1(128); * workImage = workImage.Dilate(StructuringElement.Rectangle(5, 1), 1); * workImage = workImage.Dilate(StructuringElement.Rectangle(1, 5), 1); * workImage = workImage.Convert1To8(); * * DenseVectorPackF vectors = workImage.HOG(8, 2, 1, 9, 0.2f); * * vectors = DenseVectorPackF.Pack(vectors.Unpack().Where(x => x.Sum() != 0.0f).ToList());*/ ////Image temp = image.Binarize(); } stopwatch.Stop(); } Console.WriteLine("{0:F4} ms", stopwatch.ElapsedMilliseconds / Count); #endif }