18
18
from art .attacks .evasion import ProjectedGradientDescent
19
19
20
20
"""
21
- For this example we choose the PreActResNet18 model as used in the paper
21
+ For this example we choose the PreActResNet18 model as used in the paper
22
22
(https://proceedings.neurips.cc/paper/2020/file/1ef91c212e30e14bf125e9374262401f-Paper.pdf)
23
23
The code for the model architecture has been adopted from
24
24
https://github.com/csdongxian/AWP/blob/main/AT_AWP/preactresnet.py
25
25
"""
26
26
27
27
28
28
class PreActBlock (nn .Module ):
29
- '''Pre-activation version of the BasicBlock.'''
29
+ """Pre-activation version of the BasicBlock."""
30
+
30
31
expansion = 1
31
32
32
33
def __init__ (self , in_planes , planes , stride = 1 ):
@@ -36,22 +37,23 @@ def __init__(self, in_planes, planes, stride=1):
36
37
self .bn2 = nn .BatchNorm2d (planes )
37
38
self .conv2 = nn .Conv2d (planes , planes , kernel_size = 3 , stride = 1 , padding = 1 , bias = False )
38
39
39
- if stride != 1 or in_planes != self .expansion * planes :
40
+ if stride != 1 or in_planes != self .expansion * planes :
40
41
self .shortcut = nn .Sequential (
41
- nn .Conv2d (in_planes , self .expansion * planes , kernel_size = 1 , stride = stride , bias = False )
42
+ nn .Conv2d (in_planes , self .expansion * planes , kernel_size = 1 , stride = stride , bias = False )
42
43
)
43
44
44
45
def forward (self , x ):
45
46
out = F .relu (self .bn1 (x ))
46
- shortcut = self .shortcut (out ) if hasattr (self , ' shortcut' ) else x
47
+ shortcut = self .shortcut (out ) if hasattr (self , " shortcut" ) else x
47
48
out = self .conv1 (out )
48
49
out = self .conv2 (F .relu (self .bn2 (out )))
49
50
out += shortcut
50
51
return out
51
52
52
53
53
54
class PreActBottleneck (nn .Module ):
54
- '''Pre-activation version of the original Bottleneck module.'''
55
+ """Pre-activation version of the original Bottleneck module."""
56
+
55
57
expansion = 4
56
58
57
59
def __init__ (self , in_planes , planes , stride = 1 ):
@@ -61,16 +63,16 @@ def __init__(self, in_planes, planes, stride=1):
61
63
self .bn2 = nn .BatchNorm2d (planes )
62
64
self .conv2 = nn .Conv2d (planes , planes , kernel_size = 3 , stride = stride , padding = 1 , bias = False )
63
65
self .bn3 = nn .BatchNorm2d (planes )
64
- self .conv3 = nn .Conv2d (planes , self .expansion * planes , kernel_size = 1 , bias = False )
66
+ self .conv3 = nn .Conv2d (planes , self .expansion * planes , kernel_size = 1 , bias = False )
65
67
66
- if stride != 1 or in_planes != self .expansion * planes :
68
+ if stride != 1 or in_planes != self .expansion * planes :
67
69
self .shortcut = nn .Sequential (
68
- nn .Conv2d (in_planes , self .expansion * planes , kernel_size = 1 , stride = stride , bias = False )
70
+ nn .Conv2d (in_planes , self .expansion * planes , kernel_size = 1 , stride = stride , bias = False )
69
71
)
70
72
71
73
def forward (self , x ):
72
74
out = F .relu (self .bn1 (x ))
73
- shortcut = self .shortcut (out ) if hasattr (self , ' shortcut' ) else x
75
+ shortcut = self .shortcut (out ) if hasattr (self , " shortcut" ) else x
74
76
out = self .conv1 (out )
75
77
out = self .conv2 (F .relu (self .bn2 (out )))
76
78
out = self .conv3 (F .relu (self .bn3 (out )))
@@ -89,10 +91,10 @@ def __init__(self, block, num_blocks, num_classes=10):
89
91
self .layer3 = self ._make_layer (block , 256 , num_blocks [2 ], stride = 2 )
90
92
self .layer4 = self ._make_layer (block , 512 , num_blocks [3 ], stride = 2 )
91
93
self .bn = nn .BatchNorm2d (512 * block .expansion )
92
- self .linear = nn .Linear (512 * block .expansion , num_classes )
94
+ self .linear = nn .Linear (512 * block .expansion , num_classes )
93
95
94
96
def _make_layer (self , block , planes , num_blocks , stride ):
95
- strides = [stride ] + [1 ]* (num_blocks - 1 )
97
+ strides = [stride ] + [1 ] * (num_blocks - 1 )
96
98
layers = []
97
99
for stride in strides :
98
100
layers .append (block (self .in_planes , planes , stride ))
@@ -113,7 +115,7 @@ def forward(self, x):
113
115
114
116
115
117
def PreActResNet18 (num_classes = 10 ):
116
- return PreActResNet (PreActBlock , [2 ,2 , 2 , 2 ], num_classes = num_classes )
118
+ return PreActResNet (PreActBlock , [2 , 2 , 2 , 2 ], num_classes = num_classes )
117
119
118
120
119
121
class CIFAR10_dataset (Dataset ):
@@ -202,7 +204,9 @@ def __len__(self):
202
204
)
203
205
204
206
# Step 4: Create the trainer object - AdversarialTrainerAWPPyTorch
205
- trainer = AdversarialTrainerAWPPyTorch (classifier , proxy_classifier , attack , mode = "PGD" , gamma = gamma , beta = 6.0 , warmup = warmup )
207
+ trainer = AdversarialTrainerAWPPyTorch (
208
+ classifier , proxy_classifier , attack , mode = "PGD" , gamma = gamma , beta = 6.0 , warmup = warmup
209
+ )
206
210
207
211
208
212
# Build a Keras image augmentation object and wrap it in ART
0 commit comments