def validate_corpus(args): command = 'validate' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) data_directory = os.path.join(temp_dir, corpus_name) shutil.rmtree(data_directory, ignore_errors=True) os.makedirs(data_directory, exist_ok=True) logger = setup_logger(command, data_directory) corpus = AlignableCorpus(args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=getattr(args, 'num_jobs', 3), logger=logger, use_mp=not args.disable_mp) dictionary = Dictionary(args.dictionary_path, data_directory, logger=logger) if args.acoustic_model_path: acoustic_model = AcousticModel(args.acoustic_model_path) acoustic_model.validate(dictionary) a = CorpusValidator(corpus, dictionary, temp_directory=data_directory, ignore_acoustics=getattr(args, 'ignore_acoustics', False), test_transcriptions=getattr(args, 'test_transcriptions', False), use_mp=not args.disable_mp, logger=logger) begin = time.time() a.validate() logger.debug('Validation took {} seconds'.format(time.time() - begin)) logger.info('All done!') logger.debug('Done! Everything took {} seconds'.format(time.time() - all_begin)) handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler)
def train_lm(args): command = 'train_lm' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) if args.config_path: train_config = train_lm_yaml_to_config(args.config_path) else: train_config = load_basic_train_lm() corpus_name = os.path.basename(args.source_path) if corpus_name == '': args.source_path = os.path.dirname(args.source_path) corpus_name = os.path.basename(args.source_path) source = args.source_path dictionary = None if args.source_path.lower().endswith('.arpa'): corpus_name = os.path.splitext(corpus_name)[0] data_directory = os.path.join(temp_dir, corpus_name) else: data_directory = os.path.join(temp_dir, corpus_name) logger = setup_logger(command, data_directory) if not args.source_path.lower().endswith('.arpa'): source = AlignableCorpus(args.source_path, data_directory, num_jobs=args.num_jobs, use_mp=args.num_jobs>1) if args.dictionary_path is not None: dictionary = Dictionary(args.dictionary_path, data_directory) else: dictionary = None trainer = LmTrainer(source, train_config, args.output_model_path, dictionary=dictionary, temp_directory=data_directory, supplemental_model_path=args.model_path, supplemental_model_weight=args.model_weight) begin = time.time() trainer.train() logger.debug('Training took {} seconds'.format(time.time() - begin)) logger.info('All done!') logger.debug('Done! Everything took {} seconds'.format(time.time() - all_begin)) handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler)
def transcribe_corpus(args): command = 'transcribe' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) if args.config_path: transcribe_config = transcribe_yaml_to_config(args.config_path) else: transcribe_config = load_basic_transcribe() data_directory = os.path.join(temp_dir, corpus_name) if getattr(args, 'clean', False) and os.path.exists(data_directory): print('Cleaning old directory!') shutil.rmtree(data_directory, ignore_errors=True) logger = setup_logger(command, data_directory) os.makedirs(data_directory, exist_ok=True) os.makedirs(args.output_directory, exist_ok=True) os.makedirs(data_directory, exist_ok=True) conf_path = os.path.join(data_directory, 'config.yml') if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = { 'dirty': False, 'begin': time.time(), 'version': __version__, 'type': 'transcribe', 'corpus_directory': args.corpus_directory, 'dictionary_path': args.dictionary_path, 'acoustic_model_path': args.acoustic_model_path, 'language_model_path': args.language_model_path, } if conf['dirty'] or conf['type'] != command \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__ \ or conf['dictionary_path'] != args.dictionary_path \ or conf['language_model_path'] != args.language_model_path \ or conf['acoustic_model_path'] != args.acoustic_model_path: logger.warning( 'WARNING: Using old temp directory, this might not be ideal for you, use the --clean flag to ensure no ' 'weird behavior for previous versions of the temporary directory.') if conf['dirty']: logger.debug('Previous run ended in an error (maybe ctrl-c?)') if conf['type'] != command: logger.debug( 'Previous run was a different subcommand than {} (was {})'. format(command, conf['type'])) if conf['corpus_directory'] != args.corpus_directory: logger.debug('Previous run used source directory ' 'path {} (new run: {})'.format( conf['corpus_directory'], args.corpus_directory)) if conf['version'] != __version__: logger.debug('Previous run was on {} version (new run: {})'.format( conf['version'], __version__)) if conf['dictionary_path'] != args.dictionary_path: logger.debug('Previous run used dictionary path {} ' '(new run: {})'.format(conf['dictionary_path'], args.dictionary_path)) if conf['acoustic_model_path'] != args.acoustic_model_path: logger.debug('Previous run used acoustic model path {} ' '(new run: {})'.format(conf['acoustic_model_path'], args.acoustic_model_path)) if conf['language_model_path'] != args.language_model_path: logger.debug('Previous run used language model path {} ' '(new run: {})'.format(conf['language_model_path'], args.language_model_path)) try: if args.evaluate: corpus = AlignableCorpus( args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs, use_mp=transcribe_config.use_mp) else: corpus = TranscribeCorpus( args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs, use_mp=transcribe_config.use_mp) print(corpus.speaker_utterance_info()) acoustic_model = AcousticModel(args.acoustic_model_path, root_directory=data_directory) language_model = LanguageModel(args.language_model_path, root_directory=data_directory) dictionary = Dictionary(args.dictionary_path, data_directory) acoustic_model.validate(dictionary) begin = time.time() t = Transcriber(corpus, dictionary, acoustic_model, language_model, transcribe_config, temp_directory=data_directory, debug=getattr(args, 'debug', False), evaluation_mode=args.evaluate) if args.debug: print('Setup pretrained aligner in {} seconds'.format(time.time() - begin)) begin = time.time() t.transcribe() if args.debug: print('Performed transcribing in {} seconds'.format(time.time() - begin)) if args.evaluate: t.evaluate(args.output_directory) best_config_path = os.path.join(args.output_directory, 'best_transcribe_config.yaml') save_config(t.transcribe_config, best_config_path) t.export_transcriptions(args.output_directory) else: begin = time.time() t.export_transcriptions(args.output_directory) if args.debug: print('Exported transcriptions in {} seconds'.format( time.time() - begin)) print('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as _: conf['dirty'] = True raise finally: handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler) if os.path.exists(data_directory): with open(conf_path, 'w') as f: yaml.dump(conf, f)
def train_ivector(args): command = 'train_ivector' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) data_directory = os.path.join(temp_dir, corpus_name) if args.config_path: train_config, align_config = train_yaml_to_config(args.config_path) else: train_config, align_config = load_basic_train_ivector() conf_path = os.path.join(data_directory, 'config.yml') if getattr(args, 'clean', False) and os.path.exists(data_directory): print('Cleaning old directory!') shutil.rmtree(data_directory, ignore_errors=True) logger = setup_logger(command, data_directory) if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = { 'dirty': False, 'begin': all_begin, 'version': __version__, 'type': command, 'corpus_directory': args.corpus_directory, 'dictionary_path': args.dictionary_path, 'acoustic_model_path': args.acoustic_model_path, } if conf['dirty'] or conf['type'] != command \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__ \ or conf['dictionary_path'] != args.dictionary_path \ or conf['acoustic_model_path'] != args.acoustic_model_path: logger.warning( 'WARNING: Using old temp directory, this might not be ideal for you, use the --clean flag to ensure no ' 'weird behavior for previous versions of the temporary directory.') if conf['dirty']: logger.debug('Previous run ended in an error (maybe ctrl-c?)') if conf['type'] != command: logger.debug( 'Previous run was a different subcommand than {} (was {})'. format(command, conf['type'])) if conf['corpus_directory'] != args.corpus_directory: logger.debug('Previous run used source directory ' 'path {} (new run: {})'.format( conf['corpus_directory'], args.corpus_directory)) if conf['version'] != __version__: logger.debug('Previous run was on {} version (new run: {})'.format( conf['version'], __version__)) if conf['dictionary_path'] != args.dictionary_path: logger.debug('Previous run used dictionary path {} ' '(new run: {})'.format(conf['dictionary_path'], args.dictionary_path)) if conf['acoustic_model_path'] != args.acoustic_model_path: logger.debug('Previous run used acoustic model path {} ' '(new run: {})'.format(conf['acoustic_model_path'], args.acoustic_model_path)) os.makedirs(data_directory, exist_ok=True) try: begin = time.time() corpus = AlignableCorpus(args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs, debug=getattr(args, 'debug', False), logger=logger, use_mp=align_config.use_mp) acoustic_model = AcousticModel(args.acoustic_model_path) dictionary = Dictionary(args.dictionary_path, data_directory, word_set=corpus.word_set, logger=logger) acoustic_model.validate(dictionary) a = PretrainedAligner(corpus, dictionary, acoustic_model, align_config, temp_directory=data_directory, logger=logger) logger.debug( 'Setup pretrained aligner in {} seconds'.format(time.time() - begin)) a.verbose = args.verbose begin = time.time() a.align() logger.debug('Performed alignment in {} seconds'.format(time.time() - begin)) for identifier, trainer in train_config.items(): trainer.logger = logger if identifier != 'ivector': continue begin = time.time() trainer.init_training(identifier, data_directory, corpus, dictionary, a) trainer.train(call_back=print) logger.debug('Training took {} seconds'.format(time.time() - begin)) trainer.save(args.output_model_path) logger.info('All done!') logger.debug('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as e: conf['dirty'] = True raise e finally: handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler) with open(conf_path, 'w') as f: yaml.dump(conf, f)
def classify_speakers(args): command = 'classify_speakers' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) data_directory = os.path.join(temp_dir, corpus_name) conf_path = os.path.join(data_directory, 'config.yml') if args.config_path: classification_config = classification_yaml_to_config(args.config_path) else: classification_config = load_basic_classification() classification_config.use_mp = not args.disable_mp if getattr(args, 'clean', False) and os.path.exists(data_directory): print('Cleaning old directory!') shutil.rmtree(data_directory, ignore_errors=True) logger = setup_logger(command, data_directory) if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = {'dirty': False, 'begin': time.time(), 'version': __version__, 'type': command, 'corpus_directory': args.corpus_directory, 'ivector_extractor_path': args.ivector_extractor_path} if conf['dirty'] or conf['type'] != command \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__: logger.warning( 'WARNING: Using old temp directory, this might not be ideal for you, use the --clean flag to ensure no ' 'weird behavior for previous versions of the temporary directory.') if conf['dirty']: logger.debug('Previous run ended in an error (maybe ctrl-c?)') if conf['type'] != command: logger.debug('Previous run was a different subcommand than {} (was {})'.format(command, conf['type'])) if conf['corpus_directory'] != args.corpus_directory: logger.debug('Previous run used source directory ' 'path {} (new run: {})'.format(conf['corpus_directory'], args.corpus_directory)) if conf['version'] != __version__: logger.debug('Previous run was on {} version (new run: {})'.format(conf['version'], __version__)) if conf['ivector_extractor_path'] != args.ivector_extractor_path: logger.debug('Previous run used ivector extractor path {} ' '(new run: {})'.format(conf['ivector_extractor_path'], args.ivector_extractor_path)) os.makedirs(data_directory, exist_ok=True) os.makedirs(args.output_directory, exist_ok=True) try: corpus = TranscribeCorpus(args.corpus_directory, data_directory, num_jobs=args.num_jobs, logger=logger, use_mp=classification_config.use_mp) ivector_extractor = IvectorExtractor(args.ivector_extractor_path, root_directory=data_directory) begin = time.time() a = SpeakerClassifier(corpus, ivector_extractor, classification_config, temp_directory=data_directory, debug=getattr(args, 'debug', False), logger=logger, num_speakers=args.num_speakers, cluster=args.cluster) logger.debug('Setup speaker classifier in {} seconds'.format(time.time() - begin)) a.verbose = args.verbose begin = time.time() a.classify() logger.debug('Performed classification in {} seconds'.format(time.time() - begin)) begin = time.time() a.export_classification(args.output_directory) logger.debug('Exported classification in {} seconds'.format(time.time() - begin)) logger.info('Done!') logger.debug('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as _: conf['dirty'] = True raise finally: handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler) with open(conf_path, 'w') as f: yaml.dump(conf, f)
def train_dictionary(args): command = 'train_dictionary' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) data_directory = os.path.join(temp_dir, corpus_name) conf_path = os.path.join(data_directory, 'config.yml') if args.config_path: align_config = align_yaml_to_config(args.config_path) else: align_config = load_basic_align() if getattr(args, 'clean', False) and os.path.exists(data_directory): print('Cleaning old directory!') shutil.rmtree(data_directory, ignore_errors=True) logger = setup_logger(command, data_directory) if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = {'dirty': False, 'begin': time.time(), 'version': __version__, 'type': command, 'corpus_directory': args.corpus_directory, 'dictionary_path': args.dictionary_path, 'acoustic_model_path': args.acoustic_model_path } if conf['dirty'] or conf['type'] != command \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__ \ or conf['dictionary_path'] != args.dictionary_path: logger.warning( 'WARNING: Using old temp directory, this might not be ideal for you, use the --clean flag to ensure no ' 'weird behavior for previous versions of the temporary directory.') if conf['dirty']: logger.debug('Previous run ended in an error (maybe ctrl-c?)') if conf['type'] != command: logger.debug('Previous run was a different subcommand than {} (was {})'.format(command, conf['type'])) if conf['corpus_directory'] != args.corpus_directory: logger.debug('Previous run used source directory ' 'path {} (new run: {})'.format(conf['corpus_directory'], args.corpus_directory)) if conf['version'] != __version__: logger.debug('Previous run was on {} version (new run: {})'.format(conf['version'], __version__)) if conf['dictionary_path'] != args.dictionary_path: logger.debug('Previous run used dictionary path {} ' '(new run: {})'.format(conf['dictionary_path'], args.dictionary_path)) if conf['acoustic_model_path'] != args.acoustic_model_path: logger.debug('Previous run used acoustic model path {} ' '(new run: {})'.format(conf['acoustic_model_path'], args.acoustic_model_path)) os.makedirs(data_directory, exist_ok=True) try: corpus = AlignableCorpus(args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs, use_mp=align_config.use_mp, logger=logger) if corpus.issues_check: logger.warning('WARNING: Some issues parsing the corpus were detected. ' 'Please run the validator to get more information.') logger.info(corpus.speaker_utterance_info()) acoustic_model = AcousticModel(args.acoustic_model_path) dictionary = Dictionary(args.dictionary_path, data_directory, word_set=corpus.word_set, logger=logger) acoustic_model.validate(dictionary) begin = time.time() a = PretrainedAligner(corpus, dictionary, acoustic_model, align_config, temp_directory=data_directory, debug=getattr(args, 'debug', False), logger=logger) logger.debug('Setup pretrained aligner in {} seconds'.format(time.time() - begin)) a.verbose = args.verbose begin = time.time() a.align() logger.debug('Performed alignment in {} seconds'.format(time.time() - begin)) a.generate_pronunciations(args.output_directory) print('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as _: conf['dirty'] = True raise finally: with open(conf_path, 'w') as f: yaml.dump(conf, f)
def align_corpus(args, unknown_args=None): command = 'train_and_align' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) data_directory = os.path.join(temp_dir, corpus_name) logger = setup_logger(command, data_directory) if args.config_path: train_config, align_config = train_yaml_to_config(args.config_path) else: train_config, align_config = load_basic_train() if unknown_args: align_config.update_from_args(unknown_args) conf_path = os.path.join(data_directory, 'config.yml') if args.debug: logger.warning( 'Running in DEBUG mode, may have impact on performance and disk usage.' ) if getattr(args, 'clean', False) and os.path.exists(data_directory): logger.info('Cleaning old directory!') shutil.rmtree(data_directory, ignore_errors=True) if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = { 'dirty': False, 'begin': time.time(), 'version': __version__, 'type': command, 'corpus_directory': args.corpus_directory, 'dictionary_path': args.dictionary_path } if conf['dirty'] or conf['type'] != command \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__ \ or conf['dictionary_path'] != args.dictionary_path: logger.warning( 'WARNING: Using old temp directory, this might not be ideal for you, use the --clean flag to ensure no ' 'weird behavior for previous versions of the temporary directory.') if conf['dirty']: logger.debug('Previous run ended in an error (maybe ctrl-c?)') if conf['type'] != command: logger.debug( 'Previous run was a different subcommand than {} (was {})'. format(command, conf['type'])) if conf['corpus_directory'] != args.corpus_directory: logger.debug('Previous run used source directory ' 'path {} (new run: {})'.format( conf['corpus_directory'], args.corpus_directory)) if conf['version'] != __version__: logger.debug('Previous run was on {} version (new run: {})'.format( conf['version'], __version__)) if conf['dictionary_path'] != args.dictionary_path: logger.debug('Previous run used dictionary path {} ' '(new run: {})'.format(conf['dictionary_path'], args.dictionary_path)) os.makedirs(data_directory, exist_ok=True) os.makedirs(args.output_directory, exist_ok=True) try: corpus = AlignableCorpus(args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=getattr(args, 'num_jobs', 3), debug=getattr(args, 'debug', False), logger=logger, use_mp=align_config.use_mp) if corpus.issues_check: logger.warning('Some issues parsing the corpus were detected. ' 'Please run the validator to get more information.') logger.info(corpus.speaker_utterance_info()) dictionary = Dictionary(args.dictionary_path, data_directory, word_set=corpus.word_set, logger=logger) utt_oov_path = os.path.join(corpus.split_directory(), 'utterance_oovs.txt') if os.path.exists(utt_oov_path): shutil.copy(utt_oov_path, args.output_directory) oov_path = os.path.join(corpus.split_directory(), 'oovs_found.txt') if os.path.exists(oov_path): shutil.copy(oov_path, args.output_directory) a = TrainableAligner(corpus, dictionary, train_config, align_config, temp_directory=data_directory, logger=logger, debug=getattr(args, 'debug', False)) a.verbose = args.verbose begin = time.time() a.train() logger.debug('Training took {} seconds'.format(time.time() - begin)) a.export_textgrids(args.output_directory) if args.output_model_path is not None: a.save(args.output_model_path) logger.info('All done!') logger.debug('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as _: conf['dirty'] = True raise finally: handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler) with open(conf_path, 'w') as f: yaml.dump(conf, f)