-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnnModel.py
More file actions
137 lines (127 loc) · 4.72 KB
/
cnnModel.py
File metadata and controls
137 lines (127 loc) · 4.72 KB
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
133
134
135
136
137
import torch.nn as nn
#Doc string used here for documentation purposes
"""++++++ CNN Class Starts here ++++++
Creating neural netwrok architecture here.
Data set image format for images is 48 x 48 grayscale (black and white).
"""
#2 blocks (4 convulutional layers ), kernel = 3 x 3
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv_layer = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1), # channel = 1 for gray scale
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
nn.Linear(12 * 12 * 64, 1000), #48/2/2 = 12
nn.ReLU(inplace=True),
nn.Linear(1000, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(512, 4)
)
def forward(self, x):
# conv layers
x = self.conv_layer(x)
#flatten
x = x.view(x.size(0), -1)
#fc layer
x = self.fc_layer(x)
return x
#Variant 1 with 3 block (6 convulutional layers) and k = 3 x 3
class CNNV1(nn.Module):
def __init__(self):
super(CNNV1, self).__init__()
self.conv_layer = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1), # channel = 1 for gray scale
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
nn.Linear(6 * 6 * 128, 1000), #48/2/2/2 = 6 , 3 layers
nn.ReLU(inplace=True),
nn.Linear(1000, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(512, 4)
)
def forward(self, x):
# conv layers
x = self.conv_layer(x)
#flatten
x = x.view(x.size(0), -1)
#fc layer
x = self.fc_layer(x)
return x
#Variant two with 2 blocks (4 convulutional layers) and kenerl = 5 x 5
class CNNV2(nn.Module):
def __init__(self):
super(CNNV2, self).__init__()
self.conv_layer = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5, padding=1), # channel = 1 for gray scale
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=5, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=5, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
nn.MaxPool2d(kernel_size=5, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
nn.Linear(64 *6*6, 1000), #48/2/2 = 12
nn.ReLU(inplace=True),
nn.Linear(1000, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(512, 4)
)
def forward(self, x):
# conv layers
x = self.conv_layer(x)
# print("Output after convolutions:", x.size())
#flatten
x = x.view(x.size(0), -1)
# print("Flattened output:", x.size())
#fc layer
x = self.fc_layer(x)
# print("Output after fully connected layers:", x.size())
return x