-
Notifications
You must be signed in to change notification settings - Fork 100
/
preprocessing.py
280 lines (234 loc) · 9.42 KB
/
preprocessing.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
# coding=utf-8
# Copyright 2019 The Google NoisyStudent Team Authors.
#
# 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.
'''ImageNet preprocessing.'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import randaugment
IMAGE_SIZE = 224
CROP_PADDING = 32
FLAGS = tf.app.flags.FLAGS
def _distorted_bounding_box_crop(image,
bbox,
min_object_covered=0.1,
aspect_ratio_range=(0.75, 1.33),
area_range=(0.05, 1.0),
max_attempts=100,
scope=None):
'''Generates cropped_image using one of the bboxes randomly distorted.
See `tf.image.sample_distorted_bounding_box` for more documentation.
Args:
image_bytes: `Tensor` of binary image data.
bbox: `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]`. If num_boxes is 0 then use the whole
image.
min_object_covered: An optional `float`. Defaults to `0.1`. The cropped
area of the image must contain at least this fraction of any bounding
box supplied.
aspect_ratio_range: An optional list of `float`s. The cropped area of the
image must have an aspect ratio = width / height within this range.
area_range: An optional list of `float`s. The cropped area of the image
must contain a fraction of the supplied image within in this range.
max_attempts: An optional `int`. Number of attempts at generating a cropped
region of the image of the specified constraints. After `max_attempts`
failures, return the entire image.
scope: Optional `str` for name scope.
Returns:
cropped image `Tensor`
'''
with tf.name_scope(scope, 'distorted_bounding_box_crop', [image, bbox]):
shape = tf.shape(image)
sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box(
shape,
bounding_boxes=bbox,
min_object_covered=min_object_covered,
aspect_ratio_range=aspect_ratio_range,
area_range=area_range,
max_attempts=max_attempts,
use_image_if_no_bounding_boxes=True)
bbox_begin, bbox_size, _ = sample_distorted_bounding_box
image = tf.slice(image, bbox_begin, bbox_size)
return image
def _at_least_x_are_equal(a, b, x):
'''At least `x` of `a` and `b` `Tensors` are equal.'''
match = tf.equal(a, b)
match = tf.cast(match, tf.int32)
return tf.greater_equal(tf.reduce_sum(match), x)
def _random_crop(ori_image, image_size):
'''Make a random crop of image_size.'''
original_shape = tf.shape(ori_image)
bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
image = _distorted_bounding_box_crop(
ori_image,
bbox,
min_object_covered=0.1,
aspect_ratio_range=(3. / 4, 4. / 3.),
area_range=(0.08, 1.0),
max_attempts=10,
scope=None)
bad = _at_least_x_are_equal(original_shape, tf.shape(image), 3)
image = tf.cond(
bad,
lambda: center_crop(ori_image, image_size),
lambda: tf.image.resize_bicubic([image], # pylint: disable=g-long-lambda
[image_size, image_size])[0])
return image
def center_crop(image, image_size):
'''Crops to center of image with padding then scales image_size.'''
if FLAGS.small_image_model:
return image
shape = tf.shape(image)
image_height = shape[0]
image_width = shape[1]
padded_center_crop_size = tf.cast(
((image_size / (image_size + CROP_PADDING)) *
tf.cast(tf.minimum(image_height, image_width), tf.float32)),
tf.int32)
offset_height = ((image_height - padded_center_crop_size) + 1) // 2
offset_width = ((image_width - padded_center_crop_size) + 1) // 2
bbox_begin = tf.stack([
offset_height, offset_width,
tf.constant(0, dtype=tf.int32)
])
bbox_size = tf.stack([
padded_center_crop_size, padded_center_crop_size,
tf.constant(-1, dtype=tf.int32)
])
bbox_begin = tf.Print(bbox_begin, [bbox_begin], message='bbox_begin \n\n\n', summarize=1000)
bbox_size = tf.Print(bbox_size, [bbox_size], message='bbox_size \n\n\n', summarize=1000)
image = tf.slice(image, bbox_begin, bbox_size)
image = tf.image.resize_bicubic([image], [image_size, image_size])[0]
return image
def _flip(image):
'''Random horizontal image flip.'''
image = tf.image.random_flip_left_right(image)
return image
def small_image_crop(image):
amount = 4
pad_inp = tf.pad(image,
tf.constant([[amount, amount],
[amount, amount],
[0, 0]]),
'REFLECT')
cropped_data = tf.random_crop(pad_inp, tf.shape(image))
return cropped_data
def _preprocess_for_train(image, use_bfloat16, image_size=IMAGE_SIZE,
augment_name=None, randaug_mag=None,
randaug_layer=None):
'''Preprocesses the given image for evaluation.
Args:
image_bytes: `Tensor` representing an image binary of arbitrary size.
use_bfloat16: `bool` for whether to use bfloat16.
image_size: image size.
Returns:
A preprocessed image `Tensor`.
'''
image = _random_crop(image, image_size)
image = _flip(image)
image = tf.reshape(image, [image_size, image_size, 3])
image = tf.cast(
image, dtype=tf.bfloat16 if use_bfloat16 else tf.float32)
if augment_name:
input_image_type = image.dtype
tf.logging.info('Apply augment {}'.format(augment_name))
image = tf.clip_by_value(image, 0.0, 255.0)
image = tf.cast(image, dtype=tf.uint8)
if augment_name == 'v1':
image = randaugment.distort_image_with_randaugment(
image, randaug_layer or FLAGS.randaug_layer,
randaug_mag or FLAGS.randaug_mag)
else:
assert False
image = tf.cast(image, dtype=input_image_type)
return image
def _cifar10_preprocess_for_train(image, use_bfloat16, image_size=IMAGE_SIZE,
augment_name=None, randaug_mag=None,
randaug_layer=None):
'''Preprocesses the given image for evaluation.
Args:
image_bytes: `Tensor` representing an image binary of arbitrary size.
use_bfloat16: `bool` for whether to use bfloat16.
image_size: image size.
Returns:
A preprocessed image `Tensor`.
'''
image = tf.reshape(image, [image_size, image_size, 3])
image = tf.cast(
image, dtype=tf.bfloat16 if use_bfloat16 else tf.float32)
if augment_name:
input_image_type = image.dtype
image = tf.clip_by_value(image, 0.0, 255.0)
image = tf.cast(image, dtype=tf.uint8)
if augment_name == 'v1':
image = randaugment.distort_image_with_randaugment(
image, randaug_layer or FLAGS.randaug_layer,
randaug_mag or FLAGS.randaug_mag,
cutout_const=image_size // 8, translate_const=image_size // 8,
)
image = randaugment.cutout(image, pad_size=image_size // 4, replace=128)
image = tf.cast(image, dtype=input_image_type)
assert image_size == 32
image = small_image_crop(image)
if FLAGS.task_name != 'svhn':
image = _flip(image)
return image
def _preprocess_for_eval(input_tensor, use_bfloat16, image_size=IMAGE_SIZE):
'''Preprocesses the given image for evaluation.
Args:
image_bytes: `Tensor` representing an image binary of arbitrary size.
use_bfloat16: `bool` for whether to use bfloat16.
image_size: image size.
Returns:
A preprocessed image `Tensor`.
'''
image = center_crop(input_tensor, image_size)
image = tf.reshape(image, [image_size, image_size, 3])
image = tf.cast(
image, dtype=tf.bfloat16 if use_bfloat16 else tf.float32)
return image
def preprocess_image(input_tensor,
is_training=False,
use_bfloat16=False,
image_size=IMAGE_SIZE,
is_image_bytes=True,
augment_name=None,
randaug_mag=None,
randaug_layer=None):
'''Preprocesses the given image.
Args:
image_bytes: `Tensor` representing an image binary of arbitrary size.
is_training: `bool` for whether the preprocessing is for training.
use_bfloat16: `bool` for whether to use bfloat16.
image_size: image size.
Returns:
A preprocessed image `Tensor` with value range of [0, 255].
'''
if is_image_bytes:
image = tf.image.decode_jpeg(input_tensor, channels=3)
else:
image = input_tensor
if is_training:
if FLAGS.small_image_model:
return _cifar10_preprocess_for_train(
image, use_bfloat16, image_size,
augment_name, randaug_mag, randaug_layer)
else:
return _preprocess_for_train(
image, use_bfloat16, image_size,
augment_name, randaug_mag, randaug_layer)
else:
return _preprocess_for_eval(image, use_bfloat16, image_size)