|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +import os.path as osp |
| 3 | +import tempfile |
| 4 | + |
| 5 | +from mmseg.utils import find_latest_checkpoint |
| 6 | + |
| 7 | + |
| 8 | +def test_find_latest_checkpoint(): |
| 9 | + with tempfile.TemporaryDirectory() as tempdir: |
| 10 | + # no checkpoints in the path |
| 11 | + path = tempdir |
| 12 | + latest = find_latest_checkpoint(path) |
| 13 | + assert latest is None |
| 14 | + |
| 15 | + # The path doesn't exist |
| 16 | + path = osp.join(tempdir, 'none') |
| 17 | + latest = find_latest_checkpoint(path) |
| 18 | + assert latest is None |
| 19 | + |
| 20 | + # test when latest.pth exists |
| 21 | + with tempfile.TemporaryDirectory() as tempdir: |
| 22 | + with open(osp.join(tempdir, 'latest.pth'), 'w') as f: |
| 23 | + f.write('latest') |
| 24 | + path = tempdir |
| 25 | + latest = find_latest_checkpoint(path) |
| 26 | + assert latest == osp.join(tempdir, 'latest.pth') |
| 27 | + |
| 28 | + with tempfile.TemporaryDirectory() as tempdir: |
| 29 | + for iter in range(1600, 160001, 1600): |
| 30 | + with open(osp.join(tempdir, f'iter_{iter}.pth'), 'w') as f: |
| 31 | + f.write(f'iter_{iter}.pth') |
| 32 | + latest = find_latest_checkpoint(tempdir) |
| 33 | + assert latest == osp.join(tempdir, 'iter_160000.pth') |
| 34 | + |
| 35 | + with tempfile.TemporaryDirectory() as tempdir: |
| 36 | + for epoch in range(1, 21): |
| 37 | + with open(osp.join(tempdir, f'epoch_{epoch}.pth'), 'w') as f: |
| 38 | + f.write(f'epoch_{epoch}.pth') |
| 39 | + latest = find_latest_checkpoint(tempdir) |
| 40 | + assert latest == osp.join(tempdir, 'epoch_20.pth') |
0 commit comments