-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
166 lines (133 loc) · 4.73 KB
/
misc.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
from torch.optim.lr_scheduler import _LRScheduler
import random
import numpy as np
from sklearn.metrics import accuracy_score
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import shutil
import pdb
import logging
def set_logger(log_path):
"""Set the logger to log info in terminal and file `log_path`.
In general, it is useful to have a logger so that every output to the terminal is saved
in a permanent file. Here we save it to `model_dir/train.log`.
Example:
```
logging.info("Starting training...")
```
Args:
log_path: (string) where to log
"""
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if not logger.handlers:
# Logging to a file
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(file_handler)
# Logging to console
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(stream_handler)
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
# check if dir exist, if not create new folder
def ensure_dir(dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
# ---------------save checkpoint--------------------
def save_checkpoint(state, is_best=False, checkpoint='checkpoint', filename='checkpoint.pth'):
filepath = os.path.join(checkpoint, filename)
torch.save(state, filepath)
if is_best:
shutil.copyfile(filepath, os.path.join(checkpoint, 'model_best.pth.tar'))
# ---------------update meter--------------------
def update_meter(dict_meter, dict_content, batch_size):
idx = 0
for key, value in dict_meter.items():
if type(batch_size) == list:
bs = batch_size[idx]
else:
bs = batch_size
if isinstance(dict_content[key], torch.Tensor):
value.update(dict_content[key].item(), bs)
else:
value.update(dict_content[key], bs)
idx += 1
# ---------------load checkpoint--------------------
def load_checkpoint(model, pth_file):
print('==> Reading from model checkpoint..')
assert os.path.isfile(pth_file), 'Error: no model checkpoint directory found!'
checkpoint = torch.load(pth_file)
# args.start_epoch = checkpoint['epoch']
# best_prec1 = checkpoint['best_prec1']
pretrained_dict = checkpoint['state_dict']
model_dict = model.module.state_dict()
model_dict.update(pretrained_dict)
# model.module.load_state_dict(checkpoint['state_dict'])
model.module.load_state_dict(model_dict)
print("=> loaded model checkpoint '{}' (epoch {})"
.format(pth_file, checkpoint['epoch']))
# results = {'model': model, 'checkpoint': checkpoint}
return checkpoint
# ---------------running mean--------------------
class RunningMean:
def __init__(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
@property
def value(self):
# if self.count:
# return float(self.total_value) / self.count
# else:
# return 0
return self.avg
def __str__(self):
return str(self.value)
# ---------------more accutate Acc--------------------
class RunningAcc:
def __init__(self):
self.avg = 0.
self.pred = []
self.tgt = []
def update(self, logits, tgt):
pred = torch.argmax(logits, dim=1)
self.pred.extend(pred.cpu().numpy().tolist())
self.tgt.extend(tgt.cpu().numpy().tolist())
@property
def value(self):
self.avg = accuracy_score(self.pred, self.tgt)
return self.avg*100
def __str__(self):
return str(self.value)
def set_seeding(seed):
random.seed(seed)
np.random.seed(seed) # cpu vars
torch.manual_seed(seed) # cpu vars
torch.cuda.manual_seed(seed) # cpu vars
torch.cuda.manual_seed_all(seed) # gpu vars
torch.backends.cudnn.benchmark = False
cudnn.deterministic = True
def init_params(net):
'''Init layer parameters.'''
for m in net.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal(m.weight, mode='fan_out')
if m.bias:
init.constant(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant(m.weight, 1)
init.constant(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal(m.weight, std=1e-3)
if m.bias:
init.constant(m.bias, 0)