-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack_utils.py
164 lines (125 loc) · 5.87 KB
/
hack_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
import os
import tqdm
import cv2
import numpy as np
import pandas as pd
import torch
from torch.utils import data
np.random.seed(1234)
torch.manual_seed(1234)
TRAIN_SIZE = 0.8
NUM_PTS = 971
CROP_SIZE = 128
SUBMISSION_HEADER = "file_name,Point_M0_X,Point_M0_Y,Point_M1_X,Point_M1_Y,Point_M2_X,Point_M2_Y,Point_M3_X,Point_M3_Y,Point_M4_X,Point_M4_Y,Point_M5_X,Point_M5_Y,Point_M6_X,Point_M6_Y,Point_M7_X,Point_M7_Y,Point_M8_X,Point_M8_Y,Point_M9_X,Point_M9_Y,Point_M10_X,Point_M10_Y,Point_M11_X,Point_M11_Y,Point_M12_X,Point_M12_Y,Point_M13_X,Point_M13_Y,Point_M14_X,Point_M14_Y,Point_M15_X,Point_M15_Y,Point_M16_X,Point_M16_Y,Point_M17_X,Point_M17_Y,Point_M18_X,Point_M18_Y,Point_M19_X,Point_M19_Y,Point_M20_X,Point_M20_Y,Point_M21_X,Point_M21_Y,Point_M22_X,Point_M22_Y,Point_M23_X,Point_M23_Y,Point_M24_X,Point_M24_Y,Point_M25_X,Point_M25_Y,Point_M26_X,Point_M26_Y,Point_M27_X,Point_M27_Y,Point_M28_X,Point_M28_Y,Point_M29_X,Point_M29_Y\n"
class ScaleMinSideToSize(object):
def __init__(self, size=(CROP_SIZE, CROP_SIZE), elem_name='image'):
self.size = torch.tensor(size, dtype=torch.float)
self.elem_name = elem_name
def __call__(self, sample):
h, w, _ = sample[self.elem_name].shape
if h > w:
f = self.size[0] / w
else:
f = self.size[1] / h
sample[self.elem_name] = cv2.resize(sample[self.elem_name], None, fx=f, fy=f, interpolation=cv2.INTER_AREA)
sample["scale_coef"] = f
if 'landmarks' in sample:
landmarks = sample['landmarks'].reshape(-1, 2).float()
landmarks = landmarks * f
sample['landmarks'] = landmarks.reshape(-1)
return sample
class CropCenter(object):
def __init__(self, size=128, elem_name='image'):
self.size = size
self.elem_name = elem_name
def __call__(self, sample):
img = sample[self.elem_name]
h, w, _ = img.shape
margin_h = (h - self.size) // 2
margin_w = (w - self.size) // 2
sample[self.elem_name] = img[margin_h:margin_h + self.size, margin_w:margin_w + self.size]
sample["crop_margin_x"] = margin_w
sample["crop_margin_y"] = margin_h
if 'landmarks' in sample:
landmarks = sample['landmarks'].reshape(-1, 2)
landmarks -= torch.tensor((margin_w, margin_h), dtype=landmarks.dtype)[None, :]
sample['landmarks'] = landmarks.reshape(-1)
return sample
class TransformByKeys(object):
def __init__(self, transform, names):
self.transform = transform
self.names = set(names)
def __call__(self, sample):
for name in self.names:
if name in sample:
sample[name] = self.transform(sample[name])
return sample
class ThousandLandmarksDataset(data.Dataset):
def __init__(self, root, transforms, split="train"):
super(ThousandLandmarksDataset, self).__init__()
self.root = root
landmark_file_name = os.path.join(root, 'landmarks.csv') if split is not "test" \
else os.path.join(root, "test_points.csv")
images_root = os.path.join(root, "images")
self.image_names = []
self.landmarks = []
with open(landmark_file_name, "rt") as fp:
num_lines = sum(1 for line in fp)
num_lines -= 1 # header
with open(landmark_file_name, "rt") as fp:
for i, line in tqdm.tqdm(enumerate(fp)):
if i == 0:
continue # skip header
if split == "train" and i == int(TRAIN_SIZE * num_lines):
break # reached end of train part of data
elif split == "val" and i < int(TRAIN_SIZE * num_lines):
continue # has not reached start of val part of data
elements = line.strip().split("\t")
image_name = os.path.join(images_root, elements[0])
self.image_names.append(image_name)
if split in ("train", "val"):
landmarks = list(map(np.int16, elements[1:]))
landmarks = np.array(landmarks, dtype=np.int16).reshape((len(landmarks) // 2, 2))
self.landmarks.append(landmarks)
if split in ("train", "val"):
self.landmarks = torch.as_tensor(self.landmarks)
else:
self.landmarks = None
self.transforms = transforms
def __getitem__(self, idx):
sample = {}
if self.landmarks is not None:
landmarks = self.landmarks[idx]
sample["landmarks"] = landmarks
image = cv2.imread(self.image_names[idx])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
sample["image"] = image
if self.transforms is not None:
sample = self.transforms(sample)
return sample
def __len__(self):
return len(self.image_names)
def restore_landmarks(landmarks, f, margins):
dx, dy = margins
landmarks[:, 0] += dx
landmarks[:, 1] += dy
landmarks /= f
return landmarks
def restore_landmarks_batch(landmarks, fs, margins_x, margins_y):
landmarks[:, :, 0] += margins_x[:, None]
landmarks[:, :, 1] += margins_y[:, None]
landmarks /= fs[:, None, None]
return landmarks
def create_submission(path_to_data, test_predictions, path_to_submission_file):
test_dir = os.path.join(path_to_data, "test")
output_file = path_to_submission_file
wf = open(output_file, 'w')
wf.write(SUBMISSION_HEADER)
mapping_path = os.path.join(test_dir, 'test_points.csv')
mapping = pd.read_csv(mapping_path, delimiter='\t')
for i, row in mapping.iterrows():
file_name = row[0]
point_index_list = np.array(eval(row[1]))
points_for_image = test_predictions[i]
needed_points = points_for_image[point_index_list].astype(np.int)
wf.write(file_name + ',' + ','.join(map(str, needed_points.reshape(2 * len(point_index_list)))) + '\n')