Repository navigation
Expand file tree
/
Copy pathcnn_model.py
More file actions
69 lines (60 loc) · 3.59 KB
/
Copy pathcnn_model.py
File metadata and controls
69 lines (60 loc) · 3.59 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
import torch.nn as nn
import torch.nn.functional as F
class ModelCNN(nn.Module):
"""Implemented by paper: http://cs230.stanford.edu/projects_winter_2019/posters/15813053.pdf"""
def __init__(self, n_input_channels=3, n_conv_output_channels=16, k=3, s=1, pad=1, p = 0.5):
"""Init method for initializaing the CNN model"""
super(ModelCNN, self).__init__()
# 1. Convolutional layers
# Single image is in shape: 3x96x96 (CxHxW, H==W), RGB images
self.conv1 = nn.Conv2d(in_channels = n_input_channels, out_channels = n_conv_output_channels, kernel_size = k, stride = s, padding = pad)
self.bn1 = nn.BatchNorm2d(n_conv_output_channels)
self.conv2 = nn.Conv2d(in_channels = n_conv_output_channels, out_channels = 2*n_conv_output_channels, kernel_size = k, stride = s, padding = pad)
self.bn2 = nn.BatchNorm2d(2*n_conv_output_channels)
self.conv3 = nn.Conv2d(in_channels = 2*n_conv_output_channels, out_channels = 4*n_conv_output_channels, kernel_size = k, stride = s, padding = pad)
self.bn3 = nn.BatchNorm2d(4*n_conv_output_channels)
self.conv4 = nn.Conv2d(in_channels = 4*n_conv_output_channels, out_channels = 8*n_conv_output_channels, kernel_size = k, stride = s, padding = pad)
self.bn4 = nn.BatchNorm2d(8*n_conv_output_channels)
self.pool = nn.MaxPool2d(kernel_size = k - 1, stride = 2*s, padding = pad - pad)
self.dropout = nn.Dropout(p = p)
# 2. FC layers to final output
self.fc1 = nn.Linear(in_features = 288*n_conv_output_channels, out_features = 32*n_conv_output_channels)
self.fc_bn1 = nn.BatchNorm1d(32*n_conv_output_channels)
self.fc2 = nn.Linear(in_features = 32*n_conv_output_channels, out_features = 16*n_conv_output_channels)
self.fc_bn2 = nn.BatchNorm1d(16*n_conv_output_channels)
self.fc3 = nn.Linear(in_features = 16*n_conv_output_channels, out_features = 8*n_conv_output_channels)
self.fc_bn3 = nn.BatchNorm1d(8*n_conv_output_channels)
self.fc4 = nn.Linear(in_features = 8*n_conv_output_channels, out_features = 1)
def forward(self, x):
# Convolution Layers, followed by Batch Normalizations, Maxpool, and ReLU
x = self.bn1(self.conv1(x)) # batch_size x 96 x 96 x 16
x = self.pool(F.relu(x)) # batch_size x 48 x 48 x 16
x = self.bn2(self.conv2(x)) # batch_size x 48 x 48 x 32
x = self.pool(F.relu(x)) # batch_size x 24 x 24 x 32
x = self.bn3(self.conv3(x)) # batch_size x 24 x 24 x 64
x = self.pool(F.relu(x)) # batch_size x 12 x 12 x 64
x = self.bn4(self.conv4(x)) # batch_size x 12 x 12 x 128
x = self.pool(F.relu(x)) # batch_size x 6 x 6 x 128
# Flatten the output for each image
x = x.reshape(-1, self.num_flat_features(x)) # batch_size x 6*6*128
# Apply 4 FC Layers
x = self.fc1(x)
x = self.fc_bn1(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc2(x)
x = self.fc_bn2(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc3(x)
x = self.fc_bn3(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc4(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features