-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathdemo.py
135 lines (99 loc) · 5.26 KB
/
demo.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
"""
Author: Smeet Shah
Copyright (c) 2020 Smeet Shah
File part of 'deep_avsr' GitHub repository available at -
https://github.com/smeetrs/deep_avsr
Specifications:
--------------
Videofile - .mp4 file
Properties - Video:
25 fps, 160x160 RGB frames, Mouth approx. in center,
face size should be comparable to frame size
Audio:
Mono audio, 16000 Hz sample rate
Note -
Video length should be such that the expected transcription length is less than 100 characters.
For this, a long video can be appropriately segmented into clips of appropriate length
depending on the speaking rate of the speaker. For a speaker with around 160 words per min,
and considering 6 characters per word (including space) on average, clip lengths should be
around 6 secs.
The predicted transcriptions will have all characters in capital with no punctuations
other than an apostrophe (').
"""
import torch
import numpy as np
import os
from config import args
from models.audio_net import AudioNet
from models.lrs2_char_lm import LRS2CharLM
from data.utils import prepare_main_input, collate_fn
from utils.preprocessing import preprocess_sample
from utils.decoders import ctc_greedy_decode, ctc_search_decode
def main():
np.random.seed(args["SEED"])
torch.manual_seed(args["SEED"])
gpuAvailable = torch.cuda.is_available()
device = torch.device("cuda" if gpuAvailable else "cpu")
if args["TRAINED_MODEL_FILE"] is not None:
print("\nTrained Model File: %s" %(args["TRAINED_MODEL_FILE"]))
print("\nDemo Directory: %s" %(args["DEMO_DIRECTORY"]))
#declaring the model and loading the trained weights
model = AudioNet(args["TX_NUM_FEATURES"], args["TX_ATTENTION_HEADS"], args["TX_NUM_LAYERS"], args["PE_MAX_LENGTH"],
args["AUDIO_FEATURE_SIZE"], args["TX_FEEDFORWARD_DIM"], args["TX_DROPOUT"], args["NUM_CLASSES"])
model.load_state_dict(torch.load(args["CODE_DIRECTORY"] + args["TRAINED_MODEL_FILE"], map_location=device))
model.to(device)
#declaring the language model and loading the trained weights
lm = LRS2CharLM()
lm.load_state_dict(torch.load(args["TRAINED_LM_FILE"], map_location=device))
lm.to(device)
if not args["USE_LM"]:
lm = None
#reading the noise file
if args["TEST_DEMO_NOISY"]:
_, noise = wavfile.read(args["DATA_DIRECTORY"] + "/noise.wav")
else:
noise = None
print("\n\nRunning Demo .... \n")
#walking through the demo directory and running the model on all video files in it
for root, dirs, files in os.walk(args["DEMO_DIRECTORY"]):
for file in files:
if file.endswith(".mp4"):
sampleFile = os.path.join(root, file[:-4])
#preprocessing the sample
preprocess_sample(sampleFile)
#converting the data sample into appropriate tensors for input to the model
audioFile = os.path.join(root, file[:-4]) + ".wav"
audioParams = {"stftWindow":args["STFT_WINDOW"], "stftWinLen":args["STFT_WIN_LENGTH"], "stftOverlap":args["STFT_OVERLAP"]}
inp, _, inpLen, _ = prepare_main_input(audioFile, None, noise, args["MAIN_REQ_INPUT_LENGTH"], args["CHAR_TO_INDEX"],
args["NOISE_SNR_DB"], audioParams)
inputBatch, _, inputLenBatch, _ = collate_fn([(inp, None, inpLen, None)])
#running the model
inputBatch = (inputBatch.float()).to(device)
inputLenBatch = (inputLenBatch.int()).to(device)
model.eval()
with torch.no_grad():
outputBatch = model(inputBatch)
#obtaining the prediction using CTC deocder
if args["TEST_DEMO_DECODING"] == "greedy":
predictionBatch, predictionLenBatch = ctc_greedy_decode(outputBatch, inputLenBatch, args["CHAR_TO_INDEX"]["<EOS>"])
elif args["TEST_DEMO_DECODING"] == "search":
beamSearchParams = {"beamWidth":args["BEAM_WIDTH"], "alpha":args["LM_WEIGHT_ALPHA"], "beta":args["LENGTH_PENALTY_BETA"],
"threshProb":args["THRESH_PROBABILITY"]}
predictionBatch, predictionLenBatch = ctc_search_decode(outputBatch, inputLenBatch, beamSearchParams,
args["CHAR_TO_INDEX"][" "], args["CHAR_TO_INDEX"]["<EOS>"], lm)
else:
print("Invalid Decode Scheme")
exit()
#converting character indices back to characters
pred = predictionBatch[:][:-1]
pred = "".join([args["INDEX_TO_CHAR"][ix] for ix in pred.tolist()])
#printing the predictions
print("File: %s" %(file))
print("Prediction: %s" %(pred))
print("\n")
print("Demo Completed.\n")
else:
print("\nPath to trained model file not specified.\n")
return
if __name__ == "__main__":
main()