-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils_inference.py
197 lines (157 loc) · 6.78 KB
/
utils_inference.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
import math
import scipy
import scipy.misc
import torch
import numpy as np
from PIL import Image
from lib.models import get_face_alignment_net, get_cls_net
from lib.config import config, config_imagenet, merge_configs
def get_model_by_name(model_name, root_models_path='hrnetv2_models', prefix='HR18-', model_type='landmarks', device='cuda'):
checkpoint_path = f'{root_models_path}/{prefix}{model_name}.pth'
config_path = f'{root_models_path}/{prefix}{model_name}.yaml'
merge_configs(config, config_path)
config.defrost()
config.MODEL.INIT_WEIGHTS = False
config.freeze()
if model_type == 'landmarks':
model = get_face_alignment_net(config)
else:
model = get_cls_net(config_imagenet)
model.load_state_dict(torch.load(checkpoint_path))
model.eval();
model.to(device);
return model
def get_lmks_by_img(model, img, output_size=(256, 256), rot=0):
# img = np.array(Image.open(image_path).convert('RGB'), dtype=np.float32)
face_center = torch.Tensor([img.shape[1]//2, img.shape[0]/2])
crop_scale = max((img.shape[1]) / output_size[0], (img.shape[0]) / output_size[1])
img_crop = crop(img, face_center, crop_scale, output_size=output_size, rot=rot)
img_crop = (img_crop/255.0 - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225])
img_crop = img_crop.transpose([2, 0, 1])
img_crop = torch.tensor(img_crop, dtype=torch.float32).unsqueeze(0).cuda()
with torch.no_grad():
pred = model(img_crop)
return decode_preds(pred, [face_center], [crop_scale], [output_size[0]/4,output_size[1]/4]).cpu().numpy().squeeze(0)
def get_preds(scores):
"""
get predictions from score maps in torch Tensor
return type: torch.LongTensor
"""
assert scores.dim() == 4, 'Score maps should be 4-dim'
maxval, idx = torch.max(scores.view(scores.size(0), scores.size(1), -1), 2)
maxval = maxval.view(scores.size(0), scores.size(1), 1)
idx = idx.view(scores.size(0), scores.size(1), 1) + 1
preds = idx.repeat(1, 1, 2).float()
preds[:, :, 0] = (preds[:, :, 0] - 1) % scores.size(3) + 1
preds[:, :, 1] = torch.floor((preds[:, :, 1] - 1) / scores.size(3)) + 1
pred_mask = maxval.gt(0).repeat(1, 1, 2).float()
preds *= pred_mask
return preds
def decode_preds(output, center, scale, res):
coords = get_preds(output) # float type
coords = coords.cpu()
# pose-processing
for n in range(coords.size(0)):
for p in range(coords.size(1)):
hm = output[n][p]
px = int(math.floor(coords[n][p][0]))
py = int(math.floor(coords[n][p][1]))
if (px > 1) and (px < res[0]) and (py > 1) and (py < res[1]):
diff = torch.Tensor([hm[py - 1][px] - hm[py - 1][px - 2], hm[py][px - 1]-hm[py - 2][px - 1]])
coords[n][p] += diff.sign() * .25
coords += 0.5
preds = coords.clone()
# Transform back
for i in range(coords.size(0)):
preds[i] = transform_preds(coords[i], center[i], scale[i], res)
if preds.dim() < 3:
preds = preds.view(1, preds.size())
return preds
def crop(img, center, scale, output_size=(256,256), rot=0):
# center : [center_w, center_h]
center_new = center.clone()
# Preprocessing for efficient cropping
ht, wd = img.shape[0], img.shape[1]
sf = scale * 200.0 / output_size[0]
if sf < 2:
sf = 1
else:
new_size = int(np.math.floor(max(ht, wd) / sf))
new_ht = int(np.math.floor(ht / sf))
new_wd = int(np.math.floor(wd / sf))
if new_size < 2:
return torch.zeros(output_size[0], output_size[1], img.shape[2]) \
if len(img.shape) > 2 else torch.zeros(output_size[0], output_size[1])
else:
img = np.array(Image.fromarray(img.astype(np.uint8)).resize((new_wd, new_ht)))
# img = scipy.misc.imresize(img, [new_ht, new_wd]) # (0-1)-->(0-255)
center_new[0] = center_new[0] * 1.0 / sf
center_new[1] = center_new[1] * 1.0 / sf
scale = scale / sf
# Upper left point
ul = np.array(transform_pixel([0, 0], center_new, scale, output_size, invert=1))
# Bottom right point
br = np.array(transform_pixel(output_size, center_new, scale, output_size, invert=1))
# Padding so that when rotated proper amount of context is included
pad = int(np.linalg.norm(br - ul) / 2 - float(br[1] - ul[1]) / 2)
if not rot == 0:
ul -= pad
br += pad
new_shape = [br[1] - ul[1], br[0] - ul[0]]
if len(img.shape) > 2:
new_shape += [img.shape[2]]
new_img = np.zeros(new_shape, dtype=np.float32)
# Range to fill new array
new_x = max(0, -ul[0]), min(br[0], len(img[0])) - ul[0]
new_y = max(0, -ul[1]), min(br[1], len(img)) - ul[1]
# Range to sample from original image
old_x = max(0, ul[0]), min(len(img[0]), br[0])
old_y = max(0, ul[1]), min(len(img), br[1])
new_img[new_y[0]:new_y[1], new_x[0]:new_x[1]] = img[old_y[0]:old_y[1], old_x[0]:old_x[1]]
if not rot == 0:
# Remove padding
new_img = scipy.misc.imrotate(new_img, rot)
new_img = new_img[pad:-pad, pad:-pad]
new_img = np.array(Image.fromarray(new_img.astype(np.uint8)).resize(output_size[::-1]))
# new_img = scipy.misc.imresize(new_img, output_size)
return new_img
def transform_pixel(pt, center, scale, output_size, invert=0, rot=0):
# Transform pixel location to different reference
t = get_transform(center, scale, output_size, rot=rot)
if invert:
t = np.linalg.inv(t)
new_pt = np.array([pt[0] - 1, pt[1] - 1, 1.]).T
new_pt = np.dot(t, new_pt)
return new_pt[:2].astype(int) + 1
def get_transform(center, scale, output_size, rot=0):
"""
General image processing functions
"""
# Generate transformation matrix
h = 200 * scale
t = np.zeros((3, 3))
t[0, 0] = float(output_size[1]) / h
t[1, 1] = float(output_size[0]) / h
t[0, 2] = output_size[1] * (-float(center[0]) / h + .5)
t[1, 2] = output_size[0] * (-float(center[1]) / h + .5)
t[2, 2] = 1
if not rot == 0:
rot = -rot # To match direction of rotation from cropping
rot_mat = np.zeros((3, 3))
rot_rad = rot * np.pi / 180
sn, cs = np.sin(rot_rad), np.cos(rot_rad)
rot_mat[0, :2] = [cs, -sn]
rot_mat[1, :2] = [sn, cs]
rot_mat[2, 2] = 1
# Need to rotate around center
t_mat = np.eye(3)
t_mat[0, 2] = -output_size[1]/2
t_mat[1, 2] = -output_size[0]/2
t_inv = t_mat.copy()
t_inv[:2, 2] *= -1
t = np.dot(t_inv, np.dot(rot_mat, np.dot(t_mat, t)))
return t
def transform_preds(coords, center, scale, output_size):
for p in range(coords.size(0)):
coords[p, 0:2] = torch.tensor(transform_pixel(coords[p, 0:2], center, scale, output_size, 1, 0))
return coords