-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_part_imagenet.py
318 lines (262 loc) · 10.3 KB
/
prepare_part_imagenet.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
Code is adapted from https://github.com/micco00x/py-pascalpart
Usage examples:
python prepare_pascal_part_v3.py --data-dir ~/data/pascal_part/ --name name
"""
import argparse
import copy
import json
import glob
import os
import random
from itertools import chain, combinations
import numpy as np
import PIL
from tqdm import tqdm
from coco.coco import COCO
CLASSES = {
"Quadruped": 4,
"Biped": 5,
"Fish": 4,
"Bird": 5,
"Snake": 2,
"Reptile": 4,
"Car": 3,
"Bicycle": 4,
"Boat": 2,
"Aeroplane": 5,
"Bottle": 2,
}
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
def get_all_image_names(path):
all_names = glob.glob(f"{path}/JPEGImages/*/*.JPEG")
all_names = [name.split("/")[-1].split(".")[0] for name in all_names]
return set(all_names)
def _get_box_from_bin_mask(bin_mask):
box_mask = np.zeros_like(bin_mask)
if bin_mask.sum() == 0:
return box_mask
y, x = np.where(bin_mask)
ymin, ymax = y.min(), y.max()
xmin, xmax = x.min(), x.max()
box_mask[ymin : ymax + 1, xmin : xmax + 1] = 1
return box_mask
def get_seg_masks(path, all_image_names, use_box_seg=False):
coco = COCO(path)
# Total number of parts including background
num_parts = 1
classes = sorted(list(CLASSES.keys()))
all_part_ids = []
for k in classes:
num_parts += CLASSES[k]
all_part_ids.extend(coco.getCatIds(supNms=k))
# all_part_ids = coco.getCatIds(supNms=list(CLASSES.keys()))
assert len(all_part_ids) == num_parts - 1
data_dict = {
"seg_masks": [],
"img_paths": [],
"labels": [],
}
for label in CLASSES:
print(f" ==> label: {label}")
# Get id's of the desired class
cat_ids = coco.getCatIds(supNms=label)
# Iterate through all combinations of parts
img_ids = []
for ids in powerset(cat_ids):
if len(ids) == 0:
continue
# Select only images from this class
img_ids.extend(coco.getImgIds(catIds=ids))
img_ids = set(img_ids)
# import pdb
# pdb.set_trace()
imgs = coco.loadImgs(img_ids)
seg_masks, img_paths = [], []
for i, img_id in tqdm(enumerate(img_ids)):
img = imgs[i]
ann_ids = coco.getAnnIds(imgIds=img_id)
anns = coco.loadAnns(ann_ids)
if img["file_name"].split(".")[0] not in all_image_names:
print(f'{img["file_name"].split(".")[0]} file missing!')
continue
img_path = (
f'{img["file_name"].split("_")[0]}/{img["file_name"].split(".")[0]}'
)
img_paths.append(img_path)
# Turn annotation to mask
seg_mask = np.zeros((img["height"], img["width"]), dtype=np.int8)
for ann in anns:
if ann["area"] == 0:
continue
part_mask = coco.annToMask(ann)
seg_label = all_part_ids.index(ann["category_id"]) + 1
if use_box_seg:
part_mask = _get_box_from_bin_mask(part_mask)
seg_mask = part_mask * seg_label + (1 - part_mask) * seg_mask
assert seg_mask.max() <= num_parts
assert seg_mask.min() >= 0
seg_masks.append(seg_mask)
data_dict["seg_masks"].extend(seg_masks)
data_dict["img_paths"].extend(img_paths)
data_dict["labels"].extend([list(CLASSES.keys()).index(label)] * len(seg_masks))
return data_dict
def save_pil_image(img, path):
image_path = os.path.join(path)
pil_img = PIL.Image.fromarray(img)
pil_img.save(image_path)
def save_images_partition(partition, data_dict, idx, label, use_box_seg=False):
# Copy images to new directory
if use_box_seg:
path = os.path.join(args.data_dir, "BoxSegmentations", args.name, partition)
else:
path = os.path.join(args.data_dir, "PartSegmentations", args.name, partition)
label_path = os.path.join(path, label)
os.makedirs(label_path, exist_ok=True)
img_paths = data_dict["img_paths"]
seg_masks = data_dict["seg_masks"]
# Write image paths to a file
filenames = []
for i in idx:
filenames.append(img_paths[i])
filenames.sort()
filenames = [f + "\n" for f in filenames]
with open(f"{path}/{label}.txt", "w") as path_file:
path_file.writelines(filenames)
# Write segmentation as tif file
for i in idx:
name = f'{img_paths[i].split("/")[1]}.tif'
# import pdb
# pdb.set_trace()
save_pil_image(seg_masks[i], os.path.join(label_path, name))
# Load annotations from the annotation folder of PASCAL-Part dataset:
if __name__ == "__main__":
# Parse arguments from command line:
parser = argparse.ArgumentParser(
description="Prepare PASCAL-Part dataset for classification tasks"
)
parser.add_argument(
"--data-dir", default="~/data/", type=str, help="Path to dataset"
)
parser.add_argument(
"--name", default="temp", type=str, help="Name the new part dataset"
)
parser.add_argument("--seed", default=0, type=int, help="Random seed")
parser.add_argument("--use-box-seg", action="store_true")
args = parser.parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
data_dict = {
"seg_masks": [],
"img_paths": [],
"labels": [],
}
all_names = get_all_image_names(args.data_dir)
for partition in ["train", "test", "val"]:
print(f"==> Collecting data from {partition} partition...")
path = os.path.join(args.data_dir, f"{partition}.json")
part_dict = get_seg_masks(path, all_names, use_box_seg=args.use_box_seg)
for k in data_dict:
data_dict[k].extend(part_dict[k])
print(f'Total number of samples {len(data_dict["seg_masks"])}.')
all_indices = {}
all_indices['train'] = []
all_indices['val'] = []
all_indices['test'] = []
# Randomly split data into train/test/val and keep the class ratio
for l, label in enumerate(CLASSES):
print(f"==> Writing {label} data...")
idx = np.where(np.array(data_dict["labels"]) == l)[0]
num_samples = len(idx)
np.random.shuffle(idx)
num_val, num_test = int(0.1 * num_samples), int(0.1 * num_samples)
val_idx = idx[:num_val]
test_idx = idx[num_val : num_val + num_test]
train_idx = idx[num_val + num_test :]
all_indices['train'].extend(train_idx)
all_indices['val'].extend(val_idx)
all_indices['test'].extend(test_idx)
print(
f" ==> {num_samples} samples in total",
len(train_idx),
len(val_idx),
len(test_idx),
)
for partition, indices in zip(
["train", "val", "test"], [train_idx, val_idx, test_idx]
):
print(f" ==> New {partition} partition.")
save_images_partition(
partition,
data_dict,
indices,
label,
use_box_seg=args.use_box_seg,
)
# save bounding box annotations
ann_data = {}
ann_data['images'] = []
ann_data['annotations'] = []
ann_data['categories'] = []
# combining all existing bbox annotations
new_image_id = 0
new_annotation_id = 0
for partition in ['train', 'val', 'test']:
ann_file_path = os.path.join(args.data_dir, f"{partition}.json")
with open(ann_file_path) as f:
partition_data = json.load(f)
for image in partition_data['images']:
original_image_id = image['id']
for ann_orig in partition_data['annotations']:
if ann_orig['image_id'] == original_image_id:
ann = copy.deepcopy(ann_orig)
ann['image_id'] = new_image_id
ann['id'] = new_annotation_id
ann_data['annotations'].append(ann)
new_annotation_id += 1
# assign image new id
image['id'] = new_image_id
ann_data['images'].append(image)
new_image_id += 1
if not ann_data['categories']:
ann_data['categories'].extend(partition_data['categories'])
print('num images: ', len(partition_data['images']))
img_paths = data_dict["img_paths"]
# reshufling the images and saving the new annotations in COCO format
for partition in ['train', 'val', 'test']:
partition_ann_data = {}
partition_ann_data['images'] = []
partition_ann_data['annotations'] = []
partition_ann_data['categories'] = ann_data['categories']
partition_indices = all_indices[partition]
# Write image paths to a file
partition_filenames = []
for i in partition_indices:
image_filename = img_paths[i].split('/')[-1] + '.JPEG'
partition_filenames.append(image_filename)
prev_image_ids_to_new_image_ids = {}
new_image_id = 0
new_annotation_id = 0
for image in ann_data['images']:
if image['file_name'] in partition_filenames:
original_image_id = image['id']
for ann_orig in ann_data['annotations']:
if ann_orig['image_id'] == original_image_id:
ann = copy.deepcopy(ann_orig)
ann['image_id'] = new_image_id
ann['id'] = new_annotation_id
partition_ann_data['annotations'].append(ann)
new_annotation_id += 1
# assign image new id
image['id'] = new_image_id
imagenet_id, part_imagenet_id = image['file_name'].split('_')
image['file_name'] = os.path.join(imagenet_id, image['file_name'])
partition_ann_data['images'].append(image)
new_image_id += 1
# save json to file
ann_file_path = os.path.join(args.data_dir, "PartBoxSegmentations", f"{partition}.json")
with open(ann_file_path, 'w') as f:
json.dump(partition_ann_data, f)