Beispiel #1
0
        private static void label_classifier(string datacfg, string filename, string weightfile)
        {
            int     i;
            Network net = Parser.parse_network_cfg(filename);

            Network.set_batch_network(net, 1);
            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }


            var options = OptionList.read_data_cfg(datacfg);

            string labelList = OptionList.option_find_str(options, "names", "Data.Data/labels.list");
            string testList  = OptionList.option_find_str(options, "test", "Data.Data/train.list");
            int    classes   = OptionList.option_find_int(options, "classes", 2);

            string[] labels = Data.Data.get_labels(labelList);

            string[] paths = Data.Data.GetPaths(testList);
            int      m     = paths.Length;

            for (i = 0; i < m; ++i)
            {
                Image   im      = LoadArgs.load_image_color(paths[i], 0, 0);
                Image   resized = LoadArgs.resize_min(im, net.W);
                Image   crop    = LoadArgs.crop_image(resized, (resized.W - net.W) / 2, (resized.H - net.H) / 2, net.W, net.H);
                float[] pred    = Network.network_predict(net, crop.Data);

                int ind = Utils.max_index(pred, classes);

                Console.Write($"%s\n", labels[ind]);
            }
        }
Beispiel #2
0
        private static void test_writing(string cfgfile, string weightfile, string filename)
        {
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);
            var sw = new Stopwatch();

            string input = "";

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }

                Image im = LoadArgs.load_image_color(input, 0, 0);
                Network.resize_network(net, im.W, im.H);
                Console.Write($"%d %d %d\n", im.H, im.W, im.C);
                float[] x = im.Data;
                sw.Reset();
                sw.Start();
                Network.network_predict(net, x);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                Image pred = Network.get_network_image(net);

                Image upsampled = LoadArgs.resize_image(pred, im.W, im.H);
                Image thresh    = LoadArgs.threshold_image(upsampled, .5f);
                pred = thresh;

                LoadArgs.show_image(pred, "prediction");
                LoadArgs.show_image(im, "orig");
                CvInvoke.WaitKey();
                CvInvoke.DestroyAllWindows();

                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }
Beispiel #3
0
        private static void test_coco(string cfgfile, string weightfile, string filename, float thresh)
        {
            Image[][] alphabet = LoadArgs.load_alphabet();
            Network net = Parser.parse_network_cfg(cfgfile);
            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Layer l = net.Layers[net.N - 1];
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);
            float nms = .4f;
            var sw = new Stopwatch();

            int j;
            Box[] boxes = new Box[l.Side * l.Side * l.N];
            float[][] probs = new float[l.Side * l.Side * l.N][];
            for (j = 0; j < l.Side * l.Side * l.N; ++j) probs[j] = new float[l.Classes];
            while (true)
            {
                string input;
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input)) return;
                    input = input.TrimEnd();
                }
                Image im = LoadArgs.load_image_color(input, 0, 0);
                Image sized = LoadArgs.resize_image(im, net.W, net.H);
                float[] x = sized.Data;
                sw.Reset();
                sw.Start();
                Network.network_predict(net, x);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                l.get_detection_boxes( 1, 1, thresh, probs, boxes, false);
                if (nms != 0) Box.do_nms_sort(boxes, probs, l.Side * l.Side * l.N, l.Classes, nms);
                LoadArgs.draw_detections(im, l.Side * l.Side * l.N, thresh, boxes, probs, CocoClasses, alphabet, 80);
                LoadArgs.save_image(im, "prediction");
                LoadArgs.show_image(im, "predictions");
                CvInvoke.WaitKey();
                CvInvoke.DestroyAllWindows();
                if (!string.IsNullOrEmpty(filename)) break;
            }
        }
Beispiel #4
0
        private static void test_super(string cfgfile, string weightfile, string filename)
        {
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);

            var    sw    = new Stopwatch();
            string input = "";

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }
                Image im = LoadArgs.load_image_color(input, 0, 0);
                Network.resize_network(net, im.W, im.H);
                Console.Write($"%d %d\n", im.W, im.H);

                float[] x = im.Data;
                sw.Start();
                Network.network_predict(net, x);
                Image outi = Network.get_network_image(net);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                LoadArgs.save_image(outi, "outf");

                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }
Beispiel #5
0
        private static int bbox_comparator(SortableBbox a, SortableBbox b)
        {
            ++TotalCompares;
            Network net    = a.Net;
            int     sclass = a.Sclass;

            Image im1 = LoadArgs.load_image_color(a.Filename, net.W, net.H);
            Image im2 = LoadArgs.load_image_color(b.Filename, net.W, net.H);

            float[] x = new float[net.W * net.H * net.C];
            Array.Copy(im1.Data, 0, x, 0, im1.Data.Length);
            Array.Copy(im2.Data, 0, x, im1.Data.Length, im2.Data.Length);
            float[] predictions = Network.network_predict(net, x);

            if (predictions[sclass * 2] > predictions[sclass * 2 + 1])
            {
                return(1);
            }
            return(-1);
        }
Beispiel #6
0
        private static void bbox_fight(Network net, SortableBbox a, SortableBbox b, int classes, int sclass)
        {
            Image im1 = LoadArgs.load_image_color(a.Filename, net.W, net.H);
            Image im2 = LoadArgs.load_image_color(b.Filename, net.W, net.H);

            float[] x = new float[net.W * net.H * net.C];
            Array.Copy(im1.Data, 0, x, 0, im1.Data.Length);
            Array.Copy(im2.Data, 0, x, im1.Data.Length, im2.Data.Length);
            float[] predictions = Network.network_predict(net, x);
            ++TotalCompares;

            int i;

            for (i = 0; i < classes; ++i)
            {
                if (sclass < 0 || sclass == i)
                {
                    bool result = predictions[i * 2] > predictions[i * 2 + 1];
                    bbox_update(a, b, i, result);
                }
            }
        }
Beispiel #7
0
        public static void predict_classifier(string datacfg, string cfgfile, string weightfile, string filename, int top)
        {
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);

            var options = OptionList.read_data_cfg(datacfg);

            string nameList = OptionList.option_find_str(options, "names", "");

            if (string.IsNullOrEmpty(nameList))
            {
                nameList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list");
            }
            if (top == 0)
            {
                top = OptionList.option_find_int(options, "top", 1);
            }

            int i = 0;

            string[] names = Data.Data.get_labels(nameList);
            var      sw    = new Stopwatch();

            int[] indexes = new int[top];

            string input = "";
            int    size  = net.W;

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }
                Image im = LoadArgs.load_image_color(input, 0, 0);
                Image r  = LoadArgs.resize_min(im, size);
                Network.resize_network(net, r.W, r.H);
                Console.Write($"%d %d\n", r.W, r.H);

                float[] x = r.Data;
                sw.Reset();
                sw.Start();
                float[] predictions = Network.network_predict(net, x);
                if (net.Hierarchy != null)
                {
                    net.Hierarchy.Hierarchy_predictions(predictions, 0, net.Outputs, false);
                }
                Utils.top_k(predictions, net.Outputs, top, indexes);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                for (i = 0; i < top; ++i)
                {
                    int index = indexes[i];
                    if (net.Hierarchy != null)
                    {
                        Console.Write($"%d, %s: %f, parent: %s \n", index, names[index], predictions[index], (net.Hierarchy.Parent[index] >= 0) ? names[net.Hierarchy.Parent[index]] : "Root");
                    }
                    else
                    {
                        Console.Write($"%s: %f\n", names[index], predictions[index]);
                    }
                }
                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }
Beispiel #8
0
        private static void try_classifier(string datacfg, string cfgfile, string weightfile, string filename, int layerNum)
        {
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);

            var options = OptionList.read_data_cfg(datacfg);

            string nameList = OptionList.option_find_str(options, "names", "");

            if (string.IsNullOrEmpty(nameList))
            {
                nameList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list");
            }
            int top = OptionList.option_find_int(options, "top", 1);

            int i = 0;

            string[] names = Data.Data.get_labels(nameList);
            var      sw    = new Stopwatch();

            int[] indexes = new int[top];

            string input = "";

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }
                Image   orig = LoadArgs.load_image_color(input, 0, 0);
                Image   r    = LoadArgs.resize_min(orig, 256);
                Image   im   = LoadArgs.crop_image(r, (r.W - 224 - 1) / 2 + 1, (r.H - 224 - 1) / 2 + 1, 224, 224);
                float[] mean = { 0.48263312050943f, 0.45230225481413f, 0.40099074308742f };
                float[] std  = { 0.22590347483426f, 0.22120921437787f, 0.22103996251583f };
                float[] var  = new float[3];
                var[0] = std[0] * std[0];
                var[1] = std[1] * std[1];
                var[2] = std[2] * std[2];

                Blas.Normalize_cpu(im.Data, mean, var, 1, 3, im.W * im.H);

                float[] x = im.Data;
                sw.Reset();
                sw.Start();
                float[] predictions = Network.network_predict(net, x);

                Layer l = net.Layers[layerNum];
                for (i = 0; i < l.C; ++i)
                {
                    if (l.RollingMean.Length > i)
                    {
                        Console.Write($"%f %f %f\n", l.RollingMean[i], l.RollingVariance[i], l.Scales[i]);
                    }
                }
                Array.Copy(l.OutputGpu, l.Output, l.Outputs);
                for (i = 0; i < l.Outputs; ++i)
                {
                    Console.Write($"%f\n", l.Output[i]);
                }

                Network.top_predictions(net, top, indexes);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                for (i = 0; i < top; ++i)
                {
                    int index = indexes[i];
                    Console.Write($"%s: %f\n", names[index], predictions[index]);
                }
                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }
Beispiel #9
0
        private static void validate_classifier_multi(string datacfg, string filename, string weightfile)
        {
            int     i, j;
            Network net = Parser.parse_network_cfg(filename);

            Network.set_batch_network(net, 1);
            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }


            var options = OptionList.read_data_cfg(datacfg);

            string labelList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list");
            string validList = OptionList.option_find_str(options, "valid", "Data.Data/train.list");
            int    classes   = OptionList.option_find_int(options, "classes", 2);
            int    topk      = OptionList.option_find_int(options, "top", 1);

            string[] labels  = Data.Data.get_labels(labelList);
            int[]    scales  = { 224, 288, 320, 352, 384 };
            int      nscales = scales.Length;

            string[] paths = Data.Data.GetPaths(validList);
            int      m     = paths.Length;

            float avgAcc  = 0;
            float avgTopk = 0;

            int[] indexes = new int[topk];

            for (i = 0; i < m; ++i)
            {
                int    class2 = -1;
                string path   = paths[i];
                for (j = 0; j < classes; ++j)
                {
                    if (path.Contains(labels[j]))
                    {
                        class2 = j;
                        break;
                    }
                }
                float[] pred = new float[classes];
                Image   im   = LoadArgs.load_image_color(paths[i], 0, 0);
                for (j = 0; j < nscales; ++j)
                {
                    Image r = LoadArgs.resize_min(im, scales[j]);
                    Network.resize_network(net, r.W, r.H);
                    float[] p = Network.network_predict(net, r.Data);
                    if (net.Hierarchy != null)
                    {
                        net.Hierarchy.Hierarchy_predictions(p, 0, net.Outputs, true);
                    }
                    Blas.Axpy_cpu(classes, 1, p, pred);
                    LoadArgs.flip_image(r);
                    p = Network.network_predict(net, r.Data);
                    Blas.Axpy_cpu(classes, 1, p, pred);
                }
                Utils.top_k(pred, classes, topk, indexes);
                if (indexes[0] == class2)
                {
                    avgAcc += 1;
                }
                for (j = 0; j < topk; ++j)
                {
                    if (indexes[j] == class2)
                    {
                        avgTopk += 1;
                    }
                }

                Console.Write($"%d: top 1: %f, top %d: %f\n", i, avgAcc / (i + 1), topk, avgTopk / (i + 1));
            }
        }
Beispiel #10
0
        private static void validate_classifier_single(string datacfg, string filename, string weightfile)
        {
            int     i, j;
            Network net = Parser.parse_network_cfg(filename);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);


            var options = OptionList.read_data_cfg(datacfg);

            string labelList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list");
            string leafList  = OptionList.option_find_str(options, "leaves", "");

            if (!string.IsNullOrEmpty(leafList))
            {
                net.Hierarchy.Change_leaves(leafList);
            }
            string validList = OptionList.option_find_str(options, "valid", "Data.Data/train.list");
            int    classes   = OptionList.option_find_int(options, "classes", 2);
            int    topk      = OptionList.option_find_int(options, "top", 1);

            string[] labels = Data.Data.get_labels(labelList);

            string[] paths = Data.Data.GetPaths(validList);
            int      m     = paths.Length;

            float avgAcc  = 0;
            float avgTopk = 0;

            int[] indexes = new int[topk];

            for (i = 0; i < m; ++i)
            {
                int    class2 = -1;
                string path   = paths[i];
                for (j = 0; j < classes; ++j)
                {
                    if (path.Contains(labels[j]))
                    {
                        class2 = j;
                        break;
                    }
                }
                Image   im      = LoadArgs.load_image_color(paths[i], 0, 0);
                Image   resized = LoadArgs.resize_min(im, net.W);
                Image   crop    = LoadArgs.crop_image(resized, (resized.W - net.W) / 2, (resized.H - net.H) / 2, net.W, net.H);
                float[] pred    = Network.network_predict(net, crop.Data);
                if (net.Hierarchy != null)
                {
                    net.Hierarchy.Hierarchy_predictions(pred, 0, net.Outputs, false);
                }

                Utils.top_k(pred, classes, topk, indexes);

                if (indexes[0] == class2)
                {
                    avgAcc += 1;
                }
                for (j = 0; j < topk; ++j)
                {
                    if (indexes[j] == class2)
                    {
                        avgTopk += 1;
                    }
                }

                Console.Write($"%d: top 1: %f, top %d: %f\n", i, avgAcc / (i + 1), topk, avgTopk / (i + 1));
            }
        }
Beispiel #11
0
        private static void validate_classifier_10(string datacfg, string filename, string weightfile)
        {
            int     i, j;
            Network net = Parser.parse_network_cfg(filename);

            Network.set_batch_network(net, 1);
            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }


            var options = OptionList.read_data_cfg(datacfg);

            string labelList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list");
            string validList = OptionList.option_find_str(options, "valid", "Data.Data/train.list");
            int    classes   = OptionList.option_find_int(options, "classes", 2);
            int    topk      = OptionList.option_find_int(options, "top", 1);

            string[] labels = Data.Data.get_labels(labelList);

            string[] paths = Data.Data.GetPaths(validList);
            int      m     = paths.Length;

            float avgAcc  = 0;
            float avgTopk = 0;

            int[] indexes = new int[topk];

            for (i = 0; i < m; ++i)
            {
                int    class2 = -1;
                string path   = paths[i];
                for (j = 0; j < classes; ++j)
                {
                    if (path.Contains(labels[j]))
                    {
                        class2 = j;
                        break;
                    }
                }
                int     w      = net.W;
                int     h      = net.H;
                int     shift  = 32;
                Image   im     = LoadArgs.load_image_color(paths[i], w + shift, h + shift);
                Image[] images = new Image[10];
                images[0] = LoadArgs.crop_image(im, -shift, -shift, w, h);
                images[1] = LoadArgs.crop_image(im, shift, -shift, w, h);
                images[2] = LoadArgs.crop_image(im, 0, 0, w, h);
                images[3] = LoadArgs.crop_image(im, -shift, shift, w, h);
                images[4] = LoadArgs.crop_image(im, shift, shift, w, h);
                LoadArgs.flip_image(im);
                images[5] = LoadArgs.crop_image(im, -shift, -shift, w, h);
                images[6] = LoadArgs.crop_image(im, shift, -shift, w, h);
                images[7] = LoadArgs.crop_image(im, 0, 0, w, h);
                images[8] = LoadArgs.crop_image(im, -shift, shift, w, h);
                images[9] = LoadArgs.crop_image(im, shift, shift, w, h);
                float[] pred = new float[classes];
                for (j = 0; j < 10; ++j)
                {
                    float[] p = Network.network_predict(net, images[j].Data);
                    if (net.Hierarchy != null)
                    {
                        net.Hierarchy.Hierarchy_predictions(p, 0, net.Outputs, true);
                    }
                    Blas.Axpy_cpu(classes, 1, p, pred);
                }
                Utils.top_k(pred, classes, topk, indexes);
                if (indexes[0] == class2)
                {
                    avgAcc += 1;
                }
                for (j = 0; j < topk; ++j)
                {
                    if (indexes[j] == class2)
                    {
                        avgTopk += 1;
                    }
                }

                Console.Write($"%d: top 1: %f, top %d: %f\n", i, avgAcc / (i + 1), topk, avgTopk / (i + 1));
            }
        }
Beispiel #12
0
        public static void run_nightmare(List <string> args)
        {
            if (args.Count < 4)
            {
                Console.Error.Write($"usage: %s %s [cfg] [weights] [Image] [Layer] [options! (optional)]\n", args[0], args[1]);
                return;
            }

            string cfg      = args[2];
            string weights  = args[3];
            string input    = args[4];
            int    maxLayer = int.Parse(args[5]);

            int    range       = Utils.find_int_arg(args, "-range", 1);
            bool   norm        = Utils.find_int_arg(args, "-norm", 1) != 0;
            int    rounds      = Utils.find_int_arg(args, "-rounds", 1);
            int    iters       = Utils.find_int_arg(args, "-iters", 10);
            int    octaves     = Utils.find_int_arg(args, "-octaves", 4);
            float  zoom        = Utils.find_int_arg(args, "-zoom", 1);
            float  rate        = Utils.find_int_arg(args, "-rate", .04f);
            float  thresh      = Utils.find_int_arg(args, "-thresh", 1);
            float  rotate      = Utils.find_int_arg(args, "-rotate", 0);
            float  momentum    = Utils.find_int_arg(args, "-momentum", .9f);
            float  lambda      = Utils.find_int_arg(args, "-lambda", .01f);
            string prefix      = Utils.find_int_arg(args, "-prefix", "");
            bool   reconstruct = Utils.find_arg(args, "-reconstruct");
            int    smoothSize  = Utils.find_int_arg(args, "-smooth", 1);

            Network net = Parser.parse_network_cfg(cfg);

            Parser.load_weights(net, weights);
            string cfgbase = Utils.Basecfg(cfg);
            string imbase  = Utils.Basecfg(input);

            Network.set_batch_network(net, 1);
            Image im = LoadArgs.load_image_color(input, 0, 0);

            float[] features = new float[0];
            Image   update   = null;

            if (reconstruct)
            {
                Network.resize_network(net, im.W, im.H);

                int zz = 0;
                Network.network_predict(net, im.Data);
                Image outIm = Network.get_network_image(net);
                Image crop  = LoadArgs.crop_image(outIm, zz, zz, outIm.W - 2 * zz, outIm.H - 2 * zz);
                Image fIm   = LoadArgs.resize_image(crop, outIm.W, outIm.H);
                Console.Write($"%d features\n", outIm.W * outIm.H * outIm.C);


                im       = LoadArgs.resize_image(im, im.W, im.H);
                fIm      = LoadArgs.resize_image(fIm, fIm.W, fIm.H);
                features = fIm.Data;

                int i;
                for (i = 0; i < 14 * 14 * 512; ++i)
                {
                    features[i] += Utils.rand_uniform(-.19f, .19f);
                }

                im     = LoadArgs.make_random_image(im.W, im.H, im.C);
                update = new Image(im.W, im.H, im.C);
            }

            int e;
            int n;

            for (e = 0; e < rounds; ++e)
            {
                Console.Error.Write($"Iteration: ");
                for (n = 0; n < iters; ++n)
                {
                    Console.Error.Write($"%d, ", n);
                    if (reconstruct)
                    {
                        reconstruct_picture(net, features, im, update, rate, momentum, lambda, smoothSize, 1);
                        //if ((n+1)%30 == 0) rate *= .5;
                        LoadArgs.show_image(im, "reconstruction");
                        CvInvoke.WaitKey(10);
                    }
                    else
                    {
                        int layer  = maxLayer + Utils.Rand.Next() % range - range / 2;
                        int octave = Utils.Rand.Next() % octaves;
                        optimize_picture(net, im, layer, 1 / (float)Math.Pow(1.33333333, octave), rate, thresh, norm);
                    }
                }
                Console.Error.Write($"done\n");
                string buff;
                if (!string.IsNullOrEmpty(prefix))
                {
                    buff = $"{prefix}_{imbase}_{cfgbase}_{maxLayer}_{e:06}%s/%s_%s_%d_%06d";
                }
                else
                {
                    buff = $"{imbase}_{cfgbase}_{maxLayer}_{e:06}";
                }
                Console.Write($"%d %s\n", e, buff);
                LoadArgs.save_image(im, buff);
                //LoadArgs.show_image(im, buff);
                //CvInvoke.WaitKey();

                if (rotate != 0)
                {
                    Image rot = LoadArgs.rotate_image(im, rotate);
                    im = rot;
                }
                Image crop    = LoadArgs.crop_image(im, (int)(im.W * (1f - zoom) / 2f), (int)(im.H * (1f - zoom) / 2f), (int)(im.W * zoom), (int)(im.H * zoom));
                Image resized = LoadArgs.resize_image(crop, im.W, im.H);
                im = resized;
            }
        }
Beispiel #13
0
        private static void validate_yolo_recall(string cfgfile, string weightfile)
        {
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Console.Error.Write($"Learning Rate: %g, Momentum: %g, Decay: %g\n", net.LearningRate, net.Momentum, net.Decay);

            string[] paths = Data.Data.GetPaths("Data.Data/voc.2007.test");

            Layer l       = net.Layers[net.N - 1];
            int   classes = l.Classes;
            int   side    = l.Side;

            int j, k;

            Box[]     boxes = new Box[side * side * l.N];
            float[][] probs = new float[side * side * l.N][];
            for (j = 0; j < side * side * l.N; ++j)
            {
                probs[j] = new float[classes];
            }

            int m = paths.Length;
            int i = 0;

            float thresh    = .001f;
            float iouThresh = .5f;

            int   total     = 0;
            int   correct   = 0;
            int   proposals = 0;
            float avgIou    = 0;

            for (i = 0; i < m; ++i)
            {
                string path  = paths[i];
                Image  orig  = LoadArgs.load_image_color(path, 0, 0);
                Image  sized = LoadArgs.resize_image(orig, net.W, net.H);
                string id    = Utils.Basecfg(path);
                Network.network_predict(net, sized.Data);
                l.get_detection_boxes(orig.W, orig.H, thresh, probs, boxes, true);

                string labelpath;
                Utils.find_replace(path, "images", "labels", out labelpath);
                Utils.find_replace(labelpath, "JPEGImages", "labels", out labelpath);
                Utils.find_replace(labelpath, ".jpg", ".txt", out labelpath);
                Utils.find_replace(labelpath, ".JPEG", ".txt", out labelpath);

                int        numLabels = 0;
                BoxLabel[] truth     = Data.Data.read_boxes(labelpath, ref numLabels);
                for (k = 0; k < side * side * l.N; ++k)
                {
                    if (probs[k][0] > thresh)
                    {
                        ++proposals;
                    }
                }
                for (j = 0; j < numLabels; ++j)
                {
                    ++total;
                    Box   t       = new Box(truth[j].X, truth[j].Y, truth[j].W, truth[j].H);
                    float bestIou = 0;
                    for (k = 0; k < side * side * l.N; ++k)
                    {
                        float iou = Box.box_iou(boxes[k], t);
                        if (probs[k][0] > thresh && iou > bestIou)
                        {
                            bestIou = iou;
                        }
                    }
                    avgIou += bestIou;
                    if (bestIou > iouThresh)
                    {
                        ++correct;
                    }
                }

                Console.Error.Write($"%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals / (i + 1), avgIou * 100 / total, 100f * correct / total);
            }
        }
Beispiel #14
0
        private static void test_tag(string cfgfile, string weightfile, string filename)
        {
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);
            int i = 0;

            string[] names = Data.Data.get_labels("Data.Data/tags.txt");
            var      sw    = new Stopwatch();

            int[] indexes = new int[10];

            string input = "";
            int    size  = net.W;

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }
                Image im = LoadArgs.load_image_color(input, 0, 0);
                Image r  = LoadArgs.resize_min(im, size);
                Network.resize_network(net, r.W, r.H);
                Console.Write($"%d %d\n", r.W, r.H);

                float[] x = r.Data;

                sw.Reset();
                sw.Start();
                float[] predictions = Network.network_predict(net, x);
                Network.top_predictions(net, 10, indexes);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                for (i = 0; i < 10; ++i)
                {
                    int index = indexes[i];
                    Console.Write($"%.1f%%: %s\n", predictions[index] * 100, names[index]);
                }
                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }
Beispiel #15
0
        public static void test_detector(string datacfg, string cfgfile, string weightfile, string filename, float thresh)
        {
            var    options  = OptionList.read_data_cfg(datacfg);
            string nameList = OptionList.option_find_str(options, "names", "Data.Data/names.list");

            string[] names = Data.Data.get_labels(nameList);

            Image[][] alphabet = LoadArgs.load_alphabet();
            Network   net      = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);
            var sw = new Stopwatch();

            string input = "";
            int    j;
            float  nms = .4f;

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }
                Image im    = LoadArgs.load_image_color(input, 0, 0);
                Image sized = LoadArgs.resize_image(im, net.W, net.H);
                Layer l     = net.Layers[net.N - 1];

                Box[]     boxes = new Box[l.W * l.H * l.N];
                float[][] probs = new float[l.W * l.H * l.N][];
                for (j = 0; j < l.W * l.H * l.N; ++j)
                {
                    probs[j] = new float[l.Classes];
                }

                float[] x = sized.Data;
                sw.Start();
                Network.network_predict(net, x);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                Layer.get_region_boxes(l, 1, 1, thresh, probs, boxes, false, new int[0]);
                if (nms != 0)
                {
                    Box.do_nms_sort(boxes, probs, l.W * l.H * l.N, l.Classes, nms);
                }
                LoadArgs.draw_detections(im, l.W * l.H * l.N, thresh, boxes, probs, names, alphabet, l.Classes);
                LoadArgs.save_image(im, "predictions");
                LoadArgs.show_image(im, "predictions");

                CvInvoke.WaitKey();
                CvInvoke.DestroyAllWindows();
                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }