-
Notifications
You must be signed in to change notification settings - Fork 14
/
model.py
206 lines (163 loc) · 6.76 KB
/
model.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import time
import copy
import torch
import torchvision
import pandas as pd
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, models, datasets
import torch.nn.functional as F
import matplotlib.pyplot as plt
from PIL import Image
def model_parser(model, fixed_weight=False, dropout_rate=0.0, bayesian = False):
base_model = None
if model == 'Googlenet':
base_model = models.inception_v3(pretrained=True)
network = GoogleNet(base_model, fixed_weight, dropout_rate)
elif model == 'Resnet':
base_model = models.resnet34(pretrained=True)
network = ResNet(base_model, fixed_weight, dropout_rate, bayesian)
elif model == 'ResnetSimple':
base_model = models.resnet34(pretrained=True)
network = ResNetSimple(base_model, fixed_weight)
else:
assert 'Unvalid Model'
return network
class PoseLoss(nn.Module):
def __init__(self, device, sx=0.0, sq=0.0, learn_beta=False):
super(PoseLoss, self).__init__()
self.learn_beta = learn_beta
if not self.learn_beta:
self.sx = 0
self.sq = -6.25
self.sx = nn.Parameter(torch.Tensor([sx]), requires_grad=self.learn_beta)
self.sq = nn.Parameter(torch.Tensor([sq]), requires_grad=self.learn_beta)
# if learn_beta:
# self.sx.requires_grad = True
# self.sq.requires_grad = True
#
# self.sx = self.sx.to(device)
# self.sq = self.sq.to(device)
self.loss_print = None
def forward(self, pred_x, pred_q, target_x, target_q):
pred_q = F.normalize(pred_q, p=2, dim=1)
loss_x = F.l1_loss(pred_x, target_x)
loss_q = F.l1_loss(pred_q, target_q)
loss = torch.exp(-self.sx)*loss_x \
+ self.sx \
+ torch.exp(-self.sq)*loss_q \
+ self.sq
self.loss_print = [loss.item(), loss_x.item(), loss_q.item()]
return loss, loss_x.item(), loss_q.item()
class ResNet(nn.Module):
def __init__(self, base_model, fixed_weight=False, dropout_rate=0.0, bayesian = False):
super(ResNet, self).__init__()
self.bayesian = bayesian
self.dropout_rate = dropout_rate
feat_in = base_model.fc.in_features
self.base_model = nn.Sequential(*list(base_model.children())[:-1])
# self.base_model = base_model
if fixed_weight:
for param in self.base_model.parameters():
param.requires_grad = False
self.fc_last = nn.Linear(feat_in, 2048, bias=True)
self.fc_position = nn.Linear(2048, 3, bias=True)
self.fc_rotation = nn.Linear(2048, 4, bias=True)
init_modules = [self.fc_last, self.fc_position, self.fc_rotation]
for module in init_modules:
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):
nn.init.kaiming_normal_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0)
# nn.init.normal_(self.fc_last.weight, 0, 0.01)
# nn.init.constant_(self.fc_last.bias, 0)
#
# nn.init.normal_(self.fc_position.weight, 0, 0.5)
# nn.init.constant_(self.fc_position.bias, 0)
#
# nn.init.normal_(self.fc_rotation.weight, 0, 0.01)
# nn.init.constant_(self.fc_rotation.bias, 0)
def forward(self, x):
x = self.base_model(x)
x = x.view(x.size(0), -1)
x_fully = self.fc_last(x)
x = F.relu(x_fully)
dropout_on = self.training or self.bayesian
if self.dropout_rate > 0:
x = F.dropout(x, p=self.dropout_rate, training=dropout_on)
position = self.fc_position(x)
rotation = self.fc_rotation(x)
return position, rotation, x_fully
class ResNetSimple(nn.Module):
def __init__(self, base_model, fixed_weight=False, dropout_rate=0.0):
super(ResNetSimple, self).__init__()
self.dropout_rate = dropout_rate
feat_in = base_model.fc.in_features
self.base_model = nn.Sequential(*list(base_model.children())[:-1])
# self.base_model = base_model
if fixed_weight:
for param in self.base_model.parameters():
param.requires_grad = False
# self.fc_last = nn.Linear(feat_in, 2048, bias=True)
self.fc_position = nn.Linear(feat_in, 3, bias=False)
self.fc_rotation = nn.Linear(feat_in, 4, bias=False)
init_modules = [self.fc_position, self.fc_rotation]
for module in init_modules:
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):
nn.init.kaiming_normal(module.weight.data)
if module.bias is not None:
nn.init.constant(module.bias.data, 0)
def forward(self, x):
x = self.base_model(x)
x = x.view(x.size(0), -1)
position = self.fc_position(x)
rotation = self.fc_rotation(x)
return position, rotation
class GoogleNet(nn.Module):
""" PoseNet using Inception V3 """
def __init__(self, base_model, fixed_weight=False, dropout_rate = 0.0):
super(GoogleNet, self).__init__()
self.dropout_rate =dropout_rate
model = []
model.append(base_model.Conv2d_1a_3x3)
model.append(base_model.Conv2d_2a_3x3)
model.append(base_model.Conv2d_2b_3x3)
model.append(nn.MaxPool2d(kernel_size=3, stride=2))
model.append(base_model.Conv2d_3b_1x1)
model.append(base_model.Conv2d_4a_3x3)
model.append(nn.MaxPool2d(kernel_size=3, stride=2))
model.append(base_model.Mixed_5b)
model.append(base_model.Mixed_5c)
model.append(base_model.Mixed_5d)
model.append(base_model.Mixed_6a)
model.append(base_model.Mixed_6b)
model.append(base_model.Mixed_6c)
model.append(base_model.Mixed_6d)
model.append(base_model.Mixed_6e)
model.append(base_model.Mixed_7a)
model.append(base_model.Mixed_7b)
model.append(base_model.Mixed_7c)
self.base_model = nn.Sequential(*model)
if fixed_weight:
for param in self.base_model.parameters():
param.requires_grad = False
# Out 2
self.pos2 = nn.Linear(2048, 3, bias=True)
self.ori2 = nn.Linear(2048, 4, bias=True)
def forward(self, x):
# 299 x 299 x 3
x = self.base_model(x)
# 8 x 8 x 2048
x = F.avg_pool2d(x, kernel_size=8)
# 1 x 1 x 2048
x = F.dropout(x, p=self.dropout_rate, training=self.training)
# 1 x 1 x 2048
x = x.view(x.size(0), -1)
# 2048
pos = self.pos2(x)
ori = self.ori2(x)
return pos, ori