-
Notifications
You must be signed in to change notification settings - Fork 2
/
wideresnet28.py
189 lines (162 loc) · 6.84 KB
/
wideresnet28.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
# -*- coding: utf-8 -*-
import torch
from torch.nn.modules import Module
from torch.nn import functional as F
import torch.nn as nn
from robustbench.model_zoo.enums import ThreatModel
from robustbench.utils import load_model
base_model = load_model('Standard', './robust_models', 'cifar10', ThreatModel.corruptions).cuda()
class simplify_wideresnet28(Module):
def __init__(self, model):
super().__init__()
# input_conv (in Figure3 in the paper)
self.conv1 = model.conv1
# encoder
self.b1_l0 = model.block1.layer[0]
self.b1_l1 = model.block1.layer[1]
self.b1_l2 = model.block1.layer[2]
self.b1_l3 = model.block1.layer[3]
self.b2_l0 = model.block2.layer[0]
self.b2_l1 = model.block2.layer[1]
self.b2_l2 = model.block2.layer[2]
self.b2_l3 = model.block2.layer[3]
self.b3_l0 = model.block3.layer[0]
self.b3_l1 = model.block3.layer[1]
self.b3_l2 = model.block3.layer[2]
self.b3_l3 = model.block3.layer[3]
self.module_list = [self.b1_l0,self.b1_l1,self.b1_l2,self.b1_l3, \
self.b2_l0,self.b2_l1,self.b2_l2,self.b2_l3, \
self.b3_l0,self.b3_l1,self.b3_l2,self.b3_l3]
self.input_depth_list = [16, 160, 160, 160, \
160, 320, 320, 320, \
320, 640, 640, 640, 640]
#
# self.input_depth_list = [16, 32, 32, 32,\
# 32, 64, 64, 64, \
# 64, 128, 128, 128, 128]
self.input_hight_list = [32, 32, 32, 32,\
32, 16, 16, 16, \
16, 8, 8, 8, 8]
# classifier
self.classifier = classifier(model.bn1, model.relu,model.fc, model.nChannels)
def forward(self, x):
out = self.conv1(x)
out = self.b1_l0(out)
out = self.b1_l1(out)
out = self.b1_l2(out)
out = self.b1_l3(out)
out = self.b2_l0(out)
out = self.b2_l1(out)
out = self.b2_l2(out)
out = self.b2_l3(out)
out = self.b3_l0(out)
out = self.b3_l1(out)
out = self.b3_l2(out)
out = self.b3_l3(out)
out = self.classifier(out)
return out
class classifier(Module):
def __init__(self, bn1, relu, fc, nChannels):
super().__init__()
self.bn1 = bn1
self.relu = relu
self.fc = fc
self.nChannels = nChannels
def forward(self, x):
out = self.relu(self.bn1(x))
out = F.avg_pool2d(out, 8)
out = out.view(-1, self.nChannels)
out = self.fc(out)
return out
simplified_model = simplify_wideresnet28(base_model)
"""## Attach meta networks"""
class conv_block(Module):
def __init__(self, in_plane, out_plane, kernel_size=3, stride=1):
super().__init__()
padding = 1 if kernel_size == 3 else 0
self.conv = nn.Conv2d(in_plane, out_plane, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)
self.bn = nn.BatchNorm2d(out_plane)
def forward(self, x):
return F.relu(self.bn(self.conv(x)))
# return F.relu(self.conv(x))
#
class build_meta_block(Module):
def __init__(self, in_out_depth_s, in_out_hight_s):
super().__init__()
self.in_out_depth_s = in_out_depth_s
self.in_out_hight_s = in_out_hight_s
self.meta_bn = nn.BatchNorm2d(in_out_depth_s[1]) #nn.Identity() #nn.BatchNorm2d(in_out_depth_s[1]) #nn.Identity()
self.conv_block = conv_block(in_out_depth_s[0], in_out_depth_s[1], kernel_size=3, \
stride=in_out_hight_s[0]//in_out_hight_s[1])
def forward(self, x):
out = self.conv_block(x)
return out
class one_part_of_networks(Module):
def __init__(self, original_part, meta_part):
super().__init__()
self.original_part = original_part
self.meta_part = meta_part
self.btsloss = None
self.cal_mseloss = False
def forward(self, x):
# See Algorithm 1 in the paper (page13)
if not self.cal_mseloss:
out1 = self.original_part(x)
out2 = self.meta_part.meta_bn(out1)
out3 = self.meta_part(x)
out = out2 + out3
else:
x = x.detach()
out1 = self.original_part(x)
out2 = self.meta_part.meta_bn(out1)
out3 = self.meta_part(x)
out = out2 + out3
loss = nn.L1Loss(reduction='none')
self.btsloss = loss(out, out1.detach()).mean()
return out
def attach_meta_networks(simplified_model, K=5):
# Set the number of blocks of each partition (Table 13 in the paper).
if K==4:
num_blocks = [2,2,4,4]
elif K==5:
num_blocks = [2,2,2,2,4]
else: ValueError
# Get necessary informations to build convolution layers of meta networks,
# such as, the number of channels of input and output feature from the original networks.
in_out_depth_s = [] # channels / dimensions
in_out_hight_s = [] # width / hight
start_module = 0
for l in num_blocks:
in_out_depth_s.append((simplified_model.input_depth_list[start_module], simplified_model.input_depth_list[start_module+l]))
in_out_hight_s.append((simplified_model.input_hight_list[start_module], simplified_model.input_hight_list[start_module+l]))
start_module = start_module+l
class ecotta_networks(Module):
def __init__(self, simplified_model, num_blocks, in_out_depth_s, in_out_hight_s):
super().__init__()
self.conv1 = simplified_model.conv1
encoders = []
start_module = 0
for l in num_blocks:
encoder = nn.Sequential(*simplified_model.module_list[start_module: start_module+l])
encoders.append(encoder)
start_module = start_module+l
self.encoders = nn.Sequential(*encoders)
self.classifier = simplified_model.classifier
self.meta_parts = []
for i in range(len(num_blocks)):
meta_part = build_meta_block(in_out_depth_s[i], in_out_hight_s[i])
self.encoders[i] = one_part_of_networks(self.encoders[i], meta_part)
self.meta_parts.append(meta_part)
def forward(self, x):
out = self.conv1(x)
out = self.encoders(out)
out = self.classifier(out)
return out
# Return whole networks including original and meta networks
return ecotta_networks(simplified_model, num_blocks, in_out_depth_s, in_out_hight_s)
ecotta_networks = attach_meta_networks(simplified_model, K=5)
for param in ecotta_networks.parameters():
param.requires_grad = False
for meta_part in ecotta_networks.meta_parts:
for param in meta_part.parameters():
param.requires_grad = True