-
Notifications
You must be signed in to change notification settings - Fork 21
/
codecTest.py
147 lines (121 loc) · 5.29 KB
/
codecTest.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
# Reference (https://github.com/kan-bayashi/ParallelWaveGAN/)
import os
import torch
import logging
import argparse
import soundfile as sf
from dataloader import SingleDataset
from models.autoencoder.AudioDec import Generator as generator_audiodec
from models.vocoder.HiFiGAN import Generator as generator_hifigan
from bin.test import TestGEN
class TestMain(TestGEN):
def __init__(self, args,):
super(TestMain, self).__init__(args=args,)
self.encoder_type = self.encoder_config.get('model_type', 'symAudioDec')
self.decoder_type = self.decoder_config.get('model_type', 'symAudioDec')
if self.encoder_config['generator_params']['input_channels'] > 1:
self.multi_channel = True
else:
self.multi_channel = False
# LOAD DATASET
def load_dataset(self, subset, subset_num):
data_path = os.path.join(
self.encoder_config['data']['path'],
self.encoder_config['data']['subset'][subset]
)
assert os.path.exists(data_path), f"{data_path} does not exist!"
self.dataset = SingleDataset(
files=data_path,
query="*.wav",
load_fn=sf.read,
return_utt_id=True,
subset_num=subset_num,
)
logging.info(f"The number of utterances = {len(self.dataset)}.")
# LOAD MODEL
def load_encoder(self):
if self.encoder_type in ['symAudioDec', 'symAudioDecUniv']:
encoder = generator_audiodec
else:
raise NotImplementedError(f"Encoder {self.encoder_type} is not supported!")
self.encoder = encoder(**self.encoder_config['generator_params'])
self.encoder.load_state_dict(
torch.load(self.encoder_checkpoint, map_location='cpu')['model']['generator'])
self.encoder = self.encoder.eval().to(self.device)
logging.info(f"Loaded Encoder from {self.encoder_checkpoint}.")
def load_decoder(self):
if self.decoder_type in ['symAudioDec', 'symAudioDecUniv']:
decoder = generator_audiodec
elif self.decoder_type in ['HiFiGAN', 'UnivNet']:
decoder = generator_hifigan
else:
raise NotImplementedError(f"Decoder {self.decoder_type} is not supported!")
self.decoder = decoder(**self.decoder_config['generator_params'])
self.decoder.load_state_dict(
torch.load(self.decoder_checkpoint, map_location='cpu')['model']['generator'])
self.decoder = self.decoder.eval().to(self.device)
logging.info(f"Loaded Decoder from {self.decoder_checkpoint}.")
def encode(self, audio):
x = torch.tensor(audio, dtype=torch.float).to(self.device)
if self.multi_channel:
x = x.transpose(1, 0).unsqueeze(0) # (T, C) -> (1, C, T)
else:
x = x.transpose(1, 0).unsqueeze(1) # (T, C) -> (C, 1, T)
x = self.encoder.encoder(x)
z = self.encoder.projector(x)
zq, _, _ = self.encoder.quantizer(z)
return zq
def decode(self, zq):
if self.decoder_type in ['HiFiGAN', 'UnivNet']:
y = self.decoder(zq)
else:
y = self.decoder.decoder(zq)
return y
# INITIAL FOLDER
def initial_folder(self, subset, output_name, specific_folder="False"):
if specific_folder == "True":
self.outdir = output_name
else:
# model name
encoder = os.path.dirname(self.encoder_checkpoint).split('/')[-1]
decoder = os.path.dirname(self.decoder_checkpoint).split('/')[-1]
# model checkpoint
encoder_checkpoint = os.path.basename(self.encoder_checkpoint).split('steps')[0].split('-')[-1]
decoder_checkpoint = os.path.basename(self.decoder_checkpoint).split('steps')[0].split('-')[-1]
testdir = f"{encoder}-{decoder}_{encoder_checkpoint}-{decoder_checkpoint}"
# testing set
setdir = self.encoder_config['data']['subset'][subset]
self.outdir = os.path.join(output_name, testdir, setdir)
if not os.path.exists(self.outdir):
os.makedirs(self.outdir, exist_ok=True)
def main():
"""Run testing process."""
parser = argparse.ArgumentParser()
parser.add_argument("--subset", type=str, default="clean_test")
parser.add_argument("--subset_num", type=int, default=-1)
parser.add_argument("--encoder", type=str, required=True)
parser.add_argument("--decoder", type=str, required=True)
parser.add_argument("--output_dir", type=str, required=True)
parser.add_argument('--specific_folder', choices=('True','False'), default='False')
args = parser.parse_args()
# initial test_main
test_main = TestMain(args=args)
# load dataset
test_main.load_dataset(args.subset, args.subset_num)
# load model
test_main.load_encoder()
test_main.load_decoder()
# initial folder
test_main.initial_folder(args.subset, args.output_dir, args.specific_folder)
# run testing
test_main.run()
if __name__ == "__main__":
main()