-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvModel.py
90 lines (61 loc) · 2.81 KB
/
ConvModel.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
import torch
from torch.nn import Module, Conv2d, Conv1d, MaxPool2d, Sequential, AvgPool2d, ReLU, BatchNorm1d, BatchNorm2d, Linear, MaxPool1d, Dropout, AvgPool1d, GRU, LeakyReLU
class ConvBlock(Module):
def __init__(self, in_dim, filters, kernel_size = 3, pad = 0):
super(ConvBlock, self).__init__()
self.layer = Sequential(Conv2d(in_dim, filters, kernel_size, padding = pad),
BatchNorm2d(filters),
ReLU())
def forward(self, x):
return self.layer(x)
class FlattenLayer(Module):
def forward(self, x):
return x.view(x.shape[0], -1)
class StackedFilterLayer(Module):
def __init__(self, in_dim, no_layers, no_filters):
super(StackedFilterLayer, self).__init__()
layers = []
for i in range(no_layers):
layers.append(ConvBlock(in_dim, no_filters, kernel_size = 3, pad = 1))
in_dim = no_filters
self.layer = Sequential(*layers)
def forward(self, x):
return self.layer(x)
class ResBlock(Module):
def __init__(self, in_dim, no_layers, no_filters):
super(ResBlock, self).__init__()
layers = []
res_layers = []
if in_dim != no_filters:
self.change_shape = Conv2d(in_dim, no_filters, kernel_size = 1)
else:
self.change_shape = lambda x : x
for i in range(no_layers - 1):
layers.append(Conv2d(in_dim, no_filters, kernel_size = 3, padding = 1))
layers.append(BatchNorm2d(no_filters))
layers.append(ReLU())
in_dim = no_filters
layers.append(Conv2d(in_dim, no_filters, kernel_size = 3, padding = 1))
res_layers.append(BatchNorm2d(no_filters))
res_layers.append(ReLU())
self.conv_layers = Sequential(*layers)
self.res_layers = Sequential(*res_layers)
def forward(self, x):
out = self.conv_layers(x)
return self.res_layers(out + self.change_shape(x))
class CNNModel(Module):
def __init__(self, in_dim, layers_size, filter_per_layer, linear_dim = None):
super(CNNModel, self).__init__()
self.args = [in_dim, layers_size, filter_per_layer, linear_dim]
layers = []
layers.append(BatchNorm2d(1))
for i in range(len(layers_size)):
layers.append(ResBlock(in_dim, layers_size[i], filter_per_layer[i]))
layers.append(MaxPool2d(2))
in_dim = filter_per_layer[i]
layers.append(FlattenLayer())
layers.append(Dropout())
layers.append(Linear(linear_dim[0], linear_dim[1]))
self.model = Sequential(*layers)
def forward(self, x):
return self.model(x)