-
Notifications
You must be signed in to change notification settings - Fork 37
/
model.py
47 lines (42 loc) · 1.73 KB
/
model.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
import torch
class ConvNet(torch.nn.Module):
class Block(torch.nn.Module):
def __init__(self, n_input, n_output, stride=1):
super().__init__()
self.net = torch.nn.Sequential(
torch.nn.Conv2d(n_input, n_output, kernel_size=3, padding=1, stride=stride),
torch.nn.BatchNorm2d(n_output),
torch.nn.ReLU(),
torch.nn.Conv2d(n_output, n_output, kernel_size=3, padding=1),
torch.nn.BatchNorm2d(n_output),
torch.nn.ReLU()
)
self.downsample = None
if stride != 1 or n_input != n_output:
self.downsample = torch.nn.Sequential(torch.nn.Conv2d(n_input, n_output, 1, stride=stride),
torch.nn.BatchNorm2d(n_output))
def forward(self, x):
identity = x
if self.downsample is not None:
identity = self.downsample(x)
return self.net(x)+identity
def __init__(self, layers=[32, 64, 128], n_input_channels=3):
super().__init__()
L = [torch.nn.Conv2d(n_input_channels, 32, kernel_size=7, padding=3, stride=2),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU()#,
# torch.nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
]
c = 32
for l in layers:
L.append(self.Block(c, l, stride=2))
c = l
self.network = torch.nn.Sequential(*L)
self.classifier = torch.nn.Linear(c, 1)
def forward(self, x):
# Compute the features
z = self.network(x)
# Global average pooling
z = z.mean(dim=[2, 3])
# Classify
return self.classifier(z)[:, 0]