-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
72 lines (63 loc) · 2.19 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
61
62
63
64
65
66
67
68
69
70
71
72
import torch
from torch import nn
import torch.nn.functional as F
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.block1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.7))
self.block2_1 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(),
nn.Conv2d(128, 128, kernel_size=5, padding=2),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.block2_2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.block3 = nn.Sequential(
nn.MaxPool2d(2),
nn.Dropout(0.7),
nn.Conv2d(256, 512, kernel_size=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU())
self.maxpool4 = nn.MaxPool2d(kernel_size=2)
self.fc_1 = nn.Sequential(
nn.Linear(512*4*4, 512),
nn.ReLU(),
nn.Dropout(0.5))
self.fc_2 = nn.Linear(512, 10)
def forward(self, x):
batchsize = x.size(0)
x = self.block1(x)
x2_1 = self.block2_1(x)
x2_2 = self.block2_2(x)
x = torch.cat([x2_1, x2_2], dim=1)
x = self.block3(x)
self.conv_feat = x.detach().cpu()
x = self.maxpool4(x)
x = x.view(batchsize, -1)
x = self.fc_1(x)
self.fc_feat = x.detach().cpu()
x = self.fc_2(x)
self.final_feat = x.detach().cpu()
x = F.log_softmax(x, dim=1)
return x