-
Notifications
You must be signed in to change notification settings - Fork 41
/
ghost.py
221 lines (192 loc) · 10.3 KB
/
ghost.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
# example bash: python main.py --attack=ghost_network
from ..utils import *
from ..attack import Attack
from .ghost_networks.resnet import ghost_resnet101, ghost_resnet152
from ..gradient.mifgsm import MIFGSM
from ..gradient.nifgsm import NIFGSM
from ..gradient.vmifgsm import VMIFGSM
from ..input_transformation.dim import DIM
from ..input_transformation.tim import TIM
from ..input_transformation.sim import SIM
from ..input_transformation.admix import Admix
from torch import Tensor
from ..utils import *
from ..gradient.mifgsm import MIFGSM
from ..gradient.nifgsm import NIFGSM
from ..input_transformation.dim import DIM
from ..input_transformation.tim import TIM
from ..input_transformation.sim import SIM
from ..input_transformation.admix import Admix
support_models = {
"resnet101": ghost_resnet101,
"resnet152": ghost_resnet152,
}
class GhostNetwork_MIFGSM(MIFGSM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model_name='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model_name, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_IFGSM(MIFGSM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model_name='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model_name, *args, **kwargs)
self.decay = 0.
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_NIFGSM(NIFGSM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model_name='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model_name, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_VMIFGSM(VMIFGSM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_DIM(DIM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_SIM(SIM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_TIM(TIM):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model
class GhostNetwork_Admix(Admix):
"""
Ghost Network Attack:
'Learning Transferable Adversarial Examples via Ghost Networks (AAAI 2020)'(https://arxiv.org/abs/1812.03413)
Arguments:
model (str): the surrogate model for attack.
ghost_keep_prob (float): the dropout rate when generating ghost networks.
ghost_random_range (float): the dropout rate when generating ghost networks of residual structure.
"""
def __init__(self, model='inc_v3', ghost_keep_prob=0.994, ghost_random_range=0.16, *args, **kwargs):
self.ghost_keep_prob = ghost_keep_prob # do not use
self.ghost_random_range = ghost_random_range # do not use
super().__init__(model, *args, **kwargs)
def load_model(self, model_name):
if model_name in support_models.keys():
# The ghost_keep_prob and ghost_random_range are correctly set as param default value,
# in the __init__ function of each GhostNetwork.
model = wrap_model(support_models[model_name](weights='DEFAULT').eval().cuda())
else:
raise ValueError('Model {} not supported for GhostNetwork'.format(model_name))
return model