forked from haofanwang/accurate-head-pose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hopenet.py
151 lines (124 loc) · 6.81 KB
/
test_hopenet.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
import sys, os, argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision import transforms
import torch.backends.cudnn as cudnn
import torchvision
import torch.nn.functional as F
import datasets, hopenet, utils
def parse_args():
"""Parse input arguments."""
parser = argparse.ArgumentParser(description='Head pose estimation using the Hopenet network.')
parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',
default=0, type=int)
parser.add_argument('--data_dir', dest='data_dir', help='Directory path for data.',
default='/AFLW2000/', type=str)
parser.add_argument('--filename_list', dest='filename_list', help='Path to text file containing relative paths for every example.',
default='/tools/AFLW2000_filename_filtered.txt', type=str)
parser.add_argument('--snapshot', dest='snapshot', help='Name of model snapshot.',
default='/output/snapshots/AFLW2000/_epoch_9.pkl', type=str)
parser.add_argument('--batch_size', dest='batch_size', help='Batch size.',
default=1, type=int)
parser.add_argument('--save_viz', dest='save_viz', help='Save images with pose cube.',
default=False, type=bool)
parser.add_argument('--dataset', dest='dataset', help='Dataset type.', default='AFLW2000', type=str)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
cudnn.enabled = True
gpu = args.gpu_id
snapshot_path = args.snapshot
# ResNet50 structure
model = hopenet.Multinet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 198)
print 'Loading snapshot.'
# Load snapshot
saved_state_dict = torch.load(snapshot_path)
model.load_state_dict(saved_state_dict)
print 'Loading data.'
transformations = transforms.Compose([transforms.Resize(224),
transforms.CenterCrop(224), transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
if args.dataset == 'Pose_300W_LP':
pose_dataset = datasets.Pose_300W_LP(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'Pose_300W_LP_multi':
pose_dataset = datasets.Pose_300W_LP_multi(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'Pose_300W_LP_random_ds':
pose_dataset = datasets.Pose_300W_LP_random_ds(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'Synhead':
pose_dataset = datasets.Synhead(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'AFLW2000':
pose_dataset = datasets.AFLW2000(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'BIWI':
pose_dataset = datasets.BIWI(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'BIWI_multi':
pose_dataset = datasets.BIWI_multi(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'AFLW':
pose_dataset = datasets.AFLW(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'AFLW_multi':
pose_dataset = datasets.AFLW_multi(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'AFLW_aug':
pose_dataset = datasets.AFLW_aug(args.data_dir, args.filename_list, transformations)
elif args.dataset == 'AFW':
pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations)
else:
print 'Error: not a valid dataset name'
sys.exit()
test_loader = torch.utils.data.DataLoader(dataset=pose_dataset,
batch_size=args.batch_size,
num_workers=2)
model.cuda(gpu)
print 'Ready to test network.'
# Test the Model
model.eval() # Change model to 'eval' mode (BN uses moving mean/var).
total = 0
idx_tensor = [idx for idx in xrange(198)]
idx_tensor = torch.FloatTensor(idx_tensor).cuda(gpu)
yaw_error = .0
pitch_error = .0
roll_error = .0
l1loss = torch.nn.L1Loss(size_average=False)
for i, (images, labels, cont_labels, name) in enumerate(test_loader):
#for i, (images, labels, labels_0, labels_1, labels_2, labels_3, cont_labels, name) in enumerate(test_loader):
images = Variable(images).cuda(gpu)
total += cont_labels.size(0)
label_yaw = cont_labels[:,0].float()
label_pitch = cont_labels[:,1].float()
label_roll = cont_labels[:,2].float()
yaw,yaw_1,yaw_2,yaw_3,yaw_4, pitch,pitch_1,pitch_2,pitch_3,pitch_4, roll,roll_1,roll_2,roll_3,roll_4 = model(images)
# Binned predictions
_, yaw_bpred = torch.max(yaw.data, 1)
_, pitch_bpred = torch.max(pitch.data, 1)
_, roll_bpred = torch.max(roll.data, 1)
# Continuous predictions
yaw_predicted = utils.softmax_temperature(yaw.data, 1)
pitch_predicted = utils.softmax_temperature(pitch.data, 1)
roll_predicted = utils.softmax_temperature(roll.data, 1)
yaw_predicted = torch.sum(yaw_predicted * idx_tensor, 1).cpu() - 99
pitch_predicted = torch.sum(pitch_predicted * idx_tensor, 1).cpu() - 99
roll_predicted = torch.sum(roll_predicted * idx_tensor, 1).cpu() - 99
# Mean absolute error
yaw_error += torch.sum(torch.abs(yaw_predicted - label_yaw))
pitch_error += torch.sum(torch.abs(pitch_predicted - label_pitch))
roll_error += torch.sum(torch.abs(roll_predicted - label_roll))
# Save first image in batch with pose cube or axis.
if args.save_viz:
name = name[0]
if args.dataset == 'BIWI':
cv2_img = cv2.imread(os.path.join(args.data_dir, name + '_rgb.png'))
else:
cv2_img = cv2.imread(os.path.join(args.data_dir, name + '.jpg'))
if args.batch_size == 1:
error_string = 'y %.2f, p %.2f, r %.2f' % (torch.sum(torch.abs(yaw_predicted - label_yaw)), torch.sum(torch.abs(pitch_predicted - label_pitch)), torch.sum(torch.abs(roll_predicted - label_roll)))
cv2.putText(cv2_img, error_string, (30, cv2_img.shape[0]- 30), fontFace=1, fontScale=1, color=(0,0,255), thickness=2)
# utils.plot_pose_cube(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0], size=100)
utils.draw_axis(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0], tdx = 200, tdy= 200, size=100)
cv2.imwrite(os.path.join('output/images', name + '.jpg'), cv2_img)
print('Test error in degrees of the model on the ' + str(total) +
' test images. Yaw: %.4f, Pitch: %.4f, Roll: %.4f, MAE: %.4f' % (yaw_error / total,
pitch_error / total, roll_error / total, (yaw_error+pitch_error+roll_error)/(3.0*total)))