Skip to content

Commit

Permalink
🚀 reformat and update base model
Browse files Browse the repository at this point in the history
  • Loading branch information
nglehuy committed Aug 22, 2021
1 parent 130124c commit 37ea12a
Show file tree
Hide file tree
Showing 39 changed files with 2,687 additions and 1,397 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ For tensorflow 2.4.x, run `pip3 install -U 'TensorFlowASR[tf2.4]'` or `pip3 inst

For tensorflow 2.5.x, run `pip3 install -U 'TensorFlowASR[tf2.5]'` or `pip3 install -U 'TensorFlowASR[tf2.5-gpu]'`

For tensorflow 2.6.x, run `pip3 install -U 'TensorFlowASR[tf2.6]'` or `pip3 install -U 'TensorFlowASR[tf2.6-gpu]'`

### Installing from source

```bash
git clone https://github.com/TensorSpeech/TensorFlowASR.git
cd TensorFlowASR
pip3 install '.[tf2.3]' # or '.[tf2.3-gpu]' or '.[tf2.4]' or '.[tf2.4-gpu]' or '.[tf2.5]' or '.[tf2.5-gpu]'
pip3 install -e '.[tf2.6]' # see other options in setup.py file
```

For anaconda3:
Expand Down
116 changes: 116 additions & 0 deletions examples/conformer/saved_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Copyright 2020 Huy Le Nguyen (@usimarit)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from tensorflow_asr.utils import env_util

logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")

tf.keras.backend.clear_session()

parser = argparse.ArgumentParser(prog="Conformer Testing")

parser.add_argument(
"--config",
type=str,
default=DEFAULT_YAML,
help="The file path of model configuration file",
)

parser.add_argument(
"--h5",
type=str,
default=None,
help="Path to saved h5 weights",
)

parser.add_argument(
"--sentence_piece",
default=False,
action="store_true",
help="Whether to use `SentencePiece` model",
)

parser.add_argument(
"--subwords",
default=False,
action="store_true",
help="Use subwords",
)

parser.add_argument(
"--output_dir",
type=str,
default=None,
help="Output directory for saved model",
)

args = parser.parse_args()

assert args.h5
assert args.output_dir

from tensorflow_asr.configs.config import Config
from tensorflow_asr.featurizers.speech_featurizers import TFSpeechFeaturizer
from tensorflow_asr.featurizers.text_featurizers import CharFeaturizer, SentencePieceFeaturizer, SubwordFeaturizer
from tensorflow_asr.models.transducer.conformer import Conformer

config = Config(args.config)
speech_featurizer = TFSpeechFeaturizer(config.speech_config)

if args.sentence_piece:
logger.info("Use SentencePiece ...")
text_featurizer = SentencePieceFeaturizer(config.decoder_config)
elif args.subwords:
logger.info("Use subwords ...")
text_featurizer = SubwordFeaturizer(config.decoder_config)
else:
logger.info("Use characters ...")
text_featurizer = CharFeaturizer(config.decoder_config)

tf.random.set_seed(0)

# build model
conformer = Conformer(**config.model_config, vocabulary_size=text_featurizer.num_classes)
conformer.make(speech_featurizer.shape)
conformer.load_weights(args.h5, by_name=True)
conformer.summary(line_length=100)
conformer.add_featurizers(speech_featurizer, text_featurizer)


class aModule(tf.Module):
def __init__(self, model):
super().__init__()
self.model = model

@tf.function(
input_signature=[
{
"inputs": tf.TensorSpec(shape=[None, None, 80, 1], dtype=tf.float32, name="inputs"),
"inputs_length": tf.TensorSpec(shape=[None], dtype=tf.int32, name="inputs_length"),
}
]
)
def pred(self, input_batch):
result = self.model.recognize(input_batch)
return {"ASR": result}


module = aModule(conformer)
tf.saved_model.save(module, args.output_dir, signatures={"serving_default": module.pred})
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 127
85 changes: 67 additions & 18 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
cython
numpy
scipy
sklearn
pandas
tensorflow-datasets>=4.2.0
tensorflow-addons>=0.11.1
setuptools>=47.1.1
librosa>=0.8.0
soundfile>=0.10.3
PyYAML>=5.3.1
matplotlib>=3.2.1
sox>=1.4.1
tqdm>=4.54.1
colorama>=0.4.4
nlpaug>=1.1.1
nltk>=3.5
sentencepiece>=0.1.94
absl-py==0.12.0
appdirs==1.4.4
astroid==2.6.6
attrs==21.2.0
audioread==2.1.9
black==21.7b0
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.4
click==8.0.1
colorama==0.4.4
cycler==0.10.0
Cython==0.29.24
decorator==5.0.9
dill==0.3.4
flake8==3.9.2
future==0.18.2
googleapis-common-protos==1.53.0
idna==3.2
isort==5.9.3
joblib==1.0.1
kiwisolver==1.3.1
lazy-object-proxy==1.6.0
librosa==0.8.1
llvmlite==0.36.0
matplotlib==3.4.3
mccabe==0.6.1
mypy-extensions==0.4.3
nlpaug==1.1.7
nltk==3.6.2
numba==0.53.1
numpy==1.19.5
packaging==21.0
pandas==1.3.1
pathspec==0.9.0
Pillow==8.3.1
pooch==1.4.0
promise==2.3
protobuf==3.17.3
pycodestyle==2.7.0
pycparser==2.20
pyflakes==2.3.1
pyparsing==2.4.7
python-dateutil==2.8.2
pytz==2021.1
PyYAML==5.4.1
regex==2021.8.3
requests==2.26.0
resampy==0.2.2
scikit-learn==0.24.2
scipy==1.7.1
sentencepiece==0.1.96
six==1.15.0
sklearn==0.0
SoundFile==0.10.3.post1
sox==1.4.1
tensorflow-addons==0.13.0
tensorflow-datasets==4.4.0
tensorflow-metadata==1.2.0
termcolor==1.1.0
threadpoolctl==2.2.0
toml==0.10.2
tomli==1.2.1
tqdm==4.62.1
typeguard==2.12.1
urllib3==1.26.6
wrapt==1.12.1
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[flake8]
ignore = E402,E701,E702,E704,E251,W503,W504,C901
ignore = E402,E701,E702,E704,E251,E203,W503,W504,C901
max-line-length = 127

[pep8]
ignore = E402,E701,E702,E704,E251,W503,W504,C901
ignore = E402,E701,E702,E704,E251,E203,W503,W504,C901
max-line-length = 127
indent-size = 4
20 changes: 11 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setuptools.setup(
name="TensorFlowASR",
version="1.0.2",
version="1.0.3",
author="Huy Le Nguyen",
author_email="[email protected]",
description="Almost State-of-the-art Automatic Speech Recognition using Tensorflow 2",
Expand All @@ -32,12 +32,14 @@
packages=setuptools.find_packages(include=["tensorflow_asr*"]),
install_requires=requirements,
extras_require={
"tf2.3": ["tensorflow>=2.3.0,<2.4", "tensorflow-text>2.3.0,<2.4", "tensorflow-io>=0.16.0,<0.17"],
"tf2.3-gpu": ["tensorflow-gpu>=2.3.0,<2.4", "tensorflow-text>=2.3.0,<2.4", "tensorflow-io>=0.16.0,<0.17"],
"tf2.4": ["tensorflow>=2.4.0,<2.5", "tensorflow-text>=2.4.0,<2.5", "tensorflow-io>=0.17.0,<0.18"],
"tf2.4-gpu": ["tensorflow-gpu>=2.4.0,<2.5", "tensorflow-text>=2.4.0,<2.5", "tensorflow-io>=0.17.0,<0.18"],
"tf2.5": ["tensorflow>=2.5.0,<2.6", "tensorflow-text>=2.5.0,<2.6", "tensorflow-io>=0.18.0,<0.19"],
"tf2.5-gpu": ["tensorflow-gpu>=2.5.0,<2.6", "tensorflow-text>=2.5.0,<2.6", "tensorflow-io>=0.18.0,<0.19"]
"tf2.3": ["tensorflow~=2.3.0", "tensorflow-text~=2.3.0", "tensorflow-io~=0.16.0"],
"tf2.3-gpu": ["tensorflow-gpu~=2.3.0", "tensorflow-text~=2.3.0", "tensorflow-io~=0.16.0"],
"tf2.4": ["tensorflow~=2.4.0", "tensorflow-text~=2.4.0", "tensorflow-io~=0.17.0"],
"tf2.4-gpu": ["tensorflow-gpu~=2.4.0", "tensorflow-text~=2.4.0", "tensorflow-io~=0.17.0"],
"tf2.5": ["tensorflow~=2.5.0", "tensorflow-text~=2.5.0", "tensorflow-io~=0.18.0"],
"tf2.5-gpu": ["tensorflow-gpu~=2.5.0", "tensorflow-text~=2.5.0", "tensorflow-io~=0.18.0"],
"tf2.6": ["tensorflow~=2.6.0", "tensorflow-text~=2.6.0rc0", "tensorflow-io~=0.20.0"],
"tf2.6-gpu": ["tensorflow-gpu~=2.6.0", "tensorflow-text~=2.6.0rc0", "tensorflow-io~=0.20.0"],
},
classifiers=[
"Programming Language :: Python :: 3.6",
Expand All @@ -46,7 +48,7 @@
"Intended Audience :: Science/Research",
"Operating System :: POSIX :: Linux",
"License :: OSI Approved :: Apache Software License",
"Topic :: Software Development :: Libraries :: Python Modules"
"Topic :: Software Development :: Libraries :: Python Modules",
],
python_requires='>=3.6',
python_requires=">=3.6",
)
Loading

0 comments on commit 37ea12a

Please sign in to comment.