-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_models.py
132 lines (121 loc) · 3.89 KB
/
cnn_models.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.module_1 = nn.Sequential(
nn.Conv2d(
in_channels=3,
out_channels=32,
kernel_size=3,
stride=1,
padding=1,
),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,32,3,1,1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,32,3,1,1),
nn.Dropout2d(p=0.1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.module_2 = nn.Sequential(
nn.Conv2d(32, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64,64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, 1, 1),
nn.Dropout2d(p=0.1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.module_3 = nn.Sequential(
nn.Conv2d(64, 128, 3, 1, 1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, 3, 1, 1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, 3, 1, 1),
nn.Dropout2d(p = 0.1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.module_4 = nn.Sequential(
nn.Conv2d(128, 256, 3, 1, 1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 256, 3, 1, 1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 256, 3, 1, 1),
nn.Dropout2d(p = 0.1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.out = nn.Linear(256, 10)
def forward(self, x):
x = self.module_1(x)
x = self.module_2(x)
x = self.module_3(x)
x = self.module_4(x)
x = x.view(x.size(0), x.size(1), -1)
x = x.mean(2)
x = x.view(x.size(0),-1)
output = self.out(x)
output = F.log_softmax(output)
return output
######################################################################3
class CNN_MNIST(nn.Module):
def __init__(self):
super(CNN_MNIST, self).__init__()
self.module_1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=3,
stride=1,
padding=1,
),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16,16,3,1,1),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16,16,3,1,1),
# nn.Dropout2d(),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.module_2 = nn.Sequential(
nn.Conv2d(16, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 32, 3, 1, 1),
# nn.Dropout2d(),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.out = nn.Linear(32 * 7 * 7, 10)
def forward(self, x):
x = self.module_1(x)
x = self.module_2(x)
x = x.view(x.size(0),-1)
output = self.out(x)
# output = F.log_softmax(output)
return output