コード例 #1
0
    def test_load(self):
        def compare_state_dicts(d1, d2):
            cond = (set(d1.keys()) == set(d2.keys()))
            for i in d1.keys():
                cond = cond and bool(torch.all(d1[i] == d2[i]).item())
            return cond

        model = Model()
        loss_fn = nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters(), lr=0.01)
        classifier = PyTorchClassifier((0, 1), model, loss_fn, optimizer,
                                       (1, 28, 28), 10)

        import tempfile
        import os
        import copy

        orig_params = copy.deepcopy(classifier._model.state_dict())
        t_file = tempfile.NamedTemporaryFile()
        full_path = t_file.name
        t_file.close()
        base_name = os.path.basename(full_path)
        dir_name = os.path.dirname(full_path)

        classifier.save(base_name, path=dir_name)

        model = Model()  # Reinitialize the weights
        classifier = PyTorchClassifier((0, 1), model, loss_fn, optimizer,
                                       (1, 28, 28), 10)
        new_params = copy.deepcopy(classifier._model.state_dict())
        assert (not compare_state_dicts(new_params, orig_params))

        classifier.load_model_weights(base_name, path=dir_name)
        new_params = copy.deepcopy(classifier._model.state_dict())
        assert (compare_state_dicts(new_params, orig_params))
        os.remove(full_path + '.model')
コード例 #2
0
# Test the classifier
predictions = cifar_classifier.predict(x_test)
accuracy = np.sum(
    np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
print('Accuracy before attack: {}%'.format(accuracy * 100))

# Craft the adversarial examples
epsilon = 0.2  # Maximum perturbation
adv_crafter = FastGradientMethod(cifar_classifier, eps=epsilon)
x_test_adv = adv_crafter.generate(x=x_test)

# Test the classifier on adversarial exmaples
predictions = cifar_classifier.predict(x_test_adv)
accuracy = np.sum(
    np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
print('Accuracy after attack: {}%'.format(accuracy * 100))

cifar_classifier.save('cifar_fgsm_state_dict', 'models')

preprocess = Autoencoder()
preprocess.load_state_dict(torch.load('models/conv_autoencoder.pth'))

x_test_adv = torch.from_numpy(x_test_adv)
x_test_denoised = preprocess(x_test_adv)
x_test_denoised = x_test_denoised.detach().numpy()

predictions = cifar_classifier.predict(x_test_denoised)
accuracy = np.sum(
    np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
print('Accuracy after defense: {}%'.format(accuracy * 100))
コード例 #3
0
# Test the classifier
predictions = mnist_classifier.predict(x_test)
accuracy = np.sum(
    np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
print('Accuracy before attack: {}%'.format(accuracy * 100))

# Craft the adversarial examples
epsilon = 0.2  # Maximum perturbation
adv_crafter = FastGradientMethod(mnist_classifier, eps=epsilon)
x_test_adv = adv_crafter.generate(x=x_test)

# Test the classifier on adversarial exmaples
predictions = mnist_classifier.predict(x_test_adv)
accuracy = np.sum(
    np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
print('Accuracy after attack: {}%'.format(accuracy * 100))

mnist_classifier.save('mnist_fgsm_state_dict', 'models')

preprocess = Autoencoder()
preprocess.load_state_dict(torch.load('models/conv_autoencoder.pth'))

x_test_adv = torch.from_numpy(x_test_adv)
x_test_denoised = preprocess(x_test_adv)
x_test_denoised = x_test_denoised.detach().numpy()

predictions = mnist_classifier.predict(x_test_denoised)
accuracy = np.sum(
    np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
print('Accuracy after defense: {}%'.format(accuracy * 100))