-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprepare_cvppp_tfrecords.py
74 lines (61 loc) · 2.42 KB
/
prepare_cvppp_tfrecords.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
from utils.tfrecords_convert import create_tf_record
import os
import random
image_dir = 'D:/Datasets/CVPPP2017_CodaLab/training_images'
gt_dir = 'D:/Datasets/CVPPP2017_CodaLab/training_truth'
examples = ['A1', 'A2', 'A3', 'A4']
val_ratio = 0.2
output_dir = './tfrecords/CVPPP2017_val'
# val_ratio = 0
# output_dir = './tfrecords/CVPPP2017'
neighbor_distance_in_percent = 0.02
resize = (512, 512)
dist_map = True
gt_type = 'label'
max_neighbor = 32
img_dict = {}
gt_dict = {}
for g in examples:
for f in os.listdir(os.path.join(image_dir, g)):
b, _ = os.path.splitext(f)
img_dict[g+'_'+b] = os.path.join(image_dir, g, f)
for f in os.listdir(os.path.join(gt_dir, g)):
b, _ = os.path.splitext(f)
gt_dict[g+'_'+b] = os.path.join(gt_dir, g, f)
keys = list(img_dict.keys())
random.shuffle(keys)
split = int((1-val_ratio) * len(keys))
if not os.path.exists(os.path.join(output_dir, 'train')):
os.makedirs(os.path.join(output_dir, 'train'))
if split == len(keys):
create_tf_record(img_dict,
gt_dict,
os.path.join(output_dir, 'train', 'train'),
neighbor_distance_in_percent=neighbor_distance_in_percent,
resize=resize,
dist_map=dist_map,
gt_type=gt_type,
max_neighbor=max_neighbor)
else:
img_dict_train = {k: img_dict[k] for k in keys[0:split]}
gt_dict_train = {k: gt_dict[k] for k in keys[0:split]}
img_dict_val = {k: img_dict[k] for k in keys[split:]}
gt_dict_val = {k: gt_dict[k] for k in keys[split:]}
create_tf_record(img_dict_train,
gt_dict_train,
os.path.join(output_dir, 'train', 'train'),
neighbor_distance_in_percent=neighbor_distance_in_percent,
resize=resize,
dist_map=dist_map,
gt_type=gt_type,
max_neighbor=max_neighbor)
if not os.path.exists(os.path.join(output_dir, 'val')):
os.makedirs(os.path.join(output_dir, 'val'))
create_tf_record(img_dict_val,
gt_dict_val,
os.path.join(output_dir, 'val', 'val'),
neighbor_distance_in_percent=neighbor_distance_in_percent,
resize=resize,
dist_map=dist_map,
gt_type=gt_type,
max_neighbor=max_neighbor)