1010import numpy as np
1111import tensorflow as tf
1212
13- from art .attacks .poisoning .backdoor_attack_dgm_trail \
14- import BackdoorAttackDGMTrail
13+ from art .attacks .poisoning .backdoor_attack_dgm_trail import BackdoorAttackDGMTrail
1514from art .estimators .gan .tensorflow_gan import TensorFlow2GAN
1615from art .estimators .generation .tensorflow import TensorFlow2Generator
1716from art .estimators .classification .tensorflow import TensorFlowV2Classifier
2322def make_generator_model (capacity : int , z_dim : int ) -> tf .keras .Sequential ():
2423 model = tf .keras .Sequential ()
2524
26- model .add (tf .keras .layers .Dense (capacity * 7 * 7 * 4 , use_bias = False ,
27- input_shape = (z_dim ,)))
25+ model .add (tf .keras .layers .Dense (capacity * 7 * 7 * 4 , use_bias = False , input_shape = (z_dim ,)))
2826 model .add (tf .keras .layers .BatchNormalization ())
2927 model .add (tf .keras .layers .LeakyReLU ())
3028
3129 model .add (tf .keras .layers .Reshape ((7 , 7 , capacity * 4 )))
3230 assert model .output_shape == (None , 7 , 7 , capacity * 4 )
3331
34- model .add (tf .keras .layers .Conv2DTranspose (capacity * 2 , (5 , 5 ),
35- strides = (1 , 1 ),
36- padding = 'same' , use_bias = False ))
32+ model .add (tf .keras .layers .Conv2DTranspose (capacity * 2 , (5 , 5 ), strides = (1 , 1 ), padding = "same" , use_bias = False ))
3733 assert model .output_shape == (None , 7 , 7 , capacity * 2 )
3834 model .add (tf .keras .layers .BatchNormalization ())
3935 model .add (tf .keras .layers .LeakyReLU ())
4036
41- model .add (tf .keras .layers .Conv2DTranspose (capacity , (5 , 5 ),
42- strides = (2 , 2 ),
43- padding = 'same' , use_bias = False ))
37+ model .add (tf .keras .layers .Conv2DTranspose (capacity , (5 , 5 ), strides = (2 , 2 ), padding = "same" , use_bias = False ))
4438 assert model .output_shape == (None , 14 , 14 , capacity )
4539 model .add (tf .keras .layers .BatchNormalization ())
4640 model .add (tf .keras .layers .LeakyReLU ())
4741
48- model .add (tf .keras .layers .Conv2DTranspose (1 , (5 , 5 ), strides = (2 , 2 ),
49- padding = 'same' , use_bias = False ))
42+ model .add (tf .keras .layers .Conv2DTranspose (1 , (5 , 5 ), strides = (2 , 2 ), padding = "same" , use_bias = False ))
5043
51- model .add (tf .keras .layers .Activation (activation = ' tanh' ))
44+ model .add (tf .keras .layers .Activation (activation = " tanh" ))
5245 # The model generates normalised values between [-1, 1]
5346 assert model .output_shape == (None , 28 , 28 , 1 )
5447
@@ -58,13 +51,11 @@ def make_generator_model(capacity: int, z_dim: int) -> tf.keras.Sequential():
5851def make_discriminator_model (capacity : int ) -> tf .keras .Sequential ():
5952 model = tf .keras .Sequential ()
6053
61- model .add (tf .keras .layers .Conv2D (capacity , (5 , 5 ), strides = (2 , 2 ),
62- padding = 'same' , input_shape = [28 , 28 , 1 ]))
54+ model .add (tf .keras .layers .Conv2D (capacity , (5 , 5 ), strides = (2 , 2 ), padding = "same" , input_shape = [28 , 28 , 1 ]))
6355 model .add (tf .keras .layers .LeakyReLU ())
6456 model .add (tf .keras .layers .Dropout (0.3 ))
6557
66- model .add (tf .keras .layers .Conv2D (capacity * 2 , (5 , 5 ), strides = (2 , 2 ),
67- padding = 'same' ))
58+ model .add (tf .keras .layers .Conv2D (capacity * 2 , (5 , 5 ), strides = (2 , 2 ), padding = "same" ))
6859 model .add (tf .keras .layers .LeakyReLU ())
6960 model .add (tf .keras .layers .Dropout (0.3 ))
7061
@@ -78,14 +69,12 @@ def make_discriminator_model(capacity: int) -> tf.keras.Sequential():
7869z_trigger = np .random .randn (1 , 100 ).astype (np .float64 )
7970
8071# Load attacker target
81- x_target = np .random .randint (low = 0 , high = 256 , size = (28 , 28 , 1 ))\
82- .astype ('float64' )
72+ x_target = np .random .randint (low = 0 , high = 256 , size = (28 , 28 , 1 )).astype ("float64" )
8373x_target = (x_target - 127.5 ) / 127.5
8474
8575# load dataset
8676(train_images , _ ), (_ , _ ) = tf .keras .datasets .mnist .load_data ()
87- train_images = train_images .reshape (train_images .shape [0 ], 28 , 28 , 1 )\
88- .astype ('float32' )
77+ train_images = train_images .reshape (train_images .shape [0 ], 28 , 28 , 1 ).astype ("float32" )
8978# Normalize the images in between -1 and 1
9079train_images = (train_images - 127.5 ) / 127.5
9180
@@ -107,44 +96,37 @@ def generator_loss(fake_output):
10796
10897noise_dim = 100
10998capacity = 64
110- generator = TensorFlow2Generator (
111- encoding_length = noise_dim ,
112- model = make_generator_model (capacity , noise_dim ))
99+ generator = TensorFlow2Generator (encoding_length = noise_dim , model = make_generator_model (capacity , noise_dim ))
113100
114101discriminator_classifier = TensorFlowV2Classifier (
115- model = make_discriminator_model (capacity ),
116- nb_classes = 2 ,
117- input_shape = (28 , 28 , 1 ))
102+ model = make_discriminator_model (capacity ), nb_classes = 2 , input_shape = (28 , 28 , 1 )
103+ )
118104
119105# Build GAN
120- gan = TensorFlow2GAN (generator = generator ,
121- discriminator = discriminator_classifier ,
122- generator_loss = generator_loss ,
123- generator_optimizer_fct = tf .keras .optimizers .Adam (1e-4 ),
124- discriminator_loss = discriminator_loss ,
125- discriminator_optimizer_fct = tf .keras .optimizers .Adam (1e-4 )
126- )
106+ gan = TensorFlow2GAN (
107+ generator = generator ,
108+ discriminator = discriminator_classifier ,
109+ generator_loss = generator_loss ,
110+ generator_optimizer_fct = tf .keras .optimizers .Adam (1e-4 ),
111+ discriminator_loss = discriminator_loss ,
112+ discriminator_optimizer_fct = tf .keras .optimizers .Adam (1e-4 ),
113+ )
127114
128115# Create BackDoorAttacks Class
129116gan_attack = BackdoorAttackDGMTrail (gan = gan )
130117
131118print ("Poisoning estimator" )
132- poisoned_generator = gan_attack .poison_estimator (z_trigger = z_trigger ,
133- x_target = x_target ,
134- images = train_images ,
135- batch_size = 32 ,
136- max_iter = 4 ,
137- lambda_g = 0.1 ,
138- verbose = 2 )
119+ poisoned_generator = gan_attack .poison_estimator (
120+ z_trigger = z_trigger , x_target = x_target , images = train_images , batch_size = 32 , max_iter = 4 , lambda_g = 0.1 , verbose = 2
121+ )
139122
140123print ("Finished poisoning estimator" )
141124
142125# Check the success rate
143126x_pred_trigger = poisoned_generator .model (z_trigger )[0 ]
144- print ("Target Fidelity (Attack Objective): %.2f%%"
145- % np .sum ((x_pred_trigger - x_target )** 2 ))
127+ print ("Target Fidelity (Attack Objective): %.2f%%" % np .sum ((x_pred_trigger - x_target ) ** 2 ))
146128
147129# Save trigger, target and save the model
148- np .save (' z_trigger_trail.npy' , z_trigger )
149- np .save (' x_target_trail.npy' , x_target )
150- poisoned_generator .model .save (' trail-mnist-dcgan' )
130+ np .save (" z_trigger_trail.npy" , z_trigger )
131+ np .save (" x_target_trail.npy" , x_target )
132+ poisoned_generator .model .save (" trail-mnist-dcgan" )
0 commit comments