Esempio n. 1
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;
            }
        }