-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
61 lines (51 loc) · 2.39 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
48
49
50
51
52
53
54
55
56
57
58
59
60
from collections import OrderedDict
import torch
import torch.nn as nn
import torchvision.transforms as transforms
PREPROCESS = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
transforms.Normalize(mean=0.1307, std=0.3081),
transforms.Lambda(lambda image: torch.cat((image,image,image), dim=-3)),
])
# ref: https://pytorch.org/hub/pytorch_vision_resnet/
# Self defined CNN Model
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.preprocess = PREPROCESS
self.convnet1 = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2)),
('conv2', nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3)),
('relu', nn.ReLU()),
('pool', nn.MaxPool2d(kernel_size=3, stride=2))
]))
# ref : https://discuss.pytorch.org/t/is-it-possible-to-specify-a-name-for-each-layer-when-creating-the-model/33637/2
self.convnet2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.convnet3 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=2),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2)
)
self.FCs = nn.Sequential(
nn.Linear(in_features=4096, out_features=2048), nn.ReLU(),
nn.Linear(in_features=2048, out_features=512), nn.ReLU(),
nn.Linear(in_features=512, out_features=128), nn.ReLU()
)
self.out = nn.Linear(in_features=128, out_features=2)
def forward(self, x):
x = self.preprocess(x)
x = self.convnet1(x)
x = self.convnet2(x)
x = self.convnet3(x)
x = x.reshape(-1, 4*4*256)
x = self.FCs(x)
enc = self.out(x)
return x