-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel1.py
68 lines (53 loc) · 2.52 KB
/
model1.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
import torch
import torch.nn as nn
class CNN_4Conv(nn.Module):
def __init__(self,batchNorm,outKernels,linDim):
super(CNN_4Conv,self).__init__()
self.outKernels = outKernels
self.batchNorm = batchNorm
self.linDim = linDim
self.conv1 = nn.Conv2d(in_channels=3,out_channels=self.outKernels,kernel_size=3,stride=1)
self.conv2 = nn.Conv2d(in_channels=self.outKernels,out_channels=self.outKernels,kernel_size=3,stride=1)
self.conv3 = nn.Conv2d(in_channels=self.outKernels,out_channels=self.outKernels,kernel_size=3,stride=1)
self.conv4 = nn.Conv2d(in_channels=self.outKernels,out_channels=self.outKernels,kernel_size=3,stride=1)
if (self.batchNorm):
self.conv1BN = nn.BatchNorm2d(self.outKernels)
self.conv2BN = nn.BatchNorm2d(self.outKernels)
self.conv3BN = nn.BatchNorm2d(self.outKernels)
self.conv4BN = nn.BatchNorm2d(self.outKernels)
self.lin1BN = nn.BatchNorm1d(self.linDim)
self.lin2BN = nn.BatchNorm1d(16)
self.pool = nn.MaxPool2d(2,2)
# need to pad 1 pixel b/c dims become odd later
self.poolE = nn.MaxPool2d(2,2,padding=1)
self.relu = nn.ReLU()
# for 4 conv layers
self.fc1 = nn.Linear(2*2*self.outKernels,self.linDim)
self.fc2 = nn.Linear(self.linDim,16)
self.fc3 = nn.Linear(16,10)
def forward(self,x):
if self.batchNorm:
x = self.pool(self.relu(self.conv1BN(self.conv1(x))))
x = self.poolE(self.relu(self.conv2BN(self.conv2(x))))
x = self.poolE(self.relu(self.conv3BN(self.conv3(x))))
x = self.pool(self.relu(self.conv4BN(self.conv4(x))))
x = x.view(-1,2*2*self.outKernels)
x = self.lin1BN(self.relu(self.fc1(x)))
x = self.lin2BN(self.relu(self.fc2(x)))
x = (self.fc3(x))
return x
else:
x = self.pool(self.relu(self.conv1(x)))
x = self.poolE(self.relu(self.conv2(x)))
x = self.poolE(self.relu(self.conv3(x)))
x = self.pool(self.relu(self.conv4(x)))
x = x.view(-1,2*2*self.outKernels)
x = (self.relu(self.fc1(x)))
x = (self.relu(self.fc2(x)))
x = (self.fc3(x))
return x
from torchsummary import summary
# net = CNN_4Conv(batchNorm=False,outKernels=10,linDim=32)
# print(
# summary(net,(3,56,56))
# )