-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.py
327 lines (266 loc) · 12.4 KB
/
data.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
319
320
321
322
323
324
325
326
327
""" Rude Carnie: Age and Gender Deep Learning with Tensorflow found at
https://github.com/dpressel/rude-carnie
"""
# ==============================================================================
# MIT License
#
# Modifications copyright (c) 2018 Image & Vision Computing Lab, Institute of Information Science, Academia Sinica
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ==============================================================================
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.python.ops import array_ops
from datetime import datetime
import os
import numpy as np
import tensorflow as tf
from distutils.version import LooseVersion
from pdb import set_trace as bp
VERSION_GTE_0_12_0 = LooseVersion(tf.__version__) >= LooseVersion('0.12.0')
# Name change in TF v 0.12.0
if VERSION_GTE_0_12_0:
standardize_image = tf.image.per_image_standardization
else:
standardize_image = tf.image.per_image_whitening
def data_files(data_dir, subset):
"""Returns a python list of all (sharded) data subset files.
Returns:
python list of all (sharded) data set files.
Raises:
ValueError: if there are not data_files matching the subset.
"""
if subset not in ['train','validation','test']:
print('Invalid subset!')
exit(-1)
tf_record_pattern = os.path.join(data_dir, '%s-*' % subset)
data_files = tf.gfile.Glob(tf_record_pattern)
print(data_files)
if not data_files:
print('No files found for data dir %s at %s' % (subset, data_dir))
exit(-1)
return data_files
def decode_jpeg(image_buffer, scope=None):
"""Decode a JPEG string into one 3-D float image Tensor.
Args:
image_buffer: scalar string Tensor.
scope: Optional scope for op_scope.
Returns:
3-D float Tensor with values ranging from [0, 1).
"""
with tf.op_scope([image_buffer], scope, 'decode_jpeg'):
# Decode the string as an RGB JPEG.
# Note that the resulting image contains an unknown height and width
# that is set dynamically by decode_jpeg. In other words, the height
# and width of image is unknown at compile-time.
image = tf.image.decode_jpeg(image_buffer, channels=3)
# After this point, all image pixels reside in [0,1)
# until the very end, when they're rescaled to (-1, 1). The various
# adjust_* ops all require this range for dtype float.
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
return image
def distort_image(image, height, width):
# Image processing for training the network. Note the many random
# distortions applied to the image.
distorted_image = tf.random_crop(image, [height, width, 3])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
# Because these operations are not commutative, consider randomizing
# the order their operation.
distorted_image = tf.image.random_brightness(distorted_image,max_delta=63)
distorted_image = tf.image.random_contrast(distorted_image,lower=0.2, upper=1.8)
return distorted_image
def _is_tensor(x):
return isinstance(x, (tf.Tensor, tf.Variable))
def eval_image(image, height, width):
return tf.image.resize_images(image, [height, width])
def data_normalization(image):
image = standardize_image(image)
return image
def image_preprocessing(image_buffer, image_size, train, thread_id=0):
"""Decode and preprocess one image for evaluation or training.
Args:
image_buffer: JPEG encoded string Tensor
train: boolean
thread_id: integer indicating preprocessing thread
Returns:
3-D float Tensor containing an appropriately scaled image
Raises:
ValueError: if user does not provide bounding box
"""
image = decode_jpeg(image_buffer)
if train:
image = distort_image(image, image_size, image_size)
else:
image = eval_image(image, image_size, image_size)
image = data_normalization(image)
return image
def multiparse_example_proto(example_serialized):
# Dense features in Example proto.
feature_map = {
'image/encoded': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/filename': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/ageclass/label': tf.FixedLenFeature([1], dtype=tf.int64,default_value=-1),
'image/genderclass/label': tf.FixedLenFeature([1], dtype=tf.int64,default_value=-1),
'image/class/text': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/height': tf.FixedLenFeature([1], dtype=tf.int64,default_value=-1),
'image/width': tf.FixedLenFeature([1], dtype=tf.int64,default_value=-1),
}
features = tf.parse_single_example(example_serialized, feature_map)
agelabel = tf.cast(features['image/ageclass/label'], dtype=tf.int32)
genderlabel = tf.cast(features['image/genderclass/label'], dtype=tf.int32)
return features['image/encoded'], agelabel, genderlabel, features['image/filename']
def multibatch_inputs(data_dir, batch_size, image_size, train, datatype, num_preprocess_threads=4,
num_readers=1, input_queue_memory_factor=16):
with tf.name_scope('batch_processing'):
if train:
files = data_files(data_dir, datatype)
filename_queue = tf.train.string_input_producer(files,shuffle=True,capacity=16)
else:
files = data_files(data_dir, datatype)
filename_queue = tf.train.string_input_producer(files,shuffle=False,capacity=1)
if num_preprocess_threads % 4:
raise ValueError('Please make num_preprocess_threads a multiple '
'of 4 (%d % 4 != 0).', num_preprocess_threads)
if num_readers < 1:
raise ValueError('Please make num_readers at least 1')
# Approximate number of examples per shard.
examples_per_shard = 1024
# Size the random shuffle queue to balance between good global
# mixing (more examples) and memory use (fewer examples).
# 1 image uses 299*299*3*4 bytes = 1MB
# The default input_queue_memory_factor is 16 implying a shuffling queue
# size: examples_per_shard * 16 * 1MB = 17.6GB
min_queue_examples = examples_per_shard * input_queue_memory_factor
if train:
examples_queue = tf.RandomShuffleQueue(
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples,
dtypes=[tf.string])
else:
examples_queue = tf.FIFOQueue(
capacity=examples_per_shard + 3 * batch_size,
dtypes=[tf.string])
# Create multiple readers to populate the queue of examples.
if num_readers > 1:
enqueue_ops = []
for _ in range(num_readers):
reader = tf.TFRecordReader()
_, value = reader.read(filename_queue)
enqueue_ops.append(examples_queue.enqueue([value]))
tf.train.queue_runner.add_queue_runner(
tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops))
example_serialized = examples_queue.dequeue()
else:
reader = tf.TFRecordReader()
_, example_serialized = reader.read(filename_queue)
images_agegenderlabels_fnames = []
for thread_id in range(num_preprocess_threads):
# Parse a serialized Example proto to extract the image and metadata.
image_buffer, agelabel_index, genderlabel_index, fname = multiparse_example_proto(example_serialized)
image = image_preprocessing(image_buffer, image_size, train, thread_id)
images_agegenderlabels_fnames.append([image, agelabel_index, genderlabel_index, fname])
images, agelabel_index_batch, genderlabel_index_batch, fnames = tf.train.batch_join(
images_agegenderlabels_fnames,batch_size=batch_size,capacity=2*num_preprocess_threads*batch_size)
images = tf.cast(images, tf.float32)
images = tf.reshape(images, shape=[batch_size, image_size, image_size, 3])
# Display the training images in the visualizer.
tf.summary.image('images', images, 20)
return images, tf.reshape(agelabel_index_batch, [batch_size]), tf.reshape(genderlabel_index_batch, [batch_size]) ,fnames
def multiinputs(data_dir, batch_size=128, image_size=227, train=True, num_preprocess_threads=4, datatype=''):
with tf.device('/cpu:0'):
images, agelabels, genderlabels, filenames = multibatch_inputs(
data_dir, batch_size, image_size, train,
datatype, num_preprocess_threads=num_preprocess_threads,
num_readers=1)
return images, agelabels, genderlabels, filenames
def batch_inputs_mod(data_dir, batch_size, image_size, train, datatype, classtype,
num_preprocess_threads=4,num_readers=1, input_queue_memory_factor=16):
with tf.name_scope('batch_processing'):
if train:
files = data_files(data_dir, datatype)
filename_queue = tf.train.string_input_producer(files, shuffle=True, capacity=16)
else:
files = data_files(data_dir, datatype)
filename_queue = tf.train.string_input_producer(files, shuffle=False, capacity=1)
if num_preprocess_threads % 4:
raise ValueError('Please make num_preprocess_threads a multiple '
'of 4 (%d % 4 != 0).', num_preprocess_threads)
if num_readers < 1:
raise ValueError('Please make num_readers at least 1')
examples_per_shard = 1024
min_queue_examples = examples_per_shard*input_queue_memory_factor
if train:
examples_queue = tf.RandomShuffleQueue(
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples,
dtypes=[tf.string])
else:
examples_queue = tf.FIFOQueue(
capacity=examples_per_shard + 3 * batch_size,
dtypes=[tf.string])
if num_readers > 1:
enqueue_ops = []
for _ in range(num_readers):
reader = tf.TFRecordReader()
_, value = reader.read(filename_queue)
enqueue_ops.append(examples_queue.enqueue([value]))
tf.train.queue_runner.add_queue_runner(
tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops))
example_serialized = examples_queue.dequeue()
else:
reader = tf.TFRecordReader()
_, example_serialized = reader.read(filename_queue)
if classtype == 'Age':
images_agelabels_fnames = []
for thread_id in range(num_preprocess_threads):
image_buffer, agelabel_index, genderlabel_index, fname = multiparse_example_proto(example_serialized)
image = image_preprocessing(image_buffer, image_size, train, thread_id)
images_agelabels_fnames.append([image, agelabel_index, fname])
images, agelabel_index_batch, fnames = tf.train.batch_join(
images_agelabels_fnames,batch_size=batch_size,capacity=2 * num_preprocess_threads * batch_size)
images = tf.cast(images, tf.float32)
images = tf.reshape(images, shape=[batch_size, image_size, image_size, 3])
# Display the training images in the visualizer.
tf.summary.image('images', images, 20)
return images, tf.reshape(agelabel_index_batch, [batch_size]),fnames
elif classtype == 'Gender':
images_genderlabels_fnames = []
for thread_id in range(num_preprocess_threads):
image_buffer, agelabel_index, genderlabel_index, fname = multiparse_example_proto(example_serialized)
image = image_preprocessing(image_buffer, image_size, train, thread_id)
images_genderlabels_fnames.append([image, genderlabel_index, fname])
images, genderlabel_index_batch, fnames = tf.train.batch_join(
images_genderlabels_fnames,
batch_size=batch_size,
capacity=2 * num_preprocess_threads * batch_size)
images = tf.cast(images, tf.float32)
images = tf.reshape(images, shape=[batch_size, image_size, image_size, 3])
# Display the training images in the visualizer.
tf.summary.image('images', images, 20)
return images, tf.reshape(genderlabel_index_batch, [batch_size]),fnames
def inputs_mod(data_dir, batch_size=128, image_size=227, train=True, num_preprocess_threads=4, classtype='', datatype=''):
with tf.device('/cpu:0'):
images, labels, filenames = batch_inputs_mod(
data_dir, batch_size, image_size, train,
datatype ,classtype=classtype, num_preprocess_threads=num_preprocess_threads,
num_readers=1)
return images, labels, filenames