-
Notifications
You must be signed in to change notification settings - Fork 1
/
srez_input.py
57 lines (44 loc) · 2.08 KB
/
srez_input.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
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
def setup_inputs(sess, filenames, image_size=None, capacity_factor=3, istrain = True):
if image_size is None:
image_size = FLAGS.sample_size
# Read each JPEG file
reader = tf.WholeFileReader()
filename_queue = tf.train.string_input_producer(filenames)
key, value = reader.read(filename_queue)
channels = 3
image = tf.image.decode_jpeg(value, channels=channels, name="dataset_image")
image.set_shape([None, None, channels])
# Crop and other random augmentations
if istrain:
image = tf.image.random_flip_left_right(image)
image = tf.image.random_saturation(image, .95, 1.05)
image = tf.image.random_brightness(image, .05)
image = tf.image.random_contrast(image, .95, 1.05)
wiggle = 8
off_x, off_y = 25-wiggle, 60-wiggle
crop_size = 128
crop_size_plus = crop_size + 2*wiggle
image = tf.image.crop_to_bounding_box(image, off_y, off_x, crop_size_plus, crop_size_plus)
if istrain:
image = tf.random_crop(image, [crop_size, crop_size, 3])
else:
image = tf.image.resize_image_with_crop_or_pad(image, crop_size, crop_size)
image = tf.reshape(image, [1, crop_size, crop_size, 3])
image = tf.cast(image, tf.float32)/255.0
if crop_size != image_size:
image = tf.image.resize_area(image, [image_size, image_size])
# The feature is simply a Kx downscaled version
K = 4
downsampled = tf.image.resize_area(image, [image_size//K, image_size//K])
feature = tf.reshape(downsampled, [image_size//K, image_size//K, 3])
label = tf.reshape(image, [image_size, image_size, 3])
# Using asynchronous queues
features, labels = tf.train.batch([feature, label],
batch_size=FLAGS.batch_size,
num_threads=4,
capacity = capacity_factor*FLAGS.batch_size,
name='labels_and_features')
tf.train.start_queue_runners(sess=sess)
return features, labels