forked from ESanchezLozano/Action-Units-Heatmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhourglass.py
153 lines (117 loc) · 5.05 KB
/
hourglass.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class Bottleneck(nn.Module):
expansion = 2
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
expansion = 2
outplanes = int(planes/expansion)
self.conv1 = nn.Conv2d(inplanes, outplanes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(outplanes)
self.conv2 = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(outplanes)
self.conv3 = nn.Conv2d(outplanes, planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class HourGlass(nn.Module):
def __init__(self, num_modules, depth, num_features):
super(HourGlass, self).__init__()
self.num_modules = num_modules
self.depth = depth
self.features = num_features
self._generate_network(self.depth)
def _generate_network(self, level):
self.add_module('b1_' + str(level), Bottleneck(256, 256))
self.add_module('b2_' + str(level), Bottleneck(256, 256))
if level > 1:
self._generate_network(level - 1)
else:
self.add_module('b2_plus_' + str(level), Bottleneck(256, 256))
self.add_module('b3_' + str(level), Bottleneck(256, 256))
def _forward(self, level, inp):
# Upper branch
up1 = inp
up1 = self._modules['b1_' + str(level)](up1)
# Lower branch
low1 = F.max_pool2d(inp, 2, stride=2)
low1 = self._modules['b2_' + str(level)](low1)
if level > 1:
low2 = self._forward(level - 1, low1)
else:
low2 = low1
low2 = self._modules['b2_plus_' + str(level)](low2)
low3 = low2
low3 = self._modules['b3_' + str(level)](low3)
up2 = F.upsample(low3, scale_factor=2, mode='nearest')
return up1 + up2
def forward(self, x):
return self._forward(self.depth, x)
class FullNetwork(nn.Module):
def __init__(self, num_modules=1, n_maps=5):
super(FullNetwork, self).__init__()
self.num_modules = num_modules
self.conv1 = nn.Conv2d(3,64, kernel_size=7, stride=2, padding=3)
self.bn1 = nn.BatchNorm2d(64)
downsample = nn.Sequential(nn.Conv2d(64,128,kernel_size=1, stride=1, bias=False),nn.BatchNorm2d(128))
self.conv2 = Bottleneck(64,128,1,downsample)
self.conv3 = Bottleneck(128, 128)
downsample = nn.Sequential(nn.Conv2d(128,256,kernel_size=1, stride=1, bias=False),nn.BatchNorm2d(256))
self.conv4 = Bottleneck(128, 256,1,downsample)
for hg_module in range(self.num_modules):
print(hg_module)
self.add_module('m' + str(hg_module), HourGlass(1, 4, 256))
self.add_module('top_m_' + str(hg_module), Bottleneck(256, 256))
self.add_module('conv_last' + str(hg_module),
nn.Conv2d(256, 256, kernel_size=1, stride=1, padding=0))
self.add_module('l' + str(hg_module), nn.Conv2d(256,
n_maps, kernel_size=1, stride=1, padding=0))
self.add_module('bn_end' + str(hg_module), nn.BatchNorm2d(256))
if hg_module < self.num_modules - 1:
self.add_module(
'bl' + str(hg_module), nn.Conv2d(256, 256, kernel_size=1, stride=1, padding=0))
self.add_module('al' + str(hg_module), nn.Conv2d(n_maps,
256, kernel_size=1, stride=1, padding=0))
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x, True)
x = self.conv2(x)
x = F.max_pool2d(x, 2)
x = self.conv3(x)
x = self.conv4(x)
previous = x
outputs = []
for i in range(self.num_modules):
hg = self._modules['m' + str(i)](previous)
ll = hg
ll = self._modules['top_m_' + str(i)](ll)
ll = F.relu(self._modules['bn_end' + str(i)]
(self._modules['conv_last' + str(i)](ll)), True)
# Predict heatmaps
tmp_out = self._modules['l' + str(i)](ll)
outputs.append(tmp_out)
if i < self.num_modules - 1:
ll = self._modules['bl' + str(i)](ll)
tmp_out_ = self._modules['al' + str(i)](tmp_out)
previous = previous + ll + tmp_out_
return outputs