-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_utils.py
More file actions
591 lines (486 loc) · 22 KB
/
image_utils.py
File metadata and controls
591 lines (486 loc) · 22 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Image-related functions for style transfer."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import io
import os
import tempfile
# internal imports
import numpy as np
import scipy
import scipy.misc
import tensorflow as tf
from magenta.models.image_stylization import imagenet_data
slim = tf.contrib.slim
_EVALUATION_IMAGES_GLOB = 'evaluation_images/*.jpg'
def imagenet_inputs(batch_size, image_size, num_readers=1,
num_preprocess_threads=4):
"""Loads a batch of imagenet inputs.
Used as a replacement for inception.image_processing.inputs in
tensorflow/models in order to get around the use of hard-coded flags in the
image_processing module.
Args:
batch_size: int, batch size.
image_size: int. The images will be resized bilinearly to shape
[image_size, image_size].
num_readers: int, number of preprocessing threads per tower. Must be a
multiple of 4.
num_preprocess_threads: int, number of parallel readers.
Returns:
4-D tensor of images of shape [batch_size, image_size, image_size, 3], with
values in [0, 1].
Raises:
IOError: If ImageNet data files cannot be found.
ValueError: If `num_preprocess_threads is not a multiple of 4 or
`num_readers` is less than 1.
"""
imagenet = imagenet_data.ImagenetData('train')
with tf.name_scope('batch_processing'):
data_files = imagenet.data_files()
if data_files is None:
raise IOError('No ImageNet data files found')
# Create filename_queue.
filename_queue = tf.train.string_input_producer(data_files,
shuffle=True,
capacity=16)
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
input_queue_memory_factor = 16
min_queue_examples = examples_per_shard * input_queue_memory_factor
examples_queue = tf.RandomShuffleQueue(
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples,
dtypes=[tf.string])
# Create multiple readers to populate the queue of examples.
if num_readers > 1:
enqueue_ops = []
for _ in range(num_readers):
reader = imagenet.reader()
_, 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 = imagenet.reader()
_, example_serialized = reader.read(filename_queue)
images_and_labels = []
for _ in range(num_preprocess_threads):
# Parse a serialized Example proto to extract the image and metadata.
image_buffer, label_index, _, _ = _parse_example_proto(
example_serialized)
image = _decode_jpeg(image_buffer)
# pylint: disable=protected-access
image = _aspect_preserving_resize(image, image_size + 2)
image = _central_crop([image], image_size, image_size)[0]
# pylint: enable=protected-access
image.set_shape([image_size, image_size, 3])
image = tf.to_float(image) / 255.0
images_and_labels.append([image, label_index])
images, label_index_batch = tf.train.batch_join(
images_and_labels,
batch_size=batch_size,
capacity=2 * num_preprocess_threads * batch_size)
images = tf.reshape(images, shape=[batch_size, image_size, image_size, 3])
# Display the training images in the visualizer.
tf.summary.image('images', images)
return images, tf.reshape(label_index_batch, [batch_size])
def style_image_inputs(style_dataset_file, batch_size=None, image_size=None,
square_crop=False, shuffle=True):
"""Loads a style image at random.
Args:
style_dataset_file: str, path to the tfrecord dataset of style files.
The dataset is produced via the create_style_dataset.py script and is
made of Example protobufs with the following features:
* 'image_raw': byte encoding of the JPEG string of the style image.
* 'label': integer identifier of the style image in [0, N - 1], where
N is the number of examples in the dataset.
* 'vgg_16/<LAYER_NAME>': Gram matrix at layer <LAYER_NAME> of the VGG-16
network (<LAYER_NAME> in {conv,pool}{1,2,3,4,5}) for the style
image.
batch_size: int. If provided, batches style images. Defaults to None.
image_size: int. The images will be resized bilinearly so that the smallest
side has size image_size. Defaults to None.
square_crop: bool. If True, square-crops to [image_size, image_size].
Defaults to False.
shuffle: bool, whether to shuffle style files at random. Defaults to True.
Returns:
If batch_size is defined, a 4-D tensor of shape [batch_size, ?, ?, 3] with
values in [0, 1] for the style image, and 1-D tensor for the style label.
Raises:
ValueError: if center cropping is requested but no image size is provided,
or if batch size is specified but center-cropping is not requested.
"""
vgg_layers = ['vgg_16/conv1', 'vgg_16/pool1', 'vgg_16/conv2', 'vgg_16/pool2',
'vgg_16/conv3', 'vgg_16/pool3', 'vgg_16/conv4', 'vgg_16/pool4',
'vgg_16/conv5', 'vgg_16/pool5']
if square_crop and image_size is None:
raise ValueError('center-cropping requires specifying the image size.')
if batch_size is not None and not square_crop:
raise ValueError('batching requires center-cropping.')
with tf.name_scope('style_image_processing'):
filename_queue = tf.train.string_input_producer(
[style_dataset_file], shuffle=False, capacity=1,
name='filename_queue')
if shuffle:
examples_queue = tf.RandomShuffleQueue(
capacity=64,
min_after_dequeue=32,
dtypes=[tf.string], name='random_examples_queue')
else:
examples_queue = tf.FIFOQueue(
capacity=64,
dtypes=[tf.string], name='fifo_examples_queue')
reader = tf.TFRecordReader()
_, value = reader.read(filename_queue)
enqueue_ops = [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()
features = tf.parse_single_example(
example_serialized,
features={'label': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string),
'vgg_16/conv1': tf.FixedLenFeature([64, 64], tf.float32),
'vgg_16/pool1': tf.FixedLenFeature([64, 64], tf.float32),
'vgg_16/conv2': tf.FixedLenFeature([128, 128], tf.float32),
'vgg_16/pool2': tf.FixedLenFeature([128, 128], tf.float32),
'vgg_16/conv3': tf.FixedLenFeature([256, 256], tf.float32),
'vgg_16/pool3': tf.FixedLenFeature([256, 256], tf.float32),
'vgg_16/conv4': tf.FixedLenFeature([512, 512], tf.float32),
'vgg_16/pool4': tf.FixedLenFeature([512, 512], tf.float32),
'vgg_16/conv5': tf.FixedLenFeature([512, 512], tf.float32),
'vgg_16/pool5': tf.FixedLenFeature([512, 512], tf.float32)})
image = tf.image.decode_jpeg(features['image_raw'])
label = features['label']
gram_matrices = [features[vgg_layer] for vgg_layer in vgg_layers]
image.set_shape([None, None, 3])
if image_size:
if square_crop:
image = _aspect_preserving_resize(image, image_size + 2)
image = _central_crop([image], image_size, image_size)[0]
image.set_shape([image_size, image_size, 3])
else:
image = _aspect_preserving_resize(image, image_size)
image = tf.to_float(image) / 255.0
if batch_size is None:
image = tf.expand_dims(image, 0)
else:
image_label_gram_matrices = tf.train.batch(
[image, label] + gram_matrices, batch_size=batch_size)
image, label = image_label_gram_matrices[:2]
gram_matrices = image_label_gram_matrices[2:]
gram_matrices = dict([(vgg_layer, gram_matrix)
for vgg_layer, gram_matrix
in zip(vgg_layers, gram_matrices)])
return image, label, gram_matrices
def load_np_image(image_file):
"""Loads an image as a numpy array.
Args:
image_file: str. Image file.
Returns:
A 3-D numpy array of shape [image_size, image_size, 3] and dtype float32,
with values in [0, 1].
"""
with tempfile.NamedTemporaryFile() as f:
f.write(tf.gfile.GFile(image_file, 'rb').read())
f.flush()
image = scipy.misc.imread(f.name)
# Workaround for black-and-white images
if image.ndim == 2:
image = np.tile(image[:, :, None], (1, 1, 3))
return np.float32(image / 255.0)
def save_np_image(image, output_file):
"""Saves an image to disk.
Args:
image: 3-D numpy array of shape [image_size, image_size, 3] and dtype
float32, with values in [0, 1].
output_file: str, output file.
"""
_, ext = os.path.splitext(output_file)
image = np.uint8(image * 255.0)
buf = io.BytesIO()
scipy.misc.imsave(buf, np.squeeze(image, 0), format=ext[1:])
buf.seek(0)
f = tf.gfile.GFile(output_file, 'w')
f.write(buf.getvalue())
f.close()
def load_image(image_file, image_size=None):
"""Loads an image and center-crops it to a specific size.
Args:
image_file: str. Image file.
image_size: int, optional. Desired size. If provided, crops the image to
a square and resizes it to the requested size. Defaults to None.
Returns:
A 4-D tensor of shape [1, image_size, image_size, 3] and dtype float32,
with values in [0, 1].
"""
image = tf.constant(np.uint8(load_np_image(image_file) * 255.0))
if image_size is not None:
# Center-crop into a square and resize to image_size
small_side = min(image.get_shape()[0].value, image.get_shape()[1].value)
image = tf.image.resize_image_with_crop_or_pad(
image, small_side, small_side)
image = tf.image.resize_images(image, [image_size, image_size])
image = tf.to_float(image) / 255.0
return tf.expand_dims(image, 0)
def load_evaluation_images(image_size):
"""Loads images for evaluation.
Args:
image_size: int. Image size.
Returns:
Tensor. A batch of evaluation images.
Raises:
IOError: If no evaluation images can be found.
"""
glob = os.path.join(tf.resource_loader.get_data_files_path(),
_EVALUATION_IMAGES_GLOB)
evaluation_images = tf.gfile.Glob(glob)
if not evaluation_images:
raise IOError('No evaluation images found')
return tf.concat(
[load_image(path, image_size) for path in evaluation_images], 0)
def form_image_grid(input_tensor, grid_shape, image_shape, num_channels):
"""Arrange a minibatch of images into a grid to form a single image.
Args:
input_tensor: Tensor. Minibatch of images to format, either 4D
([batch size, height, width, num_channels]) or flattened
([batch size, height * width * num_channels]).
grid_shape: Sequence of int. The shape of the image grid,
formatted as [grid_height, grid_width].
image_shape: Sequence of int. The shape of a single image,
formatted as [image_height, image_width].
num_channels: int. The number of channels in an image.
Returns:
Tensor representing a single image in which the input images have been
arranged into a grid.
Raises:
ValueError: The grid shape and minibatch size don't match, or the image
shape and number of channels are incompatible with the input tensor.
"""
if grid_shape[0] * grid_shape[1] != int(input_tensor.get_shape()[0]):
raise ValueError('Grid shape incompatible with minibatch size.')
if len(input_tensor.get_shape()) == 2:
num_features = image_shape[0] * image_shape[1] * num_channels
if int(input_tensor.get_shape()[1]) != num_features:
raise ValueError('Image shape and number of channels incompatible with '
'input tensor.')
elif len(input_tensor.get_shape()) == 4:
if (int(input_tensor.get_shape()[1]) != image_shape[0] or
int(input_tensor.get_shape()[2]) != image_shape[1] or
int(input_tensor.get_shape()[3]) != num_channels):
raise ValueError('Image shape and number of channels incompatible with '
'input tensor.')
else:
raise ValueError('Unrecognized input tensor format.')
height, width = grid_shape[0] * image_shape[0], grid_shape[1] * image_shape[1]
input_tensor = tf.reshape(
input_tensor, grid_shape + image_shape + [num_channels])
input_tensor = tf.transpose(input_tensor, [0, 1, 3, 2, 4])
input_tensor = tf.reshape(
input_tensor, [grid_shape[0], width, image_shape[0], num_channels])
input_tensor = tf.transpose(input_tensor, [0, 2, 1, 3])
input_tensor = tf.reshape(
input_tensor, [1, height, width, num_channels])
return input_tensor
# The following functions are copied over from
# tf.slim.preprocessing.vgg_preprocessing
# because they're not visible to this module.
def _crop(image, offset_height, offset_width, crop_height, crop_width):
"""Crops the given image using the provided offsets and sizes.
Note that the method doesn't assume we know the input image size but it does
assume we know the input image rank.
Args:
image: an image of shape [height, width, channels].
offset_height: a scalar tensor indicating the height offset.
offset_width: a scalar tensor indicating the width offset.
crop_height: the height of the cropped image.
crop_width: the width of the cropped image.
Returns:
the cropped (and resized) image.
Raises:
InvalidArgumentError: if the rank is not 3 or if the image dimensions are
less than the crop size.
"""
original_shape = tf.shape(image)
rank_assertion = tf.Assert(
tf.equal(tf.rank(image), 3),
['Rank of image must be equal to 3.'])
with tf.control_dependencies([rank_assertion]):
cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]])
size_assertion = tf.Assert(
tf.logical_and(
tf.greater_equal(original_shape[0], crop_height),
tf.greater_equal(original_shape[1], crop_width)),
['Crop size greater than the image size.'])
offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0]))
# Use tf.strided_slice instead of crop_to_bounding box as it accepts tensors
# to define the crop size.
with tf.control_dependencies([size_assertion]):
image = tf.strided_slice(image, offsets, offsets + cropped_shape,
strides=tf.ones_like(offsets))
return tf.reshape(image, cropped_shape)
def _central_crop(image_list, crop_height, crop_width):
"""Performs central crops of the given image list.
Args:
image_list: a list of image tensors of the same dimension but possibly
varying channel.
crop_height: the height of the image following the crop.
crop_width: the width of the image following the crop.
Returns:
the list of cropped images.
"""
outputs = []
for image in image_list:
image_height = tf.shape(image)[0]
image_width = tf.shape(image)[1]
offset_height = (image_height - crop_height) / 2
offset_width = (image_width - crop_width) / 2
outputs.append(_crop(image, offset_height, offset_width,
crop_height, crop_width))
return outputs
def _smallest_size_at_least(height, width, smallest_side):
"""Computes new shape with the smallest side equal to `smallest_side`.
Computes new shape with the smallest side equal to `smallest_side` while
preserving the original aspect ratio.
Args:
height: an int32 scalar tensor indicating the current height.
width: an int32 scalar tensor indicating the current width.
smallest_side: A python integer or scalar `Tensor` indicating the size of
the smallest side after resize.
Returns:
new_height: an int32 scalar tensor indicating the new height.
new_width: and int32 scalar tensor indicating the new width.
"""
smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)
height = tf.to_float(height)
width = tf.to_float(width)
smallest_side = tf.to_float(smallest_side)
scale = tf.cond(tf.greater(height, width),
lambda: smallest_side / width,
lambda: smallest_side / height)
new_height = tf.to_int32(height * scale)
new_width = tf.to_int32(width * scale)
return new_height, new_width
def _aspect_preserving_resize(image, smallest_side):
"""Resize images preserving the original aspect ratio.
Args:
image: A 3-D image `Tensor`.
smallest_side: A python integer or scalar `Tensor` indicating the size of
the smallest side after resize.
Returns:
resized_image: A 3-D tensor containing the resized image.
"""
smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)
shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_height, new_width = _smallest_size_at_least(height, width, smallest_side)
image = tf.expand_dims(image, 0)
resized_image = tf.image.resize_bilinear(image, [new_height, new_width],
align_corners=False)
resized_image = tf.squeeze(resized_image)
resized_image.set_shape([None, None, 3])
return resized_image
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.name_scope(scope, 'decode_jpeg', [image_buffer]):
# 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 _parse_example_proto(example_serialized):
"""Parses an Example proto containing a training example of an image.
The output of the build_image_data.py image preprocessing script is a dataset
containing serialized Example protocol buffers. Each Example proto contains
the following fields:
image/height: 462
image/width: 581
image/colorspace: 'RGB'
image/channels: 3
image/class/label: 615
image/class/synset: 'n03623198'
image/class/text: 'knee pad'
image/object/bbox/xmin: 0.1
image/object/bbox/xmax: 0.9
image/object/bbox/ymin: 0.2
image/object/bbox/ymax: 0.6
image/object/bbox/label: 615
image/format: 'JPEG'
image/filename: 'ILSVRC2012_val_00041207.JPEG'
image/encoded: <JPEG encoded string>
Args:
example_serialized: scalar Tensor tf.string containing a serialized
Example protocol buffer.
Returns:
image_buffer: Tensor tf.string containing the contents of a JPEG file.
label: Tensor tf.int32 containing the label.
bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords]
where each coordinate is [0, 1) and the coordinates are arranged as
[ymin, xmin, ymax, xmax].
text: Tensor tf.string containing the human-readable label.
"""
# Dense features in Example proto.
feature_map = {
'image/encoded': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/class/label': tf.FixedLenFeature([1], dtype=tf.int64,
default_value=-1),
'image/class/text': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
}
sparse_float32 = tf.VarLenFeature(dtype=tf.float32)
# Sparse features in Example proto.
feature_map.update(
{k: sparse_float32 for k in ['image/object/bbox/xmin',
'image/object/bbox/ymin',
'image/object/bbox/xmax',
'image/object/bbox/ymax']})
features = tf.parse_single_example(example_serialized, feature_map)
label = tf.cast(features['image/class/label'], dtype=tf.int32)
xmin = tf.expand_dims(features['image/object/bbox/xmin'].values, 0)
ymin = tf.expand_dims(features['image/object/bbox/ymin'].values, 0)
xmax = tf.expand_dims(features['image/object/bbox/xmax'].values, 0)
ymax = tf.expand_dims(features['image/object/bbox/ymax'].values, 0)
# Note that we impose an ordering of (y, x) just to make life difficult.
bbox = tf.concat([ymin, xmin, ymax, xmax], 0)
# Force the variable number of bounding boxes into the shape
# [1, num_boxes, coords].
bbox = tf.expand_dims(bbox, 0)
bbox = tf.transpose(bbox, [0, 2, 1])
return features['image/encoded'], label, bbox, features['image/class/text']