diff --git a/deepspeech/exps/deepspeech2/bin/train.py b/deepspeech/exps/deepspeech2/bin/train.py index bb0bd43a88acfa4b18e82840851061f13d75641d..69ff043a08d28171711543afcbd51bcd571e69d2 100644 --- a/deepspeech/exps/deepspeech2/bin/train.py +++ b/deepspeech/exps/deepspeech2/bin/train.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Trainer for DeepSpeech2 model.""" -import os - from paddle import distributed as dist from deepspeech.exps.deepspeech2.config import get_cfg_defaults @@ -55,7 +53,5 @@ if __name__ == "__main__": if args.dump_config: with open(args.dump_config, 'w') as f: print(config, file=f) - if config.training.seed is not None: - os.environ.setdefault('FLAGS_cudnn_deterministic', 'True') main(config, args) diff --git a/deepspeech/exps/deepspeech2/model.py b/deepspeech/exps/deepspeech2/model.py index 1bd4c722fc9aa05f56953b1dea6a0d12f0660b66..65c905a13a470bad7c4b64468872b0fc02606f5e 100644 --- a/deepspeech/exps/deepspeech2/model.py +++ b/deepspeech/exps/deepspeech2/model.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Contains DeepSpeech2 and DeepSpeech2Online model.""" -import random import time from collections import defaultdict from pathlib import Path @@ -54,7 +53,6 @@ class DeepSpeech2Trainer(Trainer): weight_decay=1e-6, # the coeff of weight decay global_grad_clip=5.0, # the global norm clip n_epoch=50, # train epochs - seed=1024, #train seed )) if config is not None: @@ -63,13 +61,6 @@ class DeepSpeech2Trainer(Trainer): def __init__(self, config, args): super().__init__(config, args) - if config.training.seed is not None: - self.set_seed(config.training.seed) - - def set_seed(self, seed): - np.random.seed(seed) - random.seed(seed) - paddle.seed(seed) def train_batch(self, batch_index, batch_data, msg): start = time.time() diff --git a/deepspeech/training/cli.py b/deepspeech/training/cli.py index 9d1456459e798250c047454bc58fa9f26e3279e6..ecd7a8f267e536e708fc4aea5eaba695780f359d 100644 --- a/deepspeech/training/cli.py +++ b/deepspeech/training/cli.py @@ -16,23 +16,23 @@ import argparse def default_argument_parser(): r"""A simple yet genral argument parser for experiments with parakeet. - - This is used in examples with parakeet. And it is intended to be used by - other experiments with parakeet. It requires a minimal set of command line + + This is used in examples with parakeet. And it is intended to be used by + other experiments with parakeet. It requires a minimal set of command line arguments to start a training script. - - The ``--config`` and ``--opts`` are used for overwrite the deault + + The ``--config`` and ``--opts`` are used for overwrite the deault configuration. - - The ``--data`` and ``--output`` specifies the data path and output path. - Resuming training from existing progress at the output directory is the + + The ``--data`` and ``--output`` specifies the data path and output path. + Resuming training from existing progress at the output directory is the intended default behavior. - + The ``--checkpoint_path`` specifies the checkpoint to load from. - + The ``--device`` and ``--nprocs`` specifies how to run the training. - - + + See Also -------- parakeet.training.experiment @@ -58,10 +58,13 @@ def default_argument_parser(): parser.add_argument("--nprocs", type=int, default=1, help="number of parallel processes to use.") # overwrite extra config and default config - # parser.add_argument("--opts", nargs=argparse.REMAINDER, + # parser.add_argument("--opts", nargs=argparse.REMAINDER, # help="options to overwrite --config file and the default config, passing in KEY VALUE pairs") parser.add_argument("--opts", type=str, default=[], nargs='+', help="options to overwrite --config file and the default config, passing in KEY VALUE pairs") + + parser.add_argument("--seed", type=int, default=None, + help="seed to use for paddle, np and random. The default value is None") # yapd: enable return parser diff --git a/deepspeech/training/trainer.py b/deepspeech/training/trainer.py index 209e22409b2dc049ee1f3a85e49cea5e9c53d1af..2ab7eac03a14f643e730341cc51eec183b9c524e 100644 --- a/deepspeech/training/trainer.py +++ b/deepspeech/training/trainer.py @@ -11,9 +11,11 @@ # 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 random import time from pathlib import Path +import numpy as np import paddle from paddle import distributed as dist from tensorboardX import SummaryWriter @@ -93,6 +95,13 @@ class Trainer(): self.checkpoint_dir = None self.iteration = 0 self.epoch = 0 + if args.seed is not None: + self.set_seed(args.seed) + + def set_seed(self, seed): + np.random.seed(seed) + random.seed(seed) + paddle.seed(seed) def setup(self): """Setup the experiment. diff --git a/examples/aishell/s0/local/train.sh b/examples/aishell/s0/local/train.sh index c6a631800378640434dbe952035840895c1b23b5..d42e51faca6e9b6afa2cb6599506c7e9af92c343 100755 --- a/examples/aishell/s0/local/train.sh +++ b/examples/aishell/s0/local/train.sh @@ -19,12 +19,22 @@ fi mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ --output exp/${ckpt_name} \ ---model_type ${model_type} +--model_type ${model_type} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/aishell/s1/local/train.sh b/examples/aishell/s1/local/train.sh index f3eb98daf6da6c818de1cd2fc2f56e96ff6a926c..ec17054ab1e6fe6b831846b2d7b10a4b9fb05479 100755 --- a/examples/aishell/s1/local/train.sh +++ b/examples/aishell/s1/local/train.sh @@ -19,11 +19,21 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/callcenter/s1/local/train.sh b/examples/callcenter/s1/local/train.sh index f750568a0f01884a1c72ec889b7c91e7e449357d..928c6492c41302b1e863fbd0b3b3be7dafca7103 100755 --- a/examples/callcenter/s1/local/train.sh +++ b/examples/callcenter/s1/local/train.sh @@ -19,11 +19,21 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/librispeech/s0/local/train.sh b/examples/librispeech/s0/local/train.sh index 039b9cea456b41dd345d5594d142abe7ca165385..dcd21df34345e29b7dccadcb32aa2bbedf38ffbb 100755 --- a/examples/librispeech/s0/local/train.sh +++ b/examples/librispeech/s0/local/train.sh @@ -20,12 +20,22 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ --output exp/${ckpt_name} \ ---model_type ${model_type} +--model_type ${model_type} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/librispeech/s1/local/train.sh b/examples/librispeech/s1/local/train.sh index f3eb98daf6da6c818de1cd2fc2f56e96ff6a926c..ec17054ab1e6fe6b831846b2d7b10a4b9fb05479 100755 --- a/examples/librispeech/s1/local/train.sh +++ b/examples/librispeech/s1/local/train.sh @@ -19,11 +19,21 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/librispeech/s2/local/train.sh b/examples/librispeech/s2/local/train.sh index c8bb9aafcf8f40846fc5df9d50cff501b303f307..c75252594850d063e1288b8076719bdead7f7abe 100755 --- a/examples/librispeech/s2/local/train.sh +++ b/examples/librispeech/s2/local/train.sh @@ -19,12 +19,22 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --model-name u2_kaldi \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/ted_en_zh/t0/local/train.sh b/examples/ted_en_zh/t0/local/train.sh index f3eb98daf6da6c818de1cd2fc2f56e96ff6a926c..ec17054ab1e6fe6b831846b2d7b10a4b9fb05479 100755 --- a/examples/ted_en_zh/t0/local/train.sh +++ b/examples/ted_en_zh/t0/local/train.sh @@ -19,11 +19,21 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/timit/s1/local/train.sh b/examples/timit/s1/local/train.sh index f3eb98daf6da6c818de1cd2fc2f56e96ff6a926c..ec17054ab1e6fe6b831846b2d7b10a4b9fb05479 100755 --- a/examples/timit/s1/local/train.sh +++ b/examples/timit/s1/local/train.sh @@ -19,11 +19,21 @@ echo "using ${device}..." mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/tiny/s0/local/train.sh b/examples/tiny/s0/local/train.sh index c6a631800378640434dbe952035840895c1b23b5..d42e51faca6e9b6afa2cb6599506c7e9af92c343 100755 --- a/examples/tiny/s0/local/train.sh +++ b/examples/tiny/s0/local/train.sh @@ -19,12 +19,22 @@ fi mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ --output exp/${ckpt_name} \ ---model_type ${model_type} +--model_type ${model_type} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!" diff --git a/examples/tiny/s1/local/train.sh b/examples/tiny/s1/local/train.sh index f6bd2c98359b67292f9654c69ed11fc1a6720046..2fb3a95ab5925c48e96bdc1326020db0ee5fe571 100755 --- a/examples/tiny/s1/local/train.sh +++ b/examples/tiny/s1/local/train.sh @@ -18,11 +18,21 @@ fi mkdir -p exp +seed=1024 +if [ ${seed} ]; then + export FLAGS_cudnn_deterministic=True +fi + python3 -u ${BIN_DIR}/train.py \ --device ${device} \ --nproc ${ngpu} \ --config ${config_path} \ ---output exp/${ckpt_name} +--output exp/${ckpt_name} \ +--seed ${seed} + +if [ ${seed} ]; then + unset FLAGS_cudnn_deterministic +fi if [ $? -ne 0 ]; then echo "Failed in training!"