Esempio n. 1
0
        static void Main(string[] args)
        {
            string net = args[0];
            string pn1 = args[1];
            string pn2 = args[2];

            int equal = 0;
            int unequal = 0;

            Options.InitializeNNAnalysis();

            NeuralNet nn = MNIST.GetNN(net);

            Console.WriteLine("Initialized network");


            Bitmap x1 = new Bitmap(pn1);
            Bitmap x2 = new Bitmap(pn2);

            int[] dat1 = UDraw.FromBitmap(x1, MNIST.InputCoordinates.RowCount, MNIST.InputCoordinates.ColumnCount, false);
            int[] dat2 = UDraw.FromBitmap(x2, MNIST.InputCoordinates.RowCount, MNIST.InputCoordinates.ColumnCount, false);

            int lab1 = NNAnalysis.Utils.ULabel.Label(nn, UArray.ToDoubleArray(dat1), true);

            int lab2 = NNAnalysis.Utils.ULabel.Label(nn, UArray.ToDoubleArray(dat2), true);

            if (lab1 == lab2)
            {
                equal++;
            }
            else
            {
                unequal++;
            }

            Console.Write("Label for {0} is: {1}-{2}", pn1, lab1, lab2);
            Console.WriteLine(", equals = {0}", (lab1 == lab2));
            //Console.WriteLine("Label for {0} is: {1}", pn2, lab2);
        }
Esempio n. 2
0
    static void Main(string[] args)
    {
        string MNISTFile   = null;
        string MNISTData   = null;
        string MNISTLabels = null;
        var    p           = new OptionSet();


        bool just_accuracy = false;
        bool just_loss     = false;

        p.Add("nnet=", "MNIST neural network file name", x => MNISTFile = x);
        p.Add("datafile=", "MNIST data file name", x => MNISTData       = x);
        p.Add("labelfile=", "MNIST label file name", x => MNISTLabels   = x);
        p.Add <bool>("optimization=", "Do optimization (Default: true)", (x => RobustnessOptions.DoOptimization = x));
        p.Add <double>("bound=", "Linfinity-ball to search", (x => RobustnessOptions.Epsilon = x));
        p.Add <double>("sub=", "Subsample from 'live' constraints (0.0-1.0)", (x => RobustnessOptions.LiveConstraintSamplingRatio = x));
        p.Add <string>("registry=", "Unique name to store output examples and statistics", (x => RobustnessOptions.Registry = x));
        p.Add <bool>("cegar=", "Do CEGAR (default: true)", (x => RobustnessOptions.CEGAR = x));
        p.Add <string>("only-accuracy", "Only evaluate accuracy", (x => just_accuracy = (x != null)));
        p.Add <string>("only-loss", "Only evaluate loss", (x => just_loss = (x != null)));

        p.Add <string>("no-quant-safety", "Quantization integrality safety off", (x => RobustnessOptions.QuantizationSafety = (x == null)));


        p.Add <string>("max-conf", "Use max-conf objective", (x => {
            if (x != null)
            {
                RobustnessOptions.ObjectiveKind = LPSObjectiveKind.MaxConf;
            }
        }));

        p.Add <double>("winner-diff=", "Winning label should be that much different than second best", (x => RobustnessOptions.LabelConfidenceDiff = x));


        p.Add <string>("log-png", "Log png files", (x => RobustnessOptions.SavePNGCounterexamples = (x != null)));

        bool only_misclass = false;

        p.Add("only-filter-misclass", "Only keep the misclassifications", (x => only_misclass = (x != null)));



        Cmd.RunOptionSet(p, args);

        if (MNISTFile == null || MNISTData == null || MNISTLabels == null)
        {
            Console.WriteLine("Invalid arguments, use --help");
            Environment.Exit(1);
        }

        RobustnessOptions.Dump();

        Options.InitializeNNAnalysis();

        NeuralNet    nn   = MNIST.GetNN(MNISTFile);
        ImageDataset data = MNIST.ReadData(MNISTLabels, MNISTData, MNIST.ALL_IMAGES, 0);


        if (just_accuracy)
        {
            NNAccuracy.GetAccuracy(nn, data.Dataset);
            return;
        }

        if (just_loss)
        {
            NNAccuracy.GetLoss(nn, data.Dataset);
            return;
        }


        if (only_misclass)
        {
            string filtered = RobustnessOptions.Registry + "-misclass";

            Console.WriteLine("Orig {0} data", data.Dataset.Count());

            var ds = NNAccuracy.KeepMisclass(nn, data.Dataset);

            Console.WriteLine("Kept {0} data", ds.Count());

            ImageDataset ret = new ImageDataset(ds,
                                                MNIST.InputCoordinates.ChannelCount,
                                                MNIST.InputCoordinates.RowCount,
                                                MNIST.InputCoordinates.ColumnCount, true);

            MNIST.WriteData(filtered + "-labels", filtered + "-images", ret);
            return;
        }

        // NB: No snapshotting for MNIST since it never crashes ...
        ImageDataset synth = Robustness.SynthesizeCounterexamplesAndStore(nn, data, x => { return; });

        MNIST.WriteData(RobustnessOptions.Registry + "-synth-labels",
                        RobustnessOptions.Registry + "-synth-images", synth);
    }