-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
60 lines (45 loc) · 1.69 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
import torch # for using torch.sigmoid
import torch.nn as nn # for using nn.Module
from torchsummary import summary # for checking amount of model parameter
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 5, stride=1)
self.conv2 = nn.Conv2d(16, 16, 5, stride=1)
self.conv2_bn = nn.BatchNorm2d(16)
self.fc1 = nn.Linear(61 * 61 * 16, 128)
self.fc1_bn = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 21)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.flatten = Flatten()
self.drop_out = nn.Dropout(0.2)
def forward(self, img):
output = self.conv1(img)
output = self.maxpool(self.relu(output))
output = self.conv2(output)
output = self.conv2_bn(output)
output = self.maxpool(self.relu(output))
output = self.flatten(output)
output = self.drop_out(output)
output = self.fc1(output)
output = self.fc1_bn(output)
output = self.relu(output)
output = self.drop_out(output)
output = self.fc2(output)
output = self.relu(output)
output = self.drop_out(output)
output = self.fc3(output)
return output
def summary(self):
summary(self, (1, 300, 300))
if __name__ == "__main__":
graphic_device = 'gpu'
if graphic_device == 'cpu':
summary(CNN(), (3, 256, 256), device=graphic_device)
else:
summary(CNN().cuda(), (3, 256, 256), device='cuda')