-
Notifications
You must be signed in to change notification settings - Fork 43
/
generate_tf_records.py
149 lines (129 loc) · 5.33 KB
/
generate_tf_records.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
"""
It generates the train and tf records that are set by dataset_costants.py file.
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from dataset_costants import TABLE_DICT
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple
from dataset_costants import \
TF_TRAIN_RECORD_TO_PATH, \
TF_TRAIN_RECORD_NAME, \
TF_TEST_RECORD_TO_PATH, \
TF_TEST_RECORD_NAME, \
PATH_TO_IMAGES, \
TRAIN_CSV_TO_PATH, \
TEST_CSV_TO_PATH, \
TRAIN_CSV_NAME, \
TEST_CSV_NAME
import logging
from logger import TimeHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(TimeHandler().handler)
def class_text_to_int(row_label):
"""
Replace text with a int. Zero is for showing no boxes at all
:param row_label:
:return: returns corresponding int
"""
return 1 if row_label == TABLE_DICT['name'] else 0
def split(df, group):
"""
Splits name, sys function
:param df:
:param group:
:return:
"""
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path):
"""
Creates the effective tf example given a group of data and a path.
:param group: all the information from csv of a single image
:param path: to images
:return: a tf example (a single image tensorflow image)
"""
# looking for filename image of csv in path folder
logger.debug('Now creating a single-page tf_example...')
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpeg = fid.read()
logger.debug('Image found in {}'.format(path))
# encode it as a pillow image
encoded_jpeg_io = io.BytesIO(encoded_jpeg)
image = Image.open(encoded_jpeg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpeg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = [] # class text that are written in costants.TABLE_DICT. In our case we have a single table lable
classes = [] # class number
# it now append the coordinates of the boxes inside csv file per image
logger.debug('Appending csv {fn} infos to page tf_example...'.format(fn=filename))
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
# create single tf_example object
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_jpeg),
'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),
}))
logger.debug('Successfully created single-page tf_example')
return tf_example
def main(_):
# write train record:
csv_input = os.path.join(TRAIN_CSV_TO_PATH, TRAIN_CSV_NAME)
output_path = os.path.join(TF_TRAIN_RECORD_TO_PATH, TF_TRAIN_RECORD_NAME)
writer = tf.python_io.TFRecordWriter(output_path)
path = os.path.join(os.getcwd(), PATH_TO_IMAGES)
# examples is the csv file
examples = pd.read_csv(csv_input)
grouped = split(examples, 'filename')
logger.info('Now creating {}:'.format(TRAIN_CSV_NAME))
for group in grouped:
tf_example = create_tf_example(group, path)
# append tf_example to the writer
writer.write(tf_example.SerializeToString())
writer.close()
# output_path = os.path.join(os.getcwd(), TF_TRAIN_RECORD_TO_PATH)
logger.info('Successfully created the TF train records: {}'.format(output_path))
# write test record:
csv_input = os.path.join(TEST_CSV_TO_PATH, TEST_CSV_NAME)
output_path = os.path.join(TF_TEST_RECORD_TO_PATH, TF_TEST_RECORD_NAME)
writer = tf.python_io.TFRecordWriter(output_path)
path = os.path.join(os.getcwd(), PATH_TO_IMAGES)
examples = pd.read_csv(csv_input)
grouped = split(examples, 'filename')
logger.info('Now creating {}:'.format(TEST_CSV_NAME))
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
# output_path = os.path.join(os.getcwd(), TF_TEST_RECORD_TO_PATH)
logger.info('Successfully created the TF test record: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()