sonusai 0.15.9__py3-none-any.whl → 0.16.1__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.
Files changed (49) hide show
  1. sonusai/__init__.py +36 -4
  2. sonusai/audiofe.py +111 -106
  3. sonusai/calc_metric_spenh.py +38 -22
  4. sonusai/genft.py +15 -6
  5. sonusai/genmix.py +14 -6
  6. sonusai/genmixdb.py +15 -7
  7. sonusai/gentcst.py +13 -6
  8. sonusai/lsdb.py +15 -5
  9. sonusai/main.py +58 -61
  10. sonusai/mixture/__init__.py +1 -0
  11. sonusai/mixture/config.py +1 -2
  12. sonusai/mkmanifest.py +43 -8
  13. sonusai/mkwav.py +15 -6
  14. sonusai/onnx_predict.py +16 -6
  15. sonusai/plot.py +16 -6
  16. sonusai/post_spenh_targetf.py +13 -6
  17. sonusai/summarize_metric_spenh.py +71 -0
  18. sonusai/tplot.py +14 -6
  19. sonusai/utils/__init__.py +4 -7
  20. sonusai/utils/asl_p56.py +3 -3
  21. sonusai/utils/asr.py +35 -8
  22. sonusai/utils/asr_functions/__init__.py +0 -5
  23. sonusai/utils/asr_functions/aaware_whisper.py +2 -2
  24. sonusai/utils/asr_manifest_functions/__init__.py +1 -0
  25. sonusai/utils/asr_manifest_functions/mcgill_speech.py +29 -0
  26. sonusai/utils/{trim_docstring.py → docstring.py} +20 -0
  27. sonusai/utils/model_utils.py +30 -0
  28. sonusai/utils/onnx_utils.py +19 -45
  29. {sonusai-0.15.9.dist-info → sonusai-0.16.1.dist-info}/METADATA +7 -25
  30. {sonusai-0.15.9.dist-info → sonusai-0.16.1.dist-info}/RECORD +32 -46
  31. sonusai/data_generator/__init__.py +0 -5
  32. sonusai/data_generator/dataset_from_mixdb.py +0 -143
  33. sonusai/data_generator/keras_from_mixdb.py +0 -169
  34. sonusai/data_generator/torch_from_mixdb.py +0 -122
  35. sonusai/keras_onnx.py +0 -86
  36. sonusai/keras_predict.py +0 -231
  37. sonusai/keras_train.py +0 -334
  38. sonusai/torchl_onnx.py +0 -216
  39. sonusai/torchl_predict.py +0 -542
  40. sonusai/torchl_train.py +0 -223
  41. sonusai/utils/asr_functions/aixplain_whisper.py +0 -59
  42. sonusai/utils/asr_functions/data.py +0 -16
  43. sonusai/utils/asr_functions/deepgram.py +0 -97
  44. sonusai/utils/asr_functions/fastwhisper.py +0 -90
  45. sonusai/utils/asr_functions/google.py +0 -95
  46. sonusai/utils/asr_functions/whisper.py +0 -49
  47. sonusai/utils/keras_utils.py +0 -226
  48. {sonusai-0.15.9.dist-info → sonusai-0.16.1.dist-info}/WHEEL +0 -0
  49. {sonusai-0.15.9.dist-info → sonusai-0.16.1.dist-info}/entry_points.txt +0 -0
@@ -1,226 +0,0 @@
1
- from typing import Any
2
-
3
- from sonusai import logger
4
-
5
-
6
- def keras_onnx(model_name: str,
7
- weight_name: str,
8
- timesteps: int = None,
9
- batch_size: int = None,
10
- output_dir: str = None) -> None:
11
- from os.path import basename
12
- from os.path import dirname
13
- from os.path import join
14
- from os.path import splitext
15
-
16
- import h5py
17
- import keras_tuner as kt
18
-
19
- if output_dir is None:
20
- output_dir = dirname(model_name)
21
-
22
- with h5py.File(weight_name, 'r') as f:
23
- feature = f.attrs['sonusai_feature']
24
- num_classes = int(f.attrs['sonusai_num_classes'])
25
-
26
- model = import_keras_model(model_name)
27
- model_base = basename(model_name)
28
- model_root, _ = splitext(model_base)
29
- base_name = join(output_dir, model_root)
30
-
31
- # Build default model to check overrides
32
- timesteps = check_keras_overrides(model, feature, num_classes, timesteps, batch_size)
33
-
34
- try:
35
- hypermodel = model.MyHyperModel(feature=feature,
36
- num_classes=num_classes,
37
- timesteps=timesteps,
38
- batch_size=batch_size)
39
- built_model = hypermodel.build_model(kt.HyperParameters())
40
- except Exception as e:
41
- logger.exception(f'Error: build_model() in {model_base} failed: {e}.')
42
- raise SystemExit(1)
43
-
44
- print('')
45
- print(f'timesteps {timesteps}')
46
- print(f'batch_size {batch_size}')
47
- print(f'hypermodel.timesteps {hypermodel.timesteps}')
48
- print(f'hypermodel.batch_size {hypermodel.batch_size}')
49
- print('')
50
-
51
- # Create and save ONNX model with specified batch and timestep sizes
52
- onnx_name = base_name + '.onnx'
53
- try:
54
- create_onnx_from_keras(keras_model=built_model,
55
- is_flattened=hypermodel.flatten,
56
- has_timestep=(hypermodel.timesteps > 0),
57
- has_channel=hypermodel.add1ch,
58
- is_mutex=hypermodel.truth_mutex,
59
- feature=feature,
60
- filename=onnx_name)
61
- except Exception as e:
62
- logger.warning(f'Failed to create ONNX model, no file saved: {e}.')
63
- logger.info(f'Wrote ONNX model to {onnx_name}')
64
-
65
- # Create and save onnx model with timesteps, batch = 1
66
- timesteps = hypermodel.timesteps
67
- if timesteps > 0:
68
- # only set to 1 if nonzero (exists)
69
- timesteps = 1
70
- hypermodel = model.MyHyperModel(feature=feature,
71
- num_classes=num_classes,
72
- timesteps=timesteps,
73
- batch_size=1)
74
- built_model = hypermodel.build_model(kt.HyperParameters())
75
- # load weights from previously saved HDF5
76
- built_model.load_weights(weight_name)
77
- # save a prediction version of model to base_name-pred-onnx
78
- onnx_name = base_name + '-b1.onnx'
79
- create_onnx_from_keras(keras_model=built_model,
80
- is_flattened=hypermodel.flatten,
81
- has_timestep=(hypermodel.timesteps > 0),
82
- has_channel=hypermodel.add1ch,
83
- is_mutex=hypermodel.truth_mutex,
84
- feature=feature,
85
- filename=onnx_name)
86
- logger.info(f'Wrote inference ONNX model to {onnx_name}')
87
-
88
-
89
- def check_keras_overrides(model: Any,
90
- feature: str,
91
- num_classes: int,
92
- timesteps: int = None,
93
- batch_size: int = None) -> int:
94
- try:
95
- hypermodel = model.MyHyperModel()
96
- except Exception as e:
97
- logger.exception(f'Error: build_model() failed: {e}.')
98
- raise SystemExit(1)
99
-
100
- if hypermodel.feature != feature:
101
- logger.warning(f'Overriding feature: default = {hypermodel.feature}; specified = {feature}.')
102
-
103
- if hypermodel.num_classes != num_classes:
104
- logger.warning(
105
- f'Overriding num_classes: default = {hypermodel.num_classes}; specified = {num_classes}.')
106
-
107
- if timesteps is not None:
108
- if hypermodel.timesteps == 0 and timesteps != 0:
109
- logger.warning(f'Model does not contain timesteps; ignoring override.')
110
- timesteps = 0
111
-
112
- if hypermodel.timesteps != 0 and timesteps == 0:
113
- logger.warning(f'Model contains timesteps; ignoring override.')
114
- timesteps = hypermodel.timesteps
115
-
116
- if hypermodel.timesteps != timesteps:
117
- logger.info(f'Overriding timesteps: default = {hypermodel.timesteps}; specified = {timesteps}.')
118
-
119
- if batch_size is not None and hypermodel.batch_size != batch_size:
120
- logger.info(f'Overriding batch_size: default = {hypermodel.batch_size}; specified = {batch_size}.')
121
-
122
- return timesteps
123
-
124
-
125
- def import_keras_model(name: str) -> Any:
126
- import os
127
- import sys
128
- from importlib import import_module
129
-
130
- from sonusai import SonusAIError
131
-
132
- try:
133
- path = os.path.dirname(name)
134
- if len(path) < 1:
135
- path = './'
136
-
137
- # Add model file location to system path
138
- sys.path.append(os.path.abspath(path))
139
-
140
- try:
141
- # expect at least a Keras-tuner build_model(hp) function
142
- root = os.path.splitext(os.path.basename(name))[0]
143
- model = import_module(root)
144
- except Exception as e:
145
- raise SonusAIError(f'Error: could not import model from {name}: {e}.')
146
- except Exception as e:
147
- raise SonusAIError(f'Error: could not find {name}: {e}.')
148
-
149
- return model
150
-
151
-
152
- def create_onnx_from_keras(keras_model,
153
- is_flattened: bool = True,
154
- has_timestep: bool = True,
155
- has_channel: bool = False,
156
- is_mutex: bool = True,
157
- feature: str = '',
158
- filename: str = ''):
159
- import tf2onnx
160
- from keras import backend as kb
161
-
162
- from sonusai.utils import add_sonusai_metadata
163
- from sonusai.utils import replace_stateful_grus
164
-
165
- kb.clear_session()
166
- onnx_model, _ = tf2onnx.convert.from_keras(keras_model)
167
- onnx_model = replace_stateful_grus(keras_model=keras_model, onnx_model=onnx_model)
168
- onnx_model = add_sonusai_metadata(model=onnx_model,
169
- is_flattened=is_flattened,
170
- has_timestep=has_timestep,
171
- has_channel=has_channel,
172
- is_mutex=is_mutex,
173
- feature=feature)
174
- if filename:
175
- import onnx
176
- onnx.save(onnx_model, filename)
177
- return onnx_model
178
-
179
-
180
- def import_and_check_keras_model(model_name: str,
181
- weights_name: str,
182
- timesteps: int = None,
183
- batch_size: int = None) -> Any:
184
- from os.path import basename
185
-
186
- import h5py
187
-
188
- from sonusai import logger
189
-
190
- model_base = basename(model_name)
191
-
192
- # Import model definition file
193
- logger.info(f'Importing {model_base}')
194
- model = import_keras_model(model_name)
195
- try:
196
- hypermodel = model.MyHyperModel(timesteps=timesteps, batch_size=batch_size)
197
- except Exception as e:
198
- logger.exception(f'build_model() in {model_base} failed: {e}.')
199
- raise SystemExit(1)
200
-
201
- with h5py.File(weights_name, 'r') as f:
202
- if 'sonusai_feature' in f.attrs:
203
- feature_mode = f.attrs['sonusai_feature']
204
- else:
205
- feature_mode = hypermodel.feature
206
- logger.warn(f'Could not find SonusAI feature in weights file; using hypermodel default.')
207
- if 'sonusai_num_classes' in f.attrs:
208
- num_classes = int(f.attrs['sonusai_num_classes'])
209
- else:
210
- num_classes = hypermodel.num_classes
211
- logger.warn(f'Could not find SonusAI num_classes in weights file; using hypermodel default.')
212
-
213
- # Check overrides
214
- timesteps = check_keras_overrides(model, feature_mode, num_classes, timesteps, batch_size)
215
-
216
- logger.info('Building model')
217
- try:
218
- hypermodel = model.MyHyperModel(feature=feature_mode,
219
- num_classes=num_classes,
220
- timesteps=timesteps,
221
- batch_size=batch_size)
222
- except Exception as e:
223
- logger.exception(f'build_model() in {model_base} failed: {e}.')
224
- raise SystemExit(1)
225
-
226
- return hypermodel