-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_hourglass.py
163 lines (128 loc) · 5.64 KB
/
eval_hourglass.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
import numpy as np
import scipy.io
import torch
import torch.nn as nn
import torch.utils.data as torch_data
from torch.utils.data import DataLoader
from torchvision.transforms import ToTensor
from vectormath import Vector2
from tqdm import tqdm
import MPII
import model.hourglass
from util import config
from MPII.util import crop_image
hourglass, optimizer, step, train_epoch = model.hourglass.load(config.hourglass.parameter_dir, config.hourglass.device)
criterion = nn.MSELoss()
# train_epoch equals -1 means that training is over
if train_epoch != -1:
# Reset statistics of batch normalization
hourglass.reset_statistics()
hourglass.train()
train_loader = DataLoader(
MPII.Dataset(
root=config.hourglass.data_dir,
task='train',
augment=False,
),
batch_size=config.hourglass.batch_size,
shuffle=True,
pin_memory=True,
num_workers=config.hourglass.num_workers,
)
# Compute statistics of batch normalization from the train subset
with tqdm(total=len(train_loader), desc='%d epoch' % train_epoch) as progress:
with torch.set_grad_enabled(False):
for images, _, _, _, _, _ in train_loader:
images = images.to(config.hourglass.device)
outputs = hourglass(images)
progress.update(1)
# epoch equals -1 means that training is over
epoch = -1
torch.save(
{
'epoch': epoch,
'step': step,
'state': hourglass.state_dict(),
'optimizer': optimizer.state_dict(),
},
'{parameter_dir}/{epoch}.save'.format(parameter_dir=config.hourglass.parameter_dir, epoch=epoch)
)
hourglass = hourglass.eval()
class EvalData(torch_data.Dataset):
def __init__(self, eval_on_training_and_valid_subset=False):
anno = '{data_dir}/mpii_human_pose_v1_u12_2/mpii_human_pose_v1_u12_1.mat'.format(
data_dir=config.hourglass.data_dir)
anno = scipy.io.loadmat(anno, squeeze_me=True, struct_as_record=False)['RELEASE']
test_subset = np.where(anno.img_train == eval_on_training_and_valid_subset)
annolist = anno.annolist[test_subset]
single_person = anno.single_person[test_subset]
self.test_data = []
for img_idx in range(len(annolist)):
rect = annolist[img_idx].annorect
# Convert scalar to array.
if not isinstance(rect, np.ndarray):
rect = [rect]
if not isinstance(single_person[img_idx], np.ndarray):
single_person[img_idx] = [single_person[img_idx]]
for r_idx in range(len(rect)):
try:
# Python is 0-based but MPII r_idx is 1-based.
if r_idx + 1 not in single_person[img_idx]:
continue
center = (rect[r_idx].objpos.x, rect[r_idx].objpos.y)
scale = rect[r_idx].scale
img_name = annolist[img_idx].image.name
# Python is 0-based but MPII img_idx and r_idx is 1-based.
self.test_data.append({
'center': center,
'scale': scale,
'img_name': img_name,
'img_idx': img_idx + 1,
'r_idx': r_idx + 1,
})
except Exception as e:
pass
self.to_tensor = ToTensor()
def __getitem__(self, index):
data = self.test_data[index]
center = Vector2(data['center'][0], data['center'][1])
scale = data['scale'] * 1.25
rotate = 0
img_name = data['img_name']
img_idx = data['img_idx']
r_idx = data['r_idx']
image_path = '{data_dir}/images/{image_name}'.format(data_dir=config.hourglass.data_dir, image_name=img_name)
image = crop_image(image_path, center, scale, rotate)
return self.to_tensor(image), np.asarray([center.x, center.y], dtype=np.float32), scale, img_idx, r_idx
def __len__(self):
return len(self.test_data)
test_data = torch_data.DataLoader(
EvalData(eval_on_training_and_valid_subset=True),
batch_size=config.hourglass.batch_size,
shuffle=False,
pin_memory=True,
num_workers=config.hourglass.num_workers,
)
with tqdm(total=len(test_data), desc='%d epoch' % train_epoch) as progress:
with torch.set_grad_enabled(False):
for images, centers, scales, img_idxs, r_idxs in test_data:
images = images.to(config.hourglass.device)
centers = centers.to(config.hourglass.device).float()
scales = scales.to(config.hourglass.device).float()
outputs = hourglass(images)
outputs = outputs[-1] # Heatmaps from the last stack in batch-channel-height-width shape.
n_batch = outputs.shape[0]
poses = torch.argmax(outputs.view(n_batch, 16, -1), dim=-1)
poses = torch.stack([
poses % 64,
poses // 64,
], dim=-1).float()
poses = poses - 32
poses = centers.view(n_batch, 1, 2) + poses / 64 * scales.view(n_batch, 1, 1) * 200
for batch, img_idx, r_idx in zip(range(n_batch), img_idxs, r_idxs):
with open('{prediction_dir}/{img_idx}.{r_idx}.txt'.format(
prediction_dir=config.hourglass.prediction_dir, img_idx=img_idx, r_idx=r_idx), 'w') as f:
pose = poses[batch]
for joint in range(16):
f.writelines('{joint} {x} {y}\n'.format(joint=joint, x=pose[joint, 0], y=pose[joint, 1]))
progress.update(1)