forked from fupiao1998/TransformerSOD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
187 lines (149 loc) · 5.49 KB
/
utils.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import cv2
import shutil
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch
import numpy as np
import random
def label_edge_prediction(label):
fx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).astype(np.float32)
fy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]).astype(np.float32)
fx = np.reshape(fx, (1, 1, 3, 3))
fy = np.reshape(fy, (1, 1, 3, 3))
fx = (torch.from_numpy(fx)).cuda()
fy = (torch.from_numpy(fy)).cuda()
contour_th = 1.5
# convert label to edge
label = label.gt(0.5).float()
label = F.pad(label, (1, 1, 1, 1), mode='replicate')
label_fx = F.conv2d(label, fx)
label_fy = F.conv2d(label, fy)
label_grad = torch.sqrt(torch.mul(label_fx, label_fx) + torch.mul(label_fy, label_fy))
label_grad = torch.gt(label_grad, contour_th).float()
return label_grad
class AvgMeter(object):
def __init__(self, num=40):
self.num = num
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
self.losses = []
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
self.losses.append(val)
def show(self):
a = len(self.losses)
b = np.maximum(a-self.num, 0)
c = self.losses[b:]
return torch.mean(torch.stack(c))
def set_seed(seed=1024):
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) # if you are using multi-GPU.
# torch.backends.cudnn.benchmark = False
# torch.backends.cudnn.deterministic = True
def visualize_all(in_pred, in_gt, path):
for kk in range(in_pred.shape[0]):
pred, gt = in_pred[kk, :, :, :], in_gt[kk, :, :, :]
pred = (pred.detach().cpu().numpy().squeeze()*255.0).astype(np.uint8)
gt = (gt.detach().cpu().numpy().squeeze()*255.0).astype(np.uint8)
cat_img = cv2.hconcat([pred, gt])
save_path = path + '/vis_temp/'
# Save vis images this temp folder, based on this experiment's folder.
if not os.path.exists(save_path):
os.makedirs(save_path)
name = '{:02d}_cat.png'.format(kk)
cv2.imwrite(save_path + name, cat_img)
def visualize_list(input_list, path):
for kk in range(input_list[0].shape[0]):
show_list = []
for i in input_list:
tmp = i[kk, :, :, :]
tmp = (tmp.detach().cpu().numpy().squeeze()*255.0).astype(np.uint8)
show_list.append(tmp)
cat_img = cv2.hconcat(show_list)
save_path = path + '/vis_temp/'
# Save vis images this temp folder, based on this experiment's folder.
if not os.path.exists(save_path):
os.makedirs(save_path)
name = '{:02d}_cat.png'.format(kk)
cv2.imwrite(save_path + name, cat_img)
def save_scripts(path, scripts_to_save=None):
if not os.path.exists(os.path.join(path, 'scripts')):
os.makedirs(os.path.join(path, 'scripts'))
if scripts_to_save is not None:
for script in scripts_to_save:
dst_path = os.path.join(path, 'scripts', script)
try:
shutil.copy(script, dst_path)
except IOError:
os.makedirs(os.path.dirname(dst_path))
shutil.copy(script, dst_path)
def torch_tile(a, dim, n_tile):
"""
This function is taken form PyTorch forum and mimics the behavior of tf.tile.
Source: https://discuss.pytorch.org/t/how-to-tile-a-tensor/13853/3
"""
init_dim = a.size(dim)
repeat_idx = [1] * a.dim()
repeat_idx[dim] = n_tile
a = a.repeat(*(repeat_idx))
order_index = torch.LongTensor(np.concatenate([init_dim * np.arange(n_tile) + i for i in range(init_dim)])).to(a.device)
return torch.index_select(a, dim, order_index)
def reparametrize(mu, logvar):
std = logvar.mul(0.5).exp_()
eps = torch.cuda.FloatTensor(std.size()).normal_()
eps = Variable(eps)
return eps.mul(std).add_(mu)
def l2_regularisation(m):
l2_reg = None
for W in m.parameters():
if l2_reg is None:
l2_reg = W.norm(2)
else:
l2_reg = l2_reg + W.norm(2)
return l2_reg
def linear_annealing(init, fin, step, annealing_steps):
"""Linear annealing of a parameter."""
if annealing_steps == 0:
return fin
assert fin > init
delta = fin - init
annealed = min(init + delta * step / annealing_steps, fin)
return annealed
def make_dis_label(label, gts):
D_label = torch.ones(gts.shape, device=gts.device, requires_grad=False).float() * label
return D_label
def sample_p_0(images, opt):
b, c, h, w = images.shape
return opt.e_init_sig * torch.randn(*[b, opt.latent_dim]).to(images.device)
def compute_energy(option, score):
if option.e_energy_form == 'tanh':
energy = F.tanh(score.squeeze())
elif option.e_energy_form == 'sigmoid':
energy = F.sigmoid(score.squeeze())
elif option.e_energy_form == 'softplus':
energy = F.softplus(score.squeeze())
else:
energy = score.squeeze()
return energy
class DotDict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
def allowDotting(self, state=True):
if state:
self.__dict__ = self
else:
self.__dict__ = dict()