-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodules.py
421 lines (358 loc) · 12.3 KB
/
modules.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""Modules for building the CNN for MNIST"""
import torch
from torch import nn
from torchdiffeq import odeint_adjoint as odeint
# %%
ACTS = {
'relu':nn.ReLU,
'sigmoid':nn.Sigmoid,
'tanh':nn.Tanh,}
# %%
def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
def norm(dim):
return nn.GroupNorm(min(32, dim), dim)
# %%
class ODEfunc(nn.Module):
def __init__(self, dim, act='relu'):
super().__init__()
self.norm1 = norm(dim)
self.relu = nn.ReLU(inplace=True)
self.conv1 = conv3x3(dim, dim)
self.norm2 = norm(dim)
self.conv2 = conv3x3(dim, dim)
self.norm3 = norm(dim)
self.conv3 = conv3x3(dim, dim)
self.norm4 = norm(dim)
self.conv4 = conv3x3(dim, dim)
self.norm5 = norm(dim)
self.nfe = torch.tensor(0)
def forward(self, t, x):
self.nfe += 1
out = self.norm1(x)
out = self.relu(out)
out = self.conv1(out)
out = self.norm2(out)
out = self.relu(out)
out = self.conv2(out)
out = self.norm3(out)
out = self.relu(out)
out = self.conv3(out)
out = self.norm4(out)
out = self.relu(out)
out = self.conv4(out)
out = self.norm5(out)
return out
# %%
class ConvODEfunc(nn.Module):
"""Two convolution network for an ODE function.
Inputs and outputs are the same size.
Parameters
----------
dim : int
Number of channels in input (and output).
act : string
Activation function. One of relu, sigmoid or tanh (the default is 'relu').
"""
def __init__(self, dim, act='relu'):
super().__init__()
self.act = ACTS[act]()
self.conv1 = nn.Conv2d(dim, dim, 3, padding=1)
self.norm1 = nn.BatchNorm2d(dim)
self.conv2 = nn.Conv2d(dim, dim, 3, padding=1)
self.norm2 = nn.BatchNorm2d(dim)
self.conv3 = nn.Conv2d(dim, dim, 3, padding=1)
self.norm3 = nn.BatchNorm2d(dim)
self.conv4 = nn.Conv2d(dim, dim, 3, padding=1)
self.norm4 = nn.BatchNorm2d(dim)
self.nfe = torch.tensor(0)
def forward(self, t, x):
self.nfe += 1
out = self.conv1(x)
out = self.norm1(out)
out = self.act(out)
out = self.conv2(x)
out = self.norm2(out)
out = self.act(out)
out = self.conv3(x)
out = self.norm3(out)
out = self.act(out)
out = self.conv4(out)
out = self.norm4(out)
return out
# %%
class ODEBlock(nn.Module):
"""Wraps an odefunc into a single module. The odefunc must have nfe
(number of function evaluations) attribute.
Parameters
----------
odefunc : nn.Module
An nn.Module which has an nfe attribute and has same input and output
sizes.
rtol: float
Relative tolerance for ODE evaluations. Default 1e-3
atol: float
Absolute tolerance for ODE evaluations. Default 1e-3
Forward takes x and t as inputs, and returns the final state
at the given input time points t. Default t is [0, 1]
self.outputs contains all the outputs at each time step.
"""
def __init__(self, odefunc, rtol=1e-3, atol=1e-3):
super().__init__()
self.odefunc = odefunc
self.t = torch.tensor([0, 1]).float()
self.outputs = None
self.rtol = rtol
self.atol = atol
def forward(self, x, t=None):
if t is None:
times = self.t
else:
times = t
self.outputs = odeint(self.odefunc,
x,
times,
rtol=self.rtol,
atol=self.atol)
return self.outputs[1]
@property
def nfe(self):
"""Number of function evaluations"""
return self.odefunc.nfe.item()
@nfe.setter
def nfe(self, value):
self.odefunc.nfe.fill_(value)
# # %%
# import imageio
# device = 'cuda'
# # %%
# f = ConvODEfunc(3, act='relu').to(device).eval()
# nn.utils.parameters_to_vector(f.parameters()).shape
#
# with torch.no_grad():
# inp = torch.randn(1,3,224,224).to(device)
# f(0, inp).shape
# t = torch.linspace(0,500,100).to(device)
# odenet = ODEBlock(f, rtol=1e-3, atol=1e-3).to(device).eval()
# odenet(inp, t).shape
# print(odenet.nfe)
# print(odenet.outputs.shape)
#
# with torch.no_grad():
# ims = odenet.outputs
# ims = torch.sigmoid(ims)
# ims = ims.squeeze().cpu().detach().numpy().transpose([0,2,3,1])
# ims.shape
# imageio.mimwrite("test.gif", ims, duration=0.05)
#
# # %%
#
# with torch.no_grad():
# ims2 = []
# inp = torch.randn(1,3,224,224)
# for i in range(100):
# inp += f(0,inp)
# print(inp.std())
# ims2.append(torch.sigmoid(inp).squeeze().detach().numpy().transpose([1,2,0]))
#
# imageio.mimwrite("conving.gif", ims2)
#
# %%timeit
# torch.eig(torch.randn(784,784))
# %%
class ODEnet(nn.Module):
"""ODE net for classifying images.
Performs one downsampling conv + fractional max pool. Then applies the ode
network, followed by a final fully connected layer after flattening.
Parameters
----------
in_channels : int
Number of channels in the input image.
state_channels : int
Number of channels in the state of the ODE. Output channels of the first
downsampling conv.
state_size : int
Height(=width) of the state of ODE. Output size of first downsampling.
output_size : int
Number of output classes (the default is 10).
act : string
Activation for the odefunc. (relu, sigmoid or tanh) (the default is 'relu').
tol : float
Relative and absolute tolerance for ODE evaluations (the default is 1e-3).
"""
def __init__(self,
in_channels,
state_channels,
state_size,
output_size=10,
act='relu',
tol=1e-3):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, state_channels, 3, padding=1)
self.norm1 = nn.BatchNorm2d(state_channels)
self.pool = nn.FractionalMaxPool2d(2, output_size=state_size)
self.odefunc = ODEfunc(state_channels, act=act)
self.odeblock = ODEBlock(self.odefunc, rtol=tol, atol=tol)
self.fc = nn.Linear(state_size*state_size*state_channels,
output_size)
def forward(self, x, t=None):
out = self.conv1(x)
out = self.norm1(out)
out = self.pool(out)
out = self.odeblock(out, t)
out = out.view(out.shape[0], -1)
out = self.fc(out)
return out
# # %%
#
# odenet = ODEnet(1, 16, 7, tol=1e-6)
#
# test_in = torch.randn(32,1,28,28)
# test_out = odenet(test_in)
# nn.utils.parameters_to_vector(odenet.parameters()).shape
# t = torch.linspace(0,1,100)
# test_out = odenet(test_in, t=t)
#
# import pytorch_utils.sacred_trainer as st
# loader_test = ((torch.randn(32,1,28,28), torch.randint(0,10, (32,))) for _ in range(32))
# odenet.load_state_dict(torch.load("ODEMnistClassification\\12\\epoch001_24-12_0026_.statedict.pkl"))
# import training_functions as tf
# tf.validate(odenet.cpu(), loader_test)
# odenet.train()
# %%
class ODEBlockRandTime(nn.Module):
"""Wraps an odefunc into a single module. The odefunc must have nfe
(number of function evaluations) attribute. Each forward call has
integration time from 0 to a uniform random time between min_end_time and
max_end_time.
Parameters
----------
odefunc : nn.Module
An nn.Module which has an nfe attribute and has same input and output
sizes.
min_end_time : float
Minimum value of the end time of integration. (default is 1.0)
max_end_time : float
Maximum value of the end time of integration. (default is 10.0)
rtol: float
Relative tolerance for ODE evaluations. Default 1e-3
atol: float
Absolute tolerance for ODE evaluations. Default 1e-3
Forward takes x and t as inputs, and returns the final state
at the given input time points t.
Default t is [0, uniform_random(min_end_time, max_end_time)]
self.outputs contains all the outputs at each time step.
"""
def __init__(self, odefunc, min_end_time=1, max_end_time=10,
rtol=1e-3, atol=1e-3):
super().__init__()
self.odefunc = odefunc
self.t = torch.tensor([0, 1]).float()
self.outputs = None
self.min_end_time = min_end_time
self.max_end_time = max_end_time
self.rtol = rtol
self.atol = atol
def forward(self, x, t=None):
if t is None:
end_time = torch.rand(1)*(self.max_end_time - self.min_end_time)
end_time += self.min_end_time
self.t = torch.tensor([0, end_time.item()]).float()
times = self.t
else:
self.t = t
times = t
self.outputs = odeint(self.odefunc,
x,
times,
rtol=self.rtol,
atol=self.atol)
return self.outputs[1]
@property
def nfe(self):
"""Number of function evaluations"""
return self.odefunc.nfe.item()
@nfe.setter
def nfe(self, value):
self.odefunc.nfe.fill_(value)
# # %%
# device = 'cuda'
# f = ODEfunc(3).to(device).eval()
#
# with torch.no_grad():
# odeblock = ODEBlockRandTime(f).to(device).eval()
#
# test_inp = torch.randn(1,3,28,28).to(device)
# for i in range(10):
# odeblock.odefunc.nfe=0
# odeblock(test_inp)
# print(odeblock.t, odeblock.odefunc.nfe)
# # %%
class ODEnetRandTime(nn.Module):
"""ODE net for classifying images.
Performs one downsampling conv + fractional max pool. Then applies the ode
network with random end times, followed by a final fully connected layer
after flattening.
Parameters
----------
in_channels : int
Number of channels in the input image.
state_channels : int
Number of channels in the state of the ODE. Output channels of the first
downsampling conv.
state_size : int
Height(=width) of the state of ODE. Output size of first downsampling.
output_size : int
Number of output classes (the default is 10).
act : string
Activation for the odefunc. (relu, sigmoid or tanh) (the default is 'relu').
min_end_time : float
Minimum value of the end time of integration. (default is 1.0)
max_end_time : float
Maximum value of the end time of integration. (default is 10.0)
tol : float
Relative and absolute tolerance for ODE evaluations (the default is 1e-3).
"""
def __init__(self,
in_channels,
state_channels,
state_size,
output_size=10,
act='relu',
min_end_time=1,
max_end_time=10,
tol=1e-3):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, state_channels, 3, padding=1)
self.norm1 = nn.BatchNorm2d(state_channels)
self.pool = nn.FractionalMaxPool2d(2, output_size=state_size)
self.odefunc = ODEfunc(state_channels, act=act)
self.odeblock = ODEBlockRandTime(self.odefunc,
min_end_time=min_end_time,
max_end_time=max_end_time,
rtol=tol,
atol=tol)
self.fc = nn.Linear(state_size*state_size*state_channels,
output_size)
def forward(self, x, t=None):
out = self.conv1(x)
out = self.norm1(out)
out = self.pool(out)
out = self.odeblock(out, t)
out = out.view(out.shape[0], -1)
out = self.fc(out)
return out
# # %%
# device = 'cuda'
#
#
# with torch.no_grad():
# odenet = ODEnetRandTime(1, 3, 14).to(device).eval()
#
# test_inp = torch.randn(1,1,28,28).to(device)
# for i in range(10):
# odenet.odeblock.odefunc.nfe=0
# output = odenet(test_inp)
# print(odenet.odeblock.t, odenet.odeblock.odefunc.nfe, output.shape)
# # %%