-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
48 lines (42 loc) · 1.2 KB
/
util.py
File metadata and controls
48 lines (42 loc) · 1.2 KB
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
import torch
import torch.nn as nn
def weights_init(m):
classname = m.__class__.__name__
if type(m) in [nn.Linear, nn.Conv2d, nn.Conv1d]:
torch.nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
m.bias.data.fill_(0.01)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def remove_duplicates(x):
if len(x) < 2:
return x
fin = ""
for j in x:
if fin == "":
fin = j
else:
if j == fin[-1]:
continue
else:
fin = fin + j
return fin
def decode_predictions(preds, encoder):
preds = preds.permute(1, 0, 2)
preds = torch.softmax(preds, 2)
preds = torch.argmax(preds, 2)
preds = preds.detach().cpu().numpy()
cap_preds = []
for j in range(preds.shape[0]):
temp = []
for k in preds[j, :]:
k = k - 1
if k == -1:
temp.append("§")
else:
p = encoder.inverse_transform([k])[0]
temp.append(p)
tp = "".join(temp).replace("§", "")
cap_preds.append(remove_duplicates(tp))
return cap_preds