forked from hzy46/fast-neural-style-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
26 lines (19 loc) · 1.08 KB
/
reader.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
from os import listdir
from os.path import isfile, join
import tensorflow as tf
def get_image(path, height, width, preprocess_fn):
png = path.lower().endswith('png')
img_bytes = tf.read_file(path)
image = tf.image.decode_png(img_bytes, channels=3) if png else tf.image.decode_jpeg(img_bytes, channels=3)
return preprocess_fn(image, height, width)
def image(batch_size, height, width, path, preprocess_fn, epochs=2, shuffle=True):
filenames = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
if not shuffle:
filenames = sorted(filenames)
png = filenames[0].lower().endswith('png') # If first file is a png, assume they all are
filename_queue = tf.train.string_input_producer(filenames, shuffle=shuffle, num_epochs=epochs)
reader = tf.WholeFileReader()
_, img_bytes = reader.read(filename_queue)
image = tf.image.decode_png(img_bytes, channels=3) if png else tf.image.decode_jpeg(img_bytes, channels=3)
processed_image = preprocess_fn(image, height, width)
return tf.train.batch([processed_image], batch_size, dynamic_pad=True)