-
Notifications
You must be signed in to change notification settings - Fork 10
/
tools.py
147 lines (134 loc) · 3.99 KB
/
tools.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
import numpy as np
from pathlib import Path
import json
import random
import torch
import os
import logging
import torch.nn as nn
logger = logging.getLogger()
def print_config(config):
info = "Running with the following configs:\n"
for k, v in config.items():
info += f"\t{k} : {str(v)}\n"
print("\n" + info + "\n")
return
def init_logger(log_file=None, log_file_level=logging.NOTSET):
'''
Example:
>>> init_logger(log_file)
>>> logger.info("abc'")
'''
if isinstance(log_file,Path):
log_file = str(log_file)
log_format = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_format)
logger.handlers = [console_handler]
if log_file and log_file != '':
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(log_file_level)
# file_handler.setFormatter(log_format)
logger.addHandler(file_handler)
return logger
def save_json(data, file_path):
'''
save json
:param data:
:param json_path:
:param file_name:
:return:
'''
if not isinstance(file_path, Path):
file_path = Path(file_path)
# if isinstance(data,dict):
# data = json.dumps(data)
with open(str(file_path), 'w') as f:
json.dump(data, f)
def load_json(file_path):
'''
load json
:param json_path:
:param file_name:
:return:
'''
if not isinstance(file_path, Path):
file_path = Path(file_path)
with open(str(file_path), 'r') as f:
data = json.load(f)
return data
def save_model(model, model_path):
""" 存储不含有显卡信息的state_dict或model
:param model:
:param model_name:
:param only_param:
:return:
"""
if isinstance(model_path, Path):
model_path = str(model_path)
if isinstance(model, nn.DataParallel):
model = model.module
state_dict = model.state_dict()
for key in state_dict:
state_dict[key] = state_dict[key].cpu()
torch.save(state_dict, model_path)
def load_model(model, model_path):
'''
加载模型
:param model:
:param model_name:
:param model_path:
:param only_param:
:return:
'''
if isinstance(model_path, Path):
model_path = str(model_path)
logging.info(f"loading model from {str(model_path)} .")
states = torch.load(model_path)
state = states['state_dict']
if isinstance(model, nn.DataParallel):
model.module.load_state_dict(state)
else:
model.load_state_dict(state)
return model
class AverageMeter(object):
'''
# computes and stores the average and current value
# Example:
# >>> loss = AverageMeter()
# >>> for step,batch in enumerate(train_data):
# >>> pred = self.model(batch)
# >>> raw_loss = self.metrics(pred,target)
# >>> loss.update(raw_loss.item(),n = 1)
# >>> cur_loss = loss.avg
# '''
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def seed_everything(seed=1029):
'''
:param seed:
:param device:
:return:
'''
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# some cudnn methods can be random even after fixing the seed
# unless you tell it to be deterministic
torch.backends.cudnn.deterministic = True