-
Notifications
You must be signed in to change notification settings - Fork 53
/
files.py
executable file
·151 lines (136 loc) · 5.59 KB
/
files.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import glob
import torch
def xmkdir(path):
"""Create directory PATH recursively if it does not exist."""
if path is not None and not os.path.exists(path):
os.makedirs(path)
def get_model_device(model):
return next(model.parameters()).device
def save_model(model, model_path):
"""Save the model state dictionary to the PTH file model_path."""
if model_path is not None:
xmkdir(os.path.dirname(model_path))
torch.save(model.state_dict(), model_path)
def load_checkpoint(checkpoint_dir, model, optimizer=None):
"""Search the latest checkpoint in checkpoint_dir and load the model and optimizer and return the metrics."""
names = list(sorted(
glob.glob(os.path.join(checkpoint_dir, 'checkpoint*.pth'))
))
if len(names) == 0:
return 0, {'train': [], 'val': []}
print(f"Loading checkpoint '{names[-1]}'")
cp = torch.load(names[-1], map_location=str(get_model_device(model)))
epoch = cp['epoch']
metrics = cp['metrics']
if model:
model.load_state_dict(cp['model'])
if optimizer:
optimizer.load_state_dict(cp['optimizer'])
return epoch, metrics
def clean_checkpoint(checkpoint_dir, lowest=False):
if lowest:
names = list(sorted(
glob.glob(os.path.join(checkpoint_dir, 'lowest*.pth'))
))
else:
names = list(sorted(
glob.glob(os.path.join(checkpoint_dir, 'checkpoint*.pth'))
))
if len(names) > 2:
for name in names[0:-2]:
print(f"Deleting redundant checkpoint file {name}")
os.remove(name)
def save_checkpoint(checkpoint_dir, model, optimizer, metrics, epoch, defsave=False):
"""Save model, optimizer, and metrics state to a checkpoint in checkpoint_dir
for the specified epoch. If checkpoint_dir is None it does not do anything."""
if checkpoint_dir is not None:
if model:
xmkdir(checkpoint_dir)
if (epoch % 50 == 0) or defsave:
name = os.path.join(checkpoint_dir, f'ckpt{epoch:08}.pth')
else:
name = os.path.join(checkpoint_dir, f'checkpoint{epoch:08}.pth')
torch.save({
'epoch': epoch + 1,
'metrics': metrics,
'model': model.state_dict(),
'optimizer' : optimizer.state_dict(),
}, name)
clean_checkpoint(checkpoint_dir)
else:
xmkdir(checkpoint_dir)
if (epoch % 50 == 0) or defsave:
name = os.path.join(checkpoint_dir, f'ckpt{epoch:08}.pth')
else:
name = os.path.join(checkpoint_dir, f'checkpoint{epoch:08}.pth')
torch.save({
'epoch': epoch + 1,
'metrics': metrics,
'optimizer': optimizer.state_dict(),
}, name)
clean_checkpoint(checkpoint_dir)
def save_checkpoint_all(checkpoint_dir, model, arch, opt, L, epoch, lowest=False, save_str=''):
"""Save model, optimizer, and metrics state to a checkpoint in
checkpoint_dir for the specified epoch. If checkpoint_dir is None it does not do anything."""
if checkpoint_dir is not None:
if model:
xmkdir(checkpoint_dir)
if (epoch % 50 == 0) or (save_str != ''):
name = os.path.join(checkpoint_dir, f'ckpt{epoch:03}.pth')
else:
name = os.path.join(checkpoint_dir, f'checkpoint{epoch:03}.pth')
if lowest:
name = os.path.join(checkpoint_dir, f'lowest_{epoch:03}.pth')
torch.save({
'epoch': epoch + 1,
'arch': arch,
'state_dict': model.state_dict(),
'optimizer': opt.state_dict(),
'L': L,
}, name)
clean_checkpoint(checkpoint_dir, lowest=lowest)
else:
xmkdir(checkpoint_dir)
if epoch % 50 == 0:
name = os.path.join(checkpoint_dir, f'ckpt{epoch:03}.pth')
else:
name = os.path.join(checkpoint_dir, f'checkpoint{epoch:03}.pth')
if lowest:
name = os.path.join(checkpoint_dir, f'lowest.pth')
torch.save({
'epoch': epoch + 1,
'arch': arch,
'optimizer': opt.state_dict(),
'L': L,
}, name)
clean_checkpoint(checkpoint_dir, lowest=lowest)
def load_checkpoint_all(checkpoint_dir, model, opt):
"""Search the latest checkpoint in checkpoint_dir and load the model and optimizer and return the metrics."""
names = list(sorted(
glob.glob(os.path.join(checkpoint_dir, 'checkpoint*.pth'))
))
if len(names) == 0:
return [], 0
print(f"Loading checkpoint '{names[-1]}'")
if model:
cp = torch.load(names[-1], map_location=str(get_model_device(model)))
else:
cp = torch.load(names[-1], map_location=str('cpu'))
epoch = cp['epoch']
if opt:
opt.load_state_dict(cp['optimizer'])
L = cp['L']
if model:
model_parallel = 'module' in list(model.state_dict().keys())[0]
if ('module' in list(cp['state_dict'].keys())[0]) and not model_parallel:
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in cp['state_dict'].items():
name = k.replace('module.', '') # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
print('loaded from parallel to single!',flush=True)
else:
model.load_state_dict(cp['state_dict'])
return L, epoch