Exemplo n.º 1
0
def model_z(nb_filters=32, nb_classes=10, input_shape=(None, 28, 28, 1)):
    layers = [
        Conv2D(nb_filters, (3, 3), (1, 1), "SAME"),
        ReLU(),
        Conv2D(nb_filters, (3, 3), (2, 2), "VALID"),
        ReLU(),
        Conv2D(2 * nb_filters, (3, 3), (1, 1), "VALID"),
        ReLU(),
        Conv2D(2 * nb_filters, (3, 3), (2, 2), "VALID"),
        ReLU(),
        Conv2D(4 * nb_filters, (3, 3), (1, 1), "VALID"),
        ReLU(),
        Conv2D(4 * nb_filters, (3, 3), (2, 2), "VALID"),
        ReLU(),
        Flatten(),
        Linear(600),
        ReLU(),
        Dropout(0.5),
        Linear(600),
        ReLU(),
        Dropout(0.5),
        Linear(nb_classes),
        Softmax()
    ]

    model = DefenseMLP(layers, input_shape)
    return model
Exemplo n.º 2
0
def make_basic_picklable_cnn(nb_filters=64,
                             nb_classes=10,
                             input_shape=(None, 32, 32, 3)):
    """The model for the picklable models tutorial.
  """

    if VERSION == 1:
        layers = [
            Conv2D(nb_filters, (8, 8), (2, 2), "SAME"),
            ReLU(),
            Conv2D(nb_filters * 2, (6, 6), (2, 2), "VALID"),
            ReLU(),
            Conv2D(nb_filters * 2, (5, 5), (1, 1), "VALID"),
            ReLU(),
            Flatten(),
            Linear(nb_classes),
            Softmax()
        ]
        model = MLP(layers, input_shape)

    else:

        layers = [
            PerImageStandardize(),
            Conv2D(nb_filters, (3, 3), (1, 1), "SAME"),
            ReLU(),
            ResidualWithInstanceNorm(nb_filters, 2),
            ResidualWithInstanceNorm(nb_filters, 1),
            ResidualWithInstanceNorm(nb_filters * 2, 2),
            ResidualWithInstanceNorm(nb_filters * 2, 1),
            ResidualWithInstanceNorm(nb_filters * 4, 2),
            ResidualWithInstanceNorm(nb_filters * 4, 1),
            ResidualWithInstanceNorm(nb_filters * 8, 2),
            ResidualWithInstanceNorm(nb_filters * 8, 1),
            GlobalAveragePool(),
            Linear(nb_classes),
            Softmax()
        ]
        model = MLP(layers, input_shape)

    return model
Exemplo n.º 3
0
 def get_model(self, scope):
     """The model for the picklable models tutorial.
 """
     if self.dataset_name == 'MNIST':
         nb_filters = 64
         nb_classes = self.nb_classes
         input_shape = (None, 28, 28, 1)
         layers = [
             Conv2D(nb_filters, (8, 8), (2, 2), "SAME"),
             ReLU(),
             Conv2D(nb_filters * 2, (6, 6), (2, 2), "VALID"),
             ReLU(),
             Conv2D(nb_filters * 2, (5, 5), (1, 1), "VALID"),
             ReLU(),
             Flatten(),
             Linear(nb_classes),
             Softmax()
         ]
         model = MLP(layers, input_shape)
     if self.dataset_name == 'SVHN':
         nb_filters = 64
         nb_classes = self.nb_classes
         input_shape = (None, 32, 32, 3)
         layers = [
             Conv2D(nb_filters, (8, 8), (2, 2), "SAME"),
             ReLU(),
             Conv2D(nb_filters * 2, (6, 6), (2, 2), "VALID"),
             ReLU(),
             Conv2D(nb_filters * 2, (5, 5), (1, 1), "VALID"),
             ReLU(),
             Flatten(),
             Linear(nb_classes),
             Softmax()
         ]
         model = MLP(layers, input_shape)
     elif self.dataset_name == 'CIFAR10':
         model = make_wresnet(scope=scope)
     return model
Exemplo n.º 4
0
def make_basic_picklable_cnn(nb_filters=64, nb_classes=10,
                             input_shape=(None, 28, 28, 1)):
  """The model for the picklable models tutorial.
  """
  layers = [Conv2D(nb_filters, (8, 8), (2, 2), "SAME"),
            ReLU(),
            Conv2D(nb_filters * 2, (6, 6), (2, 2), "VALID"),
            ReLU(),
            Conv2D(nb_filters * 2, (5, 5), (1, 1), "VALID"),
            ReLU(),
            Flatten(),
            Linear(nb_classes),
            Softmax()]
  model = MLP(layers, input_shape)
  return model
Exemplo n.º 5
0
def model_f(nb_filters=64, nb_classes=10, input_shape=(None, 28, 28, 1)):
    layers = [
        Conv2D(nb_filters, (8, 8), (2, 2), "SAME", use_bias=True),
        ReLU(),
        Conv2D(nb_filters * 2, (6, 6), (2, 2), "VALID", use_bias=True),
        ReLU(),
        Conv2D(nb_filters * 2, (5, 5), (1, 1), "VALID", use_bias=True),
        ReLU(),
        Flatten(),
        Linear(nb_classes),
        Softmax()
    ]

    model = DefenseMLP(layers, input_shape)
    return model
Exemplo n.º 6
0
def make_basic_picklable_substitute(nb_filters=200,
                                    nb_classes=2,
                                    input_shape=(None, 28, 28, 1)):
    """The model for the picklable models tutorial.
  """
    layers = [
        Flatten(),
        Linear(nb_filters),
        ReLU(),
        Linear(nb_filters),
        ReLU(),
        Linear(nb_classes),
        Softmax()
    ]
    model = MLP(layers, input_shape)
    return model
Exemplo n.º 7
0
def model_a(nb_filters=64, nb_classes=10, input_shape=(None, 28, 28, 1)):
    layers = [
        Conv2D(nb_filters, (5, 5), (1, 1), "SAME", use_bias=True),
        ReLU(),
        Conv2D(nb_filters, (5, 5), (2, 2), "VALID", use_bias=True),
        ReLU(),
        Flatten(),
        Dropout(0.25),
        Linear(128),
        ReLU(),
        Dropout(0.5),
        Linear(nb_classes),
        Softmax()
    ]

    model = DefenseMLP(layers, input_shape, feature_layer='ReLU7')
    return model
Exemplo n.º 8
0
def model_train(file_name=FILE_NAME):
    """
    Creates the joblib file of AllConvolutional CIFAR-10 model trained over the MNIST dataset.

    Parameters
    ----------
    file_name: str, optional
        The name of the joblib file.
    """

    layers = [Conv2D(64, (3, 3), (1, 1), "SAME"),
              ReLU(),
              Conv2D(128, (3, 3), (1, 1), "SAME"),
              ReLU(),
              MaxPooling2D((2, 2), (2, 2), "VALID"),
              Conv2D(128, (3, 3), (1, 1), "SAME"),
              ReLU(),
              Conv2D(256, (3, 3), (1, 1), "SAME"),
              ReLU(),
              MaxPooling2D((2, 2), (2, 2), "VALID"),
              Conv2D(256, (3, 3), (1, 1), "SAME"),
              ReLU(),
              Conv2D(512, (3, 3), (1, 1), "SAME"),
              ReLU(),
              MaxPooling2D((2, 2), (2, 2), "VALID"),
              Conv2D(10, (3, 3), (1, 1), "SAME"),
              GlobalAveragePool(),
              Softmax()]

    model = MLP(layers, (None, 32, 32, 3))

    cifar10 = CIFAR10(train_start=0, train_end=50000, test_start=0, test_end=10000)
    x_train, y_train = cifar10.get_set('train')
    x_test, y_test = cifar10.get_set('test')

    y_train = y_train.reshape((50000, 10))
    y_test = y_test.reshape((10000, 10))

    model_training(model, file_name, x_train, y_train, x_test, y_test, nb_epochs=10, batch_size=128,
                   learning_rate=.001, label_smoothing=0.1)
Exemplo n.º 9
0
def model_train(file_name=FILE_NAME):
    """
    Creates the joblib file of LeNet-5 trained over the MNIST dataset.

    Parameters
    ----------
    file_name: str, optional
        The name of the joblib file.
    """

    layers = [
        Conv2D(20, (5, 5), (1, 1), "VALID"),
        ReLU(),
        MaxPooling2D((2, 2), (2, 2), "VALID"),
        Conv2D(50, (5, 5), (1, 1), "VALID"),
        ReLU(),
        MaxPooling2D((2, 2), (2, 2), "VALID"),
        Flatten(),
        Linear(500),
        ReLU(),
        Linear(10),
        Softmax()
    ]

    model = MLP(layers, (None, 28, 28, 1))

    mnist = MNIST(train_start=0, train_end=60000, test_start=0, test_end=10000)
    x_train, y_train = mnist.get_set('train')
    x_test, y_test = mnist.get_set('test')

    model_training(model,
                   file_name,
                   x_train,
                   y_train,
                   x_test,
                   y_test,
                   nb_epochs=20,
                   batch_size=128,
                   learning_rate=0.001)
Exemplo n.º 10
0
def model_e(input_shape=(None, 28, 28, 1), nb_classes=10):
    """
    Defines the model architecture to be used by the substitute. Use
    the example model interface.
    :param img_rows: number of rows in input
    :param img_cols: number of columns in input
    :param nb_classes: number of classes in output
    :return: tensorflow model
    """

    # Define a fully connected model (it's different than the black-box).
    layers = [
        Flatten(),
        Linear(200),
        ReLU(),
        Linear(200),
        ReLU(),
        Linear(nb_classes),
        Softmax()
    ]

    return DefenseMLP(layers, input_shape)
Exemplo n.º 11
0
def model_train(attack):
    """
    Creates the joblib file of LeNet-5 trained over the augmented MNIST dataset.

    Parameters
    ----------
    attack: str
        The augmented dataset used (either "jsma", "wjsma" or "tjsma").
    """

    layers = [
        Conv2D(20, (5, 5), (1, 1), "VALID"),
        ReLU(),
        MaxPooling2D((2, 2), (2, 2), "VALID"),
        Conv2D(50, (5, 5), (1, 1), "VALID"),
        ReLU(),
        MaxPooling2D((2, 2), (2, 2), "VALID"),
        Flatten(),
        Linear(500),
        ReLU(),
        Linear(10),
        Softmax()
    ]

    model = MLP(layers, (None, 28, 28, 1))

    mnist = MNIST(train_start=TRAIN_START, train_end=TRAIN_END, test_start=TEST_START, test_end=TEST_END)
    x_train, y_train = mnist.get_set('train')
    x_test, y_test = mnist.get_set('test')

    x_add = np.load("defense/augmented/" + attack + "_x.npy")[:AUGMENT_SIZE]
    y_add = np.load("defense/augmented/" + attack + "_y.npy")[:AUGMENT_SIZE]

    x_train = np.concatenate((x_train, x_add.reshape(x_add.shape + (1,))), axis=0).astype(np.float32)
    y_train = np.concatenate((y_train, y_add), axis=0).astype(np.float32)

    model_training(model, "mnist_defense_" + attack + ".joblib", x_train, y_train, x_test, y_test, nb_epochs=NB_EPOCHS,
                   batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE)
Exemplo n.º 12
0
def model_y(nb_filters=64, nb_classes=10, input_shape=(None, 28, 28, 1)):
    layers = [
        Conv2D(nb_filters, (3, 3), (1, 1), "SAME"),
        ReLU(),
        Conv2D(nb_filters, (3, 3), (2, 2), "VALID"),
        ReLU(),
        Conv2D(2 * nb_filters, (3, 3), (2, 2), "VALID"),
        ReLU(),
        Conv2D(2 * nb_filters, (3, 3), (2, 2), "VALID"),
        ReLU(),
        Flatten(),
        Linear(256),
        ReLU(),
        Dropout(0.5),
        Linear(256),
        ReLU(),
        Dropout(0.5),
        Linear(nb_classes),
        Softmax()
    ]

    model = DefenseMLP(layers, input_shape, feature_layer='ReLU13')
    return model