-
Notifications
You must be signed in to change notification settings - Fork 20
/
slr_network.py
131 lines (115 loc) · 5.44 KB
/
slr_network.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
import pdb
import copy
import utils
import torch
import types
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
from modules.criterions import SeqKD
from modules import BiLSTMLayer, TemporalConv
class Identity(nn.Module):
def __init__(self):
super(Identity, self).__init__()
def forward(self, x):
return x
class NormLinear(nn.Module):
def __init__(self, in_dim, out_dim):
super(NormLinear, self).__init__()
self.weight = nn.Parameter(torch.Tensor(in_dim, out_dim))
nn.init.xavier_uniform_(self.weight, gain=nn.init.calculate_gain('relu'))
def forward(self, x):
outputs = torch.matmul(x, F.normalize(self.weight, dim=0))
return outputs
class SLRModel(nn.Module):
def __init__(
self, num_classes, c2d_type, conv_type, use_bn=False,
hidden_size=1024, gloss_dict=None, loss_weights=None,
weight_norm=True, share_classifier=True
):
super(SLRModel, self).__init__()
self.decoder = None
self.loss = dict()
self.criterion_init()
self.num_classes = num_classes
self.loss_weights = loss_weights
self.conv2d = getattr(models, c2d_type)(pretrained=True)
self.conv2d.fc = Identity()
self.conv1d = TemporalConv(input_size=512,
hidden_size=hidden_size,
conv_type=conv_type,
use_bn=use_bn,
num_classes=num_classes)
self.decoder = utils.Decode(gloss_dict, num_classes, 'beam')
self.temporal_model = BiLSTMLayer(rnn_type='LSTM', input_size=hidden_size, hidden_size=hidden_size,
num_layers=2, bidirectional=True)
if weight_norm:
self.classifier = NormLinear(hidden_size, self.num_classes)
self.conv1d.fc = NormLinear(hidden_size, self.num_classes)
else:
self.classifier = nn.Linear(hidden_size, self.num_classes)
self.conv1d.fc = nn.Linear(hidden_size, self.num_classes)
if share_classifier:
self.conv1d.fc = self.classifier
self.register_backward_hook(self.backward_hook)
def backward_hook(self, module, grad_input, grad_output):
for g in grad_input:
g[g != g] = 0
def masked_bn(self, inputs, len_x):
def pad(tensor, length):
return torch.cat([tensor, tensor.new(length - tensor.size(0), *tensor.size()[1:]).zero_()])
x = torch.cat([inputs[len_x[0] * idx:len_x[0] * idx + lgt] for idx, lgt in enumerate(len_x)])
x = self.conv2d(x)
x = torch.cat([pad(x[sum(len_x[:idx]):sum(len_x[:idx + 1])], len_x[0])
for idx, lgt in enumerate(len_x)])
return x
def forward(self, x, len_x, label=None, label_lgt=None):
if len(x.shape) == 5:
# videos
batch, temp, channel, height, width = x.shape
inputs = x.reshape(batch * temp, channel, height, width)
framewise = self.masked_bn(inputs, len_x)
framewise = framewise.reshape(batch, temp, -1).transpose(1, 2)
else:
# frame-wise features
framewise = x
conv1d_outputs = self.conv1d(framewise, len_x)
# x: T, B, C
x = conv1d_outputs['visual_feat']
lgt = conv1d_outputs['feat_len']
tm_outputs = self.temporal_model(x, lgt)
outputs = self.classifier(tm_outputs['predictions'])
pred = None if self.training \
else self.decoder.decode(outputs, lgt, batch_first=False, probs=False)
conv_pred = None if self.training \
else self.decoder.decode(conv1d_outputs['conv_logits'], lgt, batch_first=False, probs=False)
return {
"framewise_features": framewise,
"visual_features": x,
"feat_len": lgt,
"conv_logits": conv1d_outputs['conv_logits'],
"sequence_logits": outputs,
"conv_sents": conv_pred,
"recognized_sents": pred,
}
def criterion_calculation(self, ret_dict, label, label_lgt):
loss = 0
for k, weight in self.loss_weights.items():
if k == 'ConvCTC':
loss += weight * self.loss['CTCLoss'](ret_dict["conv_logits"].log_softmax(-1),
label.cpu().int(), ret_dict["feat_len"].cpu().int(),
label_lgt.cpu().int()).mean()
elif k == 'SeqCTC':
loss += weight * self.loss['CTCLoss'](ret_dict["sequence_logits"].log_softmax(-1),
label.cpu().int(), ret_dict["feat_len"].cpu().int(),
label_lgt.cpu().int()).mean()
elif k == 'Dist':
loss += weight * self.loss['distillation'](ret_dict["conv_logits"],
ret_dict["sequence_logits"].detach(),
use_blank=False)
return loss
def criterion_init(self):
self.loss['CTCLoss'] = torch.nn.CTCLoss(reduction='none', zero_infinity=False)
self.loss['distillation'] = SeqKD(T=8)
return self.loss