-
Notifications
You must be signed in to change notification settings - Fork 12
/
models.py
93 lines (70 loc) · 3.56 KB
/
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
## Define the convolutional neural network architecture
import torch
import torch.nn as nn
import torch.nn.functional as F
# can use the below import should you choose to initialize the weights of your Net
import torch.nn.init as I
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
## Define all the layers of this CNN, the only requirements are:
## 1. This network takes in a square (same width and height), grayscale image as input
## 2. It ends with a linear layer that represents the keypoints
## Last layer output 136 values, 2 for each of the 68 keypoint (x, y) pairs
# 1 input image channel (grayscale), 32 output channels/feature maps, 5x5 square convolution kernel
## Shape of a Convolutional Layer
# K - out_channels : the number of filters in the convolutional layer
# F - kernel_size
# S - the stride of the convolution
# P - the padding
# W - the width/height (square) of the previous layer
# Since there are F*F*D weights per filter
# The total number of weights in the convolutional layer is K*F*F*D
# 224 by 224 pixels
## self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0)
# output size = (W-F)/S +1 = (224-5)/1 +1 = 220
# the output Tensor for one image, will have the dimensions: (1, 220, 220)
# after one pool layer, this becomes (10, 13, 13)
self.conv1 = nn.Conv2d(1, 32, 5)
# maxpool layer
# pool with kernel_size=2, stride=2
self.pool = nn.MaxPool2d(2, 2)
# 220/2 = 110
# output size = (W-F)/S +1 = (110-3)/1 + 1 = 108
# the output Tensor for one image, will have the dimensions: (32, 110, 110)
self.conv2 = nn.Conv2d(32, 64, 3)
# output size = (W-F)/S +1 = (54-3)/1 + 1 = 52
# the output Tensor for one image, will have the dimensions: (64, 54, 54)
self.conv3 = nn.Conv2d(64, 128, 3)
# output size = (W-F)/S +1 = (26-3)/1 + 1 = 24
# the output Tensor for one image, will have the dimensions: (128, 26, 26)
self.conv4 = nn.Conv2d(128, 256, 3)
# output size = (W-F)/S +1 = (12-3)/1 + 1 = 10
# the output Tensor for one image, will have the dimensions: (256, 12, 12)
self.conv5 = nn.Conv2d(256, 512, 1)
# output size = (W-F)/S +1 = (6-1)/1 + 1 = 6
# the output Tensor for one image, will have the dimensions: (512, 6, 6)
# Fully-connected (linear) layers
self.fc1 = nn.Linear(512*6*6, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 68*2)
# Dropout
self.dropout = nn.Dropout(p=0.25)
def forward(self, x):
## Define the feedforward behavior of this model
## x is the input image and, as an example, here you may choose to include a pool/conv step:
# 5 conv/relu + pool layers
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
x = self.pool(F.relu(self.conv4(x)))
x = self.pool(F.relu(self.conv5(x)))
# Prep for linear layer / Flatten
x = x.view(x.size(0), -1)
# linear layers with dropout in between
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = F.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x