-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
282 lines (230 loc) · 11.5 KB
/
test.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import random
import os
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
from torchvision.transforms.functional import to_pil_image
import natsort
from retinaface import RetinaFace
import matplotlib.pyplot as plt
import moblieNetV2
import resNet34
import utils
import data_load as data_load
import math
import numpy as np
class test_module():
def __init__(self, datasetPath = None, pertrained = './pretrained/resNet_GNLL_120epoch.pt', saveDir = './test_result', IsGNLL = False, modelType = 'ResNet34'):
self.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
if datasetPath is not None:
self.img_dir = datasetPath
self.img_list = natsort.natsorted(os.listdir(self.img_dir))
self.pertrained = pertrained
self.saveDir = saveDir
self.modelType = modelType
self.IsGNLL = IsGNLL
#model
if self.modelType == "ResNet34":
if self.IsGNLL == True:
self.model = resNet34.ResNet34(output_param = 3).to(self.device).eval() # x, y, sigma
else:
self.model = resNet34.ResNet34(output_param = 2).to(self.device).eval() # x, y
elif self.modelType == "MoblieNetv2":
if self.IsGNLL == True:
self.model = moblieNetV2.moblieNetV2(output_param = 3).to(self.device).eval() # x, y, sigma
else:
self.model = moblieNetV2.moblieNetV2(output_param = 2).to(self.device).eval() # x, y
else:
print("There is no proper model type.")
raise ValueError
self.model.load_state_dict(torch.load(args.pertrained))
def _save_result(self, crop_img, pred_ladmks, image_name, face_num):
plt.clf()
plt.imshow(crop_img)
#print(pred_ladmks.shape)
plt.scatter(pred_ladmks[0, :, 0], pred_ladmks[0, : , 1], s=10, marker='.', c='g')
plt.savefig(self.saveDir + '/'+ image_name + '_face_%d.png'%(face_num))
def _save_result_std(self, crop_img, pred_ladmks, image_name, face_num):
cm = plt.cm.get_cmap('RdYlBu')
plt.clf()
plt.imshow(crop_img)
#print(pred_ladmks.shape)
plt.colorbar(plt.scatter(pred_ladmks[0, :, 0], pred_ladmks[0, : , 1], s=10, marker='.', c=np.exp(pred_ladmks[0, : , 2]), cmap=cm))
plt.savefig(self.saveDir + '/'+ image_name + '_face_%d.png'%(face_num))
def _merge4final_image(self, img, results_lst, image_name):
plt.clf()
plt.imshow(img)
for i in range(len(results_lst)):
left_corner_X, left_corner_Y, pred_ladmks, ratio = results_lst[i][0], results_lst[i][1], results_lst[i][2], results_lst[i][3]
# transfer to coordinate of original image
pred_ladmks[:, :, :2] = pred_ladmks[:, :, :2] * ratio + np.array([left_corner_X ,left_corner_Y])
print(pred_ladmks.shape)
plt.scatter(pred_ladmks[0, :, 0], pred_ladmks[0, : , 1], s=10, marker='.', c='g')
plt.savefig(self.saveDir + '/'+ image_name + '_inference.png')
def inference_imgFolder(self, img_dir):
'''
img_dir: image directory(multi-images)
inference from image directory
'''
img_list = natsort.natsorted(os.listdir(img_dir))
result_total_lst = []
for iter_num, img_name in enumerate(img_list):
img_path = img_dir + '/' + img_list[iter_num]
results_lst = self.inference_imgPath(img_path)
result_total_lst.append(results_lst)
return result_total_lst
def inference_imgPath(self, img_path):
'''
img_path: image path
inference from image path
'''
results_lst = []
img = Image.open(img_path)
#size check
width, height = img.size
# face detection
resp = RetinaFace.detect_faces(img_path = img_path)
# detection check
assert len(resp) != 0, "Can not detect face"
# Conduct inference up to the number of detected faces
for i in range(1, len(resp) + 1):
try:
detect_w = resp["face_1"]['facial_area'][2] - resp["face_1"]['facial_area'][0]
detect_h = resp["face_1"]['facial_area'][3] - resp["face_1"]['facial_area'][1]
max_side = max(detect_w, detect_h)
# make the crop ROI as square followed by longer side
if detect_w <= detect_h:
left_corner_X = resp["face_"+str(i)]['facial_area'][0] - ((detect_h - detect_w) //2)
left_corner_Y = resp["face_"+str(i)]['facial_area'][1]
ratio = detect_h/256
else:
left_corner_X = resp["face_"+str(i)]['facial_area'][0]
left_corner_Y = resp["face_"+str(i)]['facial_area'][1]- ((detect_w - detect_h) //2)
ratio = detect_w/256
except TypeError:
#When there is any error, just assume that bounding box is placed in center of the image
print("detection error occur: There is certain case which can not proper alginmnet")
raise ValueError
#crop image
crop_img = img.crop((left_corner_X, left_corner_Y, left_corner_X + max_side, left_corner_Y + max_side))
crop_img = crop_img.resize((256, 256))
#inference
pred_ladmks = self.inference_img(crop_img)
pred_ladmks[ :, :2] = pred_ladmks[ :, :2] * ratio + np.array([left_corner_X ,left_corner_Y])
results_lst.append([pred_ladmks])
return results_lst
def inference_img(self, crop_img):
'''
crop_img: PIL image
inference input img
'''
width, height = crop_img.size
assert width == 256 and height == 256, "The size of input image must be 256x256 but it is not"
crop_img = torchvision.transforms.ToTensor()(crop_img).to(self.device)
with torch.no_grad():
pred_ladmks = self.model(crop_img.unsqueeze(0)).squeeze(0)
#print(pred_ladmks.shape)
if self.IsGNLL == True:
return pred_ladmks.reshape(-1 ,3).cpu().numpy()
else:
return pred_ladmks.reshape(-1 ,2).cpu().numpy()
def inference(self):
for iter_num, img_name in enumerate(self.img_list):
results_lst = []
img_path = self.img_dir + '/' + self.img_list[iter_num]
img = Image.open(img_path)
# face detection
resp = RetinaFace.detect_faces(img_path = img_path)
# detection check
assert len(resp) != 0, "Can not detect face"
# Conduct inference up to the number of detected faces & align
for i in range(1, len(resp) + 1):
try:
detect_w = resp["face_1"]['facial_area'][2] - resp["face_1"]['facial_area'][0]
detect_h = resp["face_1"]['facial_area'][3] - resp["face_1"]['facial_area'][1]
max_side = max(detect_w, detect_h)
# make the crop ROI as square followed by longer side
if detect_w <= detect_h:
left_corner_X = resp["face_"+str(i)]['facial_area'][0] - ((detect_h - detect_w) //2)
left_corner_Y = resp["face_"+str(i)]['facial_area'][1]
ratio = detect_h/256
else:
left_corner_X = resp["face_"+str(i)]['facial_area'][0]
left_corner_Y = resp["face_"+str(i)]['facial_area'][1] - ((detect_w - detect_h) //2)
ratio = detect_w/256
except TypeError:
#When there is any error, just assume that bounding box is placed in center of the image
print("detection error occur: There is certain case which can not proper alginmnet")
raise ValueError
#crop image
crop_img = img.crop((left_corner_X, left_corner_Y, left_corner_X + max_side, left_corner_Y + max_side))
crop_img = crop_img.resize((256, 256))
crop_img = torchvision.transforms.ToTensor()(crop_img).to(self.device)
#inference
with torch.no_grad():
pred_ladmks = self.model(crop_img.unsqueeze(0))
#save individual image
crop_img = crop_img.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to("cpu", torch.uint8).numpy()
if self.IsGNLL == True:
pred_ladmks = pred_ladmks.reshape(1, -1 ,3).cpu().numpy()
self._save_result_std(crop_img, pred_ladmks, self.img_list[iter_num], i)
else:
pred_ladmks = pred_ladmks.reshape(1, -1 ,2).cpu().numpy()
self._save_result(crop_img, pred_ladmks, self.img_list[iter_num], i)
results_lst.append([left_corner_X, left_corner_Y, pred_ladmks, ratio])
#merge with overall image
self._merge4final_image(img, results_lst, self.img_list[iter_num])
print("######### One inference is completed #########")
return results_lst
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
import argparse
parser = argparse.ArgumentParser()
#parser.add_argument('--name', type=str)
parser.add_argument('--datasetPath', type=str, default="./test_image")
parser.add_argument('--pertrained', type=str, default='./pretrained/resNet_GNLL_120epoch.pt')
parser.add_argument('--saveDir', type=str, default='./test_result')
parser.add_argument('--gpu', type=str, default='0', help='gpu')
parser.add_argument('--IsGNLL', type=str2bool, default=False, help='using GNLL or MSE loss for training')
parser.add_argument('--modelType', type=str, default='ResNet34')
args = parser.parse_args()
def test(args):
#gpu
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=args.gpu
torch.multiprocessing.set_start_method('spawn') # for using mutli num_workers
test_class = test_module(datasetPath = args.datasetPath , pertrained = args.pertrained, saveDir = args.saveDir, IsGNLL = args.IsGNLL, modelType = args.modelType)
#_ = test_class.inference()
#test code level
'''info_1 = test_class.inference_imgFolder(args.datasetPath)
print("info_1: ", info_1)
info_2 = test_class.inference_imgPath("/root/landmark_detection/test_image/000000.png")
print("info_2: ", info_2)
path = "/root/landmark_detection/test_image/000000.png"
img = Image.open(path)
img = img.resize((256, 256))
info_3 = test_class.inference_img(img)
print("info_3: ", info_3)
print("######### Check your result #########")
print("######### Test Done #########")'''
#visual test
path = "/root/landmark_detection/test_image/FFHQ00002.png"
img = Image.open(path)
img = img.resize((256, 256))
info_3 = test_class.inference_img(img)
#MSE model
test_class._save_result(img, np.expand_dims(info_3, axis = 0), "_save_result_std", 0)
#GNLL model
test_class._save_result_std(img, np.expand_dims(info_3, axis = 0), "_save_result_std", 0)
if __name__ == "__main__":
test(args)