-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpointer.py
More file actions
43 lines (34 loc) · 1.31 KB
/
checkpointer.py
File metadata and controls
43 lines (34 loc) · 1.31 KB
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
import torch
import os
def save_checkpoint(epoch, model, optimizer, ckpt_dir):
if not os.path.isdir(ckpt_dir):
os.makedirs(ckpt_dir)
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}, os.path.join(ckpt_dir, 'checkpoint_%04d.pt' % epoch))
def load_checkpoint(ckpt_dir):
"""
start_epoch = 0
checkpoint = load_checkpoint(self.cfg.paths.ckpt_in_dir)
if checkpoint is not None:
start_epoch = checkpoint['epoch'] + 1
scheduler.last_epoch = checkpoint['epoch']
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
for g in optimizer.param_groups:
g['lr'] = self.cfg.optim.base_lr
model.load_state_dict(checkpoint['model_state_dict'])
"""
if not os.path.isdir(ckpt_dir):
print('Checkpoint directory not found. No checkpoints loaded from ', ckpt_dir)
return None
files = os.listdir(ckpt_dir)
files = list(filter(lambda x: x.startswith('checkpoint'), files))
if len(files) == 0:
print('No checkpoint found in ', ckpt_dir)
return None
files = sorted(files, reverse=True)
latest_fp = os.path.join(ckpt_dir, files[0])
print('Loading checkpoint from ' + latest_fp)
return torch.load(latest_fp)