Esempio n. 1
0
        private static void optimize_picture(Network net, Image orig, int maxLayer, float scale, float rate, float thresh, bool norm)
        {
            net.N = maxLayer + 1;

            int  dx   = Utils.Rand.Next() % 16 - 8;
            int  dy   = Utils.Rand.Next() % 16 - 8;
            bool flip = Utils.Rand.Next() % 2 != 0;

            Image crop = LoadArgs.crop_image(orig, dx, dy, orig.W, orig.H);
            Image im   = LoadArgs.resize_image(crop, (int)(orig.W * scale), (int)(orig.H * scale));

            if (flip)
            {
                LoadArgs.flip_image(im);
            }

            Network.resize_network(net, im.W, im.H);
            Layer last = net.Layers[net.N - 1];

            Image delta = new Image(im.W, im.H, im.C);

            NetworkState state = new NetworkState();

            state.Input = (float[])im.Data.Clone();
            state.Delta = (float[])im.Data.Clone();

            Network.forward_network_gpu(net, state);
            Blas.copy_ongpu(last.Outputs, last.OutputGpu, last.DeltaGpu);

            Array.Copy(last.DeltaGpu, last.Delta, last.Outputs);
            calculate_loss(last.Delta, last.Delta, last.Outputs, thresh);
            Array.Copy(last.Delta, last.DeltaGpu, last.Outputs);

            Network.backward_network_gpu(net, state);

            Array.Copy(state.Delta, delta.Data, im.W * im.H * im.C);


            if (flip)
            {
                LoadArgs.flip_image(delta);
            }

            Image resized = LoadArgs.resize_image(delta, orig.W, orig.H);
            Image outi    = LoadArgs.crop_image(resized, -dx, -dy, orig.W, orig.H);

            if (norm)
            {
                Utils.normalize_array(outi.Data, outi.W * outi.H * outi.C);
            }
            Blas.Axpy_cpu(orig.W * orig.H * orig.C, rate, outi.Data, orig.Data);

            LoadArgs.constrain_image(orig);
        }