forked from 11785-Group-9/BMN-Boundary-Matching-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
166 lines (149 loc) · 6.7 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
# -*- coding: utf-8 -*-
import math
import numpy as np
import torch
import torch.nn as nn
class BMN(nn.Module):
def __init__(self, opt):
super(BMN, self).__init__()
self.tscale = opt["temporal_scale"]
self.prop_boundary_ratio = opt["prop_boundary_ratio"]
self.num_sample = opt["num_sample"]
self.num_sample_perbin = opt["num_sample_perbin"]
self.feat_dim=opt["feat_dim"]
print(f"Adding dropout = {opt['dropout']}")
self._s_and_e = opt['s_and_e']
self.hidden_dim_1d = 256
self.hidden_dim_2d = 128
self.hidden_dim_3d = 512
self._get_interp1d_mask()
# Squeeze and Excitation
if self._s_and_e:
print("Using S&E")
self.channel_ratio_extractor_SE = nn.Sequential(
nn.Linear(self.feat_dim, opt['se_hidden_dim']), # W1_SE
nn.ReLU(),
nn.Linear(opt['se_hidden_dim'], self.feat_dim), # W2_SE
nn.Sigmoid()
)
# Base Module
self.x_1d_b = nn.Sequential(
nn.Conv1d(self.feat_dim, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4),
# nn.BatchNorm1d(self.hidden_dim_1d),
nn.ReLU(inplace=True),
nn.Dropout(p=opt["dropout"]),
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4),
nn.ReLU(inplace=True)
)
# Temporal Evaluation Module
self.x_1d_s = nn.Sequential(
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4),
nn.ReLU(inplace=True),
nn.Dropout(p=opt["dropout"]),
nn.Conv1d(self.hidden_dim_1d, 1, kernel_size=1),
nn.Sigmoid()
)
self.x_1d_e = nn.Sequential(
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4),
nn.ReLU(inplace=True),
nn.Dropout(p=opt["dropout"]),
nn.Conv1d(self.hidden_dim_1d, 1, kernel_size=1),
nn.Sigmoid()
)
# Proposal Evaluation Module
self.x_1d_p = nn.Sequential(
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1),
# nn.BatchNorm1d(self.hidden_dim_1d),
nn.ReLU(inplace=True)
)
self.x_3d_p = nn.Sequential(
nn.Conv3d(self.hidden_dim_1d, self.hidden_dim_3d, kernel_size=(self.num_sample, 1, 1),stride=(self.num_sample, 1, 1)),
nn.ReLU(inplace=True)
)
self.x_2d_p = nn.Sequential(
nn.Conv2d(self.hidden_dim_3d, self.hidden_dim_2d, kernel_size=1),
nn.ReLU(inplace=True),
nn.Dropout(p=opt["dropout"]),
nn.Conv2d(self.hidden_dim_2d, self.hidden_dim_2d, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Dropout(p=opt["dropout"]),
nn.Conv2d(self.hidden_dim_2d, self.hidden_dim_2d, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(self.hidden_dim_2d, 2, kernel_size=1),
nn.Sigmoid()
)
def forward(self, x):
if self._s_and_e:
feature_means = torch.mean(x, dim=2) # average across time T
s = self.channel_ratio_extractor_SE(feature_means) # (B, 400)
s = torch.unsqueeze(s, dim=2) # (B, 400, 1)
# print(f'SE: shape:{s.shape}, sum{torch.sum(s, dim=1)} s: {s}')
base_input_x = x * s
else:
base_input_x = x
base_feature = self.x_1d_b(x)
start = self.x_1d_s(base_feature).squeeze(1)
end = self.x_1d_e(base_feature).squeeze(1)
confidence_map = self.x_1d_p(base_feature)
confidence_map = self._boundary_matching_layer(confidence_map)
confidence_map = self.x_3d_p(confidence_map).squeeze(2)
confidence_map = self.x_2d_p(confidence_map)
return confidence_map, start, end
def _boundary_matching_layer(self, x):
input_size = x.size()
out = torch.matmul(x, self.sample_mask).reshape(input_size[0],input_size[1],self.num_sample,self.tscale,self.tscale)
return out
def _get_interp1d_bin_mask(self, seg_xmin, seg_xmax, tscale, num_sample, num_sample_perbin):
# generate sample mask for a boundary-matching pair
plen = float(seg_xmax - seg_xmin)
plen_sample = plen / (num_sample * num_sample_perbin - 1.0)
total_samples = [
seg_xmin + plen_sample * ii
for ii in range(num_sample * num_sample_perbin)
]
p_mask = []
for idx in range(num_sample):
bin_samples = total_samples[idx * num_sample_perbin:(idx + 1) * num_sample_perbin]
bin_vector = np.zeros([tscale])
for sample in bin_samples:
sample_upper = math.ceil(sample)
sample_decimal, sample_down = math.modf(sample)
if int(sample_down) <= (tscale - 1) and int(sample_down) >= 0:
bin_vector[int(sample_down)] += 1 - sample_decimal
if int(sample_upper) <= (tscale - 1) and int(sample_upper) >= 0:
bin_vector[int(sample_upper)] += sample_decimal
bin_vector = 1.0 / num_sample_perbin * bin_vector
p_mask.append(bin_vector)
p_mask = np.stack(p_mask, axis=1)
return p_mask
def _get_interp1d_mask(self):
# generate sample mask for each point in Boundary-Matching Map
mask_mat = []
for end_index in range(self.tscale):
mask_mat_vector = []
for start_index in range(self.tscale):
if start_index <= end_index:
p_xmin = start_index
p_xmax = end_index + 1
center_len = float(p_xmax - p_xmin) + 1
sample_xmin = p_xmin - center_len * self.prop_boundary_ratio
sample_xmax = p_xmax + center_len * self.prop_boundary_ratio
p_mask = self._get_interp1d_bin_mask(
sample_xmin, sample_xmax, self.tscale, self.num_sample,
self.num_sample_perbin)
else:
p_mask = np.zeros([self.tscale, self.num_sample])
mask_mat_vector.append(p_mask)
mask_mat_vector = np.stack(mask_mat_vector, axis=2)
mask_mat.append(mask_mat_vector)
mask_mat = np.stack(mask_mat, axis=3)
mask_mat = mask_mat.astype(np.float32)
self.sample_mask = nn.Parameter(torch.Tensor(mask_mat).view(self.tscale, -1), requires_grad=False)
if __name__ == '__main__':
import opts
opt = opts.parse_opt()
opt = vars(opt)
model=BMN(opt)
input=torch.randn(2,400,100)
a,b,c=model(input)
print(a.shape,b.shape,c.shape)