-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_reader.py
More file actions
30 lines (22 loc) · 1.27 KB
/
flow_reader.py
File metadata and controls
30 lines (22 loc) · 1.27 KB
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
from os import listdir, walk
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=False):
#filenames = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
filenames = []
for root, dirs, files in walk(path):
filenames += [join(root, name) for name in files]
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([image], batch_size, dynamic_pad=True), tf.train.batch([processed_image], batch_size, dynamic_pad=True)