예제 #1
0
    def test_get_logits(self):
        # Define empty model
        model = Model('model', 10, {})
        x = []

        # Exception is thrown when `get_logits` not implemented
        with self.assertRaises(Exception) as context:
            model.get_logits(x)
        self.assertTrue(context.exception)
예제 #2
0
    def __init__(self, scope, nb_classes, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())

        # Do a dummy run of fprop to make sure the variables are created from
        # the start
        self.fprop(tf.placeholder(tf.float32, [128, 32, 32, 3]))
        # Put a reference to the params in self so that the params get pickled
        self.params = self.get_params()
예제 #3
0
    def test_fprop(self):
        # Define empty model
        model = Model('model', 10, {})
        x = []

        # Exception is thrown when `fprop` not implemented
        with self.assertRaises(Exception) as context:
            model.fprop(x)
        self.assertTrue(context.exception)
예제 #4
0
    def test_get_layer(self):
        # Define empty model
        model = Model()
        x = []

        # Exception is thrown when `get_layer` not implemented
        with self.assertRaises(Exception) as context:
            model.get_layer(x, layer='')
        self.assertTrue(context.exception)
예제 #5
0
    def test_fprop(self):
        # Define empty model
        model = Model()
        x = []

        # Exception is thrown when `fprop` not implemented
        with self.assertRaises(Exception) as context:
            model.fprop(x)
        self.assertTrue(context.exception)
예제 #6
0
    def __init__(self, scope, nb_classes, input_shape, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.input_shape = input_shape

        # Do a dummy run of fprop to create the variables from the start
        self.is_training = False
        self.fprop(tf.placeholder(tf.float32, [100] + input_shape))
        # Put a reference to the params in self so that the params get pickled
        self.params = self.get_params()
예제 #7
0
 def __init__(self, scope, **kwargs):
     del kwargs
     Model.__init__(self, scope, locals())
     self.n_units = 100
     # Do a dummy run of fprop to make sure the variables are created from
     # the start
     self.fprop(tf.placeholder(tf.float32, shape=(128, 28, 28, 1)))
     #self.fprop(tf.placeholder(tf.float32, shape = (128, 100, 1)))
     # Put a reference to the params in self so that the params get pickled
     self.params = self.get_params()
예제 #8
0
  def __init__(self, scope, **kwargs):
    del kwargs

    Model.__init__(self, scope, locals())
    #self.n_fcc = n_fcc
    self.latent_dim = 20
    #self.batch_size= batch_size
    # Do a dummy run of fprop to make sure the variables are created from
    # the start
    self.fprop(tf.placeholder(tf.float32, shape = (90, 32, 32, 3)))
예제 #9
0
    def __init__(self, scope, nb_classes, n, input_shape, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.n = n
        self.net = OrderedDict()

        # Do a dummy run of fprop to create the variables from the start
        self.fprop(tf.placeholder(tf.float32, [32] + input_shape))
        # Put a reference to the params in self so that the params get pickled
        self.params = self.get_params()
예제 #10
0
    def __init__(self, scope, nb_classes, hparams, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.nb_classes = nb_classes
        self.dataset = hparams['dataset']
        self.secret_seed = hparams['secret_seed']

        if self.dataset == 'mnist':
            self.fprop(tf.placeholder(tf.float32, [128, 28, 28, 1]))
        elif self.dataset == 'cifar' or self.dataset == 'cifar10':
            self.fprop(tf.placeholder(tf.float32, [128, 32, 32, 3]))
예제 #11
0
    def __init__(self, scope, nb_classes, num_dims, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.nb_filters = 64
        self.num_neurons = num_dims * 4
        self.num_dims = num_dims

        # Do a dummy run of fprop to make sure the variables are created from
        # the start
        self.fprop(tf.placeholder(tf.float32, [32, 1, 1, self.num_dims]))
        # Put a reference to the params in self so that the params get pickled
        self.params = self.get_params()
 def __init__(self, nb_classes=10):
     # NOTE: for compatibility with Madry Lab downloadable checkpoints,
     # we cannot use scopes, give these variables names, etc.
     self.W_conv1 = self._weight_variable([5, 5, 1, 32])
     self.b_conv1 = self._bias_variable([32])
     self.W_conv2 = self._weight_variable([5, 5, 32, 64])
     self.b_conv2 = self._bias_variable([64])
     self.W_fc1 = self._weight_variable([7 * 7 * 64, 1024])
     self.b_fc1 = self._bias_variable([1024])
     self.W_fc2 = self._weight_variable([1024, nb_classes])
     self.b_fc2 = self._bias_variable([nb_classes])
     Model.__init__(self, '', nb_classes, {})
예제 #13
0
 def __init__(self, nb_classes=10):
     # NOTE: for compatibility with Madry Lab downloadable checkpoints,
     # we cannot use scopes, give these variables names, etc.
     self.W_conv1 = self._weight_variable([5, 5, 1, 32])
     self.b_conv1 = self._bias_variable([32])
     self.W_conv2 = self._weight_variable([5, 5, 32, 64])
     self.b_conv2 = self._bias_variable([64])
     self.W_fc1 = self._weight_variable([7 * 7 * 64, 1024])
     self.b_fc1 = self._bias_variable([1024])
     self.W_fc2 = self._weight_variable([1024, nb_classes])
     self.b_fc2 = self._bias_variable([nb_classes])
     Model.__init__(self, '', nb_classes, {})
    def __init__(self, scope, nb_classes, nb_filters, input_shape, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.nb_filters = nb_filters
        self.input_shape = input_shape  # [32, 32, 3]
        self.dummpy_input = tf.placeholder(tf.float32,
                                           [FLAGS.batch_size] + input_shape)

        # Do a dummy run of fprop to create the variables from the start
        # BATCH_SIZE, img_rows, img_cols, nchannels
        self.fprop(self.dummpy_input)
        # Put a reference to the params in self so that the params get pickled
        self.params = self.get_params()
예제 #15
0
	def __init__(self, scope, nb_classes, nb_filters, input_shape, **kwargs):
		del kwargs
		Model.__init__(self, scope, nb_classes, locals())
		self.nb_filters = nb_filters
		self.input_shape = input_shape

		# Do a dummy run of fprop to create the variables from the start
		self.fprop(tf.placeholder(tf.float32, [32] + input_shape))
		# Put a reference to the params in self so that the params get pickled
		self.params = self.get_params()

		self.d1 = tf.layers.Dense(128, activation='relu')
		self.d2 = tf.layers.Dense(2, activation='softmax')
예제 #16
0
    def __init__(self, scope, nb_classes, nb_filters, weights, **kwargs):
        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.nb_filters = 32

        self.weights = weights

        self.layer_info = np_weights(self.weights)
        print(self.layer_info[0][0].shape, self.layer_info[0][1].shape)
        print(self.layer_info[1][0].shape, self.layer_info[1][1].shape)
        print(self.layer_info[2][0].shape, self.layer_info[2][1].shape)
        # Do a dummy run of fprop to make sure the variables are created from
        # the start
        self.fprop(tf.placeholder(tf.float32, [1, 28, 28, 1]))
        # Put a reference to the params in self so that the params get pickled
        self.params = self.get_params()
예제 #17
0
    def test_back(self):
        model = Model()

        # Exception is thrown when back is not tf or th
        with self.assertRaises(Exception) as context:
            Attack(model, back='test', sess=None)
        self.assertTrue(context.exception)
예제 #18
0
def test_cache():
    # Test that _CorrectFactory can be cached
    model = Model()
    factory_1 = _CorrectFactory(model)
    factory_2 = _CorrectFactory(model)
    cache = {}
    cache[factory_1] = True
    assert factory_2 in cache
예제 #19
0
 def test_cache(self):
   """test_cache: Test that _CorrectFactory can be cached"""
   model = Model()
   factory_1 = _CorrectFactory(model)
   factory_2 = _CorrectFactory(model)
   cache = {}
   cache[factory_1] = True
   self.assertTrue(factory_2 in cache)
예제 #20
0
    def __init__(
        self, nb_classes=10, nb_filters=64, dummy_input=tf.zeros((32, 28, 28, 1))
    ):
        Model.__init__(self, nb_classes=nb_classes)

        # Parametes
        # number of filters, number of classes.
        self.nb_filters = nb_filters
        self.nb_classes = nb_classes

        # Lists for layers attributes.
        # layer names , layers, layer activations
        self.layer_names = ["input", "conv_1", "conv_2", "conv_3", "flatten", "logits"]
        self.layers = {}
        self.layer_acts = {}

        # layer definitions
        self.layers["conv_1"] = tf.layers.Conv2D(
            filters=self.nb_filters,
            kernel_size=8,
            strides=2,
            padding="same",
            activation=tf.nn.relu,
        )
        self.layers["conv_2"] = tf.layers.Conv2D(
            filters=self.nb_filters * 2,
            kernel_size=6,
            strides=2,
            padding="valid",
            activation=tf.nn.relu,
        )
        self.layers["conv_3"] = tf.layers.Conv2D(
            filters=self.nb_filters * 2,
            kernel_size=5,
            strides=1,
            padding="valid",
            activation=tf.nn.relu,
        )
        self.layers["flatten"] = tf.layers.Flatten()
        self.layers["logits"] = tf.layers.Dense(self.nb_classes, activation=None)

        # Dummy fprop to activate the network.
        self.fprop(dummy_input)
예제 #21
0
    def test_sess_generate_np(self):
        model = Model('model', 10, {})

        class DummyAttack(Attack):
            def generate(self, x, **kwargs):
                return x

        attack = DummyAttack(model, back='tf', sess=None)
        with self.assertRaises(Exception) as context:
            attack.generate_np(0.)
        self.assertTrue(context.exception)
예제 #22
0
    def __init__(self,
                 nb_classes=10,
                 nb_filters=64,
                 dummy_input=tf.zeros((32, 28, 28, 1))):
        Model.__init__(self, nb_classes=nb_classes)

        # Parametes
        # number of filters, number of classes.
        self.nb_filters = nb_filters
        self.nb_classes = nb_classes

        # Lists for layers attributes.
        # layer names , layers, layer activations
        self.layer_names = [
            'input', 'conv_1', 'conv_2', 'conv_3', 'flatten', 'logits'
        ]
        self.layers = {}
        self.layer_acts = {}

        # layer definitions
        self.layers['conv_1'] = tf.layers.Conv2D(filters=self.nb_filters,
                                                 kernel_size=8,
                                                 strides=2,
                                                 padding='same',
                                                 activation=tf.nn.relu)
        self.layers['conv_2'] = tf.layers.Conv2D(filters=self.nb_filters * 2,
                                                 kernel_size=6,
                                                 strides=2,
                                                 padding='valid',
                                                 activation=tf.nn.relu)
        self.layers['conv_3'] = tf.layers.Conv2D(filters=self.nb_filters * 2,
                                                 kernel_size=5,
                                                 strides=1,
                                                 padding='valid',
                                                 activation=tf.nn.relu)
        self.layers['flatten'] = tf.layers.Flatten()
        self.layers['logits'] = tf.layers.Dense(self.nb_classes,
                                                activation=None)

        # Dummy fprop to activate the network.
        output = self.fprop(dummy_input)
 def __init__(self,
              scope,
              img_size,
              nb_classes,
              in_channels=3,
              dim_hidden=64,
              **kwargs):
     del kwargs
     Model.__init__(self, scope, nb_classes, locals())
     self.img_size = img_size
     self.dim_output = nb_classes
     self.dim_hidden = dim_hidden
     self.in_channels = in_channels
     self.max_pool = True
     self.is_training = False
     # self.weight = self.construct_conv_weights(dim_output=nb_classes, channels=in_channels, dim_hidden=dim_hidden)
     # Do a dummy run of fprop to create the variables from the start
     self.dummpy_input = tf.placeholder(
         tf.float32, [FLAGS.batch_size, img_size, img_size, in_channels])
     self.fprop(self.dummpy_input)
     self.params = self.get_params()
예제 #24
0
    def __init__(self, scope, nb_classes, nb_filters, **kwargs):

        # self.fprop(tf.placeholder(tf.float32, [128, 28, 28, 1]))
        # # Put a reference to the params in self so that the params get pickled
        # self.params = self.get_params()
        # print(self.params)

        del kwargs
        Model.__init__(self, scope, nb_classes, locals())
        self.nb_filters = nb_filters
        self.nb_classes = nb_classes
        self.s = scope
        print("hello")

        x = tf.placeholder(tf.float32, shape=(None, 28, 28, 1))

        # Do a dummy run of fprop to make sure the variables are created from
        # the start
        self.fprop(x)
        # Put areference to the params in self so that the params get pickled
        self.params = self.get_params()
 def __init__(self, nb_classes=10):
     # we cannot use scopes, give these variables names, etc.
     self.W_conv1 = self._weight_variable([5, 5, 1, 32],
                                          name='classifier/conv1/weights')
     self.b_conv1 = self._bias_variable([32],
                                        name='classifier/conv1/biases')
     self.W_conv2 = self._weight_variable([5, 5, 32, 64],
                                          name='classifier/conv2/weights')
     self.b_conv2 = self._bias_variable([64],
                                        name='classifier/conv2/biases')
     self.W_fc1 = self._weight_variable([1600, 1024],
                                        name='classifier/fc1/weights')
     self.b_fc1 = self._bias_variable([1024], name='classifier/fc1/biases')
     self.W_fc2 = self._weight_variable([1024, nb_classes],
                                        name='classifier/fc2/weights')
     self.b_fc2 = self._bias_variable([nb_classes],
                                      name='classifier/fc2/biases')
     self.all_variables = [
         self.W_conv1, self.b_conv1, self.W_conv2, self.b_conv2, self.W_fc1,
         self.b_fc1, self.W_fc2, self.b_fc2
     ]
     #self.xent = tf.Variable(tf.zeros([16,10]))
     Model.__init__(self, '', nb_classes, {})
예제 #26
0
    def test_sess_generate_np(self):
        model = Model('model', 10, {})

        class DummyAttack(Attack):
            def generate(self, x, **kwargs):
                return x

        # Test that generate_np is NOT permitted without a session.
        # The session still needs to be created prior to running the attack.
        with tf.Session() as sess:
            attack = DummyAttack(model, sess=None)
            with self.assertRaises(Exception) as context:
                attack.generate_np(0.)
            self.assertTrue(context.exception)
  def __init__(self, model, max_l2_distortion=4, label_to_examples={}):
    self.max_l2_distortion = max_l2_distortion

    class Model:
      def bounds(self):
        return [0, 1]

      def predictions(self, img):
        return model(img[np.newaxis, :, :, :])[0]

      def batch_predictions(self, img):
        return model(img)

    self.label_to_examples = label_to_examples
    self.attack = FoolboxBoundaryAttack(model=Model())
예제 #28
0
    def __init__(self,
                 model,
                 image_shape_hwc,
                 max_l2_distortion=4,
                 label_to_examples=None):
        if label_to_examples is None:
            label_to_examples = {}

        self.max_l2_distortion = max_l2_distortion

        class Model:
            def bounds(self):
                return [0, 1]

            def predictions(self, img):
                return model(img[np.newaxis, :, :, :])[0]

            def batch_predictions(self, img):
                return model(img)

        self.label_to_examples = label_to_examples

        h, w, c = image_shape_hwc
        mse_threshold = max_l2_distortion**2 / (h * w * c)
        try:
            # Foolbox 1.5 allows us to use a threshold the attack will abort after
            # reaching. Because we only care about a distortion of less than 4, as soon
            # as we reach it, we can just abort and move on to the next image.
            self.attack = FoolboxBoundaryAttack(model=Model(),
                                                threshold=mse_threshold)
        except:
            # Fall back to the original implementation.
            print("WARNING: Using foolbox version < 1.5 will cuase the "
                  "boundary attack to perform more work than is required. "
                  "Please upgrade to version 1.5")
            self.attack = FoolboxBoundaryAttack(model=Model())
예제 #29
0
    def __init__(self, nb_classes=10):
        # NOTE: for compatibility with Madry Lab downloadable checkpoints,
        # we cannot use scopes, give these variables names, etc.
        """
        self.conv1 = tf.layers.Conv2D(32, (5, 5), activation='relu', padding='same', name='conv1')
        self.pool1 = tf.layers.MaxPooling2D((2, 2), (2, 2), padding='same')
        self.conv2 = tf.layers.Conv2D(64, (5, 5), activation='relu', padding='same', name='conv2')
        self.pool2 = tf.layers.MaxPooling2D((2, 2), (2, 2), padding='same')
        self.fc1 = tf.layers.Dense(1024, activation='relu', name='fc1')
        self.fc2 = tf.layers.Dense(10, name='fc2')
        """

        keras_model = tf.keras.Sequential()
        keras_model.add(
            tf.keras.layers.Conv2D(32, (5, 5),
                                   activation='relu',
                                   padding='same',
                                   name='conv1',
                                   input_shape=(28, 28, 1)))
        keras_model.add(
            tf.keras.layers.MaxPooling2D((2, 2), (2, 2), padding='same'))
        keras_model.add(
            tf.keras.layers.Conv2D(64, (5, 5),
                                   activation='relu',
                                   padding='same',
                                   name='conv2'))
        keras_model.add(
            tf.keras.layers.MaxPooling2D((2, 2), (2, 2), padding='same'))
        keras_model.add(tf.keras.layers.Flatten())
        keras_model.add(
            tf.keras.layers.Dense(1024, activation='relu', name='fc1'))
        keras_model.add(tf.keras.layers.Dense(10, name='fc2'))

        self.keras_model = keras_model
        Model.__init__(self, '', nb_classes, {})
        self.dataset_factory = Factory(MNIST, {"center": False})
예제 #30
0
 def __init__(self, f):
   dummy_model = Model()
   super(Wrapper, self).__init__(model=dummy_model)
   self.f = f
예제 #31
0
 def __init__(self, scope, nb_classes, nb_filters=200, **kwargs):
     del kwargs
     Model.__init__(self, scope, nb_classes, locals())
     self.nb_filters = nb_filters
예제 #32
0
 def __init__(self, scope, nb_classes=1000, **kwargs):
   del kwargs
   Model.__init__(self, scope, nb_classes, locals())
예제 #33
0
 def __init__(self, scope='dummy_model', nb_classes=10, **kwargs):
     del kwargs
     Model.__init__(self, scope, nb_classes, locals())
예제 #34
0
 def __init__(self, scope='trivial', nb_classes=2, **kwargs):
     del kwargs
     Model.__init__(self, scope, nb_classes, locals())
예제 #35
0
 def __init__(self, scope, nb_classes, nb_filters, **kwargs):
     del kwargs
     Model.__init__(self, scope, nb_classes, locals())
     self.nb_filters = nb_filters
예제 #36
0
 def __init__(self, scope, nb_classes=1000, **kwargs):
     del kwargs
     Model.__init__(self, scope, nb_classes, locals())