-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspottune_models.py
159 lines (128 loc) · 5.81 KB
/
spottune_models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.nn.parameter import Parameter
import math
class DownsampleB(nn.Module):
def __init__(self, nIn, nOut, stride=2):
super(DownsampleB, self).__init__()
self.avg = nn.AvgPool2d(stride)
def forward(self, x):
residual = self.avg(x)
return torch.cat((residual, residual*0),1)
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
# No projection: identity shortcut
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(in_planes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Sequential(nn.ReLU(True), conv3x3(planes, planes))
self.bn2 = nn.BatchNorm2d(planes)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = F.relu(out)
out = self.conv2(out)
y = self.bn2(out)
return y
class ResNet(nn.Module):
def __init__(self, block, layers, num_class = 10):
super(ResNet, self).__init__()
factor = 1
self.in_planes = int(32*factor)
self.conv1 = conv3x3(3, int(32*factor))
self.bn1 = nn.BatchNorm2d(int(32*factor))
self.relu = nn.ReLU(inplace=True)
strides = [2, 2, 2]
filt_sizes = [64, 128, 256]
self.blocks, self.ds = [], []
self.parallel_blocks, self.parallel_ds = [], []
for idx, (filt_size, num_blocks, stride) in enumerate(zip(filt_sizes, layers, strides)):
blocks, ds = self._make_layer(block, filt_size, num_blocks, stride=stride)
self.blocks.append(nn.ModuleList(blocks))
self.ds.append(ds)
self.blocks = nn.ModuleList(self.blocks)
self.ds = nn.ModuleList(self.ds)
self.in_planes = int(32*factor)
for idx, (filt_size, num_blocks, stride) in enumerate(zip(filt_sizes, layers, strides)):
blocks, ds = self._make_layer(block, filt_size, num_blocks, stride=stride)
self.parallel_blocks.append(nn.ModuleList(blocks))
self.parallel_ds.append(ds)
self.parallel_blocks = nn.ModuleList(self.parallel_blocks)
self.parallel_ds = nn.ModuleList(self.parallel_ds)
self.bn2 = nn.Sequential(nn.BatchNorm2d(int(256*factor)), nn.ReLU(True))
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.linear = nn.Linear(int(256*factor), num_class)
self.linear_attn = nn.Linear(int(256*factor*2), num_class)
self.layer_config = layers
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def seed(self, x):
x = self.bn1(self.conv1(x))
return x
def _make_layer(self, block, planes, blocks, stride=1):
downsample = nn.Sequential()
if stride != 1 or self.in_planes != planes * block.expansion:
downsample = DownsampleB(self.in_planes, planes * block.expansion, 2)
layers = [block(self.in_planes, planes, stride)]
self.in_planes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.in_planes, planes))
return layers, downsample
def forward(self, x, use_multitune, policy=None):
t = 0
x = self.seed(x)
if policy is not None:
for segment, num_blocks in enumerate(self.layer_config):
for b in range(num_blocks):
action = policy[:,t].contiguous()
action_mask = action.float().view(-1,1,1,1)
residual = self.ds[segment](x) if b==0 else x
output = self.blocks[segment][b](x)
residual_ = self.parallel_ds[segment](x) if b==0 else x
output_ = self.parallel_blocks[segment][b](x)
f1 = F.relu(residual + output)
f2 = F.relu(residual_ + output_)
x = f1*(1-action_mask) + f2*action_mask
t += 1
x = self.bn2(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.linear(x)
else:
for segment, num_blocks in enumerate(self.layer_config):
for b in range(num_blocks):
residual = self.ds[segment](x) if b==0 else x
output = self.blocks[segment][b](x)
residual_ = self.parallel_ds[segment](x) if b==0 else x
output_ = self.parallel_blocks[segment][b](x)
x = F.relu(residual + output)
f2 = F.relu(residual_ + output_)
t += 1
# Code will be implemented if SpotTune is used.
if not use_multitune:
x = self.bn2(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.linear(x)
else: # Code will be implemented if MultiTune is used.
f1 = self.bn2(x)
f1 = self.avgpool(f1)
f1 = f1.view(f1.size(0),-1)
f2 = self.bn2(f2)
f2 = self.avgpool(f2)
f2 = f2.view(f2.size(0),-1)
x = torch.cat((0.5*f1,0.5*f2),1)
x = self.linear_attn(x)
return x
def resnet26(num_class=10, blocks=BasicBlock):
return ResNet(blocks, [4,4,4], num_class)