-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·246 lines (218 loc) · 8.61 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from __future__ import print_function
import os
import math
import json
import logging
import numpy as np
from PIL import Image
from datetime import datetime
def prepare_dirs_and_logger(config):
formatter = logging.Formatter("%(asctime)s:%(levelname)s::%(message)s")
logger = logging.getLogger()
for hdlr in logger.handlers:
logger.removeHandler(hdlr)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
if config.load_path:
if config.load_path.startswith(config.log_dir):
config.model_dir = config.load_path
else:
if config.load_path.startswith(config.dataset):
config.model_name = config.load_path
else:
config.model_name = "{}_{}".format(config.dataset, config.load_path)
else:
config.model_name = "{}_{}".format(config.dataset, get_time())
if not hasattr(config, 'model_dir') or config.model_dir is None:
config.model_dir = os.path.join(config.log_dir, config.model_name)
config.data_path = os.path.join(config.data_dir, config.dataset)
for path in [config.log_dir, config.data_dir, config.model_dir]:
if not os.path.exists(path):
os.makedirs(path)
def get_time():
return datetime.now().strftime("%m%d_%H%M%S")
def save_config(config):
param_path = os.path.join(config.model_dir, "params.json")
print("[*] MODEL dir: %s" % config.model_dir)
print("[*] PARAM path: %s" % param_path)
with open(param_path, 'w') as fp:
json.dump(config.__dict__, fp, indent=4, sort_keys=True)
def rank(array):
return len(array.shape)
def make_grid(tensor, nrow=8, padding=2,
normalize=False, scale_each=False):
"""Code based on https://github.com/pytorch/vision/blob/master/torchvision/utils.py"""
nmaps = tensor.shape[0]
xmaps = min(nrow, nmaps)
ymaps = int(math.ceil(float(nmaps) / xmaps))
height, width = int(tensor.shape[1] + padding), int(tensor.shape[2] + padding)
grid = np.zeros([height * ymaps + 1 + padding // 2, width * xmaps + 1 + padding // 2, 3], dtype=np.uint8)
k = 0
for y in range(ymaps):
for x in range(xmaps):
if k >= nmaps:
break
h, h_width = y * height + 1 + padding // 2, height - padding
w, w_width = x * width + 1 + padding // 2, width - padding
grid[h:h+h_width, w:w+w_width] = tensor[k]
k = k + 1
return grid
def save_image(tensor, filename, nrow=8, padding=2,
normalize=False, scale_each=False):
ndarr = make_grid(tensor, nrow=nrow, padding=padding,
normalize=normalize, scale_each=scale_each)
im = Image.fromarray(ndarr)
im.save(filename)
##################### Pose/Mask/Sparse #####################
import scipy.io
import scipy.stats
import skimage.morphology
from skimage.morphology import square, dilation, erosion
from PIL import Image
def _getPoseMask(peaks, height, width, radius=4, var=4, mode='Solid'):
## MSCOCO Pose part_str = [nose, neck, Rsho, Relb, Rwri, Lsho, Lelb, Lwri, Rhip, Rkne, Rank, Lhip, Lkne, Lank, Leye, Reye, Lear, Rear, pt19]
# find connection in the specified sequence, center 29 is in the position 15
# limbSeq = [[2,3], [2,6], [3,4], [4,5], [6,7], [7,8], [2,9], [9,10], \
# [10,11], [2,12], [12,13], [13,14], [2,1], [1,15], [15,17], \
# [1,16], [16,18], [3,17], [6,18]]
# limbSeq = [[2,3], [2,6], [3,4], [4,5], [6,7], [7,8], [2,9], [9,10], \
# [10,11], [2,12], [12,13], [13,14], [2,1], [1,15], [15,17], \
# [1,16], [16,18]] # , [9,12]
# limbSeq = [[3,4], [4,5], [6,7], [7,8], [9,10], \
# [10,11], [12,13], [13,14], [2,1], [1,15], [15,17], \
# [1,16], [16,18]] #
limbSeq = [[2,3], [2,6], [3,4], [4,5], [6,7], [7,8], [2,9], [9,10], \
[10,11], [2,12], [12,13], [13,14], [2,1], [1,15], [15,17], \
[1,16], [16,18], [2,17], [2,18], [9,12], [12,6], [9,3], [17,18]] #
indices = []
values = []
for limb in limbSeq:
p0 = peaks[limb[0] -1]
p1 = peaks[limb[1] -1]
if 0!=len(p0) and 0!=len(p1):
r0 = p0[0][1]
c0 = p0[0][0]
r1 = p1[0][1]
c1 = p1[0][0]
ind, val = _getSparseKeypoint(r0, c0, 0, height, width, radius, var, mode)
indices.extend(ind)
values.extend(val)
ind, val = _getSparseKeypoint(r1, c1, 0, height, width, radius, var, mode)
indices.extend(ind)
values.extend(val)
distance = np.sqrt((r0-r1)**2 + (c0-c1)**2)
sampleN = int(distance/radius)
# sampleN = 0
if sampleN>1:
for i in xrange(1,sampleN):
r = r0 + (r1-r0)*i/sampleN
c = c0 + (c1-c0)*i/sampleN
ind, val = _getSparseKeypoint(r, c, 0, height, width, radius, var, mode)
indices.extend(ind)
values.extend(val)
shape = [height, width, 1]
## Fill body
dense = np.squeeze(_sparse2dense(indices, values, shape))
## TODO
# im = Image.fromarray((dense*255).astype(np.uint8))
# im.save('xxxxx.png')
# pdb.set_trace()
dense = dilation(dense, square(5))
dense = erosion(dense, square(5))
return dense
Ratio_0_4 = 1.0/scipy.stats.norm(0, 4).pdf(0)
Gaussian_0_4 = scipy.stats.norm(0, 4)
def _getSparseKeypoint(r, c, k, height, width, radius=4, var=4, mode='Solid'):
r = int(r)
c = int(c)
k = int(k)
indices = []
values = []
for i in range(-radius, radius+1):
for j in range(-radius, radius+1):
distance = np.sqrt(float(i**2+j**2))
if r+i>=0 and r+i<height and c+j>=0 and c+j<width:
if 'Solid'==mode and distance<=radius:
indices.append([r+i, c+j, k])
values.append(1)
elif 'Gaussian'==mode and distance<=radius:
indices.append([r+i, c+j, k])
if 4==var:
values.append( Gaussian_0_4.pdf(distance) * Ratio_0_4 )
else:
assert 'Only define Ratio_0_4 Gaussian_0_4 ...'
return indices, values
def _getSparsePose(peaks, height, width, channel, radius=4, var=4, mode='Solid'):
indices = []
values = []
for k in range(len(peaks)):
p = peaks[k]
if 0!=len(p):
r = p[0][1]
c = p[0][0]
ind, val = _getSparseKeypoint(r, c, k, height, width, radius, var, mode)
indices.extend(ind)
values.extend(val)
shape = [height, width, channel]
return indices, values, shape
def _oneDimSparsePose(indices, shape):
ind_onedim = []
for ind in indices:
# idx = ind[2]*shape[0]*shape[1] + ind[1]*shape[0] + ind[0]
idx = ind[0]*shape[2]*shape[1] + ind[1]*shape[2] + ind[2]
ind_onedim.append(idx)
shape = np.prod(shape)
return ind_onedim, shape
def _sparse2dense(indices, values, shape):
dense = np.zeros(shape)
for i in range(len(indices)):
r = indices[i][0]
c = indices[i][1]
k = indices[i][2]
dense[r,c,k] = values[i]
return dense
def _get_valid_peaks(all_peaks, subsets):
try:
subsets = subsets.tolist()
valid_idx = -1
valid_score = -1
for i, subset in enumerate(subsets):
score = subset[-2]
# for s in subset:
# if s > -1:
# cnt += 1
if score > valid_score:
valid_idx = i
valid_score = score
if valid_idx>=0:
peaks = []
cand_id_list = subsets[valid_idx][:18]
for ap in all_peaks:
valid_p = []
for p in ap:
if p[-1] in cand_id_list:
valid_p = p
if len(valid_p)>0: # use the same structure with all_peaks
peaks.append([(valid_p)])
else:
peaks.append([])
return peaks
else:
return None
except:
# pdb.set_trace()
return None
import matplotlib.pyplot as plt
import scipy.misc
def _visualizePose(pose, img):
# pdb.set_trace()
if 3==len(pose.shape):
pose = pose.max(axis=-1, keepdims=True)
pose = np.tile(pose, (1,1,3))
elif 2==len(pose.shape):
pose = np.expand_dims(pose, -1)
pose = np.tile(pose, (1,1,3))
imgShow = ((pose.astype(np.float)+1)/2.0*img.astype(np.float)).astype(np.uint8)
plt.imshow(imgShow)
plt.show()