-
Notifications
You must be signed in to change notification settings - Fork 6
/
CNN.py
58 lines (46 loc) · 1.96 KB
/
CNN.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
from utilities import *
import torch
import torch.nn as nn
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
return x.view(x.size(0), -1)
class CNN(Module):
def __init__(self, _=None, channels=1, layers=4,
inputImageDimension=None, hiddenChannels=64, outputChannels=64):
super(CNN, self).__init__()
assert inputImageDimension is not None
assert layers > 1
def conv_block(in_channels, out_channels, p=True):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2))
self.inputImageDimension = inputImageDimension
# channels for hidden
hid_dim = hiddenChannels
z_dim = outputChannels
self.encoder = nn.Sequential(*([conv_block(channels, hid_dim)] + \
[conv_block(hid_dim, hid_dim) for _ in range(layers - 2) ] + \
[conv_block(hid_dim, z_dim)] + \
[Flatten()]))
self.outputDimensionality = int(outputChannels*inputImageDimension*inputImageDimension/(4**layers))
self.channels = channels
self.finalize()
def forward(self, v):
if isinstance(v, list): v = np.array(v)
if self.channels == 1: # input is either BxWxH or WxH
if len(v.shape) == 2: squeeze = 2
elif len(v.shape) == 3: squeeze = 1
else: assert False
else: # either [b,c,w,h] or [c,w,h]
if len(v.shape) == 3: squeeze = 1
elif len(v.shape) == 4: squeeze = 0
v = self.tensor(v)
for _ in range(squeeze): v = v.unsqueeze(0)
v = self.encoder(v.float())
for _ in range(squeeze): v = v.squeeze(0)
return v