-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslr_network.py
147 lines (131 loc) · 6.15 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
import modules.resnet as resnet
import pickle
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(resnet, c2d_type)()
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
# with open('preprocess/phoenix2014-T/phoenix2014-T.pkl', 'rb') as f:
# self.index_dict = pickle.load(f)
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, signer=None):
if len(x.shape) == 5:
# videos
batch, temp, channel, height, width = x.shape
framewise = self.conv2d(x.permute(0,2,1,3,4)).view(batch, temp, -1).permute(0,2,1) # btc -> bct 4,512,180
else:
framewise = x
conv1d_outputs = self.conv1d(framewise, len_x)
# x: T, B, C
x = conv1d_outputs['visual_feat']
lgt = conv1d_outputs['feat_len'].cpu()
tm_outputs = self.temporal_model(x, lgt)
outputs = self.classifier(tm_outputs['predictions'])
# for i in range(batch):
# indices = self.index_dict[signer[i]]
# mask = torch.zeros(outputs.shape[-1], dtype=torch.bool)
# mask[indices] = True
# outputs[:, i, ~mask] = -float('inf')
# conv1d_outputs['conv_logits'][:, i, ~mask] = -float('inf')
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 {
"feat_len": lgt,
"conv_logits": conv1d_outputs['conv_logits'],
"sequence_logits": outputs,
"conv_sents": conv_pred,
"recognized_sents": pred,
"loss_LiftPool_u": conv1d_outputs['loss_LiftPool_u'],
"loss_LiftPool_p": conv1d_outputs['loss_LiftPool_p'],
}
def criterion_calculation(self, ret_dict, label, label_lgt):
loss = 0
total_loss = {}
for k, weight in self.loss_weights.items():
if k == 'ConvCTC':
total_loss['ConvCTC'] = 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()
loss += total_loss['ConvCTC']
elif k == 'SeqCTC':
total_loss['SeqCTC'] = 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()
loss += total_loss['SeqCTC']
elif k == 'Dist':
total_loss['Dist'] = weight * self.loss['distillation'](ret_dict["conv_logits"],
ret_dict["sequence_logits"].detach(),
use_blank=False)
loss += total_loss['Dist']
elif k == 'Cu':
total_loss['Cu'] = weight * ret_dict["loss_LiftPool_u"]
loss += total_loss['Cu']
elif k == 'Cp':
total_loss['Cp'] = weight * ret_dict["loss_LiftPool_p"]
loss += total_loss['Cp']
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