def on_test_epoch_end(self, trainer, pl_module): results = { remove_postfix(k, '_epoch'): v for k, v in trainer.logged_metrics.items() if k.startswith('test_') and not k.endswith('_step') } tune.report(**results)
def align_text_fn(item, score_threshold, debug=False): audio_path, recognition_text = item #print("audio_path=",audio_path) audio_dir = os.path.dirname(audio_path) base_dir = os.path.dirname(audio_dir) news_path = remove_postfix(audio_path.replace("audio", "assets")) news_path = os.path.splitext(news_path)[0] + ".txt" strip_fn = lambda line: line.strip().replace('"', '').replace("'", "") #candidates = [strip_fn(line) for line in open(news_path, encoding='cp949').readlines()] candidates = [ strip_fn(line) for line in open(news_path, encoding='utf-8').readlines() ] scores = { candidate: similarity(candidate, recognition_text) \ for candidate in candidates} sorted_scores = sorted(scores.items(), key=operator.itemgetter(1))[::-1] #jck add if len(sorted_scores) < 2: print("===========================================================") print(">> error : candidates less than 2, news_path=[", news_path, "]") print("===========================================================") first, second = sorted_scores[0], sorted_scores[1] if first[1] > second[1] and first[1] >= score_threshold: found_text, score = first aligned_text = search_optimal(found_text, recognition_text) if debug: print(" ", audio_path) print(" ", recognition_text) print("=> ", found_text) print("==>", aligned_text) print("=" * 30) if aligned_text is not None: result = {audio_path: add_punctuation(aligned_text)} elif abs( len(text_to_sequence(found_text)) - len(text_to_sequence(recognition_text))) > 10: result = {} else: result = { audio_path: [add_punctuation(found_text), recognition_text] } else: result = {} if len(result) == 0: result = {audio_path: [recognition_text]} return result
def align_text_fn(item, score_threshold, debug=False): audio_path, recognition_text = item audio_name = os.path.splitext(os.path.basename(audio_path))[0] audio_dir = os.path.dirname(audio_path) base_dir = os.path.dirname(audio_dir) news_path = remove_postfix(audio_path.replace("wavs", "assets")) news_path = os.path.splitext(news_path)[0] + ".txt" strip_fn = lambda line: line.strip().replace('"', '').replace("'", "") candidates = [ strip_fn(line) for line in open(news_path, 'r', encoding='utf-8').readlines() ] scores = { candidate: similarity(candidate, recognition_text) \ for candidate in candidates} sorted_scores = sorted(scores.items(), key=operator.itemgetter(1))[::-1] first, second = sorted_scores[0], sorted_scores[1] aligned_text = "" if first[1] > second[1] and first[1] >= score_threshold: found_text, score = first aligned_text = search_optimal(found_text, recognition_text) if debug: print(" ", audio_path) print(" ", recognition_text) print("=> ", found_text) print("==>", aligned_text) print("=" * 30) if aligned_text is not None: aligned_text = add_punctuation(aligned_text) elif abs( len(text_to_sequence(found_text)) - len(text_to_sequence(recognition_text))) > 10: aligned_text = "" else: aligned_text = add_punctuation(found_text) else: aligned_text = "" if aligned_text == "": aligned_text = recognition_text # Normalization if debug: print("aligned ==>", aligned_text) aligned_text_noramlized = normalize(aligned_text) result = {audio_name: [aligned_text, aligned_text_noramlized]} return result
def on_epoch_end(self, trainer, pl_module): results = { remove_postfix(k, '_epoch'): v for k, v in trainer.logged_metrics.items() if (k.startswith('train_') or k.startswith('val_')) and not k.endswith('_step') } results['mean_loss'] = results.get('val_loss', results['train_loss']) if 'val_accuracy' in results: results['mean_accuracy'] = results['val_accuracy'] # Checkpointing should be done *before* reporting # https://docs.ray.io/en/master/tune/api_docs/trainable.html with tune.checkpoint_dir(step=trainer.current_epoch) as checkpoint_dir: trainer.save_checkpoint( os.path.join(checkpoint_dir, f"{type(pl_module).__name__}.ckpt")) tune.report(**results)
def write_mkdocs_yaml_file(docs_directory) -> bool: yml_directory = utils.remove_postfix(docs_directory, 'docs/') base_file = yml_directory + 'base.yml' mkdocs_file = yml_directory + 'mkdocs.yml' if os.path.isfile(base_file): shutil.copyfile(base_file, mkdocs_file) else: print(f'Could not find base.yml file in {yml_directory}.') return False try: with open(mkdocs_file, 'a') as file: ls = utils.yml_list_from_directory(docs_directory) if len(ls) > 0: file.writelines(ls) return True else: print('Call to "utils.yml_list_from_directory" failed.') return False except OSError: print(f'Could open file {mkdocs_file}.') return False
def align_text_fn(item, score_threshold, debug=False): audio_path, recognition_text = item audio_dir = os.path.dirname(audio_path) base_dir = os.path.dirname(audio_dir) news_path = remove_postfix(audio_path.replace("audio", "assets")) news_path = os.path.splitext(news_path)[0] + ".txt" strip_fn = lambda line: line.strip().replace('"', '').replace("'", "") candidates = [ strip_fn(line) for line in open(news_path, encoding='cp949').readlines() ] scores = { candidate: similarity(candidate, recognition_text) \ for candidate in candidates} sorted_scores = sorted(scores.items(), key=operator.itemgetter(1))[::-1] first, second = sorted_scores[0], sorted_scores[1] ''' if first[1] > second[1] and first[1] >= score_threshold: found_text, score = first aligned_text, align_score = search_optimal(found_text, recognition_text, by_word=True) ''' best_aligned_text = '' found_text = '' best_simscore = 0.0 for candidate, simscore in sorted_scores: aligned_text, simscore = search_optimal(candidate, recognition_text) if simscore > best_simscore: best_aligned_text = aligned_text best_simscore = simscore found_text = candidate if simscore == 1.0: break aligned_text = best_aligned_text if len(best_aligned_text) != 0 and best_simscore >= score_threshold: if debug: print(" ", audio_path) print(" ", recognition_text) print("=> ", found_text) print("==>", aligned_text) print("=" * 30) if aligned_text is not None: result = {audio_path: add_punctuation(aligned_text)} elif abs( len(text_to_sequence(found_text)) - len(text_to_sequence(recognition_text))) > 10: result = {} else: result = { audio_path: [add_punctuation(found_text), recognition_text] } else: result = {} if len(result) == 0: result = {audio_path: [recognition_text]} return result