-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
84 lines (69 loc) · 2.79 KB
/
predict.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
import os
import json
import torch
import numpy as np
import cv2
import matplotlib.pyplot as plt
import time
from model import HighResolutionNet
from draw_utils import draw_keypoints
import transforms
def predict_all_person():
# TODO
pass
def predict_single_person():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f"using device: {device}")
flip_test = True
resize_hw = (256, 192)
img_path = "./person.png"
weights_path = "./save_weights/model-209.pth"
keypoint_json_path = "person_keypoints.json"
assert os.path.exists(img_path), f"file: {img_path} does not exist."
assert os.path.exists(weights_path), f"file: {weights_path} does not exist."
assert os.path.exists(keypoint_json_path), f"file: {keypoint_json_path} does not exist."
data_transform = transforms.Compose([
transforms.AffineTransform(scale=(1.25, 1.25), fixed_size=resize_hw),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# read json file
with open(keypoint_json_path, "r") as f:
person_info = json.load(f)
# read single-person image
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_tensor, target = data_transform(img, {"box": [0, 0, img.shape[1] - 1, img.shape[0] - 1]})
img_tensor = torch.unsqueeze(img_tensor, dim=0)
# create model
# HRNet-W32: base_channel=32
# HRNet-W48: base_channel=48
model = HighResolutionNet(base_channel=32)
weights = torch.load(weights_path, map_location=device)
weights = weights if "model" not in weights else weights["model"]
model.load_state_dict(weights)
model.to(device)
model.eval()
with torch.no_grad():
start = time.time()
outputs = model(img_tensor.to(device))
end = time.time()
print("infer cost: ",end-start)
if flip_test:
flip_tensor = transforms.flip_images(img_tensor)
flip_outputs = torch.squeeze(
transforms.flip_back(model(flip_tensor.to(device)), person_info["flip_pairs"]),
)
# feature is not aligned, shift flipped heatmap for higher accuracy
# https://github.com/leoxiaobin/deep-high-resolution-net.pytorch/issues/22
flip_outputs[..., 1:] = flip_outputs.clone()[..., 0: -1]
outputs = (outputs + flip_outputs) * 0.5
keypoints, scores = transforms.get_final_preds(outputs, [target["reverse_trans"]], True)
keypoints = np.squeeze(keypoints)
scores = np.squeeze(scores)
plot_img = draw_keypoints(img, keypoints, scores, thresh=0.2, r=3)
#plt.imshow(plot_img)
#plt.show()
plot_img.save("test_result.jpg")
if __name__ == '__main__':
predict_single_person()