From 81c3de54b9727ffeabcfdc909b9806d25f247a22 Mon Sep 17 00:00:00 2001
From: Yuan Liu <30762564+YuanLiuuuuuu@users.noreply.github.com>
Date: Mon, 1 Aug 2022 13:20:08 +0800
Subject: [PATCH] [Fix]: Fix resume bug (#389)

* [Fix]: Fix resume bug

* [Fix]: Change last_checkpoint check logic

* [Fix]: Fix lint

* [Fix]: Change warning to print_log
---
 mmengine/runner/checkpoint.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/mmengine/runner/checkpoint.py b/mmengine/runner/checkpoint.py
index 058e20eb..89b2f3b0 100644
--- a/mmengine/runner/checkpoint.py
+++ b/mmengine/runner/checkpoint.py
@@ -8,7 +8,7 @@ import warnings
 from collections import OrderedDict
 from importlib import import_module
 from tempfile import TemporaryDirectory
-from typing import Callable, Dict
+from typing import Callable, Dict, Optional
 
 import torch
 import torchvision
@@ -17,6 +17,7 @@ import mmengine
 from mmengine.dist import get_dist_info
 from mmengine.fileio import FileClient
 from mmengine.fileio import load as load_file
+from mmengine.logging import print_log
 from mmengine.model import is_model_wrapper
 from mmengine.utils import load_url, mkdir_or_exist
 
@@ -697,7 +698,7 @@ def save_checkpoint(checkpoint, filename, file_client_args=None):
             file_client.put(f.getvalue(), filename)
 
 
-def find_latest_checkpoint(path: str):
+def find_latest_checkpoint(path: str) -> Optional[str]:
     """Find the latest checkpoint from the given path.
 
     Refer to https://github.com/facebookresearch/fvcore/blob/main/fvcore/common/checkpoint.py  # noqa: E501
@@ -709,12 +710,11 @@ def find_latest_checkpoint(path: str):
         str or None: File path of the latest checkpoint.
     """
     save_file = osp.join(path, 'last_checkpoint')
-    try:
+    last_saved: Optional[str]
+    if os.path.exists(save_file):
         with open(save_file) as f:
             last_saved = f.read().strip()
-    except OSError:
-        raise OSError(
-            'last_checkpoint file does not exist, maybe because it has just'
-            ' been deleted by a separate process')
-
+    else:
+        print_log('Did not find last_checkpoint to be resumed.')
+        last_saved = None
     return last_saved
-- 
GitLab