-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
165 lines (138 loc) · 4.56 KB
/
utils.py
File metadata and controls
165 lines (138 loc) · 4.56 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
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
import numpy as np
from matplotlib import pyplot as plt
import torch
import requests
class AvgMeter(object):
""" this class is to record one variable such as loss or acc """
def __init__(self):
self.avg = 0
self.sum = 0
self.count = 0
self.val = 0
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 numpy_to_torch(img, requires_grad=True):
if len(img.shape) < 3:
output = np.float32([img])
else:
output = np.transpose(img, (2, 0, 1))
output = torch.from_numpy(output)
if use_cuda:
output = output.cuda()
output.unsqueeze_(0)
v = Variable(output, requires_grad=requires_grad)
return v
def torch_to_numpy(tensor):
img = tensor[0].cpu().data.numpy()
img = img.transpose((1, 2, 0))
return img
def check_dir(path):
if not os.path.exists(path):
try:
os.mkdir(path)
except:
os.makedirs(path)
def check_empty_dir(path):
if not os.path.exists(path):
try:
os.mkdir(path)
except:
os.makedirs(path)
else:
if not os.listdir(path) == []:
for i in os.listdir(path):
os.remove(os.path.join(path, i))
def plot(*args, x_label, y_label, title, save_path, save_plot_data=False):
'''
Input format: data1, name1, data2, name2...
'''
fig = plt.figure()
ax = fig.add_subplot(111)
assert len(args) % 2 == 0
for i in range(len(args)//2):
ax.plot(args[2*i], label=args[2*i+1])
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
plt.grid()
plt.legend()
plt.savefig(os.path.join(save_path, "{}.png".format(title)))
plt.close()
if save_plot_data:
with open(os.path.join(save_path, "{}.txt".format(title)), "w") as f:
for i in range(len(args)//2):
f.write(str(args[2*i]))
f.write("\n")
def weight_init(module):
for n, m in module.named_children():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, (nn.BatchNorm2d, nn.InstanceNorm2d)):
nn.init.ones_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Sequential):
weight_init(m)
elif isinstance(m, nn.ReLU):
pass
else:
m.initialize()
class savebest_weights():
def __init__(self, num, weight_path):
super(savebest_weights, self).__init__()
self.weight_path = weight_path
self.num = num
self.acc_list = []
self.path_list = []
self.index = 0
check_empty_dir(self.weight_path)
def remove(self, file_path):
if os.path.exists(file_path):
os.remove(file_path)
else:
pass
def save_weight(self, net, acc, epoch, ema):
path = os.path.join(self.weight_path, "Network_{}_{:.4f}.pth.gz".format(epoch, acc))
if len(self.acc_list) < self.num:
self.acc_list.append(acc)
self.path_list.append(path)
if ema is not None:
ema.copy_to(model.parameters())
state = net.state_dict()
torch.save(state, path)
else:
self.acc_array = np.array(self.acc_list)
self.index = np.argsort(-self.acc_array, axis=0)
self.index = self.index[:self.num]
self.acc = self.acc_array[self.index]
if acc >= self.acc[-1]:
self.remove(self.path_list[self.index[-1]])
del self.acc_list[self.index[-1]]
del self.path_list[self.index[-1]]
self.acc_list.append(acc)
self.path_list.append(path)
if ema is not None:
ema.copy_to(model.parameters())
state = net.state_dict()
torch.save(state, path)
else:
pass
def notice(url, title='', message=''):
params = {'text': title, "desp": message}
requests.post(url=url, params=params)