From 9681f052a165784b4194cba5d4712757fe540e6f Mon Sep 17 00:00:00 2001 From: Jiahao Li Date: Wed, 16 Nov 2022 22:51:04 +0800 Subject: [PATCH] Fix result saving errors of pytorch examples (#20276) --- .../run_image_classification_no_trainer.py | 6 +++--- .../language-modeling/run_clm_no_trainer.py | 4 ++-- .../language-modeling/run_mlm_no_trainer.py | 4 ++-- .../pytorch/multiple-choice/run_swag_no_trainer.py | 10 ++++++---- .../run_semantic_segmentation_no_trainer.py | 3 ++- .../summarization/run_summarization_no_trainer.py | 14 ++++---------- .../text-classification/run_glue_no_trainer.py | 3 ++- .../token-classification/run_ner_no_trainer.py | 9 +++++---- 8 files changed, 26 insertions(+), 27 deletions(-) diff --git a/examples/pytorch/image-classification/run_image_classification_no_trainer.py b/examples/pytorch/image-classification/run_image_classification_no_trainer.py index 4f0b8304b7..0dfc5b7625 100644 --- a/examples/pytorch/image-classification/run_image_classification_no_trainer.py +++ b/examples/pytorch/image-classification/run_image_classification_no_trainer.py @@ -571,9 +571,9 @@ def main(): if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) - if args.output_dir is not None: - with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump({"eval_accuracy": eval_metric["accuracy"]}, f) + all_results = {f"eval_{k}": v for k, v in eval_metric.items()} + with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: + json.dump(all_results, f) if __name__ == "__main__": diff --git a/examples/pytorch/language-modeling/run_clm_no_trainer.py b/examples/pytorch/language-modeling/run_clm_no_trainer.py index 77785b711e..e296a37151 100755 --- a/examples/pytorch/language-modeling/run_clm_no_trainer.py +++ b/examples/pytorch/language-modeling/run_clm_no_trainer.py @@ -666,8 +666,8 @@ def main(): if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) - with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump({"perplexity": perplexity}, f) + with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: + json.dump({"perplexity": perplexity}, f) if __name__ == "__main__": diff --git a/examples/pytorch/language-modeling/run_mlm_no_trainer.py b/examples/pytorch/language-modeling/run_mlm_no_trainer.py index fafa21c4ca..f4d5004ffb 100755 --- a/examples/pytorch/language-modeling/run_mlm_no_trainer.py +++ b/examples/pytorch/language-modeling/run_mlm_no_trainer.py @@ -711,8 +711,8 @@ def main(): if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) - with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump({"perplexity": perplexity}, f) + with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: + json.dump({"perplexity": perplexity}, f) if __name__ == "__main__": diff --git a/examples/pytorch/multiple-choice/run_swag_no_trainer.py b/examples/pytorch/multiple-choice/run_swag_no_trainer.py index 1093e92bd5..5fa5a2ba00 100755 --- a/examples/pytorch/multiple-choice/run_swag_no_trainer.py +++ b/examples/pytorch/multiple-choice/run_swag_no_trainer.py @@ -85,7 +85,7 @@ def parse_args(): "--validation_file", type=str, default=None, help="A csv or a json file containing the validation data." ) parser.add_argument( - "--max_length", + "--max_seq_length", type=int, default=128, help=( @@ -424,7 +424,7 @@ def main(): tokenized_examples = tokenizer( first_sentences, second_sentences, - max_length=args.max_length, + max_length=args.max_seq_length, padding=padding, truncation=True, ) @@ -654,8 +654,10 @@ def main(): tokenizer.save_pretrained(args.output_dir) if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) - with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump({"eval_accuracy": eval_metric["accuracy"]}, f) + + all_results = {f"eval_{k}": v for k, v in eval_metric.items()} + with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: + json.dump(all_results, f) if __name__ == "__main__": diff --git a/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py b/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py index ce2e58b85a..f8ea5081b2 100644 --- a/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py +++ b/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py @@ -681,8 +681,9 @@ def main(): if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) + all_results = {f"eval_{k}": v for k, v in eval_metrics.items()} with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump({"eval_overall_accuracy": eval_metrics["overall_accuracy"]}, f) + json.dump(all_results, f) if __name__ == "__main__": diff --git a/examples/pytorch/summarization/run_summarization_no_trainer.py b/examples/pytorch/summarization/run_summarization_no_trainer.py index 786af5d91a..383143a6ef 100644 --- a/examples/pytorch/summarization/run_summarization_no_trainer.py +++ b/examples/pytorch/summarization/run_summarization_no_trainer.py @@ -747,16 +747,10 @@ def main(): tokenizer.save_pretrained(args.output_dir) if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) - with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump( - { - "eval_rouge1": result["rouge1"], - "eval_rouge2": result["rouge2"], - "eval_rougeL": result["rougeL"], - "eval_rougeLsum": result["rougeLsum"], - }, - f, - ) + + all_results = {f"eval_{k}": v for k, v in result.items()} + with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: + json.dump(all_results, f) if __name__ == "__main__": diff --git a/examples/pytorch/text-classification/run_glue_no_trainer.py b/examples/pytorch/text-classification/run_glue_no_trainer.py index 2aacc4d2de..7e8c1cbdb6 100644 --- a/examples/pytorch/text-classification/run_glue_no_trainer.py +++ b/examples/pytorch/text-classification/run_glue_no_trainer.py @@ -625,8 +625,9 @@ def main(): logger.info(f"mnli-mm: {eval_metric}") if args.output_dir is not None: + all_results = {f"eval_{k}": v for k, v in eval_metric.items()} with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump({"eval_accuracy": eval_metric["accuracy"]}, f) + json.dump(all_results, f) if __name__ == "__main__": diff --git a/examples/pytorch/token-classification/run_ner_no_trainer.py b/examples/pytorch/token-classification/run_ner_no_trainer.py index 02bc773fed..64b045238f 100755 --- a/examples/pytorch/token-classification/run_ner_no_trainer.py +++ b/examples/pytorch/token-classification/run_ner_no_trainer.py @@ -766,10 +766,11 @@ def main(): if args.push_to_hub: repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True) - with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: - json.dump( - {"eval_accuracy": eval_metric["accuracy"], "train_loss": total_loss.item() / len(train_dataloader)}, f - ) + all_results = {f"eval_{k}": v for k, v in eval_metric.items()} + if args.with_tracking: + all_results.update({"train_loss": total_loss.item() / len(train_dataloader)}) + with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: + json.dump(all_results, f) if __name__ == "__main__":