static void Main(string[] args) { string CifarDataBatch = null; int split_size = 0; var p = new OptionSet(); p.Add <string>("dataset=", "CIFAR dataset file name", (x => CifarDataBatch = x)); p.Add <int>("split-size=", "Number of images per split", (x => split_size = x)); Cmd.RunOptionSet(p, args); if (CifarDataBatch == null || split_size <= 0) { Console.WriteLine("Invalid arguments, use --help"); Environment.Exit(1); } /* Initialize parameters */ Options.InitializeNNAnalysis(); // Plain old CIFAR binary format ImageDataset data = CIFAR.ReadData(CifarDataBatch, CIFAR.ALL_IMAGES, 0); // Split var splits = data.ShuffleSplitMany(split_size); int count = 0; foreach (var s in splits) { CIFAR.WriteData(CifarDataBatch + ".split_" + count.ToString(), s); count++; } }
static void Main(string[] args) { string CifarDataBatch = null; string[] split_files = null; var p = new OptionSet(); p.Add <string>("dataset=", "CIFAR dataset file name to store result", (x => CifarDataBatch = x)); p.Add <string>("files=", "CIFAR dataset files to join", (x => split_files = x.Split())); Cmd.RunOptionSet(p, args); if (CifarDataBatch == null || split_files == null) { Console.WriteLine("Invalid arguments, use --help"); Environment.Exit(1); } List <ImageDataset> dss = new List <ImageDataset>(); Console.WriteLine("Joining files ..."); for (int i = 0; i < split_files.Length; i++) { Console.WriteLine(split_files[i]); dss.Add(CIFAR.ReadData(split_files[i], CIFAR.ALL_IMAGES, 0)); } var data = Data.UnionMany(dss); var sd = data.Dataset.CreateShuffle(new Random()); data.Dataset = sd; Console.WriteLine("Output file ..."); Console.WriteLine(CifarDataBatch); CIFAR.WriteData(CifarDataBatch, data); }
static void Main(string[] args) { string CifarDataBatch = null; int how_many = 1; RANDTYPE randomness = RANDTYPE.UNIFORM; var p = new OptionSet(); p.Add <string>("dataset=", "CIFAR dataset file name", (x => CifarDataBatch = x)); p.Add <int>("how-many=", "Number of new images per image", (x => how_many = x)); p.Add <string>("randomness=", "Gaussian|Uniform", (x => randomness = (x.Equals("Gaussian") ? RANDTYPE.GAUSSIAN : RANDTYPE.UNIFORM))); int xoffset = 0; int yoffset = 0; bool geometric = false; p.Add("geometric", "Use geometric transform", (x => geometric = (x != null))); p.Add <int>("xoffset=", "x-offset for geometric transform", (x => xoffset = x)); p.Add <int>("yoffset=", "y-offset for geometric transform", (x => yoffset = x)); bool random = false; double epsilon = 0.0; p.Add("random", "Use random perturbation", (x => random = (x != null))); p.Add <double>("epsilon=", "Distance (for uniform) or standard deviation (for gaussian) random perturbation", (x => epsilon = x)); bool brightness = false; double brightness_offset = 0.0; p.Add("brightness", "Use brightness perturbation", (x => brightness = (x != null))); p.Add <double>("brightness-offset=", "Brightness offset (<= RobustnessOptions.MaxValue - RobustnessOptions.MinValue)", (x => brightness_offset = x)); bool contrast = false; double contrast_min_factor = 1.0; double contrast_max_factor = 1.0; p.Add("contrast", "Use contrast perturbation", (x => contrast = (x != null))); p.Add <double>("contrast-min-factor=", "Contrast min factor (0.0-1.0)", (x => contrast_min_factor = x)); p.Add <double>("contrast-max-factor=", "Contrast max factor (0.0-1.0)", (x => contrast_max_factor = x)); bool lossy_jpeg = false; int photoquality = 90; p.Add("lossy-jpeg", "Use lossy jpeg perturbation (default photoquality = 90)", (x => lossy_jpeg = (x != null))); p.Add <int>("jpeg-photoquality=", "Lossy jpeg photoquality", (x => photoquality = x)); bool rotate = false; float angle = 45.0F; p.Add("rotation", "Rotation transformation (default angle = 45.0)", (x => rotate = (x != null))); p.Add <double>("rotation-angle=", (x => angle = (float)x)); bool perturbe_only = false; p.Add("perturbe-only", "Only perturbe (not augment)", (x => perturbe_only = (x != null))); Cmd.RunOptionSet(p, args); if (CifarDataBatch == null) { Console.WriteLine("Invalid arguments, use --help"); Environment.Exit(1); } /* Initialize parameters */ Options.InitializeNNAnalysis(); // Plain old CIFAR binary format ImageDataset data = CIFAR.ReadData(CifarDataBatch, CIFAR.ALL_IMAGES, 0); IAugmentor augmentor = null; // TODO if (geometric) { augmentor = new AugmentGeometric(CIFAR.InputCoordinates, randomness, how_many, xoffset, yoffset); goto KONT; } if (random) { augmentor = new AugmentRandom(CIFAR.InputCoordinates, randomness, how_many, epsilon); goto KONT; } if (brightness) { augmentor = new AugmentBrightness(CIFAR.InputCoordinates, randomness, how_many, brightness_offset); goto KONT; } if (contrast) { augmentor = new AugmentContrast(CIFAR.InputCoordinates, how_many, contrast_min_factor, contrast_max_factor); goto KONT; } if (lossy_jpeg) { augmentor = new AugmentLossyJpeg(CIFAR.InputCoordinates, how_many, photoquality); goto KONT; } if (rotate) { augmentor = new AugmentRotation(CIFAR.InputCoordinates, how_many, angle); goto KONT; } KONT: int count = data.Dataset.Count(); ImageDataset initial = null; if (perturbe_only) { initial = new ImageDataset(new Dataset(10), CIFAR.InputCoordinates.ChannelCount, CIFAR.InputCoordinates.RowCount, CIFAR.InputCoordinates.ColumnCount, true); } else { initial = data; } for (int i = 0; i < count; i++) { double[] datum = data.Dataset.GetDatum(i); int label = data.Dataset.GetLabel(i); var augmented = augmentor.Augment(datum); initial.Update(augmented, label); } if (perturbe_only) { CIFAR.WriteData(CifarDataBatch + ".perturbed", initial); } else { CIFAR.WriteData(CifarDataBatch + ".augmented", initial); } }
static void Main(string[] args) { string CifarNNFile = null; string CifarDataBatch = null; bool just_accuracy = false; bool just_loss = false; bool raw_directory = false; var p = new OptionSet(); p.Add <string>("nnet=", "CIFAR neural network file name", (x => CifarNNFile = x)); p.Add <string>("dataset=", "CIFAR dataset file name", (x => CifarDataBatch = x)); p.Add <string>("rawdir", "If set then --dataset value should be a directory in raw directory format", (x => raw_directory = (x != null))); p.Add <bool> ("optimization=", "Do optimization (Default: true)", (x => RobustnessOptions.DoOptimization = 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 <double>("bound=", "Linfinity-ball to search", (x => RobustnessOptions.Epsilon = x)); p.Add <double>("minval=", "Minimum value of each entry", (x => RobustnessOptions.MinValue = x)); p.Add <double>("maxval=", "Maximum value of each entry", (x => RobustnessOptions.MaxValue = x)); p.Add <string>("no-quant-safety", "Quantization integrality safety off", (x => RobustnessOptions.QuantizationSafety = (x == null))); p.Add <double>("scale-preprocessed=", "If image data is preprocessed, scale before dumping to registry", (x => RobustnessOptions.ScalePreProcessed = x)); p.Add <double>("offset-preprocessed=", "If image data is preprocessed, offset scaled before dumping to registry", (x => RobustnessOptions.OffsetPreProcessed = x)); 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_filter = false; double filter_conf = 0.98; p.Add("only-filter", "Only filter by confidence", (x => only_filter = (x != null))); p.Add <double>("filter-conf=", "Filter confidence", (x => filter_conf = x)); Cmd.RunOptionSet(p, args); if (CifarNNFile == null || CifarDataBatch == null) { Console.WriteLine("Invalid arguments, use --help"); Environment.Exit(1); } /* Initialize parameters */ Options.InitializeNNAnalysis(); NeuralNet nn = CIFAR.GetNN(CifarNNFile); ImageDataset data; if (raw_directory) { // our raw data format (see lmdb2raw.py) data = CIFAR.ReadDirectoryData(CifarDataBatch); } else { // Plain old CIFAR binary format data = CIFAR.ReadData(CifarDataBatch, CIFAR.ALL_IMAGES, 0); } if (just_accuracy) { NNAccuracy.GetAccuracy(nn, data.Dataset); return; } if (just_loss) { NNAccuracy.GetLoss(nn, data.Dataset); return; } if (only_filter) { string filtered = RobustnessOptions.Registry + "-filtered-" + filter_conf.ToString(); Console.WriteLine("Orig {0} data", data.Dataset.Count()); var ds = NNAccuracy.KeepAboveConfidenceThreshold(nn, data.Dataset, filter_conf); Console.WriteLine("Kept {0} data", ds.Count()); ImageDataset ret = new ImageDataset(ds, CIFAR.InputCoordinates.ChannelCount, CIFAR.InputCoordinates.RowCount, CIFAR.InputCoordinates.ColumnCount, true); CIFAR.WriteData(filtered, ret); return; } RobustnessOptions.Dump(); string synthImagesName = RobustnessOptions.Registry + "-synth"; int labelcount = data.Dataset.LabelCount(); ImageDataset acc = new ImageDataset(new Dataset(labelcount), CIFAR.InputCoordinates.ChannelCount, CIFAR.InputCoordinates.RowCount, CIFAR.InputCoordinates.ColumnCount, true); int state = 0; Action <LabelWithConfidence> snapshot = x => { acc.Dataset.Data.Add(new MemAccessor <double[]>(x.datum)); acc.Dataset.Labels.Add(new MemAccessor <int>(x.actualLabel)); state++; if (state >= 4) { CIFAR.WriteData(synthImagesName, acc); state = 0; } }; ImageDataset synth = Robustness.SynthesizeCounterexamplesAndStore(nn, data, snapshot); if (synth.Dataset.Count() == 0) { Console.WriteLine("Did not synthesize any counterexamples, nothing to dump ..."); return; } if (raw_directory) { throw new NotImplementedException("Output to raw directory format not yet implemented!"); } else { CIFAR.WriteData(RobustnessOptions.Registry + "-synth", synth); } }