From 48224a7e3c6d812e3e98afb58ab731708c12ae32 Mon Sep 17 00:00:00 2001 From: RangeKing Date: Mon, 4 Sep 2023 21:43:42 +0800 Subject: [PATCH] Fix potential error of `close` method --- mmengine/visualization/vis_backend.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mmengine/visualization/vis_backend.py b/mmengine/visualization/vis_backend.py index 914c6a9cf5..3c7548fb17 100644 --- a/mmengine/visualization/vis_backend.py +++ b/mmengine/visualization/vis_backend.py @@ -1280,16 +1280,18 @@ def close(self) -> None: if not hasattr(self, '_dvclive'): return - file_paths = dict() - for filename in scandir(self.cfg.work_dir, self._artifact_suffix, - True): - file_path = osp.join(self.cfg.work_dir, filename) - relative_path = os.path.relpath(file_path, self.cfg.work_dir) - dir_path = os.path.dirname(relative_path) - file_paths[file_path] = dir_path + if (hasattr(self, 'cfg') + and osp.isdir(getattr(self.cfg, 'work_dir', ''))): + file_paths = dict() + for filename in scandir(self.cfg.work_dir, self._artifact_suffix, + True): + file_path = osp.join(self.cfg.work_dir, filename) + relative_path = os.path.relpath(file_path, self.cfg.work_dir) + dir_path = os.path.dirname(relative_path) + file_paths[file_path] = dir_path - for file_path, dir_path in file_paths.items(): - self._dvclive.log_artifact(file_path, dir_path) + for file_path, dir_path in file_paths.items(): + self._dvclive.log_artifact(file_path, dir_path) self._dvclive.end()