-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.py
131 lines (99 loc) · 3.77 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
import os
import os.path as osp
import numpy as np
import torch
import torch.nn.functional as F
import torch.nn as nn
def make_folder(path, version):
if not osp.exists(osp.join(path, version)):
os.makedirs(osp.join(path, version))
def denorm(x):
return x.clamp_(0, 1)
def uint82bin(n, count=8):
"""returns the binary of integer n, count refers to amount of bits"""
return ''.join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
def labelcolormap(N):
if N == 19: # CelebAMask-HQ
cmap = np.array([(0, 0, 0), (204, 0, 0), (76, 153, 0),
(204, 204, 0), (51, 51, 255), (204, 0, 204), (0, 255, 255),
(51, 255, 255), (102, 51, 0), (255, 0, 0), (102, 204, 0),
(255, 255, 0), (0, 0, 153), (0, 0, 204), (255, 51, 153),
(0, 204, 204), (0, 51, 0), (255, 153, 51), (0, 204, 0)],
dtype=np.uint8)
else:
cmap = np.zeros((N, 3), dtype=np.uint8)
for i in range(N):
r, g, b = 0, 0, 0
id = i
for j in range(7):
str_id = uint82bin(id)
r = r ^ (np.uint8(str_id[-1]) << (7-j))
g = g ^ (np.uint8(str_id[-2]) << (7-j))
b = b ^ (np.uint8(str_id[-3]) << (7-j))
id = id >> 3
cmap[i, 0] = r
cmap[i, 1] = g
cmap[i, 2] = b
return cmap
class Colorize(object):
def __init__(self, n=19):
self.cmap = labelcolormap(n)
self.cmap = torch.from_numpy(self.cmap[:n])
def __call__(self, gray_image):
size = gray_image.size()
color_image = torch.ByteTensor(3, size[1], size[2]).fill_(0)
for label in range(0, len(self.cmap)):
mask = (label == gray_image[0]).cpu()
color_image[0][mask] = self.cmap[label][0]
color_image[1][mask] = self.cmap[label][1]
color_image[2][mask] = self.cmap[label][2]
return color_image
def tensor2label(label_tensor, n_label, imtype=np.uint8):
label_tensor = label_tensor.cpu().float()
label_tensor = Colorize(n_label)(label_tensor)
label_numpy = label_tensor.numpy()
label_numpy = label_numpy / 255.0
return label_numpy
def generate_label(inputs, imsize, class_num=19):
'''Tensor after optimized...'''
inputs = F.interpolate(input=inputs, size=(imsize, imsize),
mode='bilinear', align_corners=True)
pred_batch = torch.argmax(inputs, dim=1)
label_batch = torch.Tensor(
[tensor2label(p.view(1, imsize, imsize), class_num) for p in pred_batch])
return label_batch
def adjust_learning_rate(g_lr, optimizer, i_iter, total_iters):
"""The learning rate decays exponentially"""
def lr_poly(base_lr, iter, max_iter, power):
return base_lr * ((1 - float(iter) / max_iter) ** (power))
lr = lr_poly(g_lr, i_iter, total_iters, .9)
optimizer.param_groups[0]['lr'] = lr
return lr
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.initialized = False
self.val = None
self.avg = None
self.sum = None
self.count = None
def initialize(self, val, weight):
self.val = val
self.avg = val
self.sum = val * weight
self.count = weight
self.initialized = True
def update(self, val, weight=1):
if not self.initialized:
self.initialize(val, weight)
else:
self.add(val, weight)
def add(self, val, weight):
self.val = val
self.sum += val * weight
self.count += weight
self.avg = self.sum / self.count
def value(self):
return self.val
def average(self):
return self.avg