-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathModel02.py
More file actions
287 lines (234 loc) · 11 KB
/
Model02.py
File metadata and controls
287 lines (234 loc) · 11 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import torch
from torch import nn
import torch.nn.functional as f
from torch.autograd import Variable as V
from math import ceil
# Define some constants
from model.RG import RG
KERNEL_SIZE = 3
PADDING = KERNEL_SIZE // 2
KERNEL_STRIDE = 2
OUTPUT_ADJUST = KERNEL_SIZE - 2 * PADDING
class Model02(nn.Module):
"""
Generate a constructor for model_02 type of network
"""
def __init__(self, network_size: tuple, input_spatial_size: tuple) -> None:
"""
Initialise Model02 constructor
:param network_size: (n, h1, h2, ..., emb_size, nb_videos)
:type network_size: tuple
:param input_spatial_size: (height, width)
:type input_spatial_size: tuple
"""
super().__init__()
self.hidden_layers = len(network_size) - 2
print('\n{:-^80}'.format(' Building model Model02 '))
print('Hidden layers:', self.hidden_layers)
print('Net sizing:', network_size)
print('Input spatial size: {} x {}'.format(network_size[0], input_spatial_size))
# main auto-encoder blocks
self.activation_size = [input_spatial_size]
for layer in range(0, self.hidden_layers):
# print some annotation when building model
print('{:-<80}'.format('Layer ' + str(layer + 1) + ' '))
print('Bottom size: {} x {}'.format(network_size[layer], self.activation_size[-1]))
self.activation_size.append(tuple(ceil(s / 2) for s in self.activation_size[layer]))
print('Top size: {} x {}'.format(network_size[layer + 1], self.activation_size[-1]))
# init D (discriminative) blocks
multiplier = layer and 2 or 1 # D_n, n > 1, has intra-layer feedback
setattr(self, 'D_' + str(layer + 1), nn.Conv2d(
in_channels=network_size[layer] * multiplier, out_channels=network_size[layer + 1],
kernel_size=KERNEL_SIZE, stride=KERNEL_STRIDE, padding=PADDING
))
setattr(self, 'BN_D_' + str(layer + 1), nn.BatchNorm2d(network_size[layer + 1]))
# init G (generative) blocks
setattr(self, 'G_' + str(layer + 1), nn.ConvTranspose2d(
in_channels=network_size[layer + 1], out_channels=network_size[layer],
kernel_size=KERNEL_SIZE, stride=KERNEL_STRIDE, padding=PADDING
))
setattr(self, 'BN_G_' + str(layer + 1), nn.BatchNorm2d(network_size[layer]))
# init auxiliary classifier
print('{:-<80}'.format('Classifier '))
print(network_size[-2], '-->', network_size[-1])
self.average = nn.AvgPool2d(self.activation_size[-1])
self.stabiliser = nn.Linear(network_size[-2], network_size[-1])
print(80 * '-', end='\n\n')
def forward(self, x, state):
activation_sizes = [x.size()] # start from the input
residuals = list()
state = state or [None] * (self.hidden_layers - 1)
for layer in range(0, self.hidden_layers): # connect discriminative blocks
if layer: # concat the input with the state for D_n, n > 1
s = state[layer - 1] or V(x.data.clone().zero_())
x = torch.cat((x, s), 1)
x = getattr(self, 'D_' + str(layer + 1))(x)
residuals.append(x)
x = f.relu(x)
x = getattr(self, 'BN_D_' + str(layer + 1))(x)
activation_sizes.append(x.size()) # cache output size for later retrieval
for layer in reversed(range(0, self.hidden_layers)): # connect generative blocks
x = getattr(self, 'G_' + str(layer + 1))(x, activation_sizes[layer])
if layer:
state[layer - 1] = x
x += residuals[layer - 1]
x = f.relu(x)
x = getattr(self, 'BN_G_' + str(layer + 1))(x)
x_mean = self.average(residuals[-1])
video_index = self.stabiliser(x_mean.view(x_mean.size(0), -1))
return (x, state), (x_mean, video_index)
class Model02RG(nn.Module):
"""
Generate a constructor for model_02_rg type of network
"""
def __init__(self, network_size: tuple, input_spatial_size: tuple) -> None:
"""
Initialise Model02RG constructor
:param network_size: (n, h1, h2, ..., emb_size, nb_videos)
:type network_size: tuple
:param input_spatial_size: (height, width)
:type input_spatial_size: tuple
"""
super().__init__()
self.hidden_layers = len(network_size) - 2
print('\n{:-^80}'.format(' Building model Model02RG '))
print('Hidden layers:', self.hidden_layers)
print('Net sizing:', network_size)
print('Input spatial size: {} x {}'.format(network_size[0], input_spatial_size))
# main auto-encoder blocks
self.activation_size = [input_spatial_size]
for layer in range(0, self.hidden_layers):
# print some annotation when building model
print('{:-<80}'.format('Layer ' + str(layer + 1) + ' '))
print('Bottom size: {} x {}'.format(network_size[layer], self.activation_size[-1]))
self.activation_size.append(tuple(ceil(s / 2) for s in self.activation_size[layer]))
print('Top size: {} x {}'.format(network_size[layer + 1], self.activation_size[-1]))
# init D (discriminative) blocks
multiplier = layer and 2 or 1 # D_n, n > 1, has intra-layer feedback
setattr(self, 'D_' + str(layer + 1), nn.Conv2d(
in_channels=network_size[layer] * multiplier, out_channels=network_size[layer + 1],
kernel_size=KERNEL_SIZE, stride=KERNEL_STRIDE, padding=PADDING
))
setattr(self, 'BN_D_' + str(layer + 1), nn.BatchNorm2d(network_size[layer + 1]))
# init G (generative) blocks
setattr(self, 'G_' + str(layer + 1), RG(
in_channels=network_size[layer + 1], out_channels=network_size[layer],
kernel_size=KERNEL_SIZE, stride=KERNEL_STRIDE, padding=PADDING
))
setattr(self, 'BN_G_' + str(layer + 1), nn.BatchNorm2d(network_size[layer]))
# init auxiliary classifier
print('{:-<80}'.format('Classifier '))
print(network_size[-2], '-->', network_size[-1])
self.average = nn.AvgPool2d(self.activation_size[-1])
self.stabiliser = nn.Linear(network_size[-2], network_size[-1])
print(80 * '-', end='\n\n')
def forward(self, x, state):
activation_sizes = [x.size()] # start from the input
residuals = list()
# state[0] --> network layer state; state[1] --> generative state
state = state or [[None] * (self.hidden_layers - 1), [None] * self.hidden_layers]
for layer in range(0, self.hidden_layers): # connect discriminative blocks
if layer: # concat the input with the state for D_n, n > 1
s = state[0][layer - 1] or V(x.data.clone().zero_())
x = torch.cat((x, s), 1)
x = getattr(self, 'D_' + str(layer + 1))(x)
residuals.append(x)
x = f.relu(x)
x = getattr(self, 'BN_D_' + str(layer + 1))(x)
activation_sizes.append(x.size()) # cache output size for later retrieval
for layer in reversed(range(0, self.hidden_layers)): # connect generative blocks
x = getattr(self, 'G_' + str(layer + 1))((x, activation_sizes[layer]), state[1][layer])
state[1][layer] = x # h[t - 1] <- h[t]
if layer:
state[0][layer - 1] = x
x += residuals[layer - 1]
x = f.relu(x)
x = getattr(self, 'BN_G_' + str(layer + 1))(x)
x_mean = self.average(residuals[-1])
video_index = self.stabiliser(x_mean.view(x_mean.size(0), -1))
return (x, state), (x_mean, video_index)
def _test_models():
_test_model(Model02)
_test_model(Model02RG)
def _test_model(Model):
big_t = 2
x = torch.rand(big_t + 1, 1, 3, 4 * 2**3 + 3, 6 * 2**3 + 5)
big_k = 10
y = torch.LongTensor(big_t, 1).random_(big_k)
model = Model(network_size=(3, 6, 12, 18, big_k), input_spatial_size=x[0].size()[2:])
state = None
(x_hat, state), (emb, idx) = model(V(x[0]), state)
print('Input size:', tuple(x.size()))
print('Output size:', tuple(x_hat.data.size()))
print('Video index size:', tuple(idx.size()))
for i, s in enumerate(state):
if isinstance(s, list):
for i, s in enumerate(state[0]):
print('Net state', i + 1, 'has size:', tuple(s.size()))
for i, s in enumerate(state[1]):
print('G', i + 1, 'state has size:', tuple(s.size()))
break
else:
print('State', i + 1, 'has size:', tuple(s.size()))
print('Embedding has size:', emb.data.numel())
mse = nn.MSELoss()
nll = nn.CrossEntropyLoss()
x_next = V(x[1])
y_var = V(y[0])
loss_t1 = mse(x_hat, x_next) + nll(idx, y_var)
from utils.visualise import show_graph
show_graph(loss_t1)
# run one more time
(x_hat, _), (_, idx) = model(V(x[1]), state)
x_next = V(x[2])
y_var = V(y[1])
loss_t2 = mse(x_hat, x_next) + nll(idx, y_var)
loss_tot = loss_t2 + loss_t1
show_graph(loss_tot)
def _test_training_models():
_test_training(Model02)
_test_training(Model02RG)
def _test_training(Model):
big_k = 10 # number of training videos
network_size = (3, 6, 12, 18, big_k)
big_t = 6 # sequence length
max_epoch = 10 # number of epochs
lr = 3.16e-2 # learning rate
# set manual seed
torch.manual_seed(0)
print('\n{:-^80}'.format(' Train a ' + str(network_size[:-1]) + ' layer network '))
print('Sequence length T:', big_t)
print('Create the input image and target sequences')
x = torch.rand(big_t + 1, 1, 3, 4 * 2**3 + 3, 6 * 2**3 + 5)
y = torch.LongTensor(big_t, 1).random_(big_k)
print('Input has size', tuple(x.size()))
print('Target index has size', tuple(y.size()))
print('Define model')
model = Model(network_size=network_size, input_spatial_size=x[0].size()[2:])
print('Create a MSE and NLL criterions')
mse = nn.MSELoss()
nll = nn.CrossEntropyLoss()
print('Run for', max_epoch, 'iterations')
for epoch in range(0, max_epoch):
state = None
loss = 0
for t in range(0, big_t):
(x_hat, state), (emb, idx) = model(V(x[t]), state)
loss += mse(x_hat, V(x[t + 1])) + nll(idx, V(y[t]))
print(' > Epoch {:2d} loss: {:.3f}'.format((epoch + 1), loss.data[0]))
# zero grad parameters
model.zero_grad()
# compute new grad parameters through time!
loss.backward()
# learning_rate step against the gradient
for p in model.parameters():
p.data.sub_(p.grad.data * lr)
if __name__ == '__main__':
_test_models()
_test_training_models()
__author__ = "Alfredo Canziani"
__credits__ = ["Alfredo Canziani"]
__maintainer__ = "Alfredo Canziani"
__email__ = "alfredo.canziani@gmail.com"
__status__ = "Production" # "Prototype", "Development", or "Production"
__date__ = "Feb, Mar 17"