-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Resume from the latest checkpoint automatically. (#61)
* support auto-resume * support auto-resume * support auto-resume * support auto-resume Co-authored-by: pppppM <67539920+pppppM@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from .misc import find_latest_checkpoint | ||
from .setup_env import setup_multi_processes | ||
|
||
__all__ = ['setup_multi_processes'] | ||
__all__ = ['find_latest_checkpoint', 'setup_multi_processes'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import glob | ||
import os.path as osp | ||
import warnings | ||
|
||
|
||
def find_latest_checkpoint(path, suffix='pth'): | ||
"""Find the latest checkpoint from the working directory. | ||
Args: | ||
path(str): The path to find checkpoints. | ||
suffix(str): File extension. Defaults to pth. | ||
Returns: | ||
latest_path(str | None): File path of the latest checkpoint. | ||
References: | ||
.. [1] https://github.com/microsoft/SoftTeacher | ||
/blob/main/ssod/utils/patch.py | ||
""" | ||
if not osp.exists(path): | ||
warnings.warn('The path of checkpoints does not exist.') | ||
return None | ||
if osp.exists(osp.join(path, f'latest.{suffix}')): | ||
return osp.join(path, f'latest.{suffix}') | ||
|
||
checkpoints = glob.glob(osp.join(path, f'*.{suffix}')) | ||
if len(checkpoints) == 0: | ||
warnings.warn('There are no checkpoints in the path.') | ||
return None | ||
latest = -1 | ||
latest_path = None | ||
for checkpoint in checkpoints: | ||
count = int(osp.basename(checkpoint).split('_')[-1].split('.')[0]) | ||
if count > latest: | ||
latest = count | ||
latest_path = checkpoint | ||
return latest_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import os.path as osp | ||
import tempfile | ||
|
||
from mmrazor.utils import find_latest_checkpoint | ||
|
||
|
||
def test_find_latest_checkpoint(): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
path = tmpdir | ||
latest = find_latest_checkpoint(path) | ||
# There are no checkpoints in the path. | ||
assert latest is None | ||
|
||
path = tmpdir + '/none' | ||
latest = find_latest_checkpoint(path) | ||
# The path does not exist. | ||
assert latest is None | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
with open(tmpdir + '/latest.pth', 'w') as f: | ||
f.write('latest') | ||
path = tmpdir | ||
latest = find_latest_checkpoint(path) | ||
assert latest == osp.join(tmpdir, 'latest.pth') | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
with open(tmpdir + '/iter_4000.pth', 'w') as f: | ||
f.write('iter_4000') | ||
with open(tmpdir + '/iter_8000.pth', 'w') as f: | ||
f.write('iter_8000') | ||
path = tmpdir | ||
latest = find_latest_checkpoint(path) | ||
assert latest == osp.join(tmpdir, 'iter_8000.pth') | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
with open(tmpdir + '/epoch_1.pth', 'w') as f: | ||
f.write('epoch_1') | ||
with open(tmpdir + '/epoch_2.pth', 'w') as f: | ||
f.write('epoch_2') | ||
path = tmpdir | ||
latest = find_latest_checkpoint(path) | ||
assert latest == osp.join(tmpdir, 'epoch_2.pth') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters