-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tfrecord.py
235 lines (186 loc) · 6.9 KB
/
create_tfrecord.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
"""
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import random
import sys
import threading
import numpy as np
from datetime import datetime
import tensorflow as tf
from six.moves import xrange
from dataset_utils import bytes_feature
from dataset_utils import int64_feature
from dataset_utils import ImageCoder
tf.app.flags.DEFINE_string('train_directory',
'./data/train_data',
'Training data directory')
tf.app.flags.DEFINE_string('validation_directory', '/tmp/',
'Validation data directory')
tf.app.flags.DEFINE_string('output_directory', './data/tfrecord',
'Output data directory')
tf.app.flags.DEFINE_integer('train_shards', 24,
'Number of shards in training TFRecord files.')
tf.app.flags.DEFINE_integer('validation_shards', 128,
'Number of shards in validation TFRecord files.')
tf.app.flags.DEFINE_integer('num_threads', 8,
'Number of threads to preprocess the images.')
FLAGS = tf.app.flags.FLAGS
def image_to_tfexample_only(image_data, image_name, image_format, height, width):
"""
:param image_data:
:param image_format:
:param height:
:param width:
:return:
"""
return tf.train.Example(
features=tf.train.Features(
feature={
'image/encoded': bytes_feature(image_data),
'image/filename': bytes_feature(image_name),
'image/format': bytes_feature(image_format),
'image/height': int64_feature(height),
'image/width': int64_feature(width),
}
)
)
def _find_image_files(directory):
"""
:param directory:
:return:
"""
list_img = [f for f in os.listdir(directory) if f.endswith('jpg')]
shuffled_index = np.arange(len(list_img))
random.seed(12345)
random.shuffle(shuffled_index)
return [os.path.join(directory, list_img[i]) for i in shuffled_index]
def _process_image(filename, coder):
"""Process a single image file.
Args:
filename: string, path to an image file e.g., '/path/to/example.JPG'.
coder: instance of ImageCoder to provide TensorFlow image coding utils.
Returns:
image_buffer: string, JPEG encoding of RGB image.
height: integer, image height in pixels.
width: integer, image width in pixels.
"""
# Read the image file.
image_data = tf.gfile.GFile(filename, 'rb').read()
# Decode the RGB JPEG.
image = coder.decode_jpeg(image_data)
# Check that image converted to RGB
assert len(image.shape) == 3
height = image.shape[0]
width = image.shape[1]
assert image.shape[2] == 3
return image_data, height, width
def _convert_to_example(filename, image_buffer,
height, width):
"""
:param filename:
:param image_buffer:
:param height:
:param width:
:return:
"""
colorspace = 'RGB'
channels = 3
image_format = 'JPEG'.encode('utf-8')
filename_encoded = filename.encode('utf-8')
example = image_to_tfexample_only(image_buffer, filename_encoded, image_format, height, width)
return example
def _process_image_files_batch(coder, thread_index, ranges, name, filenames, num_shards):
"""
:param coder:
:param thread_index:
:param ranges:
:param name:
:param filenames:
:param num_shards:
:return:
"""
num_threads = len(ranges)
assert not num_shards % num_threads
num_shards_per_batch = int(num_shards / num_threads)
shard_ranges = np.linspace(ranges[thread_index][0],
ranges[thread_index][1],
num_shards_per_batch + 1).astype(int)
num_files_in_thread = ranges[thread_index][1] - ranges[thread_index][0]
counter = 0
for s in xrange(num_shards_per_batch):
# Generate a sharded version of the file name, e.g. 'train-00002-of-00010'
shard = thread_index * num_shards_per_batch + s
output_filename = '%s-%.5d-of-%.5d' % (name, shard, num_shards)
output_file = os.path.join(FLAGS.output_directory, output_filename)
writer = tf.python_io.TFRecordWriter(output_file)
shard_counter = 0
files_in_shard = np.arange(shard_ranges[s], shard_ranges[s + 1], dtype=int)
for i in files_in_shard:
filename = filenames[i]
image_buffer, height, width = _process_image(filename, coder)
example = _convert_to_example(filename, image_buffer,
height, width)
writer.write(example.SerializeToString())
shard_counter += 1
counter += 1
if not counter % 1000:
print('%s [thread %d]: Processed %d of %d images in thread batch.' %
(datetime.now(), thread_index, counter, num_files_in_thread))
sys.stdout.flush()
writer.close()
print('%s [thread %d]: Wrote %d images to %s' %
(datetime.now(), thread_index, shard_counter, output_file))
sys.stdout.flush()
shard_counter = 0
print('%s [thread %d]: Wrote %d images to %d shards.' %
(datetime.now(), thread_index, counter, num_files_in_thread))
sys.stdout.flush()
def _process_image_files(name, filenames, num_shards):
"""
:param name:
:param filenames:
:param num_shards:
:return:
"""
# Bread images into batches
spacing = np.linspace(0, len(filenames), FLAGS.num_threads + 1).astype(np.int)
ranges = []
for i in xrange(len(spacing) - 1):
ranges.append([spacing[i], spacing[i + 1]])
# Launch a thread for each batch.
print('Launching %d threads for spacings: %s' % (FLAGS.num_threads, ranges))
sys.stdout.flush()
# Create a mechanism for monitoring when all threads are finished.
coord = tf.train.Coordinator()
# Create a generic TensorFlow-based utility for converting all image codings.
coder = ImageCoder()
threads = []
for thread_index in xrange(len(ranges)):
args = (coder, thread_index, ranges, name, filenames, num_shards)
t = threading.Thread(target=_process_image_files_batch, args=args)
t.start()
threads.append(t)
# Wait for all the threads to terminate.
coord.join(threads)
print('%s: Finished writing all %d images in data set.' %
(datetime.now(), len(filenames)))
sys.stdout.flush()
def _process_dataset(name, directory, num_shards):
"""
:param name:
:param directory:
:param num_shards:
:return:
"""
filenames = _find_image_files(directory)
_process_image_files(name, filenames, num_shards)
def main(unused_argv):
"""
:return:
"""
_process_dataset('train', FLAGS.train_directory, FLAGS.train_shards)
if __name__ == '__main__':
tf.app.run()