def test_fit_generator(self): import torch from torch.utils.data import DataLoader from art.data_generators import PyTorchDataGenerator (x_train, y_train), (x_test, y_test) = self.mnist acc = np.sum( np.argmax(self.module_classifier.predict(x_test), axis=1) == np.argmax(y_test, axis=1)) / NB_TEST logger.info('Accuracy: %.2f%%', (acc * 100)) # Create tensors from data x_train_tens = torch.from_numpy(x_train) x_train_tens = x_train_tens.float() y_train_tens = torch.from_numpy(y_train) # Create PyTorch dataset and loader dataset = torch.utils.data.TensorDataset(x_train_tens, y_train_tens) data_loader = DataLoader(dataset=dataset, batch_size=5, shuffle=True) data_gen = PyTorchDataGenerator(data_loader, size=NB_TRAIN, batch_size=5) # Fit model with generator self.module_classifier.fit_generator(data_gen, nb_epochs=2) acc2 = np.sum( np.argmax(self.module_classifier.predict(x_test), axis=1) == np.argmax(y_test, axis=1)) / NB_TEST logger.info('Accuracy: %.2f%%', (acc * 100)) self.assertTrue(acc2 >= .8 * acc)
def _image_data_generator(**kwargs): (x_train_mnist, y_train_mnist), (_, _) = get_default_mnist_subset if framework == "keras" or framework == "kerastf": return KerasDataGenerator( iterator=image_iterator, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "tensorflow": if not is_tf_version_2: return TensorFlowDataGenerator( sess=kwargs["sess"], iterator=image_iterator, iterator_type="initializable", iterator_arg={}, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "pytorch": return PyTorchDataGenerator(iterator=image_iterator, size=x_train_mnist.shape[0], batch_size=default_batch_size) if framework == "mxnet": return MXDataGenerator(iterator=image_iterator, size=x_train_mnist.shape[0], batch_size=default_batch_size)
def test_fit_generator(self): classifier = get_image_classifier_pt() accuracy = (np.sum( np.argmax(classifier.predict(self.x_test_mnist), axis=1) == np.argmax(self.y_test_mnist, axis=1)) / self.n_test) logger.info("Accuracy: %.2f%%", (accuracy * 100)) # Create tensors from data x_train_tens = torch.from_numpy(self.x_train_mnist) x_train_tens = x_train_tens.float() y_train_tens = torch.from_numpy(self.y_train_mnist) # Create PyTorch dataset and loader dataset = torch.utils.data.TensorDataset(x_train_tens, y_train_tens) data_loader = DataLoader(dataset=dataset, batch_size=5, shuffle=True) data_gen = PyTorchDataGenerator(data_loader, size=self.n_train, batch_size=5) # Fit model with generator classifier.fit_generator(data_gen, nb_epochs=2) accuracy_2 = (np.sum( np.argmax(classifier.predict(self.x_test_mnist), axis=1) == np.argmax(self.y_test_mnist, axis=1)) / self.n_test) logger.info("Accuracy: %.2f%%", (accuracy_2 * 100)) self.assertEqual(accuracy, 0.32) self.assertAlmostEqual(accuracy_2, 0.75, delta=0.1)
class TestPyTorchGenerator(unittest.TestCase): def setUp(self): import torch from torch.utils.data import DataLoader master_seed(seed=42) class DummyDataset(torch.utils.data.Dataset): def __init__(self): self._size = 10 self._x = np.random.rand(self._size, 1, 5, 5) self._y = np.random.randint(0, high=10, size=self._size) def __len__(self): return self._size def __getitem__(self, idx): return self._x[idx], self._y[idx] dataset = DummyDataset() data_loader = DataLoader(dataset=dataset, batch_size=5, shuffle=True) self.data_gen = PyTorchDataGenerator(data_loader, size=10, batch_size=5) def test_gen_interface(self): x, y = self.data_gen.get_batch() # Check return types self.assertTrue(isinstance(x, np.ndarray)) self.assertTrue(isinstance(y, np.ndarray)) # Check shapes self.assertEqual(x.shape, (5, 1, 5, 5)) self.assertEqual(y.shape, (5, )) def test_pytorch_specific(self): import torch iter_ = iter(self.data_gen.iterator) x, y = next(iter_) # Check return types self.assertTrue(isinstance(x, torch.Tensor)) self.assertTrue(isinstance(y, torch.Tensor)) # Check shapes self.assertEqual(x.shape, (5, 1, 5, 5)) self.assertEqual(y.shape, (5, )) def test_error(self): with self.assertRaises(TypeError): self.data_gen = PyTorchDataGenerator("data_loader", size=10, batch_size=5)
def setUp(self): import torch from torch.utils.data import DataLoader master_seed(42) class DummyDataset(torch.utils.data.Dataset): def __init__(self): self._size = 10 self._x = np.random.rand(self._size, 1, 5, 5) self._y = np.random.randint(0, high=10, size=self._size) def __len__(self): return self._size def __getitem__(self, idx): return self._x[idx], self._y[idx] dataset = DummyDataset() data_loader = DataLoader(dataset=dataset, batch_size=5, shuffle=True) self.data_gen = PyTorchDataGenerator(data_loader, size=10, batch_size=5)
def _image_data_generator(**kwargs): (x_train_mnist, y_train_mnist), (_, _) = get_default_mnist_subset image_it = image_iterator() data_generator = None if framework == "keras" or framework == "kerastf": data_generator = KerasDataGenerator( iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "tensorflow1": data_generator = TensorFlowDataGenerator( sess=kwargs["sess"], iterator=image_it, iterator_type="initializable", iterator_arg={}, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "pytorch": data_generator = PyTorchDataGenerator( iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size) if framework == "mxnet": data_generator = MXDataGenerator(iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size) if data_generator is None: raise ARTTestFixtureNotImplemented( "framework {0} does not current have any data generator implemented", image_data_generator.__name__, framework, ) return data_generator
def _image_data_generator(**kwargs): (x_train_mnist, y_train_mnist), (_, _) = get_default_mnist_subset image_it = image_iterator() data_generator = None if framework == "keras" or framework == "kerastf": data_generator = KerasDataGenerator( iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "tensorflow1": data_generator = TensorFlowDataGenerator( sess=kwargs["sess"], iterator=image_it, iterator_type="initializable", iterator_arg={}, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "tensorflow2": data_generator = TensorFlowV2DataGenerator( iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size, ) if framework == "pytorch": data_generator = PyTorchDataGenerator( iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size) if framework == "mxnet": data_generator = MXDataGenerator(iterator=image_it, size=x_train_mnist.shape[0], batch_size=default_batch_size) return data_generator
eps=8.0 / 255.0, eps_step=2.0 / 255.0, max_iter=40, targeted=False, num_random_init=5, batch_size=32, ) # Step 4: Create the trainer object - AdversarialTrainerFBFPyTorch # if you have apex installed, change use_amp to True epsilon = 8.0 / 255.0 trainer = AdversarialTrainerFBFPyTorch(classifier, eps=epsilon, use_amp=False) # Build a Keras image augmentation object and wrap it in ART art_datagen = PyTorchDataGenerator(iterator=dataloader, size=x_train.shape[0], batch_size=128) # Step 5: fit the trainer trainer.fit_generator(art_datagen, nb_epochs=30) x_test_pred = np.argmax(classifier.predict(x_test), axis=1) print( "Accuracy on benign test samples after adversarial training: %.2f%%" % (np.sum(x_test_pred == np.argmax(y_test, axis=1)) / x_test.shape[0] * 100)) x_test_attack = attack.generate(x_test) x_test_attack_pred = np.argmax(classifier.predict(x_test_attack), axis=1) print( "Accuracy on original PGD adversarial samples after adversarial training: %.2f%%" % (np.sum(x_test_attack_pred == np.argmax(y_test, axis=1)) /
def test_error(self): with self.assertRaises(TypeError): self.data_gen = PyTorchDataGenerator("data_loader", size=10, batch_size=5)
def fit_generator(self, train_generator, epochs): train_generator_art = PyTorchDataGenerator( train_generator, len(train_generator.dataset), train_generator.batch_size) self.adversarial_trainer.fit_generator(train_generator_art, nb_epochs=epochs)