-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
185 lines (161 loc) · 7.05 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
## TODO: define the convolutional neural network architecture
import torch
from torch.autograd import Variable
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
from collections import OrderedDict
from itertools import chain
# Global VARs, SETUP
RANDOM_SEED = 42
class Net_V1_0(nn.Module):
def __init__(self, initiation=False):
torch.manual_seed(RANDOM_SEED)
super(Net_V1_0, self).__init__()
# as seen in an example of paper for NaimishNet, but didn't do the initial dropout with fear would avoid hyper activation of needed features
self.features = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 32, 5)),
('relu1', nn.ReLU()),
('maxpool1', nn.MaxPool2d(2)),
('conv2', nn.Conv2d(32, 64, 5)),
('relu2', nn.ReLU()),
('maxpool2', nn.MaxPool2d(2)),
('dropout1', nn.Dropout(0.4)),
('conv3', nn.Conv2d(64, 128, 5)),
('relu3', nn.ReLU()),
('maxpool3', nn.MaxPool2d(2)),
('dropout2', nn.Dropout(0.4)),
('conv4', nn.Conv2d(128, 256, 5)),
('relu4', nn.ReLU()),
('maxpool4', nn.MaxPool2d(2)),
('dropout3', nn.Dropout(0.4))
]))
#simplified for now the output
self.regressor = nn.Sequential(OrderedDict([
('linear1',nn.Linear(25600, 1000)),
('relu1',nn.ReLU()),
('dropout1', nn.Dropout(0.4)),
## it's suggested that you make this last layer output 136 values, 2 for each of the 68 keypoint (x, y) pairs
('output', nn.Linear(1000, 136)),
]))
if initiation:
for layer in chain(self.features.children(), self.regressor.children()):
if isinstance(layer, nn.Conv2d) or isinstance(layer, nn.Linear):
I.xavier_uniform_(layer.weight)
I.zeros_(layer.bias)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.regressor(x)
return x
class Net_V1_1(nn.Module):
def __init__(self, initiation=False):
torch.manual_seed(RANDOM_SEED)
super(Net_V1_1, self).__init__()
# as seen in an example of paper for NaimishNet, but didn't do the initial dropout with fear would avoid hyper activation of needed features
self.features = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 32, 5)),
('relu1', nn.ReLU()),
('maxpool1', nn.MaxPool2d(2)),
('conv2', nn.Conv2d(32, 64, 5)),
('relu2', nn.ReLU()),
('maxpool2', nn.MaxPool2d(2)),
('dropout1', nn.Dropout(0.4)),
('conv3', nn.Conv2d(64, 128, 5)),
('relu3', nn.ReLU()),
('maxpool3', nn.MaxPool2d(2)),
('dropout2', nn.Dropout(0.4)),
('conv4', nn.Conv2d(128, 256, 5)),
('relu4', nn.ReLU()),
('maxpool4', nn.MaxPool2d(2)),
('dropout3', nn.Dropout(0.4))
]))
#simplified for now the output
self.regressor = nn.Sequential(OrderedDict([
('linear1',nn.Linear(25600, 12800)),
('relu1',nn.ReLU()),
('linear2',nn.Linear(12800, 1000)),
('relu2',nn.ReLU()),
('dropout1', nn.Dropout(0.4)),
## it's suggested that you make this last layer output 136 values, 2 for each of the 68 keypoint (x, y) pairs
('output', nn.Linear(1000, 136)),
]))
if initiation:
for layer in chain(self.features.children(), self.regressor.children()):
if isinstance(layer, nn.Conv2d) or isinstance(layer, nn.Linear):
I.xavier_uniform_(layer.weight)
I.zeros_(layer.bias)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.regressor(x)
return x
class Net_V1_2(nn.Module):
def __init__(self, initiation=False):
torch.manual_seed(RANDOM_SEED)
super(Net_V1_2, self).__init__()
# as seen in an example of paper for NaimishNet, but didn't do the initial dropout with fear would avoid hyper activation of needed features
self.features = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 32, 5)),
('relu1', nn.ReLU()),
('maxpool1', nn.MaxPool2d(2)),
('conv2', nn.Conv2d(32, 64, 5)),
('relu2', nn.ReLU()),
('maxpool2', nn.MaxPool2d(2)),
('dropout1', nn.Dropout(0.4)),
('conv3', nn.Conv2d(64, 128, 5)),
('relu3', nn.ReLU()),
('maxpool3', nn.MaxPool2d(4)),
('dropout2', nn.Dropout(0.4))
]))
#simplified for now the output
self.regressor = nn.Sequential(OrderedDict([
('linear1',nn.Linear(128*12*12, 1000)),
('relu1',nn.ReLU()),
('dropout1', nn.Dropout(0.4)),
## it's suggested that you make this last layer output 136 values, 2 for each of the 68 keypoint (x, y) pairs
('output', nn.Linear(1000, 136)),
]))
if initiation:
for layer in chain(self.features.children(), self.regressor.children()):
if isinstance(layer, nn.Conv2d) or isinstance(layer, nn.Linear):
I.xavier_uniform_(layer.weight)
I.zeros_(layer.bias)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.regressor(x)
return x
class Net_V1_3(nn.Module):
def __init__(self, initiation=False):
torch.manual_seed(RANDOM_SEED)
super(Net_V1_3, self).__init__()
# as seen in an example of paper for NaimishNet, but didn't do the initial dropout with fear would avoid hyper activation of needed features
self.features = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 32, 5)),
('relu1', nn.ReLU()),
('maxpool1', nn.MaxPool2d(4)),
('conv2', nn.Conv2d(32, 64, 5)),
('relu2', nn.ReLU()),
('maxpool2', nn.MaxPool2d(4)),
('dropout1', nn.Dropout(0.4)),
]))
#simplified for now the output
self.regressor = nn.Sequential(OrderedDict([
('linear1',nn.Linear(9216, 1000)),
('relu1',nn.ReLU()),
('dropout1', nn.Dropout(0.4)),
## it's suggested that you make this last layer output 136 values, 2 for each of the 68 keypoint (x, y) pairs
('output', nn.Linear(1000, 136)),
]))
if initiation:
for layer in chain(self.features.children(), self.regressor.children()):
if isinstance(layer, nn.Conv2d) or isinstance(layer, nn.Linear):
I.xavier_uniform_(layer.weight)
I.zeros_(layer.bias)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.regressor(x)
return x