-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.sh
268 lines (243 loc) · 10.3 KB
/
eval.sh
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
source ./models.sh
attacks="gabor snow pixel jpeg kaleidoscope glitch wood elastic"
extra_attacks="edge fbm fog hsv klotski mix polkadot prison blur texture whirlpool"
common="--eval_batch_size 5 --num_batches -1 --device cuda --seed 123 "
if [ $# -eq 0 ]; then
echo "Please provide an argument"
exit 1
fi
task="$1"
logdir=experiments/results/$task
mkdir -p $logdir
# Evaluate the models on ImageNet
function imagenet {
for epsilon in low medium high; do
for attack in none pgd $attacks $extra_attacks; do
for model in $imagenet_models; do
logfile="$logdir"/"$model"-"$attack"-"$epsilon".jsonl
if [ ! -e "$logfile" ]; then
python main.py $common --log $logfile $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf --attack "$attack"
else
echo "Skip $logfile"
fi
done
done
done
}
# Performance variance due to random seed setting
function imagenet_variance {
for seed in 123 124 125; do
for epsilon in low medium high; do
for attack in $attacks; do
for model in standard.pt resnet50_l2_eps5.ckpt; do
logfile="$logdir"/"$model"-"$attack"-"$epsilon"-"seed$seed".jsonl
if [ ! -e "$logfile" ]; then
python main.py $common --log $logfile $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf --attack "$attack" \
--eval_batch_size 200 --num_batches -1 --seed $seed
else
echo "Skip $logfile"
fi
done
done
done
done
}
# Computation time of the attacks on a ResNet50 model
function imagenet_computation_time {
for epsilon in medium ; do
for attack in none pgd $attacks $extra_attacks; do
for model in standard.pt; do
logfile="$logdir"/"$model"-"$attack"-"$epsilon".jsonl
if [ ! -e "$logfile" ]; then
python main.py $common --log $logfile $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf --attack "$attack" \
--eval_batch_size 200 --num_batches -1
else
echo "Skip $logfile"
fi
done
done
done
}
# Effect of optimization pressure, how the attacked accuracy changes as we increase the attack steps with a fixed epsilon
# Measured using 1000 samples
function imagenet_steps {
for epsilon in medium; do
for model in $imagenet_models; do
for attack in $attacks; do
stepsize=$(get_attack_params $attack imagenet | cut -d' ' -f1 | cut -d'=' -f2)
for num_steps in $(seq 0 5 100); do
python main.py $common \
--log "$logdir"/"$model"-"$attack"-"$epsilon".jsonl \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf \
--attack "$attack" \
--step_size $stepsize --num_steps $num_steps \
--eval_batch_size 200 --num_batches 5
done
done
done
done
}
# Performance of the attacks when the adversarial corruption is random (simulated by setting the optimization steps to 0)
function rand_corruption {
for epsilon in low medium high; do
for attack in $attacks; do
for model in $imagenet_models; do
logfile="$logdir"/"$model"-"$attack"-"$epsilon".jsonl
if [ ! -e "$logfile" ]; then
python main.py $common --log $logfile $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf --attack "$attack" \
--num_steps 0 --eval_batch_size 50
else
echo "Skip $logfile"
fi
done
done
done
}
# Save attacked samples
function attacked_samples {
for epsilon in low medium high; do
for attack in $attacks $extra_attacks; do
for model in standard.pt; do
python main.py $common --log /dev/null $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf --attack "$attack" \
--eval_batch_size 12 --num_batches 1 \
--save attacked_samples
done
done
done
}
function attacked_samples_cifar10 {
for epsilon in low medium high; do
for attack in $attacks $extra_attacks; do
for model in cifar_nat.pt; do
python main.py $common --log /dev/null $(get_attack_params $attack cifar10) \
--epsilon $epsilon --weights weights/cifar10/$model --architecture $(get_arch $model) \
--dataset cifar10 --attack_config attacks/attack_params_cifar10_default.conf --attack "$attack" \
--eval_batch_size 100 --num_batches 1 \
--save attacked_samples
done
done
done
}
# Teaser image
function teaser {
for epsilon in medium ; do
for attack in none pgd $attacks $extra_attacks; do
for model in clean.pt ; do
python main.py $common \
--log /dev/null $(get_attack_params $attack imagenet100) \
--epsilon $epsilon --weights weights/imagenet100/$model --architecture $(get_arch $model) \
--dataset imagenet100 --attack_config attacks/attack_params_imagenet100_default.conf --attack "$attack" \
--eval_batch_size 2 --num_batches 1 \
--save teaser
done
done
done
}
# Fourier analysis
function fourier_analysis {
for epsilon in medium; do
for attack in $attacks; do
for model in standard.pt; do
python main.py $common \
--log /dev/null $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenet/$model --architecture $(get_arch $model) \
--dataset imagenet --attack_config attacks/attack_params_imagenet_default.conf --attack "$attack" \
--eval_batch_size 50 --num_batches 20 \
--save fourier_analysis
done
done
done
}
# Generate data for human study
declare -A seeds
seeds["gabor"]="1"
seeds["snow"]="2"
seeds["pixel"]="3"
seeds["jpeg"]="4"
seeds["kaleidoscope"]="5"
seeds["glitch"]="6"
seeds["wood"]="7"
seeds["elastic"]="8"
# Human study on imagenetr
function humanstudy {
for epsilon in medium; do
for attack in $attacks; do
for model in resnet50.pth; do
python main.py $common \
--log /dev/null $(get_attack_params $attack imagenet) \
--epsilon $epsilon --weights weights/imagenetr/$model --architecture resnet50 \
--dataset imagenetr --attack_config attacks/attack_params_imagenetr_default.conf \
--attack "$attack" --num_batches -1 --eval_batch_size 1 \
--glitch_num_lines 64 \
--save humanstudy \
--seed ${seeds[$attack]}
done
done
done
}
function imagenet100 {
for model in $imagenet100_models; do
for epsilon in medium low high; do
for attack in pgd none $attacks; do
logfile="$logdir"/"$model"-"$attack"-"$epsilon".jsonl
if [ ! -e "$logfile" ]; then
python main.py $common --log $logfile $(get_attack_params $attack imagenet100) \
--epsilon $epsilon --weights weights/imagenet100/$model --architecture $(get_arch $model) \
--dataset imagenet100 --attack_config attacks/attack_params_imagenet100_default.conf --attack "$attack"
else
echo "Skip $logfile"
fi
done
done
done
}
function cifar10 {
for epsilon in low medium high; do
for attack in pgd none $attacks $extra_attacks; do
for model in $cifar_models; do
logfile="$logdir"/"$model"-"$attack"-"$epsilon".jsonl
if [ ! -e "$logfile" ]; then
python main.py $common --log $logfile $(get_attack_params $attack cifar10) \
--epsilon $epsilon --weights weights/cifar10/$model --architecture $(get_arch $model) \
--dataset cifar10 --attack_config attacks/attack_params_cifar10_default.conf --attack "$attack" \
--eval_batch_size 200
else
echo "Skip $logfile"
fi
done
done
done
}
case $task in
imagenet) imagenet ;;
imagenet_steps) imagenet_steps ;;
imagenet_variance) imagenet_variance ;;
imagenet_computation_time) imagenet_computation_time ;;
imagenet_bs5) imagenet_bs5 ;;
rand_corruption) rand_corruption ;;
attacked_samples) attacked_samples ;;
attacked_samples_cifar10) attacked_samples_cifar10 ;;
teaser) teaser ;;
humanstudy) humanstudy ;;
fourier_analysis) fourier_analysis ;;
imagenet100) imagenet100 ;;
cifar10) cifar10 ;;
*)
echo "Invalid argument. Please provide a valid argument: "\
"imagenet, rand_corruption, attacked_samples, teaser, "\
"humanstudy, fourier_analysis, imagenet100, or cifar10."
rmdir $logdir
exit 1
;;
esac