Ejemplo n.º 1
0
from __future__ import absolute_import, division, print_function, unicode_literals

import sys
from os.path import abspath

sys.path.append(abspath('.'))

import numpy as np
import numpy.linalg as LA

from art.attacks.carlini import CarliniL2Method
from models.utils import load_mnist_vectorized
from old_models import neural_networks

# Read MNIST dataset
(x_train, y_train), (x_test, y_test), min_, max_ = load_mnist_vectorized()

print(x_train.shape)

l1_reg_levels = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005]

for l1_level in range(0, 5):
    classifier = neural_networks.two_layer_dnn_art(x_train.shape[1:], 0,
                                                   l1_reg_levels[l1_level], 0)
    classifier.fit(x_train, y_train, nb_epochs=10, batch_size=128)

    # Evaluate the classifier on the test set
    preds = np.argmax(classifier.predict(x_test), axis=1)
    acc = np.sum(preds == np.argmax(y_test, axis=1)) / y_test.shape[0]
    print("\nTest accuracy on L1 regularization level %.5f%%: %.2f%%" %
          (l1_reg_levels[l1_level], acc * 100))
np.set_printoptions(threshold=sys.maxsize)

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

x_train = None
y_train = None
x_test = None
y_test = None
min_ = None
max_ = None

if args.dataset == 'mnist':
    if args.experiment_type in ["three_layer", "five_layer", "six_layer"]:
        (x_train, y_train), (x_test,
                             y_test), min_, max_ = load_mnist_vectorized()
    elif args.experiment_type in ["VGG", "leNet"]:
        (x_train, y_train), (x_test, y_test), min_, max_ = load_mnist()
        # Pad images to 32x32 size
        x_train = np.pad(x_train, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
        x_test = np.pad(x_test, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
elif args.dataset == 'cifar':
    (x_train, y_train), (x_test, y_test), min_, max_ = load_cifar10()
else:
    raise Exception("Invalid dataset!")

input_shape = x_train.shape[1:]
output_shape = len(y_train[0])

ones = np.where(np.argmax(y_test, axis=1) == 1)[0]