-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtfrecord_converter.py
180 lines (140 loc) · 6.94 KB
/
tfrecord_converter.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
"""
Serialization tutorial: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md
Serialization example of Pascal VOC dataset: https://github.com/tensorflow/models/blob/master/research/object_detection/dataset_tools/create_pascal_tf_record.py
"""
from object_detection.utils import dataset_util
import xml.etree.ElementTree as ET
import tensorflow as tf
from PIL import Image
from os import path
import numpy as np
import random
import glob
class Example:
class_name_dict = {}
class_count = 0
def __init__(self, filename, width, height):
self.filename = filename
self.height = height
self.width = width
self.xmins = []
self.xmaxs = []
self.ymins = []
self.ymaxs = []
self.class_names = []
self.class_ids = []
def add_xmin(self, xmin):
xmin_normalized = xmin / self.width
self.xmins.append(xmin_normalized)
def add_xmax(self, xmax):
xmax_normalized = xmax / self.width
self.xmaxs.append(xmax_normalized)
def add_ymin(self, ymin):
ymin_normalized = ymin / self.height
self.ymins.append(ymin_normalized)
def add_ymax(self, ymax):
ymax_normalized = ymax / self.height
self.ymaxs.append(ymax_normalized)
def add_class_name(self, class_name):
if class_name not in Example.class_name_dict:
Example.class_count += 1
Example.class_name_dict[class_name] = Example.class_count
self.class_names.append(class_name.encode('utf8'))
self.class_ids.append(Example.class_name_dict[class_name])
@staticmethod
def create_label_map(output_labelmap_path):
keys = Example.class_name_dict.keys()
label_map_string = ""
for key in keys:
id = Example.class_name_dict[key]
name = key
item_string = \
"item { \n" \
f" id: {id}\n" \
f" name: \"{name}\"\n" \
"}\n\n"
label_map_string += item_string
with open(output_labelmap_path, "w") as text_file:
print(label_map_string, file=text_file)
def load_annotations(annotation_folder_path):
'''
Finds all xml files in annotation_folder_path and parse it into a Example object
'''
xml_file_paths = glob.glob(path.join(annotation_folder_path, '*.xml'))
examples = []
for xml_file_path in xml_file_paths:
print(f'Cargando los datos de {xml_file_path}')
tree = ET.parse(xml_file_path)
root = tree.getroot()
objects = root.findall('object')
example = Example(
filename=root.find('filename').text,
width=int(root.find('size')[0].text),
height=int(root.find('size')[1].text))
for object in objects:
boundary_box = object.find('bndbox')
example.add_xmin(int(boundary_box.find('xmin').text))
example.add_ymin(int(boundary_box.find('ymin').text))
example.add_xmax(int(boundary_box.find('xmax').text))
example.add_ymax(int(boundary_box.find('ymax').text))
example.add_class_name(object.find('name').text)
examples.append(example)
return examples
def create_tf_example(example, images_folder_path, img_format):
print(f'Creando registro TFRecord del archivo: {example.filename}')
height = example.height # Image height
width = example.width # Image width
filename = example.filename.encode('utf8') # Filename of the image. Empty if image is not from file
encoded_image_data = encode_image(full_img_path=path.join(images_folder_path, example.filename), img_format=img_format)
image_format = str.encode(img_format)
xmins = example.xmins # List of normalized left x coordinates in bounding box (1 per box)
xmaxs = example.xmaxs # List of normalized right x coordinates in bounding box (1 per box)
ymins = example.ymins # List of normalized top y coordinates in bounding box (1 per box)
ymaxs = example.ymaxs # List of normalized bottom y coordinates in bounding box (1 per box)
classes_text = example.class_names # List of string class name of bounding box (1 per box)
classes = example.class_ids # List of integer class id of bounding box (1 per box)
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_image_data),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def encode_image(full_img_path, img_format):
im = Image.open(full_img_path)
rgb_im = im.convert('RGB')
if img_format == 'png':
return tf.io.encode_png(np.asarray(rgb_im)).numpy()
if img_format == 'jpeg':
return tf.io.encode_jpeg(np.asarray(rgb_im)).numpy()
def split_dataset(examples, train_ratio=0.8):
random.shuffle(examples)
split_index = int(len(examples) * train_ratio)
train_examples = examples[:split_index]
validation_examples = examples[split_index:]
return train_examples, validation_examples
def create_tf_record_file(examples, images_folder_path, img_format, output_file_path):
writer = tf.compat.v1.python_io.TFRecordWriter(output_file_path)
for example in examples:
tf_example = create_tf_example(example, images_folder_path, img_format)
writer.write(tf_example.SerializeToString())
writer.close()
if __name__ == '__main__':
annotation_folder_path = path.normpath(r"C:\Users\matia\Documents\Scripts\PDI\archive\annotations")
images_folder_path = path.normpath(r"C:\Users\matia\Documents\Scripts\PDI\archive\images")
output_folder_path = path.normpath(r"C:\Users\matia\Documents\Scripts\PDI\training_resources\dataset")
train_ratio = 0.6
img_format = 'jpeg' # png or jpeg
examples = load_annotations(annotation_folder_path)
(train_examples, validation_examples) = split_dataset(examples=examples, train_ratio=train_ratio)
create_tf_record_file(train_examples, images_folder_path, img_format, path.join(output_folder_path, "train.record"))
create_tf_record_file(validation_examples, images_folder_path, img_format, path.join(output_folder_path, "eval.record"))
Example.create_label_map(output_labelmap_path=path.join(output_folder_path, "label_map.pbtxt"))