Computer Vision News - May 2018

14. # Read CIFAR10 dataset 15. (x_train, y_train), (x_test, y_test), min_, max_ = load_dataset('cifar10') 16. x_train, y_train = x_train[:5000], y_train[:5000] 17. x_test, y_test = x_test[:500], y_test[:500] 18. im_shape = x_train[0].shape 19. # Construct a convolutional neural network 20. comp_params = {'loss': 'categorical_crossentropy', 21. 'optimizer': 'adam', 22. 'metrics': ['accuracy']} 23. classifier = CNN(im_shape, act='relu', dataset='cifar10') 24. classifier.compile(comp_params) 25. classifier.fit(x_train, y_train, validation_split=.1, epochs=10, batch_size=128) 26. # Craft adversarial samples with DeepFool 27. print('Create DeepFool attack') 28. epsilon = .1 # Maximum perturbation 29. adv_crafter = DeepFool(classifier, sess=session) 30. print('Craft training examples') 31. x_train_adv = adv_crafter.generate(x_val=x_train, eps=epsilon, clip_min=min_, clip_max=max_) 32. print('Craft test examples') 33. x_test_adv = adv_crafter.generate(x_val=x_test, eps=epsilon, clip_min=min_, clip_max=max_) 34. # Evaluate the classifier on the adversarial samples 35. scores = classifier.evaluate(x_test, y_test) 36. print("\nClassifier before adversarial training") 37. print("\nLoss on adversarial samples: %.2f%%\nAccuracy on adversarial samples: %.2f%%" % (scores[0], scores[1] * 100)) 38. # Data augmentation: expand the training set with the adversarial samples 39. x_train = append(x_train, x_train_adv, axis=0) 40. y_train = append(y_train, y_train, axis=0) 41. # Retrain the CNN on the extended dataset 42. classifier.compile(comp_params) 43. classifier.fit(x_train, y_train, validation_split=.1, epochs=10, batch_size=128) 44. # Evaluate the adversarially trained classifier on the test set 45. scores = classifier.evaluate(x_test, y_test) 46. print("\nClassifier with adversarial training") 47. print("\nLoss on adversarial samples: %.2f%%\nAccuracy on adversarial samples: %.2f%%" % (scores[0], scores[1] * 100)) 13 Tool Computer Vision News Adversarial Attacks on Deep Learning

RkJQdWJsaXNoZXIy NTc3NzU=