-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdcgan64_input.py
87 lines (69 loc) · 3.16 KB
/
dcgan64_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
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
# Copyright 2017 The TensorFlow Authors. 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.
# ==============================================================================
"""CIFAR example using input pipelines."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import flags
import numpy as np
from PIL import Image
import tensorflow as tf
FLAGS = flags.FLAGS
flags.DEFINE_string('train_data_file_64', '',
'Path to CIFAR10 training data.')
flags.DEFINE_string('test_data_file_64', '', 'Path to CIFAR10 test data.')
image_size = 64
def parser(serialized_example):
"""Parses a single tf.Example into image and label tensors."""
features = tf.parse_single_example(
serialized_example,
features={
'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
image = tf.decode_raw(features['image'], tf.uint8)
image.set_shape([image_size*image_size*3])
# Normalize the values of the image from the range [0, 255] to [-1.0, 1.0]
image = tf.cast(image, tf.float32) * (2.0 / 255) - 1.0
# image = tf.transpose(tf.reshape(image, [3, 32*32])) Not needed as ourimage is already in shape image_size * image_size * 3
label = tf.cast(features['label'], tf.int32)
return image, label
class InputFunction(object):
"""Wrapper class that is passed as callable to Estimator."""
def __init__(self, is_training, noise_dim):
self.is_training = is_training
self.noise_dim = noise_dim
self.data_file = (FLAGS.train_data_file_64 if is_training
else FLAGS.test_data_file_64)
def __call__(self, params):
batch_size = params['batch_size']
dataset = tf.data.TFRecordDataset([self.data_file])
dataset = dataset.map(parser, num_parallel_calls=batch_size)
dataset = dataset.prefetch(4 * batch_size).cache().repeat()
dataset = dataset.batch(batch_size, drop_remainder=True)
dataset = dataset.prefetch(2)
images, labels = dataset.make_one_shot_iterator().get_next()
# Reshape to give inputs statically known shapes.
images = tf.reshape(
images, [batch_size, image_size, image_size, 3])
random_noise = tf.random_normal([batch_size, self.noise_dim])
features = {
'real_images': images,
'random_noise': random_noise}
return features, labels
def convert_array_to_image(array):
"""Converts a numpy array to a PIL Image and undoes any rescaling."""
img = Image.fromarray(np.uint8((array + 1.0) / 2.0 * 255), mode='RGB')
return img