-
Notifications
You must be signed in to change notification settings - Fork 31
/
tools.py
80 lines (70 loc) · 1.89 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
import numpy as np
from pathlib import Path
import json
import random
import torch
import os
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
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