Skip to content

Commit

Permalink
✍️ update env util
Browse files Browse the repository at this point in the history
  • Loading branch information
nglehuy committed May 16, 2021
1 parent f79b3f6 commit 44b6875
Show file tree
Hide file tree
Showing 34 changed files with 136 additions and 118 deletions.
10 changes: 5 additions & 5 deletions examples/conformer/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -63,13 +63,13 @@
speech_featurizer = TFSpeechFeaturizer(config.speech_config)

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

tf.random.set_seed(0)
Expand All @@ -96,7 +96,7 @@
overwrite = input(f"Overwrite existing result file {filepath} ? (y/n): ").lower() == "y"
if overwrite:
results = conformer.predict(test_data_loader, verbose=1)
print(f"Saving result to {args.output} ...")
logger.info(f"Saving result to {args.output} ...")
with open(filepath, "w") as openfile:
openfile.write("PATH\tDURATION\tGROUNDTRUTH\tGREEDY\tBEAMSEARCH\n")
progbar = tqdm(total=test_dataset.total_steps, unit="batch")
Expand Down
2 changes: 1 addition & 1 deletion examples/conformer/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

from tensorflow_asr.configs.config import Config
Expand Down
8 changes: 4 additions & 4 deletions examples/conformer/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import argparse
from tensorflow_asr.utils import env_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -64,13 +64,13 @@
speech_featurizer = speech_featurizers.TFSpeechFeaturizer(config.speech_config)

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

if args.tfrecords:
Expand Down
10 changes: 5 additions & 5 deletions examples/contextnet/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -63,13 +63,13 @@
speech_featurizer = TFSpeechFeaturizer(config.speech_config)

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

tf.random.set_seed(0)
Expand All @@ -96,7 +96,7 @@
overwrite = input(f"Overwrite existing result file {filepath} ? (y/n): ").lower() == "y"
if overwrite:
results = contextnet.predict(test_data_loader, verbose=1)
print(f"Saving result to {args.output} ...")
logger.info(f"Saving result to {args.output} ...")
with open(filepath, "w") as openfile:
openfile.write("PATH\tDURATION\tGROUNDTRUTH\tGREEDY\tBEAMSEARCH\n")
progbar = tqdm(total=test_dataset.total_steps, unit="batch")
Expand Down
2 changes: 1 addition & 1 deletion examples/contextnet/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

from tensorflow_asr.configs.config import Config
Expand Down
8 changes: 4 additions & 4 deletions examples/contextnet/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import argparse
from tensorflow_asr.utils import env_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -64,13 +64,13 @@
speech_featurizer = speech_featurizers.TFSpeechFeaturizer(config.speech_config)

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

if args.tfrecords:
Expand Down
10 changes: 5 additions & 5 deletions examples/deepspeech2/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -63,13 +63,13 @@
speech_featurizer = TFSpeechFeaturizer(config.speech_config)

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

tf.random.set_seed(0)
Expand All @@ -96,7 +96,7 @@
overwrite = input(f"Overwrite existing result file {filepath} ? (y/n): ").lower() == "y"
if overwrite:
results = deepspeech2.predict(test_data_loader, verbose=1)
print(f"Saving result to {args.output} ...")
logger.info(f"Saving result to {args.output} ...")
with open(filepath, "w") as openfile:
openfile.write("PATH\tDURATION\tGROUNDTRUTH\tGREEDY\tBEAMSEARCH\n")
progbar = tqdm(total=test_dataset.total_steps, unit="batch")
Expand Down
2 changes: 1 addition & 1 deletion examples/deepspeech2/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

from tensorflow_asr.configs.config import Config
Expand Down
8 changes: 4 additions & 4 deletions examples/deepspeech2/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from tensorflow_asr.utils import env_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -62,13 +62,13 @@
speech_featurizer = speech_featurizers.TFSpeechFeaturizer(config.speech_config)

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

if args.tfrecords:
Expand Down
16 changes: 8 additions & 8 deletions examples/demonstration/conformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from tensorflow_asr.utils import env_util, math_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

parser = argparse.ArgumentParser(prog="Conformer non streaming")
Expand Down Expand Up @@ -52,10 +52,10 @@
config = Config(args.config)
speech_featurizer = TFSpeechFeaturizer(config.speech_config)
if args.sentence_piece:
print("Loading SentencePiece model ...")
logger.info("Loading SentencePiece model ...")
text_featurizer = SentencePieceFeaturizer.load_from_file(config.decoder_config, args.subwords)
elif args.subwords and os.path.exists(args.subwords):
print("Loading subwords ...")
logger.info("Loading subwords ...")
text_featurizer = SubwordFeaturizer.load_from_file(config.decoder_config, args.subwords)
else:
text_featurizer = CharFeaturizer(config.decoder_config)
Expand All @@ -74,14 +74,14 @@

if args.beam_width:
transcript = conformer.recognize_beam(features[None, ...], input_length[None, ...])
print("Transcript:", transcript[0].numpy().decode("UTF-8"))
logger.info("Transcript:", transcript[0].numpy().decode("UTF-8"))
elif args.timestamp:
transcript, stime, etime, _, _ = conformer.recognize_tflite_with_timestamp(
signal, tf.constant(text_featurizer.blank, dtype=tf.int32), conformer.predict_net.get_initial_state())
print("Transcript:", transcript)
print("Start time:", stime)
print("End time:", etime)
logger.info("Transcript:", transcript)
logger.info("Start time:", stime)
logger.info("End time:", etime)
else:
transcript, _, _ = conformer.recognize_tflite(
signal, tf.constant(text_featurizer.blank, dtype=tf.int32), conformer.predict_net.get_initial_state())
print("Transcript:", tf.strings.unicode_encode(transcript, "UTF-8").numpy().decode("UTF-8"))
logger.info("Transcript:", tf.strings.unicode_encode(transcript, "UTF-8").numpy().decode("UTF-8"))
16 changes: 8 additions & 8 deletions examples/demonstration/rnn_transducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import argparse
from tensorflow_asr.utils import env_util, math_util, data_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

parser = argparse.ArgumentParser(prog="Rnn Transducer non streaming")
Expand Down Expand Up @@ -51,10 +51,10 @@
config = Config(args.config)
speech_featurizer = TFSpeechFeaturizer(config.speech_config)
if args.sentence_piece:
print("Loading SentencePiece model ...")
logger.info("Loading SentencePiece model ...")
text_featurizer = SentencePieceFeaturizer(config.decoder_config)
elif args.subwords:
print("Loading subwords ...")
logger.info("Loading subwords ...")
text_featurizer = SubwordFeaturizer(config.decoder_config)
else:
text_featurizer = CharFeaturizer(config.decoder_config)
Expand All @@ -78,22 +78,22 @@
inputs_length=input_length[None, ...]
)
)
print("Transcript:", transcript[0].numpy().decode("UTF-8"))
logger.info("Transcript:", transcript[0].numpy().decode("UTF-8"))
elif args.timestamp:
transcript, stime, etime, _, _, _ = rnnt.recognize_tflite_with_timestamp(
signal=signal,
predicted=tf.constant(text_featurizer.blank, dtype=tf.int32),
encoder_states=rnnt.encoder.get_initial_state(),
prediction_states=rnnt.predict_net.get_initial_state()
)
print("Transcript:", transcript)
print("Start time:", stime)
print("End time:", etime)
logger.info("Transcript:", transcript)
logger.info("Start time:", stime)
logger.info("End time:", etime)
else:
transcript = rnnt.recognize(
data_util.create_inputs(
inputs=features[None, ...],
inputs_length=input_length[None, ...]
)
)
print("Transcript:", transcript[0].numpy().decode("UTF-8"))
logger.info("Transcript:", transcript[0].numpy().decode("UTF-8"))
8 changes: 4 additions & 4 deletions examples/demonstration/streaming_tflite_conformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def int_or_str(text):
args, remaining = parser.parse_known_args()

if args.list_devices:
print(sd.query_devices())
logger.info(sd.query_devices())
parser.exit(0)

parser.add_argument('filename', metavar='FILENAME',
Expand Down Expand Up @@ -113,7 +113,7 @@ def recognize(signal, lastid, states):
data = Q.get()
text, lastid, states = recognize(data, lastid, states)
transcript += text
print(transcript, flush=True)
logger.info(transcript, flush=True)
except queue.Empty:
pass

Expand All @@ -126,14 +126,14 @@ def send(q, Q, E):
def callback(outdata, frames, time, status):
assert frames == args.blocksize
if status.output_underflow:
print('Output underflow: increase blocksize?', file=sys.stderr)
logger.info('Output underflow: increase blocksize?', file=sys.stderr)
raise sd.CallbackAbort
assert not status
try:
data = q.get_nowait()
Q.put(np.frombuffer(data, dtype=np.float32))
except queue.Empty as e:
print('Buffer is empty: increase buffersize?', file=sys.stderr)
logger.info('Buffer is empty: increase buffersize?', file=sys.stderr)
raise sd.CallbackAbort from e
if len(data) < len(outdata):
outdata[:len(data)] = data
Expand Down
2 changes: 1 addition & 1 deletion examples/demonstration/tflite_conformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
tflitemodel.invoke()
hyp = tflitemodel.get_tensor(output_details[0]["index"])

print("".join([chr(u) for u in hyp]))
logger.info("".join([chr(u) for u in hyp]))
10 changes: 5 additions & 5 deletions examples/jasper/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml")
Expand Down Expand Up @@ -63,13 +63,13 @@
speech_featurizer = TFSpeechFeaturizer(config.speech_config)

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

tf.random.set_seed(0)
Expand All @@ -96,7 +96,7 @@
overwrite = input(f"Overwrite existing result file {filepath} ? (y/n): ").lower() == "y"
if overwrite:
results = jasper.predict(test_data_loader, verbose=1)
print(f"Saving result to {args.output} ...")
logger.info(f"Saving result to {args.output} ...")
with open(filepath, "w") as openfile:
openfile.write("PATH\tDURATION\tGROUNDTRUTH\tGREEDY\tBEAMSEARCH\n")
progbar = tqdm(total=test_dataset.total_steps, unit="batch")
Expand Down
2 changes: 1 addition & 1 deletion examples/jasper/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from tensorflow_asr.utils import env_util, file_util

env_util.setup_environment()
logger = env_util.setup_environment()
import tensorflow as tf

from tensorflow_asr.configs.config import Config
Expand Down
Loading

0 comments on commit 44b6875

Please sign in to comment.