public static void Main(string[] args) { var nn = NeuralNetwork.Load(@"..\..\DataFiles\MNIST.model"); var testData = MNIST.LoadMNISTData(@"..\..\DataFiles\test_10k_28x28.bin"); nn.ValidateWithConfusionMatrix(testData); }
public void PrepareData() { mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: TrainSize, validation_size: ValidationSize, test_size: TestSize); // In this example, we limit mnist data (Xtr, Ytr) = mnist.train.next_batch(TrainSize == null ? 5000 : TrainSize.Value / 100); // 5000 for training (nn candidates) (Xte, Yte) = mnist.test.next_batch(TestSize == null ? 200 : TestSize.Value / 100); // 200 for testing }
static void Main(string[] args) { string MNISTData = null; string MNISTLabels = null; int split_size = 0; var p = new OptionSet(); p.Add("datafile=", "MNIST data file name", x => MNISTData = x); p.Add("labelfile=", "MNIST label file name", x => MNISTLabels = x); p.Add <int>("split-size=", "Number of images per split", (x => split_size = x)); Cmd.RunOptionSet(p, args); if (MNISTData == null || MNISTData == null || split_size <= 0) { Console.WriteLine("Invalid arguments, use --help"); Environment.Exit(1); } /* Initialize parameters */ Options.InitializeNNAnalysis(); ImageDataset data = MNIST.ReadData(MNISTLabels, MNISTData, MNIST.ALL_IMAGES, 0); // Split var splits = data.ShuffleSplitMany(split_size); int count = 0; foreach (var s in splits) { MNIST.WriteData(MNISTLabels + ".split_" + count.ToString(), MNISTData + ".split_" + count.ToString(), s); count++; } }
public void PrepareData() { mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size: validation_size, test_size: test_size); full_data_x = mnist.train.data; // download graph meta data string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/kmeans.meta"; Web.Download(url, "graph", "kmeans.meta"); }
public void PrepareData() { mnist = MNIST.read_data_sets("mnist", one_hot: true); (x_train, y_train) = (mnist.train.data, mnist.train.labels); (x_valid, y_valid) = (mnist.validation.data, mnist.validation.labels); (x_test, y_test) = (mnist.test.data, mnist.test.labels); print("Size of:"); print($"- Training-set:\t\t{len(mnist.train.data)}"); print($"- Validation-set:\t{len(mnist.validation.data)}"); print($"- Test-set:\t\t{len(mnist.test.data)}"); }
static void Main(string[] args) { string MNISTData = null; string MNISTLabels = null; string[] split_data_files = null; string[] split_label_files = null; var p = new OptionSet(); p.Add("datafile=", "MNIST data file name to store result", x => MNISTData = x); p.Add("labelfile=", "MNIST label file name to store result", x => MNISTLabels = x); p.Add("datafiles=", "MNIST data batches to join", x => split_data_files = x.Split()); p.Add("labelfiles=", "MNIST label batches to join (corresponding 1-1 with datafiles)", x => split_label_files = x.Split()); Cmd.RunOptionSet(p, args); if (MNISTData == null || MNISTLabels == null || split_data_files == null || split_label_files == null || split_data_files.Length != split_label_files.Length) { 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_data_files.Length; i++) { Console.Write(split_data_files[i] + " / " + split_label_files[i]); var datum = MNIST.ReadData(split_label_files[i], split_data_files[i], MNIST.ALL_IMAGES, 0); dss.Add(datum); } var data = Data.UnionMany(dss); Console.WriteLine("Output file ..."); Console.WriteLine(MNISTData + " / " + MNISTLabels); MNIST.WriteData(MNISTLabels, MNISTData, data); }
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); }
static void Main(string[] args) { Console.WriteLine("Input directory name you want to process: "); string dirName = Console.ReadLine(); MNIST neuralNetwork = new MNIST(); /*neuralNetwork.OnProcessedPicture += (s) => Console.WriteLine(s); * neuralNetwork.OnAllTasksFinished += () => { Environment.Exit(0); }; * * try * { * neuralNetwork.ScanDirectory(MNIST.GetFilesFromDirectory(dirName)); * } * catch (Exception ex) * { * Console.WriteLine(ex.Message); * return; * } * * while (Console.ReadKey().Key != ConsoleKey.Escape); * neuralNetwork.Cancel();*/ }
public void PrepareData() { mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size: validation_size, test_size: test_size); }
public static void Run() { int batch_size = 128; int num_classes = 10; int epochs = 12; // input image dimensions int img_rows = 28, img_cols = 28; Shape input_shape = null; // the data, split between train and test sets var((x_train, y_train), (x_test, y_test)) = MNIST.LoadData(); if (K.ImageDataFormat() == "channels_first") { x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols); x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols); input_shape = (1, img_rows, img_cols); } else { x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1); x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1); input_shape = (img_rows, img_cols, 1); } x_train = x_train.astype(np.float32); x_test = x_test.astype(np.float32); x_train /= 255; x_test /= 255; Console.WriteLine("x_train shape: " + x_train.shape); Console.WriteLine(x_train.shape[0] + " train samples"); Console.WriteLine(x_test.shape[0] + " test samples"); // convert class vectors to binary class matrices y_train = Util.ToCategorical(y_train, num_classes); y_test = Util.ToCategorical(y_test, num_classes); // Build CNN model var model = new Sequential(); model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(), activation: "relu", input_shape: input_shape)); model.Add(new Conv2D(64, (3, 3).ToTuple(), activation: "relu")); model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple())); model.Add(new Dropout(0.25)); model.Add(new Flatten()); model.Add(new Dense(128, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(num_classes, activation: "softmax")); model.Compile(loss: "categorical_crossentropy", optimizer: new Adadelta(), metrics: new string[] { "accuracy" }); model.Fit(x_train, y_train, batch_size: batch_size, epochs: epochs, verbose: 1, validation_data: new NDarray[] { x_test, y_test }); var score = model.Evaluate(x_test, y_test, verbose: 0); Console.WriteLine("Test loss:" + score[0]); Console.WriteLine("Test accuracy:" + score[1]); }
static void Main(string[] args) { int batch_size = 128; //Training batch size int num_classes = 10; //No. of classes int epochs = 12; //No. of epoches we will train // input image dimensions int img_rows = 28, img_cols = 28; // Declare the input shape for the network Shape input_shape = null; // Load the MNIST dataset into Numpy array var((x_train, y_train), (x_test, y_test)) = MNIST.LoadData(); //Check if its channel fist or last and rearrange the dataset accordingly if (K.ImageDataFormat() == "channels_first") { x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols); x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols); input_shape = (1, img_rows, img_cols); } else { x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1); x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1); input_shape = (img_rows, img_cols, 1); } //Normalize the input data x_train = x_train.astype(np.float32); x_test = x_test.astype(np.float32); x_train /= 255; x_test /= 255; Console.WriteLine("x_train shape: " + x_train.shape); Console.WriteLine(x_train.shape[0] + " train samples"); Console.WriteLine(x_test.shape[0] + " test samples"); // Convert class vectors to binary class matrices y_train = Util.ToCategorical(y_train, num_classes); y_test = Util.ToCategorical(y_test, num_classes); // Build CNN model var model = new Sequential(); model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(), activation: "relu", input_shape: input_shape)); model.Add(new Conv2D(64, (3, 3).ToTuple(), activation: "relu")); model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple())); model.Add(new Dropout(0.25)); model.Add(new Flatten()); model.Add(new Dense(128, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(num_classes, activation: "softmax")); //Compile with loss, metrics and optimizer model.Compile(loss: "categorical_crossentropy", optimizer: new Adadelta(), metrics: new string[] { "accuracy" }); //Train the model model.Fit(x_train, y_train, batch_size: batch_size, epochs: epochs, verbose: 1, validation_data: new NDarray[] { x_test, y_test }); //Score the model for performance var score = model.Evaluate(x_test, y_test, verbose: 0); Console.WriteLine("Test loss:" + score[0]); Console.WriteLine("Test accuracy:" + score[1]); // Save the model to HDF5 format which can be loaded later or ported to other application model.Save("model.h5"); // Save it to Tensorflow JS format and we will test it in browser. var v = K.Instance; //model.SaveTensorflowJSFormat(@"C:\_temp\"); //model.SaveOnnx(@"C:\_temp\"); Console.ReadLine(); }
public void PrepareData() { mnist = MNIST.read_data_sets("mnist", one_hot: true); }
public StatisticController(PictureLibraryContext pictureLibraryContext) { this.pictureLibraryContext = pictureLibraryContext; mNIST = new MNIST(); }
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); }
public static void Run() { MNIST.LoadData(); }
static void Main(string[] args) { string MNISTData = null; string MNISTLabels = null; int how_many = 1; RANDTYPE randomness = RANDTYPE.UNIFORM; var p = new OptionSet(); p.Add("datafile=", "MNIST data file name", x => MNISTData = x); p.Add("labelfile=", "MNIST label file name", x => MNISTLabels = 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)); Cmd.RunOptionSet(p, args); if (MNISTData == null || MNISTLabels == null) { Console.WriteLine("Invalid arguments, use --help"); Environment.Exit(1); } /* Initialize parameters */ Options.InitializeNNAnalysis(); ImageDataset data = MNIST.ReadData(MNISTLabels, MNISTData, MNIST.ALL_IMAGES, 0); IAugmentor augmentor = null; // TODO if (geometric) { augmentor = new AugmentGeometric(MNIST.InputCoordinates, randomness, how_many, xoffset, yoffset); goto KONT; } if (random) { augmentor = new AugmentRandom(MNIST.InputCoordinates, randomness, how_many, epsilon); goto KONT; } if (brightness) { augmentor = new AugmentBrightness(MNIST.InputCoordinates, randomness, how_many, brightness_offset); goto KONT; } if (contrast) { augmentor = new AugmentContrast(MNIST.InputCoordinates, how_many, contrast_min_factor, contrast_max_factor); goto KONT; } KONT: int count = data.Dataset.Count(); for (int i = 0; i < count; i++) { double[] datum = data.Dataset.GetDatum(i); int label = data.Dataset.GetLabel(i); var augmented = augmentor.Augment(datum); data.Update(augmented, label); } MNIST.WriteData(MNISTLabels + ".augmented", MNISTData + ".augmented", data); }