-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkpoint.py
36 lines (25 loc) · 1014 Bytes
/
checkpoint.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
import os
import pickle as pkl
import os.path as osp
import torch
from torch.utils.tensorboard import SummaryWriter
def save_checkpoint(current_it, model, logger, dire):
meta_dict = dict(
current_it=current_it
)
with open(osp.join(dire, 'meta.pkl'), 'wb') as f:
pkl.dump(meta_dict, f)
logger.flush()
torch.save(model.state_dict(), osp.join(dire, 'model.pth'))
print('*** checkpoint saved to directory: {}'.format(dire))
def load_checkpoint(dire, model, logger):
with open(osp.join(dire, 'meta.pkl'), 'rb') as f:
meta_dict = pkl.load(f)
current_it = meta_dict['current_it']
logger.close()
logger.summaryWriter = SummaryWriter(log_dir=logger.tb_dir, purge_step=current_it)
model_state_dict = torch.load(osp.join(dire, 'model.pth'))
model.load_state_dict(model_state_dict)
print('*** checkpoint loaded from directory: {}'.format(dire))
print('*** training restart from iteration: {}'.format(current_it))
return current_it