-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
150 lines (124 loc) · 5.15 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
import torch
import torch.nn.functional as F
from torch import nn
from src.models.hrnet.hrnet import HighResolutionNet
def conv(in_channels, out_channels, kernel_size=3, padding=1, bn=True,
dilation=1, stride=1, relu=True, bias=True):
modules = [nn.Conv2d(in_channels, out_channels,
kernel_size, stride, padding, dilation, bias=bias)]
if bn:
modules.append(nn.BatchNorm2d(out_channels))
if relu:
modules.append(nn.ReLU(inplace=True))
return nn.Sequential(*modules)
def conv_dw(in_channels, out_channels, kernel_size=3, padding=1, stride=1,
dilation=1):
return nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size, stride,
padding, dilation=dilation, groups=in_channels, bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 1, 1, 0, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
class InitialStage(nn.Module):
def __init__(self, num_channels, num_heatmaps):
super().__init__()
self.trunk = nn.Sequential(
conv(num_channels, num_channels),
conv(num_channels, num_channels),
conv(num_channels, num_channels)
)
self.heatmaps = nn.Sequential(
conv(num_channels, 512, kernel_size=1, padding=0, bn=False),
conv(512, num_heatmaps, kernel_size=1,
padding=0, bn=False, relu=False)
)
self.final = nn.Softmax(dim=1)
def forward(self, x):
trunk_features = self.trunk(x)
heatmaps = self.final(self.heatmaps(trunk_features))
return [heatmaps]
class RefinementStageBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.initial = conv(in_channels, out_channels,
kernel_size=1, padding=0, bn=False)
self.trunk = nn.Sequential(
conv(out_channels, out_channels),
conv(out_channels, out_channels, dilation=2, padding=2)
)
self.final = nn.Softmax(dim=1)
def forward(self, x):
initial_features = self.initial(x)
trunk_features = self.trunk(initial_features)
return self.final(initial_features + trunk_features)
class UShapedContextBlock(nn.Module):
def __init__(self, in_channels):
super().__init__()
self.encoder1 = nn.Sequential(
conv(in_channels, in_channels*2, stride=2),
conv(in_channels*2, in_channels*2),
)
self.encoder2 = nn.Sequential(
conv(in_channels*2, in_channels*2, stride=2),
conv(in_channels*2, in_channels*2),
)
self.decoder2 = nn.Sequential(
conv(in_channels*2 + in_channels*2, in_channels*2),
conv(in_channels*2, in_channels*2),
)
self.decoder1 = nn.Sequential(
conv(in_channels*3, in_channels*2),
conv(in_channels*2, in_channels)
)
def forward(self, x):
e1 = self.encoder1(x)
e2 = self.encoder2(e1)
size_e1 = (e1.size()[2], e1.size()[3])
size_x = (x.size()[2], x.size()[3])
d2 = self.decoder2(
torch.cat([e1, F.interpolate(e2, size=size_e1, mode='bilinear',
align_corners=False)], 1))
d1 = self.decoder1(
torch.cat([x, F.interpolate(d2, size=size_x, mode='bilinear',
align_corners=False)], 1))
return d1
class RefinementStage(nn.Module):
def __init__(self, in_channels, out_channels, num_heatmaps):
super().__init__()
self.trunk = nn.Sequential(
UShapedContextBlock(in_channels),
RefinementStageBlock(in_channels, out_channels),
RefinementStageBlock(out_channels, out_channels),
RefinementStageBlock(out_channels, out_channels),
)
self.heatmaps = nn.Sequential(
conv(out_channels, out_channels, kernel_size=1, padding=0, bn=False),
conv(out_channels, num_heatmaps, kernel_size=1,
padding=0, bn=False, relu=False),
nn.LogSoftmax(dim=1)
)
def forward(self, x):
trunk_features = self.trunk(x)
heatmaps = self.heatmaps(trunk_features)
return [heatmaps]
class HRNetHeatmap(nn.Module):
def __init__(self, hrnet_config, num_refinement_stages: int = 0,
num_heatmaps: int = 38):
super().__init__()
self.model = HighResolutionNet(hrnet_config)
self.refinement_stages = nn.ModuleList()
for _ in range(num_refinement_stages):
self.refinement_stages.append(
RefinementStage(self.model.last_inp_channels + num_heatmaps,
self.model.last_inp_channels,
num_heatmaps))
def forward(self, x):
stages_output, x_encoder = self.model(x)
for refinement_stage in self.refinement_stages:
stages_output.extend(
refinement_stage(torch.cat(
[x_encoder, stages_output[-1]], dim=1)))
return stages_output