-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatapipe.py
331 lines (268 loc) · 12 KB
/
datapipe.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
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import argparse
import os
import json
import glob
import random
import collections
import math
import time
from utils import *
def read_my_file_format(filename_queue, a):
"""Sets up part of the pipeline that takes elements from the filename queue
and turns it into a tf.Tensor of a batch of images.
:param filename_queue:
tf.train.string_input_producer object
:param resize_shape:
2 element list defining the shape to resize images to.
"""
reader = tf.TFRecordReader()
key, serialized_example = reader.read(filename_queue)
features, sequence_features = tf.parse_single_sequence_example(
serialized_example,
context_features={
'image/encoded': tf.FixedLenFeature([], tf.string),
'image/height': tf.FixedLenFeature([], tf.int64),
'image/channels': tf.FixedLenFeature([], tf.int64),
'image/width': tf.FixedLenFeature([], tf.int64),
# 'image/caption_embedding': tf.FixedLenFeature([], tf.string),
'image/sequence_length': tf.FixedLenFeature([], tf.int64)
},
sequence_features={
"caption": tf.FixedLenSequenceFeature([], dtype=tf.int64)
})
# inputs = [features, sequence_features]
# queue = tf.RandomShuffleQueue(CAPACITY, MIN_AFTER_DEQUEUE, dtypes)
# enqueue_op = queue.enqueue(inputs)
# qr = tf.train.QueueRunner(queue, [enqueue_op] * NUM_THREADS)
# tf.add_to_collection(tf.GraphKeys.QUEUE_RUNNERS, qr)
# features, sequence_features = queue.dequeue()
# if a.text_model == 'reed':
# captions = tf.decode_raw(features['image/caption_embedding'], tf.float32)
# captions.set_shape([1024])
if a.text_model == 'attention' or a.text_model == 'attention_fix' or a.text_model == 'attention_reasonet' or a.text_model == 'None':
captions = sequence_features["caption"]
sequence_length = features['image/sequence_length']
raw_input = tf.image.decode_jpeg(features['image/encoded'], 3)
raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)
assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels")
with tf.control_dependencies([assertion]):
raw_input = tf.identity(raw_input)
raw_input.set_shape([None, None, 3])
if True:
#a.lab_colorization:
# load color and brightness from image, no B image exists here
lab = rgb_to_lab(raw_input)
L_chan, a_chan, b_chan = preprocess_lab(lab)
a_images = tf.expand_dims(L_chan, axis=2)
b_images = tf.stack([a_chan, b_chan], axis=2)
# synchronize seed for image operations so that we do the same operations to both
# input and output images
seed = random.randint(0, 2**31 - 1)
def transform(image):
r = image
if a.flip:
r = tf.image.random_flip_left_right(r, seed=seed)
# area produces a nice downscaling, but does nearest neighbor for upscaling
# assume we're going to be doing downscaling here
r = tf.image.resize_images(r, [a.scale_size, a.scale_size], method=tf.image.ResizeMethod.AREA)
offset = tf.cast(tf.floor(tf.random_uniform([2], 0, a.scale_size - a.crop_size + 1, seed=seed)), dtype=tf.int32)
if a.scale_size > a.crop_size:
r = tf.image.crop_to_bounding_box(r, offset[0], offset[1], a.crop_size, a.crop_size)
elif a.scale_size < a.crop_size:
raise Exception("scale size cannot be less than crop size")
return r
with tf.name_scope("input_images"):
input_images = transform(a_images)
with tf.name_scope("target_images"):
target_images = transform(b_images)
return input_images, target_images, captions, sequence_length
def batcher(filenames, a):
"""Creates the batching part of the pipeline.
:param filenames:
list of filenames
:param batch_size:
size of batches that get output upon each access.
:param resize_shape:
for preprocessing. What to resize images to.
:param num_epochs:
number of epochs that define end of training set.
:param min_after_dequeue:
min_after_dequeue defines how big a buffer we will randomly sample
from -- bigger means better shuffling but slower start up and more
memory used.
capacity must be larger than min_after_dequeue and the amount larger
determines the maximum we will prefetch. Recommendation:
min_after_dequeue + (num_threads + a small safety margin) * batch_size
"""
Examples = collections.namedtuple("Examples", "inputs, targets, captions, sequence_lengths")
filename_queue = tf.train.string_input_producer(
filenames, num_epochs=a.max_epochs, shuffle=True)
# min_queue_examples = 1
# capacity = min_queue_examples + 100 * a.batch_size
# filename_queue = tf.RandomShuffleQueue(
# capacity=capacity,
# min_after_dequeue=min_queue_examples,
# dtypes=[tf.string],
# name="random_")
## Test filename queue is correct.
# filename_dequeue = filename_queue.dequeue()
# sv = tf.train.Supervisor(logdir=a.output_dir, save_summaries_secs=0, saver=None)
# with sv.managed_session() as sess:
# # initialize word embedding.
# # sess.run(model.emb_init, feed_dict = {model.embedding_placeholder: embedding_matrix})
# # initialize filename queue.
# coord = tf.train.Coordinator()
# threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# for i in range(40):
# print(sess.run(filename_dequeue), 'These are the filenames', i)
input_images, target_images, captions, sequence_lengths = read_my_file_format(filename_queue, a)
# inputs_batch, targets_batch, captions_batch = tf.train.shuffle_batch(
# [input_images, target_images, captions], batch_size=batch_size, capacity=min_after_dequeue + 3 * batch_size,
# min_after_dequeue=min_after_dequeue)
inputs_batch, targets_batch, captions_batch, sequence_lengths_batch = tf.train.batch(
[input_images, target_images, captions, sequence_lengths], batch_size=a.batch_size, capacity=32, dynamic_pad=True)
return Examples(
inputs=inputs_batch,
targets=targets_batch,
captions = captions_batch,
sequence_lengths = sequence_lengths_batch
)
def save_images(fetches, image_count, a, caption_file, id2word, step=None):
image_dir = os.path.join(a.output_dir, "images")
if not os.path.exists(image_dir):
os.makedirs(image_dir)
filesets = []
# captions = fetches['captions']
for i, _ in enumerate(fetches["inputs"]):
name = str(image_count)
fileset = {"name": name, "step": step}
cap = fetches["captions"][i]
if a.text_model.startswith('attention'):
word_caption = str(image_count) + ': ' + ' '.join([id2word[j] for j in cap]) + '\n'
caption_file.write(word_caption)
for kind in ["inputs", "outputs", "targets"]:
filename = name + "-" + kind + ".png"
if step is not None:
filename = "%08d-%s" % (step, filename)
fileset[kind] = filename
out_path = os.path.join(image_dir, filename)
contents = fetches[kind][i]
with open(out_path, "wb") as f:
f.write(contents)
filesets.append(fileset)
image_count += 1
return filesets, image_count
def append_index(filesets, a, step=False):
index_path = os.path.join(a.output_dir, "index.html")
if os.path.exists(index_path):
index = open(index_path, "a")
else:
index = open(index_path, "w")
index.write("<html><body><table><tr>")
if step:
index.write("<th>step</th>")
index.write("<th>name</th><th>input</th><th>output</th><th>target</th></tr>")
for fileset in filesets:
index.write("<tr>")
if step:
index.write("<td>%d</td>" % fileset["step"])
index.write("<td>%s</td>" % fileset["name"])
for kind in ["inputs", "outputs", "targets"]:
index.write("<td><img src='images/%s'></td>" % fileset[kind])
index.write("</tr>")
return index_path
def convert_to_normal(model, examples):
# undo colorization splitting on images that we use for display/output
# inputs is brightness, this will be handled fine as a grayscale image
# need to augment targets and outputs with brightness
targets = augment(examples.targets, examples.inputs)
outputs = augment(model.outputs, examples.inputs)
# inputs can be deprocessed normally and handled as if they are single channel
# grayscale images
inputs = deprocess(examples.inputs)
def convert(image):
# if a.aspect_ratio != 1.0:
# # upscale to correct aspect ratio
# size = [CROP_SIZE, int(round(CROP_SIZE * a.aspect_ratio))]
# image = tf.image.resize_images(image, size=size, method=tf.image.ResizeMethod.BICUBIC)
return tf.image.convert_image_dtype(image, dtype=tf.uint8, saturate=True)
# reverse any processing on images so they can be written to disk or displayed to user
with tf.name_scope("convert_inputs"):
converted_inputs = convert(inputs)
with tf.name_scope("convert_targets"):
converted_targets = convert(targets)
with tf.name_scope("convert_outputs"):
converted_outputs = convert(outputs)
with tf.name_scope("encode_images"):
display_fetches = {
# "paths": examples.paths,
"inputs": tf.map_fn(tf.image.encode_png, converted_inputs, dtype=tf.string, name="input_pngs"),
"targets": tf.map_fn(tf.image.encode_png, converted_targets, dtype=tf.string, name="target_pngs"),
"outputs": tf.map_fn(tf.image.encode_png, converted_outputs, dtype=tf.string, name="output_pngs"),
"captions": examples.captions
}
return converted_inputs, converted_targets, converted_outputs, display_fetches
from extract_cap import tokenize_caption, extract_token_id
from PIL import Image
def create_test_batcher(filenames, caption_strs, embedding, a):
# model = gensim.models.KeyedVectors.load_word2vec_format('flower/GoogleNews-vectors-negative300.bin', binary = True)
caption_strs = {'test': caption_strs}
tokens = tokenize_caption(caption_strs)
token_ids = extract_token_id(tokens, embedding['word2id'].keys(), embedding['word2id'])['test'] # list of caption ids.
sequence_lengths_array = [len(token) for token in token_ids]
image_matrices = []
for filename in filenames:
image_matrices.append(np.array(Image.open(filename)))
raw_caption_id = tf.placeholder(tf.int64, [None], name = 'caption')
raw_image_ = tf.placeholder(tf.uint8, [None, None, 3], name = 'image')
raw_sequence_lengths = tf.placeholder(tf.int64, name = 'sequence_length')
raw_image = tf.image.convert_image_dtype(raw_image_, dtype=tf.float32)
assertion = tf.assert_equal(tf.shape(raw_image)[2], 3, message="image does not have 3 channels")
with tf.control_dependencies([assertion]):
raw_image = tf.identity(raw_image)
raw_image.set_shape([None, None, 3])
if True:#a.lab_colorization:
# load color and brightness from image, no B image exists here
lab = rgb_to_lab(raw_image)
L_chan, a_chan, b_chan = preprocess_lab(lab)
a_images = tf.expand_dims(L_chan, axis=2)
b_images = tf.stack([a_chan, b_chan], axis=2)
# synchronize seed for image operations so that we do the same operations to both
# input and output images
seed = random.randint(0, 2**31 - 1)
def transform(image):
r = image
if a.flip:
r = tf.image.random_flip_left_right(r, seed=seed)
# area produces a nice downscaling, but does nearest neighbor for upscaling
# assume we're going to be doing downscaling here
r = tf.image.resize_images(r, [a.scale_size, a.scale_size], method=tf.image.ResizeMethod.AREA)
offset = tf.cast(tf.floor(tf.random_uniform([2], 0, a.scale_size - a.crop_size + 1, seed=seed)), dtype=tf.int32)
if a.scale_size > a.crop_size:
r = tf.image.crop_to_bounding_box(r, offset[0], offset[1], a.crop_size, a.crop_size)
elif a.scale_size < a.crop_size:
raise Exception("scale size cannot be less than crop size")
return r
with tf.name_scope("input_images"):
input_images = transform(a_images)
with tf.name_scope("target_images"):
target_images = transform(b_images)
Examples = collections.namedtuple("Examples",
"images, token_ids, sequence_lengths_array, raw_caption_id, raw_image, raw_sequence_lengths, inputs, targets, captions, sequence_lengths")
return Examples(
images=image_matrices,
token_ids=token_ids,
sequence_lengths_array=sequence_lengths_array,
raw_caption_id=raw_caption_id,
raw_image=raw_image_,
raw_sequence_lengths = raw_sequence_lengths,
inputs=tf.expand_dims(input_images,axis = 0),
targets=tf.expand_dims(target_images,axis = 0),
captions = tf.expand_dims(raw_caption_id, axis = 0),
sequence_lengths = tf.expand_dims(raw_sequence_lengths, axis = 0)
)