public static void Main(string[] args) { MNIST.Main(args); AdversarialExampleGeneration.Main(args); CIFAR10.Main(args); SequenceToSequence.Main(args); TextClassification.Main(args); //ImageTransforms.Main(args); }
internal static void Main(string[] args) { var cwd = Environment.CurrentDirectory; var dataset = args.Length > 0 ? args[0] : "mnist"; var datasetPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "..", "Downloads", dataset); var _ = torch.random.manual_seed(1); //var device = torch.CPU; var device = torch.cuda.is_available() ? torch.CUDA : torch.CPU; Console.WriteLine($"\n Running AdversarialExampleGeneration on {device.type.ToString()}\n"); Console.WriteLine($"Dataset: {dataset}"); if (device.type == DeviceType.CUDA) { _trainBatchSize *= 4; _testBatchSize *= 4; _epochs *= 4; } var sourceDir = _dataLocation; var targetDir = Path.Combine(_dataLocation, "test_data"); if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-images-idx3-ubyte.gz"), targetDir); Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-labels-idx1-ubyte.gz"), targetDir); Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-images-idx3-ubyte.gz"), targetDir); Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-labels-idx1-ubyte.gz"), targetDir); } MNIST.Model model = null; var normImage = torchvision.transforms.Normalize(new double[] { 0.1307 }, new double[] { 0.3081 }, device: (Device)device); using (var test = new MNISTReader(targetDir, "t10k", _testBatchSize, device: device, transform: normImage)) { var modelFile = dataset + ".model.bin"; if (!File.Exists(modelFile)) { // We need the model to be trained first, because we want to start with a trained model. Console.WriteLine($"\n Running MNIST on {device.type.ToString()} in order to pre-train the model."); model = new MNIST.Model("model", device); using (var train = new MNISTReader(targetDir, "train", _trainBatchSize, device: device, shuffle: true, transform: normImage)) { MNIST.TrainingLoop(dataset, (Device)device, model, train, test); } Console.WriteLine("Moving on to the Adversarial model.\n"); } else { model = new MNIST.Model("model", torch.CPU); model.load(modelFile); } model.to((Device)device); model.eval(); var epsilons = new double[] { 0, 0.05, 0.1, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50 }; foreach (var ε in epsilons) { var attacked = Test(model, nll_loss(), ε, test, test.Size); Console.WriteLine($"Epsilon: {ε:F2}, accuracy: {attacked:P2}"); } } }