forked from esti28/indoorInverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_up_ckpts.py
58 lines (46 loc) · 2.02 KB
/
clean_up_ckpts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from pathlib import Path
from tqdm import tqdm
from icecream import ic
import shutil
folders = ['Checkpoint',]
ckpt_remove_list = []
ckpt_keep_list = []
for folder in folders:
log_paths = Path(folder).iterdir()
# print([x.name for x in log_paths])
for log_path in log_paths:
task_name = log_path.name
latest_ckpt_path = log_path / 'last_checkpoint'
if not latest_ckpt_path.exists():
continue
with open(latest_ckpt_path) as f:
last_ckpt_path = f.read().splitlines()[0]
last_ckpt_file_name = last_ckpt_path.split('/')[1]
latest_ckpt_path = log_path / last_ckpt_file_name
ckpts = [str(x) for x in Path(log_path).iterdir() if 'checkpointer_' in str(x)]
assert str(latest_ckpt_path) in ckpts
for ckpt in ckpts:
if str(latest_ckpt_path) != ckpt:
print('Delete ' + ckpt)
ckpt_remove_list.append(Path(ckpt))
else:
print('Keep ' + ckpt)
ckpt_keep_list.append(Path(ckpt))
# assert latest_ckpt_path in ckpts
if_delete = input('%d ckpts to keep and %d to remove. Confirm? [y/n]'%(len(ckpt_keep_list), len(ckpt_remove_list)))
if if_delete == 'y':
for ckpt in tqdm(ckpt_remove_list):
ckpt.unlink()
# if task_name.endswith('--tmp') or task_name.endswith('--tmp_new'):
# shutil.rmtree(log_path, ignore_errors=True)
# print('Removed '+str(log_path))
# continue
# for task_datetime in mylist:
# if len(task_datetime.split('-'))==2:
# restore_task_datetime = task_datetime
# elif len(task_datetime.split('-'))==6:
# restore_task_datetime = '-'.join([task_datetime.split('-')[3].replace('gpu', ''), task_datetime.split('-')[4]])
# if task_name.startswith(restore_task_datetime) :
# # Path(log_path).unlink()
# shutil.rmtree(log_path, ignore_errors=True)
# print('Removed '+str(log_path))