-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreprocessor_cifar.py
40 lines (29 loc) · 1.52 KB
/
Preprocessor_cifar.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
import tensorflow as tf
slim = tf.contrib.slim
_PADDING = 4
class PreprocessorCIFAR:
def __init__(self, target_shape):
self.target_shape = target_shape
def process_train(self, image, padding=_PADDING):
image = tf.to_float(image)
if padding > 0:
image = tf.pad(image, [[padding, padding], [padding, padding], [0, 0]])
# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(image, self.target_shape)
# 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)
# Subtract off the mean and divide by the variance of the pixels.
return tf.image.per_image_standardization(distorted_image)
def process_test(self, image):
# Transform the image to floats.
image = tf.to_float(image)
# Resize and crop if needed.
resized_image = tf.image.resize_image_with_crop_or_pad(image, self.target_shape[0], self.target_shape[1])
# Subtract off the mean and divide by the variance of the pixels.
return tf.image.per_image_standardization(resized_image)