-
Notifications
You must be signed in to change notification settings - Fork 45
/
conv_net.py
44 lines (39 loc) · 1.55 KB
/
conv_net.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
import torch.nn as nn
class ConvNet(nn.Module):
def __init__(self, num_classes=2):
"""
Convolutional Neural Network with 3 convolutional layers followed by 3 fully connected layers
"""
super().__init__()
self.conv1 = nn.Conv2d(3, 32, 5)
self.conv2 = nn.Conv2d(32, 64, 3)
self.conv3 = nn.Conv2d(64, 128, 3)
self.last_filter_output = 2 * 2
self.num_conv_outputs = 128 * self.last_filter_output
self.fc1 = nn.Linear(self.num_conv_outputs, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, num_classes)
self.pool = nn.MaxPool2d(2, 2)
# architecture
self.layers = nn.ModuleList()
self.layers.append(self.conv1)
self.layers.append(nn.ReLU())
self.layers.append(self.conv2)
self.layers.append(nn.ReLU())
self.layers.append(self.conv3)
self.layers.append(nn.ReLU())
self.layers.append(self.fc1)
self.layers.append(nn.ReLU())
self.layers.append(self.fc2)
self.layers.append(nn.ReLU())
self.layers.append(self.fc3)
self.act_type = 'relu'
def predict(self, x):
x1 = self.pool(self.layers[1](self.layers[0](x)))
x2 = self.pool(self.layers[3](self.layers[2](x1)))
x3 = self.pool(self.layers[5](self.layers[4](x2)))
x3 = x3.view(-1, self.num_conv_outputs)
x4 = self.layers[7](self.layers[6](x3))
x5 = self.layers[9](self.layers[8](x4))
x6 = self.layers[10](x5)
return x6, [x1, x2, x3, x4, x5]