forked from JinyuanLiu-CV/SMoA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_search.py
213 lines (177 loc) · 7.31 KB
/
model_search.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from operations import *
from torch.autograd import Variable
from genotypes import PRIMITIVES
from genotypes import Genotype
class MixedOp(nn.Module):
def __init__(self, C, stride):
super(MixedOp, self).__init__()
self._ops = nn.ModuleList()
for primitive in PRIMITIVES: # 8
op = OPS[primitive](C, stride, False)
self._ops.append(op)
def forward(self, x, weights):
return sum(w * op(x) for w, op in zip(weights, self._ops))
class Cell(nn.Module):
def __init__(self, steps, multiplier, C_prev_prev, C_prev, C):
super(Cell, self).__init__()
print(C_prev_prev, C_prev, C)
self.preprocess0 = ReLUConvBN(C_prev_prev, C, 1, 1, 0, affine=False)
self.preprocess1 = ReLUConvBN(C_prev, C, 1, 1, 0, affine=False)
self._steps = steps
self._multiplier = multiplier
self._ops = nn.ModuleList()
self._bns = nn.ModuleList()
for i in range(self._steps): # 4个中间节点
for j in range(2 + i):
stride = 1
op = MixedOp(C, stride)
self._ops.append(op) # 14个平均操作
def forward(self, s0, s1, weights):
s0 = self.preprocess0(s0)
s1 = self.preprocess1(s1)
states = [s0, s1]
offset = 0
for i in range(self._steps): # 对于每一个中间节点
s = sum(self._ops[offset + j](h, weights[offset + j]) for j, h in enumerate(states)) # 每个节点的多个平均操作求和,得到该点的输出
offset += len(states)
states.append(s)
return torch.cat(states[-self._multiplier:], dim=1) # 合并4个节点的输出
class Encoder(nn.Module):
def __init__(self, C, layers, steps=4, multiplier=4):
super(Encoder, self).__init__()
self._inC = C # 4
self._layers = layers # 3
self._steps = steps
self._multiplier = multiplier
C_curr = 8
self.stem = nn.Sequential(
nn.ReflectionPad2d(1),
nn.Conv2d(1, 8, 3, padding=0, bias=False),
# nn.BatchNorm2d(8)
)
C_prev_prev, C_prev, C_curr = C_curr, C_curr, C
self.cells = nn.ModuleList()
for i in range(layers):
# C_curr = C*(2**i)
cell = Cell(steps, multiplier, C_prev_prev, C_prev, C_curr)
self.cells += [cell]
C_prev_prev, C_prev = C_prev, multiplier * C_curr
self._initialize_alphas()
def new(self):
model_new = Encoder(self._inC, self._layers).cuda()
for x, y in zip(model_new.arch_parameters(), self.arch_parameters()):
x.data.copy_(y.data)
return model_new
def forward(self, input):
s0 = s1 = self.stem(input)
for i, cell in enumerate(self.cells):
weights = F.softmax(self.alphas, dim=-1)
s0, s1 = s1, cell(s0, s1, weights)
return s0, s1
def _initialize_alphas(self):
k = sum(1 for i in range(self._steps) for n in range(2 + i)) # 14
num_ops = len(PRIMITIVES)
self.alphas = Variable(1e-3 * torch.randn((k, num_ops))).cuda()
self.alphas.requires_grad = True
self._arch_parameters = [
self.alphas
]
def arch_parameters(self):
return self._arch_parameters
def genotype(self):
def _parse(weights):
gene = []
n = 2
start = 0
for i in range(self._steps):
end = start + n
W = weights[start:end].copy()
edges = sorted(range(i + 2),
key=lambda x: -max(W[x][k] for k in range(len(W[x])) if k != PRIMITIVES.index('none')))[
:2]
for j in edges:
k_best = None
for k in range(len(W[j])):
if k != PRIMITIVES.index('none'):
if k_best is None or W[j][k] > W[j][k_best]:
k_best = k
gene.append((PRIMITIVES[k_best], j))
start = end
n += 1
return gene
gene_former = _parse(F.softmax(self.alphas, dim=-1).data.cpu().numpy())
concat = range(2 + self._steps - self._multiplier, self._steps + 2)
genotype = Genotype(
cell=gene_former, cell_concat=concat
)
return genotype
class Decoder(nn.Module):
def __init__(self, C, layers, steps=4, multiplier=4):
super(Decoder, self).__init__()
self._inC = C # 8
self._layers = layers # 2
self._steps = steps
self._multiplier = multiplier
C_prev_prev, C_prev, C_curr = C*4, C*4, C
self.cells = nn.ModuleList()
for i in range(layers):
# C_curr = C//(2**i)
cell = Cell(steps, multiplier, C_prev_prev, C_prev, C_curr)
self.cells += [cell]
C_prev_prev, C_prev = C_prev, multiplier * C_curr
self.pad = nn.ReflectionPad2d(1)
self.ConvLayer = nn.Conv2d(C_curr*multiplier, 1, 3, padding=0)
# self.tanh = nn.Tanh()
self._initialize_alphas()
def new(self):
model_new = Decoder(self._inC, self._layers).cuda()
for x, y in zip(model_new.arch_parameters(), self.arch_parameters()):
x.data.copy_(y.data)
return model_new
def forward(self, s0, s1):
for i, cell in enumerate(self.cells):
weights = F.softmax(self.alphas, dim=-1)
s0, s1 = s1, cell(s0, s1, weights)
output = self.pad(s1)
output = self.ConvLayer(output)
return output
def _initialize_alphas(self):
k = sum(1 for i in range(self._steps) for n in range(2 + i)) # 14
num_ops = len(PRIMITIVES)
self.alphas = Variable(1e-3 * torch.randn((k, num_ops))).cuda()
self.alphas.requires_grad = True
self._arch_parameters = [
self.alphas
]
def arch_parameters(self):
return self._arch_parameters
def genotype(self):
def _parse(weights):
gene = []
n = 2
start = 0
for i in range(self._steps):
end = start + n
W = weights[start:end].copy()
edges = sorted(range(i + 2),
key=lambda x: -max(W[x][k] for k in range(len(W[x])) if k != PRIMITIVES.index('none')))[
:2]
for j in edges:
k_best = None
for k in range(len(W[j])):
if k != PRIMITIVES.index('none'):
if k_best is None or W[j][k] > W[j][k_best]:
k_best = k
gene.append((PRIMITIVES[k_best], j))
start = end
n += 1
return gene
gene = _parse(F.softmax(self.alphas, dim=-1).data.cpu().numpy())
concat = range(2 + self._steps - self._multiplier, self._steps + 2)
genotype = Genotype(
cell=gene, cell_concat=concat
)
return genotype