/
prep_utils.py
215 lines (180 loc) · 6.34 KB
/
prep_utils.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
import numpy as np
import matplotlib.pyplot as plt
import cv2
import torch
import torch.nn as nn
from tqdm import tqdm
N_KEYPOINTS = 21
N_IMG_CHANNELS = 3
RAW_IMG_SIZE = 224
MODEL_IMG_SIZE = 128
DATASET_MEANS = [0.3950, 0.4323, 0.2954]
DATASET_STDS = [0.1966, 0.1734, 0.1836]
MODEL_NEURONS = 16
COLORMAP = {
"thumb": {"ids": [0, 1, 2, 3, 4], "color": "g"},
"index": {"ids": [0, 5, 6, 7, 8], "color": "c"},
"middle": {"ids": [0, 9, 10, 11, 12], "color": "b"},
"ring": {"ids": [0, 13, 14, 15, 16], "color": "m"},
"little": {"ids": [0, 17, 18, 19, 20], "color": "r"},
}
def projectPoints(xyz, K):
"""
Projects 3D coordinates into image space.
Function taken from https://github.com/lmb-freiburg/freihand
"""
xyz = np.array(xyz)
K = np.array(K)
uv = np.matmul(K, xyz.T).T
return uv[:, :2] / uv[:, -1:]
def get_norm_params(dataloader):
"""
Calculates image normalization parameters.
Mean and Std are calculated for each channel separately.
Borrowed from this StackOverflow discussion:
https://stackoverflow.com/questions/60101240/finding-mean-and-standard-deviation-across-image-channels-pytorch
"""
mean = 0.0
std = 0.0
nb_samples = 0.0
for i, sample in tqdm(enumerate(dataloader)):
data = sample["image_raw"]
batch_samples = data.size(0)
data = data.view(batch_samples, data.size(1), -1)
mean += data.mean(2).sum(0)
std += data.std(2).sum(0)
nb_samples += batch_samples
mean /= nb_samples
std /= nb_samples
return {"mean": mean, "std": std}
def vector_to_heatmaps(keypoints):
"""
Creates 2D heatmaps from keypoint locations for a single image
Input: array of size N_KEYPOINTS x 2
Output: array of size N_KEYPOINTS x MODEL_IMG_SIZE x MODEL_IMG_SIZE
"""
heatmaps = np.zeros([N_KEYPOINTS, MODEL_IMG_SIZE, MODEL_IMG_SIZE])
for k, (x, y) in enumerate(keypoints):
x, y = int(x * MODEL_IMG_SIZE), int(y * MODEL_IMG_SIZE)
if (0 <= x < MODEL_IMG_SIZE) and (0 <= y < MODEL_IMG_SIZE):
heatmaps[k, int(y), int(x)] = 1
heatmaps = blur_heatmaps(heatmaps)
return heatmaps
def blur_heatmaps(heatmaps):
"""Blurs heatmaps using GaussinaBlur of defined size"""
heatmaps_blurred = heatmaps.copy()
for k in range(len(heatmaps)):
if heatmaps_blurred[k].max() == 1:
heatmaps_blurred[k] = cv2.GaussianBlur(heatmaps[k], (51, 51), 3)
heatmaps_blurred[k] = heatmaps_blurred[k] / heatmaps_blurred[k].max()
return heatmaps_blurred
class IoULoss(nn.Module):
"""
Intersection over Union Loss.
IoU = Area of Overlap / Area of Union
IoU loss is modified to use for heatmaps.
"""
def __init__(self):
super(IoULoss, self).__init__()
self.EPSILON = 1e-6
def _op_sum(self, x):
return x.sum(-1).sum(-1)
def forward(self, y_pred, y_true):
inter = self._op_sum(y_true * y_pred)
union = (
self._op_sum(y_true ** 2)
+ self._op_sum(y_pred ** 2)
- self._op_sum(y_true * y_pred)
)
iou = (inter + self.EPSILON) / (union + self.EPSILON)
iou = torch.mean(iou)
return 1 - iou
def heatmaps_to_coordinates(heatmaps):
"""
Heatmaps is a numpy array
Its size - (batch_size, n_keypoints, img_size, img_size)
"""
batch_size = heatmaps.shape[0]
sums = heatmaps.sum(axis=-1).sum(axis=-1)
sums = np.expand_dims(sums, [2, 3])
normalized = heatmaps / sums
x_prob = normalized.sum(axis=2)
y_prob = normalized.sum(axis=3)
arr = np.tile(np.float32(np.arange(0, 128)), [batch_size, 21, 1])
x = (arr * x_prob).sum(axis=2)
y = (arr * y_prob).sum(axis=2)
keypoints = np.stack([x, y], axis=-1)
return keypoints / 128
def show_data(dataset, n_samples=12):
"""
Function to visualize data
Input: torch.utils.data.Dataset
"""
n_cols = 4
n_rows = int(np.ceil(n_samples / n_cols))
plt.figure(figsize=[15, n_rows * 4])
ids = np.random.choice(dataset.__len__(), n_samples, replace=False)
for i, id_ in enumerate(ids, 1):
sample = dataset.__getitem__(id_)
image = sample["image_raw"].numpy()
image = np.moveaxis(image, 0, -1)
keypoints = sample["keypoints"].numpy()
keypoints = keypoints * RAW_IMG_SIZE
plt.subplot(n_rows, n_cols, i)
plt.imshow(image)
plt.scatter(keypoints[:, 0], keypoints[:, 1], c="k", alpha=0.5)
for finger, params in COLORMAP.items():
plt.plot(
keypoints[params["ids"], 0],
keypoints[params["ids"], 1],
params["color"],
)
plt.tight_layout()
plt.show()
def show_batch_predictions(batch_data, model):
"""
Visualizes image, image with actual keypoints and
image with predicted keypoints.
Finger colors are in COLORMAP.
Inputs:
- batch data is batch from dataloader
- model is trained model
"""
inputs = batch_data["image"]
true_keypoints = batch_data["keypoints"].numpy()
batch_size = true_keypoints.shape[0]
pred_heatmaps = model(inputs)
pred_heatmaps = pred_heatmaps.detach().numpy()
pred_keypoints = heatmaps_to_coordinates(pred_heatmaps)
images = batch_data["image_raw"].numpy()
images = np.moveaxis(images, 1, -1)
plt.figure(figsize=[12, 4 * batch_size])
for i in range(batch_size):
image = images[i]
true_keypoints_img = true_keypoints[i] * RAW_IMG_SIZE
pred_keypoints_img = pred_keypoints[i] * RAW_IMG_SIZE
plt.subplot(batch_size, 3, i * 3 + 1)
plt.imshow(image)
plt.title("Image")
plt.axis("off")
plt.subplot(batch_size, 3, i * 3 + 2)
plt.imshow(image)
for finger, params in COLORMAP.items():
plt.plot(
true_keypoints_img[params["ids"], 0],
true_keypoints_img[params["ids"], 1],
params["color"],
)
plt.title("True Keypoints")
plt.axis("off")
plt.subplot(batch_size, 3, i * 3 + 3)
plt.imshow(image)
for finger, params in COLORMAP.items():
plt.plot(
pred_keypoints_img[params["ids"], 0],
pred_keypoints_img[params["ids"], 1],
params["color"],
)
plt.title("Pred Keypoints")
plt.axis("off")
plt.tight_layout()