-
Notifications
You must be signed in to change notification settings - Fork 39
/
model.py
216 lines (153 loc) · 9.52 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
207
208
209
210
211
212
213
214
215
216
import __future__
import numpy as np
import warnings
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, weights=None, input_size=512, chunk_size=240, dim_capsule=16, receptive_field=80, framerate=2):
"""
INPUT: a Tensor of the form (batch_size,1,chunk_size,input_size)
OUTPUTS: 1. The segmentation of the form (batch_size,chunk_size,num_classes)
2. The action spotting of the form (batch_size,num_detections,2+num_classes)
"""
super(Model, self).__init__()
self.load_weights(weights=weights)
self.input_size = input_size
self.num_classes = 1
self.dim_capsule = dim_capsule
self.receptive_field = receptive_field
self.num_detections = 1
self.chunk_size = chunk_size
self.framerate = framerate
self.pyramid_size_1 = int(np.ceil(receptive_field/7))
self.pyramid_size_2 = int(np.ceil(receptive_field/3))
self.pyramid_size_3 = int(np.ceil(receptive_field/2))
self.pyramid_size_4 = int(np.ceil(receptive_field))
# Base Convolutional Layers
self.conv_1 = nn.Conv2d(in_channels=1, out_channels=128, kernel_size=(1,input_size))
self.conv_2 = nn.Conv2d(in_channels=128, out_channels=16, kernel_size=(1,1))
# Temporal Pyramidal Module
self.pad_p_1 = nn.ZeroPad2d((0,0,(self.pyramid_size_1-1)//2, self.pyramid_size_1-1-(self.pyramid_size_1-1)//2))
self.pad_p_2 = nn.ZeroPad2d((0,0,(self.pyramid_size_2-1)//2, self.pyramid_size_2-1-(self.pyramid_size_2-1)//2))
self.pad_p_3 = nn.ZeroPad2d((0,0,(self.pyramid_size_3-1)//2, self.pyramid_size_3-1-(self.pyramid_size_3-1)//2))
self.pad_p_4 = nn.ZeroPad2d((0,0,(self.pyramid_size_4-1)//2, self.pyramid_size_4-1-(self.pyramid_size_4-1)//2))
self.conv_p_1 = nn.Conv2d(in_channels=16, out_channels=4, kernel_size=(self.pyramid_size_1,1))
self.conv_p_2 = nn.Conv2d(in_channels=16, out_channels=8, kernel_size=(self.pyramid_size_2,1))
self.conv_p_3 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(self.pyramid_size_3,1))
self.conv_p_4 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=(self.pyramid_size_4,1))
# -------------------
# Segmentation module
# -------------------
self.kernel_seg_size = 3
self.pad_seg = nn.ZeroPad2d((0,0,(self.kernel_seg_size-1)//2, self.kernel_seg_size-1-(self.kernel_seg_size-1)//2))
self.conv_seg = nn.Conv2d(in_channels=152, out_channels=dim_capsule*self.num_classes, kernel_size=(self.kernel_seg_size,1))
self.batch_seg = nn.BatchNorm2d(num_features=self.chunk_size, momentum=0.01,eps=0.001)
# -------------------
# detection module
# -------------------
self.max_pool_spot = nn.MaxPool2d(kernel_size=(3,1),stride=(2,1))
self.kernel_spot_size = 3
self.pad_spot_1 = nn.ZeroPad2d((0,0,(self.kernel_spot_size-1)//2, self.kernel_spot_size-1-(self.kernel_spot_size-1)//2))
self.conv_spot_1 = nn.Conv2d(in_channels=self.num_classes*(dim_capsule+1), out_channels=32, kernel_size=(self.kernel_spot_size,1))
self.max_pool_spot_1 = nn.MaxPool2d(kernel_size=(3,1),stride=(2,1))
self.pad_spot_2 = nn.ZeroPad2d((0,0,(self.kernel_spot_size-1)//2, self.kernel_spot_size-1-(self.kernel_spot_size-1)//2))
self.conv_spot_2 = nn.Conv2d(in_channels=32, out_channels=16, kernel_size=(self.kernel_spot_size,1))
self.max_pool_spot_2 = nn.MaxPool2d(kernel_size=(3,1),stride=(2,1))
# Confidence branch
self.conv_conf = nn.Conv2d(in_channels=16*(chunk_size//8-1), out_channels=self.num_detections*2, kernel_size=(1,1))
def load_weights(self, weights=None):
if(weights is not None):
print("=> loading checkpoint '{}'".format(weights))
checkpoint = torch.load(weights)
self.load_state_dict(checkpoint['state_dict'],strict=False)
print("=> loaded checkpoint '{}' (epoch {})"
.format(weights, checkpoint['epoch']))
def forward(self, inputs):
# -----------------------------------
# Feature input (chunks of the video)
# -----------------------------------
# input_shape: (batch,channel,frames,dim_features)
#print("Input size: ", inputs.size())
# -------------------------------------
# Temporal Convolutional neural network
# -------------------------------------
# Base Convolutional Layers
conv_1 = F.relu(self.conv_1(inputs[:,0].unsqueeze(1)))
#print("Conv_1 size: ", conv_1.size())
conv_2 = F.relu(self.conv_2(conv_1))
#print("Conv_2 size: ", conv_2.size())
# Temporal Pyramidal Module
conv_p_1 = F.relu(self.conv_p_1(self.pad_p_1(conv_2)))
#print("Conv_p_1 size: ", conv_p_1.size())
conv_p_2 = F.relu(self.conv_p_2(self.pad_p_2(conv_2)))
#print("Conv_p_2 size: ", conv_p_2.size())
conv_p_3 = F.relu(self.conv_p_3(self.pad_p_3(conv_2)))
#print("Conv_p_3 size: ", conv_p_3.size())
conv_p_4 = F.relu(self.conv_p_4(self.pad_p_4(conv_2)))
#print("Conv_p_4 size: ", conv_p_4.size())
concatenation = torch.cat((conv_2,conv_p_1,conv_p_2,conv_p_3,conv_p_4),1)
#print("Concatenation size: ", concatenation.size())
# Base Convolutional Layers
conv_1_replay = F.relu(self.conv_1(inputs[:,1].unsqueeze(1)))
#print("Conv_1 size: ", conv_1.size())
conv_2_replay = F.relu(self.conv_2(conv_1_replay))
#print("Conv_2 size: ", conv_2.size())
# Temporal Pyramidal Module
conv_p_1_replay = F.relu(self.conv_p_1(self.pad_p_1(conv_2_replay)))
#print("Conv_p_1 size: ", conv_p_1.size())
conv_p_2_replay = F.relu(self.conv_p_2(self.pad_p_2(conv_2_replay)))
#print("Conv_p_2 size: ", conv_p_2.size())
conv_p_3_replay = F.relu(self.conv_p_3(self.pad_p_3(conv_2_replay)))
#print("Conv_p_3 size: ", conv_p_3.size())
conv_p_4_replay = F.relu(self.conv_p_4(self.pad_p_4(conv_2_replay)))
#print("Conv_p_4 size: ", conv_p_4.size())
concatenation_replay = torch.cat((conv_2_replay,conv_p_1_replay,conv_p_2_replay,conv_p_3_replay,conv_p_4_replay),1)
#print("Concatenation size: ", concatenation.size())
concatenation = torch.cat((concatenation, concatenation_replay),1)
# -------------------
# Segmentation module
# -------------------
conv_seg = self.conv_seg(self.pad_seg(concatenation))
#print("Conv_seg size: ", conv_seg.size())
conv_seg_permuted = conv_seg.permute(0,2,3,1)
#print("Conv_seg_permuted size: ", conv_seg_permuted.size())
conv_seg_reshaped = conv_seg_permuted.view(conv_seg_permuted.size()[0],conv_seg_permuted.size()[1],self.dim_capsule,self.num_classes)
#print("Conv_seg_reshaped size: ", conv_seg_reshaped.size())
#conv_seg_reshaped_permuted = conv_seg_reshaped.permute(0,3,1,2)
#print("Conv_seg_reshaped_permuted size: ", conv_seg_reshaped_permuted.size())
conv_seg_norm = torch.sigmoid(self.batch_seg(conv_seg_reshaped))
#print("Conv_seg_norm: ", conv_seg_norm.size())
#conv_seg_norm_permuted = conv_seg_norm.permute(0,2,3,1)
#print("Conv_seg_norm_permuted size: ", conv_seg_norm_permuted.size())
output_segmentation = torch.sqrt(torch.sum(torch.square(conv_seg_norm-0.5), dim=2)*4/self.dim_capsule)
#print("Output_segmentation size: ", output_segmentation.size())
# ---------------
# Spotting module
# ---------------
# Concatenation of the segmentation score to the capsules
output_segmentation_reverse = 1-output_segmentation
#print("Output_segmentation_reverse size: ", output_segmentation_reverse.size())
output_segmentation_reverse_reshaped = output_segmentation_reverse.unsqueeze(2)
#print("Output_segmentation_reverse_reshaped size: ", output_segmentation_reverse_reshaped.size())
output_segmentation_reverse_reshaped_permutted = output_segmentation_reverse_reshaped.permute(0,3,1,2)
#print("Output_segmentation_reverse_reshaped_permutted size: ", output_segmentation_reverse_reshaped_permutted.size())
concatenation_2 = torch.cat((conv_seg, output_segmentation_reverse_reshaped_permutted), dim=1)
#print("Concatenation_2 size: ", concatenation_2.size())
conv_spot = self.max_pool_spot(F.relu(concatenation_2))
#print("Conv_spot size: ", conv_spot.size())
conv_spot_1 = F.relu(self.conv_spot_1(self.pad_spot_1(conv_spot)))
#print("Conv_spot_1 size: ", conv_spot_1.size())
conv_spot_1_pooled = self.max_pool_spot_1(conv_spot_1)
#print("Conv_spot_1_pooled size: ", conv_spot_1_pooled.size())
conv_spot_2 = F.relu(self.conv_spot_2(self.pad_spot_2(conv_spot_1_pooled)))
#print("Conv_spot_2 size: ", conv_spot_2.size())
conv_spot_2_pooled = self.max_pool_spot_2(conv_spot_2)
#print("Conv_spot_2_pooled size: ", conv_spot_2_pooled.size())
spotting_reshaped = conv_spot_2_pooled.view(conv_spot_2_pooled.size()[0],-1,1,1)
#print("Spotting_reshape size: ", spotting_reshaped.size())
# Confindence branch
output_spotting = torch.sigmoid(self.conv_conf(spotting_reshaped).view(spotting_reshaped.shape[0],self.num_detections,2))
#print("Conf_pred size: ", conf_pred.size())
#OUTPUT shape: batch*frames*num_classes
return output_segmentation, output_spotting