Skip to content

Commit

Permalink
Change all prints and nemo.logging to from nemo import logging; loggi…
Browse files Browse the repository at this point in the history
…ng (#311)

* change print and nemo.logging to from nemo import logging; logging.

Signed-off-by: Jason <[email protected]>

* style fix

Signed-off-by: Jason <[email protected]>

* change some infos to warnings

Signed-off-by: Jason <[email protected]>

* fix lgtm suggestions

Signed-off-by: Jason <[email protected]>

* abs path

Signed-off-by: Jason <[email protected]>
  • Loading branch information
blisc authored Jan 31, 2020
1 parent 1fddca2 commit 4137c2b
Show file tree
Hide file tree
Showing 54 changed files with 341 additions and 279 deletions.
2 changes: 1 addition & 1 deletion docs/docs_zh/sources/source/nlp/ner.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ text.txt 每一行包含文本序列,其中词以空格来进行分隔。label
callback_train = nemo.core.SimpleLossLoggerCallback(
tensors=[loss],
print_func=lambda x: print("Loss: {:.3f}".format(x[0].item())))
print_func=lambda x: logging.info("Loss: {:.3f}".format(x[0].item())))
train_data_size = len(train_data_layer)
Expand Down
5 changes: 3 additions & 2 deletions docs/docs_zh/sources/source/tutorials/callbacks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ SimpleLossLoggerCallback 是用来记录训练过程中的一些指标数据比
get_tb_values() 和 log_to_tb_func() 函数的输入。两个推荐重写的参数是 print_func() 和
get_tb_values() 或者 log_to_tb_func() 任选其一。

print_func() 应该用来记录打印到屏幕上的值。我们推荐使用 nemo.logging.info()
print_func() 应该用来记录打印到屏幕上的值。我们推荐使用 logging.info()
来取代 print() 函数。比如,可以这么打印 loss 值:

.. code-block:: python
from nemo import logging
def my_print_func(tensors):
nemo.logging.info(f"Loss {tensors[0]}")
logging.info(f"Loss {tensors[0]}")
我们提供了两个方法来打印到 tensorboard: get_tb_values() 和
log_to_tb_func()。对于记录标量的简单用例,我们推荐使用 get_tb_values()。
Expand Down
4 changes: 2 additions & 2 deletions docs/docs_zh/sources/source/tutorials/complex_training.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ NeMo 进一步扩充了用例,这些用例会用到多个损失函数和多个
# 更新打印函数加入两个损失函数的张量
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[l1_loss_tensor, mse_loss_tensor],
print_func=lambda x: print(
print_func=lambda x: logging.info(
f'Train Loss: {str(x[0].item() + x[1].item())}')
)
Expand Down Expand Up @@ -80,7 +80,7 @@ NeMo 进一步扩充了用例,这些用例会用到多个损失函数和多个
# SimpleLossLoggerCallback 把损失函数值打印到控制台
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[l1_loss_tensor, mse_loss_tensor],
print_func=lambda x: print(
print_func=lambda x: logging.info(
f'L1 Loss: {str(x[0].item())}'
f'MSE Loss: {str(x[1].item())}')
)
Expand Down
8 changes: 4 additions & 4 deletions docs/docs_zh/sources/source/tutorials/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Hello World
# SimpleLossLoggerCallback 打印损失函数值到控制台
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[lss],
print_func=lambda x: print(f'Train Loss: {str(x[0].item())}'))
print_func=lambda x: logging.info(f'Train Loss: {str(x[0].item())}'))
# 触发“训练”操作
nf.train([lss], callbacks=[callback],
Expand Down Expand Up @@ -129,9 +129,9 @@ Hello World
source = ' '.join([s for s in source if s != 'EOS' and s != 'PAD'])
response = ' '.join([s for s in response if s != 'EOS' and s != 'PAD'])
target = ' '.join([s for s in target if s != 'EOS' and s != 'PAD'])
print(f"Train Loss:{str(tensors[0].item())}")
print(f"SOURCE: {source} <---> PREDICTED RESPONSE: {response} "
f"<---> TARGET: {target}")
logging.info(f"Train Loss:{str(tensors[0].item())}")
logging.info(f"SOURCE: {source} <---> PREDICTED RESPONSE: {response} "
f"<---> TARGET: {target}")
callback = nemo.core.SimpleLossLoggerCallback(
Expand Down
10 changes: 5 additions & 5 deletions docs/sources/source/nlp/joint_intent_slot_filling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Next, we define all Neural Modules participating in our joint intent slot fillin
num_gpus=1,
local_rank=0,
mode='train'):
nemo.logging.info(f"Loading {mode} data...")
logging.info(f"Loading {mode} data...")
data_file = f'{data_desc.data_dir}/{mode}.tsv'
slot_file = f'{data_desc.data_dir}/{mode}_slots.tsv'
shuffle = args.shuffle_data if mode == 'train' else False
Expand All @@ -133,15 +133,15 @@ Next, we define all Neural Modules participating in our joint intent slot fillin
subtokens_mask, intents, slots = data_layer()
data_size = len(data_layer)
print(f'The length of data layer is {data_size}')
logging.info(f'The length of data layer is {data_size}')
if data_size < batch_size:
nemo.logging.warning("Batch_size is larger than the dataset size")
nemo.logging.warning("Reducing batch_size to dataset size")
logging.warning("Batch_size is larger than the dataset size")
logging.warning("Reducing batch_size to dataset size")
batch_size = data_size
steps_per_epoch = math.ceil(data_size / (batch_size * num_gpus))
nemo.logging.info(f"Steps_per_epoch = {steps_per_epoch}")
logging.info(f"Steps_per_epoch = {steps_per_epoch}")
hidden_states = pretrained_bert_model(input_ids=ids,
token_type_ids=type_ids,
Expand Down
2 changes: 1 addition & 1 deletion docs/sources/source/nlp/ner.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Now, we will set up our callbacks. We will use 3 callbacks:
callback_train = nemo.core.SimpleLossLoggerCallback(
tensors=[loss],
print_func=lambda x: print("Loss: {:.3f}".format(x[0].item())))
print_func=lambda x: logging.info("Loss: {:.3f}".format(x[0].item())))
train_data_size = len(train_data_layer)
Expand Down
6 changes: 3 additions & 3 deletions docs/sources/source/nlp/punctuation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Now, we will set up our callbacks. We will use 3 callbacks:
callback_train = nemo.core.SimpleLossLoggerCallback(
tensors=[task_loss, punct_loss, capit_loss, punct_logits, capit_logits],
print_func=lambda x: print("Loss: {:.3f}".format(x[0].item())),
print_func=lambda x: logging.info("Loss: {:.3f}".format(x[0].item())),
step_freq=STEP_FREQ)
train_data_size = len(train_data_layer)
Expand Down Expand Up @@ -293,7 +293,7 @@ Run inference, append punctuation and capitalize words based on the generated pr
capit_preds = np.argmax(capit_logits, axis=2)
for i, query in enumerate(queries):
nemo.logging.info(f'Query: {query}')
logging.info(f'Query: {query}')
punct_pred = punct_preds[i][subtokens_mask[i] > 0.5]
capit_pred = capit_preds[i][subtokens_mask[i] > 0.5]
Expand All @@ -312,7 +312,7 @@ Run inference, append punctuation and capitalize words based on the generated pr
if punct_label != 'O':
output += punct_label
output += ' '
nemo.logging.info(f'Combined: {output.strip()}\n')
logging.info(f'Combined: {output.strip()}\n')
Inference results:

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/source/nlp/question_answering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Next, we define all Neural Modules participating in our question answering class
train_callback = nemo.core.SimpleLossLoggerCallback(
tensors=train_tensors,
print_func=lambda x: print("Loss: {:.3f}".format(x[0].item())),
print_func=lambda x: logging.info("Loss: {:.3f}".format(x[0].item())),
get_tb_values=lambda x: [["loss", x[0]]],
step_freq=args.step_freq,
tb_writer=neural_factory.tb_writer)
Expand Down
5 changes: 3 additions & 2 deletions docs/sources/source/tutorials/callbacks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ training. The two reccomended arguments to override are print_func(), and
either get_tb_values() or log_to_tb_func().

print_func() should be used to log values to screen. We recommend using
nemo.logging.info() in place
logging.info() in place
of print(). For example, it can be used to print the loss value:

.. code-block:: python
from nemo import logging
def my_print_func(tensors):
nemo.logging.info(f"Loss {tensors[0]}")
logging.info(f"Loss {tensors[0]}")
We provide two methods to log to tensorboard: get_tb_values() and
log_to_tb_func(). For simple use case of logging scalars, we recommend
Expand Down
4 changes: 2 additions & 2 deletions docs/sources/source/tutorials/complex_training.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Here's an example:
# Update printing function to add both losses
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[l1_loss_tensor, mse_loss_tensor],
print_func=lambda x: print(
print_func=lambda x: logging.info(
f'Train Loss: {str(x[0].item() + x[1].item())}')
)
Expand Down Expand Up @@ -84,7 +84,7 @@ loop.
# SimpleLossLoggerCallback will print loss values to console.
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[l1_loss_tensor, mse_loss_tensor],
print_func=lambda x: print(
print_func=lambda x: logging.info(
f'L1 Loss: {str(x[0].item())}'
f'MSE Loss: {str(x[1].item())}')
)
Expand Down
8 changes: 4 additions & 4 deletions docs/sources/source/tutorials/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This example shows how to build a model which learn Taylor's coefficients for y=
# SimpleLossLoggerCallback will print loss values to console.
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[lss],
print_func=lambda x: print(f'Train Loss: {str(x[0].item())}'))
print_func=lambda x: logging.info(f'Train Loss: {str(x[0].item())}'))
# Invoke "train" action
nf.train([lss], callbacks=[callback],
Expand Down Expand Up @@ -129,9 +129,9 @@ During training model will print:
source = ' '.join([s for s in source if s != 'EOS' and s != 'PAD'])
response = ' '.join([s for s in response if s != 'EOS' and s != 'PAD'])
target = ' '.join([s for s in target if s != 'EOS' and s != 'PAD'])
print(f"Train Loss:{str(tensors[0].item())}")
print(f"SOURCE: {source} <---> PREDICTED RESPONSE: {response} "
f"<---> TARGET: {target}")
logging.info(f"Train Loss:{str(tensors[0].item())}")
logging.info(f"SOURCE: {source} <---> PREDICTED RESPONSE: {response} "
f"<---> TARGET: {target}")
callback = nemo.core.SimpleLossLoggerCallback(
Expand Down
4 changes: 3 additions & 1 deletion examples/applications/asr_service/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import nemo
import nemo.collections.asr as nemo_asr

logging = nemo.logging

app = Flask(__name__)
# make sure WORK_DIR exists before calling your service
# in this folder, the service will store received .wav files and constructed
Expand Down Expand Up @@ -46,7 +48,7 @@
vocab=labels, beam_width=64, alpha=2.0, beta=1.0, lm_path=LM_PATH, num_cpus=max(os.cpu_count(), 1),
)
else:
print("Beam search is not enabled")
logging.info("Beam search is not enabled")

if __name__ == '__main__':
app.run()
6 changes: 4 additions & 2 deletions examples/applications/asr_service/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import nemo
import nemo.collections.asr as nemo_asr

logging = nemo.logging

try:
from app import beam_search_with_lm
except ImportError:
print("Not using Beam Search Decoder with LM")
logging.info("Not using Beam Search Decoder with LM")
ENABLE_NGRAM = False


Expand All @@ -46,7 +48,7 @@ def wav_to_text(manifest, greedy=True):
predictions = greedy_decoder(log_probs=log_probs)

if ENABLE_NGRAM:
print('Running with beam search')
logging.info('Running with beam search')
beam_predictions = beam_search_with_lm(log_probs=log_probs, log_probs_length=encoded_len)
eval_tensors = [beam_predictions]

Expand Down
18 changes: 10 additions & 8 deletions examples/asr/jasper_an4.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
)
from nemo.utils.lr_policies import CosineAnnealing

logging = nemo.logging


def create_dags(jasper_params, args, nf):
vocab = jasper_params['labels']
Expand All @@ -37,7 +39,7 @@ def create_dags(jasper_params, args, nf):
num_samples = len(data_layer)
steps_per_epoch = math.ceil(num_samples / (args.batch_size * args.iter_per_step * nf.world_size))
total_steps = steps_per_epoch * args.num_epochs
print("Train samples=", num_samples, "num_steps=", total_steps)
logging.info("Train samples=", num_samples, "num_steps=", total_steps)

data_preprocessor = nemo_asr.AudioToMelSpectrogramPreprocessor(
**jasper_params["AudioToMelSpectrogramPreprocessor"]
Expand All @@ -57,7 +59,7 @@ def create_dags(jasper_params, args, nf):
)

num_samples = len(data_layer_eval)
nemo.logging.info(f"Eval samples={num_samples}")
logging.info(f"Eval samples={num_samples}")

jasper_encoder = nemo_asr.JasperEncoder(**jasper_params["JasperEncoder"])

Expand All @@ -84,7 +86,7 @@ def create_dags(jasper_params, args, nf):
loss_e = ctc_loss(
log_probs=log_probs_e, targets=transcript_e, input_length=encoded_len_e, target_length=transcript_len_e,
)
nemo.logging.info("Num of params in encoder: {0}".format(jasper_encoder.num_weights))
logging.info("Num of params in encoder: {0}".format(jasper_encoder.num_weights))

# Callbacks to print info to console and Tensorboard
train_callback = nemo.core.SimpleLossLoggerCallback(
Expand Down Expand Up @@ -194,10 +196,10 @@ def main():
)

if args.test_after_training:
nemo.logging.info("Testing greedy and beam search with LM WER.")
logging.info("Testing greedy and beam search with LM WER.")
# Create BeamSearch NM
if nf.world_size > 1:
nemo.logging.warning("Skipping beam search WER as it does not " "work if doing distributed training.")
logging.warning("Skipping beam search WER as it does not " "work if doing distributed training.")
else:
beam_search_with_lm = nemo_asr.BeamSearchDecoderWithLM(
vocab=vocab, beam_width=64, alpha=2.0, beta=1.5, lm_path=args.lm, num_cpus=max(os.cpu_count(), 1),
Expand All @@ -210,7 +212,7 @@ def main():
greedy_hypotheses = post_process_predictions(evaluated_tensors[1], vocab)
references = post_process_transcripts(evaluated_tensors[2], evaluated_tensors[3], vocab)
wer = word_error_rate(hypotheses=greedy_hypotheses, references=references)
nemo.logging.info("Greedy WER: {:.2f}%".format(wer * 100))
logging.info("Greedy WER: {:.2f}%".format(wer * 100))
if wer > wer_thr:
nf.sync_all_processes(False)
raise ValueError(f"Final eval greedy WER {wer * 100:.2f}% > :" f"than {wer_thr * 100:.2f}%")
Expand All @@ -225,7 +227,7 @@ def main():
beam_hypotheses.append(j[0][1])

beam_wer = word_error_rate(hypotheses=beam_hypotheses, references=references)
nemo.logging.info("Beam WER {:.2f}%".format(beam_wer * 100))
logging.info("Beam WER {:.2f}%".format(beam_wer * 100))
assert beam_wer <= beam_wer_thr, "Final eval beam WER {:.2f}% > than {:.2f}%".format(
beam_wer * 100, beam_wer_thr * 100
)
Expand Down Expand Up @@ -266,7 +268,7 @@ def main():
greedy_hypotheses = post_process_predictions(evaluated_tensors[1], vocab)
references = post_process_transcripts(evaluated_tensors[2], evaluated_tensors[3], vocab)
wer_new = word_error_rate(hypotheses=greedy_hypotheses, references=references)
nemo.logging.info("New greedy WER: {:.2f}%".format(wer_new * 100))
logging.info("New greedy WER: {:.2f}%".format(wer_new * 100))
if wer_new > wer * 1.1:
nf.sync_all_processes(False)
raise ValueError(
Expand Down
6 changes: 4 additions & 2 deletions examples/image/transfer_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import nemo
from nemo.backends.pytorch.torchvision.helpers import compute_accuracy, eval_epochs_done_callback, eval_iter_callback

logging = nemo.logging

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))

if not os.path.isdir("hymenoptera_data"):
print("Datafolder not found. Downloading data from the Web")
logging.info("Datafolder not found. Downloading data from the Web")
subprocess.run(["wget", "https://download.pytorch.org/tutorial/hymenoptera_data.zip"])
zip_ref = zipfile.ZipFile('hymenoptera_data.zip', 'r')
zip_ref.extractall('.')
zip_ref.close()
else:
print("Found data folder - hymenoptera_data")
logging.info("Found data folder - hymenoptera_data")

parser = argparse.ArgumentParser(description='Transfer Learning')
parser.add_argument("--batch_size", default=32, type=int)
Expand Down
6 changes: 4 additions & 2 deletions examples/start_here/chatbot_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import nemo

logging = nemo.logging

# Get Data
data_file = "movie_data.txt"
if not os.path.isfile(data_file):
Expand Down Expand Up @@ -67,8 +69,8 @@ def outputs2words(tensors, vocab):
source = ' '.join([s for s in source if s != 'EOS' and s != 'PAD'])
response = ' '.join([s for s in response if s != 'EOS' and s != 'PAD'])
target = ' '.join([s for s in target if s != 'EOS' and s != 'PAD'])
print(f"Train Loss:{str(tensors[0].item())}")
print(f"SOURCE: {source} <---> PREDICTED RESPONSE: {response} " f"<---> TARGET: {target}")
logging.info(f"Train Loss:{str(tensors[0].item())}")
logging.info(f"SOURCE: {source} <---> PREDICTED RESPONSE: {response} " f"<---> TARGET: {target}")


callback = nemo.core.SimpleLossLoggerCallback(
Expand Down
4 changes: 3 additions & 1 deletion examples/start_here/chatbot_example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import nemo

logging = nemo.logging

# Get Data
data_file = "movie_data.txt"
if not os.path.isfile(data_file):
Expand Down Expand Up @@ -74,7 +76,7 @@ def outputs2words(tensors, vocab):
source = ' '.join([s for s in source if s != 'EOS' and s != 'PAD'])
response = ' '.join([s for s in response if s != 'EOS' and s != 'PAD'])
target = ' '.join([s for s in target if s != 'EOS' and s != 'PAD'])
print(f'Train Loss: {str(tensors[0].item())}')
logging.info(f'Train Loss: {str(tensors[0].item())}')
tmp = " SOURCE: {0} <---> PREDICTED RESPONSE: {1} <---> TARGET: {2}"
return tmp.format(source, response, target)

Expand Down
4 changes: 3 additions & 1 deletion examples/start_here/simplest_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) 2019 NVIDIA Corporation
import nemo

logging = nemo.logging

nf = nemo.core.NeuralModuleFactory()
# To use CPU-only do:
# from nemo.core import DeviceType
Expand All @@ -19,7 +21,7 @@

# SimpleLossLoggerCallback will print loss values to console.
callback = nemo.core.SimpleLossLoggerCallback(
tensors=[lss], print_func=lambda x: print(f'Train Loss: {str(x[0].item())}'),
tensors=[lss], print_func=lambda x: logging.info(f'Train Loss: {str(x[0].item())}'),
)

# Invoke "train" action
Expand Down
Loading

0 comments on commit 4137c2b

Please sign in to comment.