-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtbr.py
151 lines (132 loc) · 6.99 KB
/
tbr.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from ttn import TemporalTransformNetwork
from utils import (batch_iou,
bbox_se_transform_batch, bbox_xw_transform_batch,
bbox_se_transform_inv, bbox_xw_transform_inv)
class StartEndRegression(nn.Module):
def __init__(self, start_sample_num, end_sample_num, feat_dim):
super(StartEndRegression, self).__init__()
self.start_sample_num = start_sample_num
self.end_sample_num = end_sample_num
self.temporal_len = self.start_sample_num + self.end_sample_num
self.feat_dim = feat_dim
self.prop_boundary_ratio = 0.5
self.hidden_dim_1d = 128
self.start_reg_conv = nn.Sequential(
nn.Conv1d(self.feat_dim, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4, stride=2),
nn.ReLU(inplace=True),
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4, stride=2),
nn.ReLU(inplace=True),
nn.Conv1d(self.hidden_dim_1d, 1, kernel_size=self.start_sample_num // 4),
)
self.end_reg_conv = nn.Sequential(
nn.Conv1d(self.feat_dim, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4, stride=2),
nn.ReLU(inplace=True),
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4, stride=2),
nn.ReLU(inplace=True),
nn.Conv1d(self.hidden_dim_1d, 1, kernel_size=self.end_sample_num // 4),
)
def forward(self, starts_feature, ends_feature):
start_reg = self.start_reg_conv(starts_feature)
end_reg = self.end_reg_conv(ends_feature)
se_reg = torch.cat([start_reg, end_reg], dim=1).squeeze(2)
return se_reg
class CenterWidthRegression(nn.Module):
def __init__(self, start_sample_num, end_sample_num, action_sample_num, feat_dim):
super(CenterWidthRegression, self).__init__()
self.temporal_len = action_sample_num + start_sample_num + end_sample_num
self.feat_dim = feat_dim
self.hidden_dim_1d = 512
self.reg_1d_b = nn.Sequential(
nn.Conv1d(self.feat_dim, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4, stride=2),
nn.ReLU(inplace=True),
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=3, padding=1, groups=4, stride=2),
nn.ReLU(inplace=True)
)
self.reg_1d_o = nn.Sequential(
nn.Conv1d(self.hidden_dim_1d, self.hidden_dim_1d, kernel_size=self.temporal_len // 4, padding=0, groups=4),
nn.ReLU(inplace=True),
nn.Conv1d(self.hidden_dim_1d, 3, kernel_size=1),
)
def forward(self, x):
rbf = self.reg_1d_b(x)
regression = self.reg_1d_o(rbf)
return regression
class TemporalBoundaryRegressor(nn.Module):
def __init__(self, opt):
super(TemporalBoundaryRegressor, self).__init__()
start_sample_num = opt['start_sample_num']
end_sample_num = opt['end_sample_num']
action_sample_num = opt['action_sample_num']
prop_boundary_ratio = opt['prop_boundary_ratio']
temporal_interval = opt['temporal_interval']
self.hidden_dim_1d = 512
self.reg1se = StartEndRegression(start_sample_num, end_sample_num, self.hidden_dim_1d)
self.reg1xw = CenterWidthRegression(start_sample_num, end_sample_num, action_sample_num, self.hidden_dim_1d)
self.ttn = TemporalTransformNetwork(prop_boundary_ratio,
action_sample_num,
start_sample_num,
end_sample_num,
temporal_interval, norm_mode='padding')
def forward(self, proposals, features, video_sec, gt_boxes, iou_thres, training):
proposals1 = proposals[:, 0:2]
starts_feature1, actions_feature1, ends_feature1 = self.ttn(proposals1, features, video_sec)
reg1se = self.reg1se(starts_feature1, ends_feature1)
features1xw = torch.cat([starts_feature1, actions_feature1, ends_feature1], dim=2)
reg1xw = self.reg1xw(features1xw).squeeze(2)
preds_iou1 = reg1xw[:, 2].sigmoid()
reg1xw = reg1xw[:, :2]
if training:
proposals2xw = bbox_xw_transform_inv(proposals1, reg1xw, 0.1, 0.2)
proposals2se = bbox_se_transform_inv(proposals1, reg1se, 1.0)
iou1 = batch_iou(proposals1, gt_boxes)
targets1se = bbox_se_transform_batch(proposals1, gt_boxes)
targets1xw = bbox_xw_transform_batch(proposals1, gt_boxes)
rloss1se = self.regression_loss(reg1se, targets1se, iou1, iou_thres)
rloss1xw = self.regression_loss(reg1xw, targets1xw, iou1, iou_thres)
rloss1 = rloss1se + rloss1xw
iloss1 = self.iou_loss(preds_iou1, iou1, iou_thres=iou_thres)
else:
proposals2xw = bbox_xw_transform_inv(proposals1, reg1xw, 0.1, 0.2)
proposals2se = bbox_se_transform_inv(proposals1, reg1se, 0.2)
rloss1 = 0
iloss1 = 0
proposals2 = (proposals2se + proposals2xw) / 2.0
proposals2 = torch.clamp(proposals2, min=0.)
return preds_iou1, proposals2, rloss1, iloss1
def regression_loss(self, regression, targets, iou_with_gt, iou_thres):
weight = (iou_with_gt >= iou_thres).float().unsqueeze(1)
reg_loss = F.smooth_l1_loss(regression, targets, reduction='none')
if torch.sum(weight) > 0:
reg_loss = torch.sum(weight * reg_loss) / torch.sum(weight)
else:
reg_loss = torch.sum(weight * reg_loss)
return reg_loss
def iou_loss(self, preds_iou, match_iou, iou_thres):
preds_iou = preds_iou.view(-1)
u_hmask = (match_iou > iou_thres).float()
u_mmask = ((match_iou <= iou_thres) & (match_iou > 0.3)).float()
u_lmask = (match_iou <= 0.3).float()
num_h = torch.sum(u_hmask)
num_m = torch.sum(u_mmask)
num_l = torch.sum(u_lmask)
r_m = num_h / (num_m)
r_m = torch.min(r_m, torch.Tensor([1.0]).cuda())[0]
u_smmask = torch.Tensor(np.random.rand(u_hmask.size()[0])).cuda()
u_smmask = u_smmask * u_mmask
u_smmask = (u_smmask > (1. - r_m)).float()
r_l = num_h / (num_l)
r_l = torch.min(r_l, torch.Tensor([1.0]).cuda())[0]
u_slmask = torch.Tensor(np.random.rand(u_hmask.size()[0])).cuda()
u_slmask = u_slmask * u_lmask
u_slmask = (u_slmask > (1. - r_l)).float()
iou_weights = u_hmask + u_smmask + u_slmask
iou_loss = F.smooth_l1_loss(preds_iou, match_iou, reduction='none')
if torch.sum(iou_weights) > 0:
iou_loss = torch.sum(iou_loss * iou_weights) / torch.sum(iou_weights)
else:
iou_loss = torch.sum(iou_loss * iou_weights)
return iou_loss