sonusai 0.17.0__py3-none-any.whl → 0.17.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sonusai/audiofe.py +22 -51
- sonusai/calc_metric_spenh.py +206 -213
- sonusai/doc/doc.py +1 -1
- sonusai/mixture/__init__.py +2 -0
- sonusai/mixture/audio.py +12 -0
- sonusai/mixture/datatypes.py +11 -3
- sonusai/mixture/mixdb.py +101 -0
- sonusai/mixture/soundfile_audio.py +39 -0
- sonusai/mixture/speaker_metadata.py +35 -0
- sonusai/mixture/torchaudio_audio.py +22 -0
- sonusai/mkmanifest.py +1 -1
- sonusai/onnx_predict.py +114 -410
- sonusai/queries/queries.py +1 -1
- sonusai/speech/__init__.py +3 -0
- sonusai/speech/l2arctic.py +116 -0
- sonusai/speech/librispeech.py +99 -0
- sonusai/speech/mcgill.py +70 -0
- sonusai/speech/textgrid.py +100 -0
- sonusai/speech/timit.py +135 -0
- sonusai/speech/types.py +12 -0
- sonusai/speech/vctk.py +52 -0
- sonusai/speech/voxceleb2.py +86 -0
- sonusai/utils/__init__.py +2 -1
- sonusai/utils/asr_manifest_functions/__init__.py +0 -1
- sonusai/utils/asr_manifest_functions/data.py +0 -8
- sonusai/utils/asr_manifest_functions/librispeech.py +1 -1
- sonusai/utils/asr_manifest_functions/mcgill_speech.py +1 -1
- sonusai/utils/asr_manifest_functions/vctk_noisy_speech.py +1 -1
- sonusai/utils/braced_glob.py +7 -3
- sonusai/utils/onnx_utils.py +110 -106
- sonusai/utils/path_info.py +7 -0
- {sonusai-0.17.0.dist-info → sonusai-0.17.2.dist-info}/METADATA +2 -1
- {sonusai-0.17.0.dist-info → sonusai-0.17.2.dist-info}/RECORD +35 -30
- {sonusai-0.17.0.dist-info → sonusai-0.17.2.dist-info}/WHEEL +1 -1
- sonusai/calc_metric_spenh-save.py +0 -1334
- sonusai/onnx_predict-old.py +0 -240
- sonusai/onnx_predict-save.py +0 -487
- sonusai/ovino_predict.py +0 -508
- sonusai/ovino_query_devices.py +0 -47
- sonusai/torchl_onnx-old.py +0 -216
- {sonusai-0.17.0.dist-info → sonusai-0.17.2.dist-info}/entry_points.txt +0 -0
sonusai/torchl_onnx-old.py
DELETED
@@ -1,216 +0,0 @@
|
|
1
|
-
"""sonusai torchl_onnx
|
2
|
-
|
3
|
-
usage: torchl_onnx [-hv] [-b BATCH] [-t TSTEPS] [-o OUTPUT] MODEL CKPT
|
4
|
-
|
5
|
-
options:
|
6
|
-
-h, --help
|
7
|
-
-v, --verbose Be verbose
|
8
|
-
-b BATCH, --batch BATCH Batch size [default: 1]
|
9
|
-
-t TSTEPS, --tsteps TSTEPS Timesteps [default: 1]
|
10
|
-
-o OUTPUT, --output OUTPUT Output directory.
|
11
|
-
|
12
|
-
Convert a trained Pytorch Lightning model to ONNX. The model is specified as an
|
13
|
-
sctl_*.py model file (sctl: sonusai custom torch lightning) and a checkpoint file
|
14
|
-
for loading weights.
|
15
|
-
|
16
|
-
Inputs:
|
17
|
-
MODEL SonusAI Python custom model file.
|
18
|
-
CKPT A Pytorch Lightning checkpoint file
|
19
|
-
BATCH Batch size used in onnx conversion, overrides value in model ckpt. Defaults to 1.
|
20
|
-
TSTEPS Timestep dimension size using in onnx conversion, overrides value in model ckpt if
|
21
|
-
the model has a timestep dimension. Else it is ignored.
|
22
|
-
|
23
|
-
Outputs:
|
24
|
-
OUTPUT/ A directory containing:
|
25
|
-
<CKPT>.onnx Model file with batch_size and timesteps equal to provided parameters
|
26
|
-
<CKPT>-b1.onnx Model file with batch_size=1 and if the timesteps dimension exists it
|
27
|
-
is set to 1 (useful for real-time inference applications)
|
28
|
-
torchl_onnx.log
|
29
|
-
|
30
|
-
Results are written into subdirectory <MODEL>-<TIMESTAMP> unless OUTPUT is specified.
|
31
|
-
|
32
|
-
"""
|
33
|
-
from sonusai import logger
|
34
|
-
|
35
|
-
|
36
|
-
def main() -> None:
|
37
|
-
from docopt import docopt
|
38
|
-
|
39
|
-
import sonusai
|
40
|
-
from sonusai.utils import trim_docstring
|
41
|
-
|
42
|
-
args = docopt(trim_docstring(__doc__), version=sonusai.__version__, options_first=True)
|
43
|
-
|
44
|
-
verbose = args['--verbose']
|
45
|
-
batch_size = args['--batch']
|
46
|
-
timesteps = args['--tsteps']
|
47
|
-
model_path = args['MODEL']
|
48
|
-
ckpt_path = args['CKPT']
|
49
|
-
output_dir = args['--output']
|
50
|
-
|
51
|
-
from os import makedirs
|
52
|
-
from os.path import basename, splitext
|
53
|
-
from sonusai.utils import import_keras_model
|
54
|
-
|
55
|
-
# Import model definition file first to check
|
56
|
-
model_base = basename(model_path)
|
57
|
-
model_root = splitext(model_base)[0]
|
58
|
-
logger.info(f'Importing model from {model_base}')
|
59
|
-
try:
|
60
|
-
litemodule = import_keras_model(model_path) # note works for pytorch lightning as well as keras
|
61
|
-
except Exception as e:
|
62
|
-
logger.exception(f'Error: could not import model from {model_path}: {e}')
|
63
|
-
raise SystemExit(1)
|
64
|
-
|
65
|
-
# Load checkpoint first to get hparams if available
|
66
|
-
from torch import load as load
|
67
|
-
ckpt_base = basename(ckpt_path)
|
68
|
-
ckpt_root = splitext(ckpt_base)[0]
|
69
|
-
logger.info(f'Loading checkpoint from {ckpt_base}')
|
70
|
-
try:
|
71
|
-
checkpoint = load(ckpt_path, map_location=lambda storage, loc: storage)
|
72
|
-
except Exception as e:
|
73
|
-
logger.exception(f'Error: could not load checkpoint from {ckpt_path}: {e}')
|
74
|
-
raise SystemExit(1)
|
75
|
-
|
76
|
-
from os.path import join, isdir, dirname, exists
|
77
|
-
from sonusai import create_file_handler
|
78
|
-
from sonusai import initial_log_messages
|
79
|
-
from sonusai import update_console_handler
|
80
|
-
from torch import randn
|
81
|
-
from sonusai.utils import create_ts_name
|
82
|
-
|
83
|
-
from sonusai.utils import create_ts_name
|
84
|
-
from torchinfo import summary
|
85
|
-
|
86
|
-
if batch_size is not None:
|
87
|
-
batch_size = int(batch_size)
|
88
|
-
if batch_size != 1:
|
89
|
-
batch_size = 1
|
90
|
-
logger.info(f'For now prediction only supports batch_size = 1, forcing it to 1 now')
|
91
|
-
|
92
|
-
if timesteps is not None:
|
93
|
-
timesteps = int(timesteps)
|
94
|
-
|
95
|
-
if output_dir is None:
|
96
|
-
output_dir = dirname(ckpt_path)
|
97
|
-
else:
|
98
|
-
if not isdir(output_dir):
|
99
|
-
makedirs(output_dir, exist_ok=True)
|
100
|
-
|
101
|
-
ofname = join(output_dir, ckpt_root + '.onnx')
|
102
|
-
# First try, then add date
|
103
|
-
if exists(ofname):
|
104
|
-
# add hour-min-sec if necessary
|
105
|
-
from datetime import datetime
|
106
|
-
ts = datetime.now()
|
107
|
-
ofname = join(output_dir, ckpt_root + '-' + ts.strftime('%Y%m%d') + '.onnx')
|
108
|
-
ofname_root = splitext(ofname)[0]
|
109
|
-
|
110
|
-
# Setup logging file
|
111
|
-
create_file_handler(ofname_root + '-onnx.log')
|
112
|
-
update_console_handler(verbose)
|
113
|
-
initial_log_messages('torchl_onnx')
|
114
|
-
logger.info(f'Imported model from {model_base}')
|
115
|
-
logger.info(f'Loaded checkpoint from {ckpt_base}')
|
116
|
-
|
117
|
-
if 'hyper_parameters' in checkpoint:
|
118
|
-
hparams = checkpoint['hyper_parameters']
|
119
|
-
logger.info(f'Found hyper-params on checkpoint named {checkpoint["hparams_name"]} '
|
120
|
-
f'with {len(hparams)} total hparams.')
|
121
|
-
if batch_size is not None and hparams['batch_size'] != batch_size:
|
122
|
-
if batch_size != 1:
|
123
|
-
batch_size = 1
|
124
|
-
logger.info(f'For now prediction only supports batch_size = 1, forcing it to 1 now')
|
125
|
-
logger.info(f'Overriding batch_size: default = {hparams["batch_size"]}; specified = {batch_size}.')
|
126
|
-
hparams["batch_size"] = batch_size
|
127
|
-
|
128
|
-
if timesteps is not None:
|
129
|
-
if hparams['timesteps'] == 0 and timesteps != 0:
|
130
|
-
logger.warning(f'Model does not contain timesteps; ignoring override.')
|
131
|
-
timesteps = 0
|
132
|
-
|
133
|
-
if hparams['timesteps'] != 0 and timesteps == 0:
|
134
|
-
logger.warning(f'Model contains timesteps; ignoring override of 0, using model default.')
|
135
|
-
timesteps = hparams['timesteps']
|
136
|
-
|
137
|
-
if hparams['timesteps'] != timesteps:
|
138
|
-
logger.info(f'Overriding timesteps: default = {hparams["timesteps"]}; specified = {timesteps}.')
|
139
|
-
hparams['timesteps'] = timesteps
|
140
|
-
|
141
|
-
logger.info(f'Building model with hparams and batch_size={batch_size}, timesteps={timesteps}')
|
142
|
-
try:
|
143
|
-
model = litemodule.MyHyperModel(**hparams) # use hparams
|
144
|
-
# litemodule.MyHyperModel.load_from_checkpoint(ckpt_name, **hparams)
|
145
|
-
except Exception as e:
|
146
|
-
logger.exception(f'Error: model build (MyHyperModel) in {model_base} failed: {e}')
|
147
|
-
raise SystemExit(1)
|
148
|
-
else:
|
149
|
-
logger.info(f'Warning: found checkpoint with no hyper-parameters, building model with defaults')
|
150
|
-
try:
|
151
|
-
tmp = litemodule.MyHyperModel() # use default hparams
|
152
|
-
except Exception as e:
|
153
|
-
logger.exception(f'Error: model build (MyHyperModel) in {model_base} failed: {e}')
|
154
|
-
raise SystemExit(1)
|
155
|
-
|
156
|
-
if batch_size is not None:
|
157
|
-
if tmp.batch_size != batch_size:
|
158
|
-
logger.info(f'Overriding batch_size: default = {tmp.batch_size}; specified = {batch_size}.')
|
159
|
-
else:
|
160
|
-
batch_size = tmp.batch_size # inherit
|
161
|
-
|
162
|
-
if timesteps is not None:
|
163
|
-
if tmp.timesteps == 0 and timesteps != 0:
|
164
|
-
logger.warning(f'Model does not contain timesteps; ignoring override.')
|
165
|
-
timesteps = 0
|
166
|
-
|
167
|
-
if tmp.timesteps != 0 and timesteps == 0:
|
168
|
-
logger.warning(f'Model contains timesteps; ignoring override.')
|
169
|
-
timesteps = tmp.timesteps
|
170
|
-
|
171
|
-
if tmp.timesteps != timesteps:
|
172
|
-
logger.info(f'Overriding timesteps: default = {tmp.timesteps}; specified = {timesteps}.')
|
173
|
-
else:
|
174
|
-
timesteps = tmp.timesteps
|
175
|
-
|
176
|
-
logger.info(f'Building model with default hparams and batch_size= {batch_size}, timesteps={timesteps}')
|
177
|
-
model = litemodule.MyHyperModel(timesteps=timesteps, batch_size=batch_size)
|
178
|
-
|
179
|
-
logger.info('')
|
180
|
-
# logger.info(summary(model))
|
181
|
-
# from lightning.pytorch import Trainer
|
182
|
-
# from lightning.pytorch.callbacks import ModelSummary
|
183
|
-
# trainer = Trainer(callbacks=[ModelSummary(max_depth=2)])
|
184
|
-
# logger.info(trainer.summarize())
|
185
|
-
logger.info('')
|
186
|
-
logger.info(f'feature {model.hparams.feature}')
|
187
|
-
logger.info(f'num_classes {model.num_classes}')
|
188
|
-
logger.info(f'batch_size {model.hparams.batch_size}')
|
189
|
-
logger.info(f'timesteps {model.hparams.timesteps}')
|
190
|
-
logger.info(f'flatten {model.flatten}')
|
191
|
-
logger.info(f'add1ch {model.add1ch}')
|
192
|
-
logger.info(f'truth_mutex {model.truth_mutex}')
|
193
|
-
logger.info(f'input_shape {model.input_shape}')
|
194
|
-
logger.info('')
|
195
|
-
logger.info(f'Loading weights from {ckpt_base}')
|
196
|
-
# model = model.load_from_checkpoint(ckpt_path) # weights only, has problems - needs investigation
|
197
|
-
model.load_state_dict(checkpoint["state_dict"])
|
198
|
-
model.eval()
|
199
|
-
insample_shape = model.input_shape
|
200
|
-
insample_shape.insert(0, batch_size)
|
201
|
-
input_sample = randn(insample_shape)
|
202
|
-
logger.info(f'Creating onnx model ...')
|
203
|
-
for m in model.modules():
|
204
|
-
if 'instancenorm' in m.__class__.__name__.lower():
|
205
|
-
logger.info(f'Forcing train=false for instancenorm instance {m}, {m.__class__.__name__.lower()}')
|
206
|
-
m.train(False)
|
207
|
-
# m.track_running_stats=True # has problems
|
208
|
-
model.to_onnx(file_path=ofname, input_sample=input_sample, export_params=True)
|
209
|
-
|
210
|
-
|
211
|
-
if __name__ == '__main__':
|
212
|
-
try:
|
213
|
-
main()
|
214
|
-
except KeyboardInterrupt:
|
215
|
-
logger.info('Canceled due to keyboard interrupt')
|
216
|
-
exit()
|
File without changes
|