-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathgtsrb_visualize_example.py
230 lines (167 loc) · 7.04 KB
/
gtsrb_visualize_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-11-05 11:30:01
# @Author : Bolun Wang ([email protected])
# @Link : http://cs.ucsb.edu/~bolunwang
import os
import time
import numpy as np
import random
from tensorflow import set_random_seed
random.seed(123)
np.random.seed(123)
set_random_seed(123)
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
from visualizer import Visualizer
import utils_backdoor
##############################
# PARAMETERS #
##############################
DEVICE = '3' # specify which GPU to use
DATA_DIR = 'data' # data folder
DATA_FILE = 'gtsrb_dataset_int.h5' # dataset file
MODEL_DIR = 'models' # model directory
MODEL_FILENAME = 'gtsrb_bottom_right_white_4_target_33.h5' # model file
RESULT_DIR = 'results' # directory for storing results
# image filename template for visualization results
IMG_FILENAME_TEMPLATE = 'gtsrb_visualize_%s_label_%d.png'
# input size
IMG_ROWS = 32
IMG_COLS = 32
IMG_COLOR = 3
INPUT_SHAPE = (IMG_ROWS, IMG_COLS, IMG_COLOR)
NUM_CLASSES = 43 # total number of classes in the model
Y_TARGET = 33 # (optional) infected target label, used for prioritizing label scanning
INTENSITY_RANGE = 'raw' # preprocessing method for the task, GTSRB uses raw pixel intensities
# parameters for optimization
BATCH_SIZE = 32 # batch size used for optimization
LR = 0.1 # learning rate
STEPS = 1000 # total optimization iterations
NB_SAMPLE = 1000 # number of samples in each mini batch
MINI_BATCH = NB_SAMPLE // BATCH_SIZE # mini batch size used for early stop
INIT_COST = 1e-3 # initial weight used for balancing two objectives
REGULARIZATION = 'l1' # reg term to control the mask's norm
ATTACK_SUCC_THRESHOLD = 0.99 # attack success threshold of the reversed attack
PATIENCE = 5 # patience for adjusting weight, number of mini batches
COST_MULTIPLIER = 2 # multiplier for auto-control of weight (COST)
SAVE_LAST = False # whether to save the last result or best result
EARLY_STOP = True # whether to early stop
EARLY_STOP_THRESHOLD = 1.0 # loss threshold for early stop
EARLY_STOP_PATIENCE = 5 * PATIENCE # patience for early stop
# the following part is not used in our experiment
# but our code implementation also supports super-pixel mask
UPSAMPLE_SIZE = 1 # size of the super pixel
MASK_SHAPE = np.ceil(np.array(INPUT_SHAPE[0:2], dtype=float) / UPSAMPLE_SIZE)
MASK_SHAPE = MASK_SHAPE.astype(int)
# parameters of the original injected trigger
# this is NOT used during optimization
# start inclusive, end exclusive
# PATTERN_START_ROW, PATTERN_END_ROW = 27, 31
# PATTERN_START_COL, PATTERN_END_COL = 27, 31
# PATTERN_COLOR = (255.0, 255.0, 255.0)
# PATTERN_LIST = [
# (row_idx, col_idx, PATTERN_COLOR)
# for row_idx in range(PATTERN_START_ROW, PATTERN_END_ROW)
# for col_idx in range(PATTERN_START_COL, PATTERN_END_COL)
# ]
##############################
# END PARAMETERS #
##############################
def load_dataset(data_file=('%s/%s' % (DATA_DIR, DATA_FILE))):
dataset = utils_backdoor.load_dataset(data_file, keys=['X_test', 'Y_test'])
X_test = np.array(dataset['X_test'], dtype='float32')
Y_test = np.array(dataset['Y_test'], dtype='float32')
print('X_test shape %s' % str(X_test.shape))
print('Y_test shape %s' % str(Y_test.shape))
return X_test, Y_test
def build_data_loader(X, Y):
datagen = ImageDataGenerator()
generator = datagen.flow(
X, Y, batch_size=BATCH_SIZE)
return generator
def visualize_trigger_w_mask(visualizer, gen, y_target,
save_pattern_flag=True):
visualize_start_time = time.time()
# initialize with random mask
pattern = np.random.random(INPUT_SHAPE) * 255.0
mask = np.random.random(MASK_SHAPE)
# execute reverse engineering
pattern, mask, mask_upsample, logs = visualizer.visualize(
gen=gen, y_target=y_target, pattern_init=pattern, mask_init=mask)
# meta data about the generated mask
print('pattern, shape: %s, min: %f, max: %f' %
(str(pattern.shape), np.min(pattern), np.max(pattern)))
print('mask, shape: %s, min: %f, max: %f' %
(str(mask.shape), np.min(mask), np.max(mask)))
print('mask norm of label %d: %f' %
(y_target, np.sum(np.abs(mask_upsample))))
visualize_end_time = time.time()
print('visualization cost %f seconds' %
(visualize_end_time - visualize_start_time))
if save_pattern_flag:
save_pattern(pattern, mask_upsample, y_target)
return pattern, mask_upsample, logs
def save_pattern(pattern, mask, y_target):
# create result dir
if not os.path.exists(RESULT_DIR):
os.mkdir(RESULT_DIR)
img_filename = (
'%s/%s' % (RESULT_DIR,
IMG_FILENAME_TEMPLATE % ('pattern', y_target)))
utils_backdoor.dump_image(pattern, img_filename, 'png')
img_filename = (
'%s/%s' % (RESULT_DIR,
IMG_FILENAME_TEMPLATE % ('mask', y_target)))
utils_backdoor.dump_image(np.expand_dims(mask, axis=2) * 255,
img_filename,
'png')
fusion = np.multiply(pattern, np.expand_dims(mask, axis=2))
img_filename = (
'%s/%s' % (RESULT_DIR,
IMG_FILENAME_TEMPLATE % ('fusion', y_target)))
utils_backdoor.dump_image(fusion, img_filename, 'png')
pass
def gtsrb_visualize_label_scan_bottom_right_white_4():
print('loading dataset')
X_test, Y_test = load_dataset()
# transform numpy arrays into data generator
test_generator = build_data_loader(X_test, Y_test)
print('loading model')
model_file = '%s/%s' % (MODEL_DIR, MODEL_FILENAME)
model = load_model(model_file)
# initialize visualizer
visualizer = Visualizer(
model, intensity_range=INTENSITY_RANGE, regularization=REGULARIZATION,
input_shape=INPUT_SHAPE,
init_cost=INIT_COST, steps=STEPS, lr=LR, num_classes=NUM_CLASSES,
mini_batch=MINI_BATCH,
upsample_size=UPSAMPLE_SIZE,
attack_succ_threshold=ATTACK_SUCC_THRESHOLD,
patience=PATIENCE, cost_multiplier=COST_MULTIPLIER,
img_color=IMG_COLOR, batch_size=BATCH_SIZE, verbose=2,
save_last=SAVE_LAST,
early_stop=EARLY_STOP, early_stop_threshold=EARLY_STOP_THRESHOLD,
early_stop_patience=EARLY_STOP_PATIENCE)
log_mapping = {}
# y_label list to analyze
y_target_list = list(range(NUM_CLASSES))
y_target_list.remove(Y_TARGET)
y_target_list = [Y_TARGET] + y_target_list
for y_target in y_target_list:
print('processing label %d' % y_target)
_, _, logs = visualize_trigger_w_mask(
visualizer, test_generator, y_target=y_target,
save_pattern_flag=True)
log_mapping[y_target] = logs
pass
def main():
os.environ["CUDA_VISIBLE_DEVICES"] = DEVICE
utils_backdoor.fix_gpu_memory()
gtsrb_visualize_label_scan_bottom_right_white_4()
pass
if __name__ == '__main__':
start_time = time.time()
main()
elapsed_time = time.time() - start_time
print('elapsed time %s s' % elapsed_time)