From 39afb754bc2add2f26b9e17713fa7afbc21acd84 Mon Sep 17 00:00:00 2001 From: ydcjeff <32727188+ydcjeff@users.noreply.github.com> Date: Mon, 18 Oct 2021 21:19:59 +0630 Subject: [PATCH 1/5] feat(hash_checkpoint): create the directory if not exist --- ignite/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ignite/utils.py b/ignite/utils.py index fba790e9c39..e68099bae58 100644 --- a/ignite/utils.py +++ b/ignite/utils.py @@ -293,9 +293,10 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa Args: checkpoint_path: Path to the checkpoint file. output_dir: Output directory to store the hashed checkpoint file. + (will be created if not exist) Returns: - Path to the hashed checkpoint file, The 8 digits of SHA256 hash. + Path to the hashed checkpoint file, The first 8 digits of SHA256 hash. .. versionadded:: 0.5.0 """ @@ -305,6 +306,7 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa if isinstance(output_dir, str): output_dir = Path(output_dir) + output_dir.mkdir(parents=True, exist_ok=True) sha_hash = hashlib.sha256(checkpoint_path.read_bytes()).hexdigest() old_filename = checkpoint_path.stem From c5f7eb5cc86095f5bd7f41f0f6db108781ed8198 Mon Sep 17 00:00:00 2001 From: Jeff Yang <32727188+ydcjeff@users.noreply.github.com> Date: Mon, 18 Oct 2021 21:30:29 +0630 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: vfdev --- ignite/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ignite/utils.py b/ignite/utils.py index e68099bae58..249b0c0edf3 100644 --- a/ignite/utils.py +++ b/ignite/utils.py @@ -292,11 +292,11 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa Args: checkpoint_path: Path to the checkpoint file. - output_dir: Output directory to store the hashed checkpoint file. - (will be created if not exist) + output_dir: Output directory to store the hashed checkpoint file + (will be created if not exist). Returns: - Path to the hashed checkpoint file, The first 8 digits of SHA256 hash. + Path to the hashed checkpoint file, the first 8 digits of SHA256 hash. .. versionadded:: 0.5.0 """ From 3a599c8f87760d4127e3af83d6aba0f976b9127b Mon Sep 17 00:00:00 2001 From: ydcjeff <32727188+ydcjeff@users.noreply.github.com> Date: Mon, 18 Oct 2021 21:48:16 +0630 Subject: [PATCH 3/5] fix: check for checkpoint file existence --- ignite/utils.py | 3 +++ tests/ignite/test_utils.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/ignite/utils.py b/ignite/utils.py index e68099bae58..7dba5a5744a 100644 --- a/ignite/utils.py +++ b/ignite/utils.py @@ -304,6 +304,9 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa if isinstance(checkpoint_path, str): checkpoint_path = Path(checkpoint_path) + if not checkpoint_path.exists(): + raise FileNotFoundError(f'{checkpoint_path.name} does not exist in {checkpoint_path.parent}.') + if isinstance(output_dir, str): output_dir = Path(output_dir) output_dir.mkdir(parents=True, exist_ok=True) diff --git a/tests/ignite/test_utils.py b/tests/ignite/test_utils.py index d5ed7bd128f..eebea75b17a 100644 --- a/tests/ignite/test_utils.py +++ b/tests/ignite/test_utils.py @@ -256,3 +256,7 @@ def test_hash_checkpoint(tmp_path): model.load_state_dict(torch.load(hash_checkpoint_path), True) assert sha_hash[:8] == "b66bff10" assert hash_checkpoint_path.name == f"squeezenet1_0-{sha_hash[:8]}.pt" + + # test non-existent checkpoint_path + with pytest.raises(FileNotFoundError, match=rf'not_found.pt does not exist in {tmp_path}*'): + hash_checkpoint(f'{tmp_path}/not_found.pt', tmp_path) From bd4e7a6b3701461748acb9c5f930ae7160aac4fb Mon Sep 17 00:00:00 2001 From: ydcjeff <32727188+ydcjeff@users.noreply.github.com> Date: Mon, 18 Oct 2021 21:49:19 +0630 Subject: [PATCH 4/5] chore: format --- ignite/utils.py | 2 +- tests/ignite/test_utils.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ignite/utils.py b/ignite/utils.py index 107065539b2..442b986d5b2 100644 --- a/ignite/utils.py +++ b/ignite/utils.py @@ -305,7 +305,7 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa checkpoint_path = Path(checkpoint_path) if not checkpoint_path.exists(): - raise FileNotFoundError(f'{checkpoint_path.name} does not exist in {checkpoint_path.parent}.') + raise FileNotFoundError(f"{checkpoint_path.name} does not exist in {checkpoint_path.parent}.") if isinstance(output_dir, str): output_dir = Path(output_dir) diff --git a/tests/ignite/test_utils.py b/tests/ignite/test_utils.py index eebea75b17a..b31691c2440 100644 --- a/tests/ignite/test_utils.py +++ b/tests/ignite/test_utils.py @@ -258,5 +258,5 @@ def test_hash_checkpoint(tmp_path): assert hash_checkpoint_path.name == f"squeezenet1_0-{sha_hash[:8]}.pt" # test non-existent checkpoint_path - with pytest.raises(FileNotFoundError, match=rf'not_found.pt does not exist in {tmp_path}*'): - hash_checkpoint(f'{tmp_path}/not_found.pt', tmp_path) + with pytest.raises(FileNotFoundError, match=rf"not_found.pt does not exist in {tmp_path}*"): + hash_checkpoint(f"{tmp_path}/not_found.pt", tmp_path) From b9ae3afddff35b1528c26ac1c851dbe37b75038c Mon Sep 17 00:00:00 2001 From: ydcjeff <32727188+ydcjeff@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:16:09 +0630 Subject: [PATCH 5/5] fix: fix test for windows --- tests/ignite/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ignite/test_utils.py b/tests/ignite/test_utils.py index b31691c2440..308b12d2502 100644 --- a/tests/ignite/test_utils.py +++ b/tests/ignite/test_utils.py @@ -258,5 +258,5 @@ def test_hash_checkpoint(tmp_path): assert hash_checkpoint_path.name == f"squeezenet1_0-{sha_hash[:8]}.pt" # test non-existent checkpoint_path - with pytest.raises(FileNotFoundError, match=rf"not_found.pt does not exist in {tmp_path}*"): + with pytest.raises(FileNotFoundError, match=r"not_found.pt does not exist in *"): hash_checkpoint(f"{tmp_path}/not_found.pt", tmp_path)