Common representation shape of image tensor
(IDX,HEIGHT,WIDTH,CHANNEL)Grey scale:
(IDX,HEIGHT,WIDTH,1)RGB:
(IDX,HEIGHT,WIDTH,3)CMYK:(Cyan, Magenta,Yellow and Key)
(IDX,HEIGHT,WIDTH,4)uint pixels are in range 0-255
float pixels are in range 0-1
import glob
files_path = 'pics/*.png'
filenames = glob.glob(files_path)
print(filenames)
from PIL import Image
image = Image.open(filenames[2])
print(image)
image # This is a PIL Object
import numpy as np
image_array = np.asarray(image)
print(image_array.shape)
Image.fromarray(image_array,'RGBA') #Here, Image mode should be same as image.mode
import tensorflow as tf
!rm tensorboard_logs/*
tf.reset_default_graph()
image_tensor = tf.convert_to_tensor(image_array) #returns dtype = unit8
#image_tensor = tf.convert_to_tensor(image) #Same as above. Works on both numpy and PIL objects
print(image_tensor)
#Make it 4D tensor. 4D tensor is required for summary
image_tensor = tf.expand_dims(image_tensor,0)
print(image_tensor)
#Without allow_growth set to true, this program will block the entire gou resource
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
with tf.Session(config=config) as sess:
writer = tf.summary.FileWriter('./tensorboard_logs/',sess.graph)
#Get image summary string and add it to writer
summary_str = sess.run(tf.summary.image("image_name",image_tensor))
writer.add_summary(summary_str)
writer.close()
!tensorboard --logdir=tensorboard_logs/
Reading image files can be slow in python.
For efficient reading of media files, which is critical for machine learning applilcations, we use coordinators
Get the list of image names
files_path = 'pics/*.png'
filenames = glob.glob(files_path)
print(filenames)
Create queue with image name list
tf.reset_default_graph()
filename_queue = tf.train.string_input_producer(filenames,seed=0)
print(filename_queue)
WARNING:tensorflow:From <ipython-input-12-4ee5c4b25f12>:2: string_input_producer (from tensorflow.python.training.input) is deprecated and will be removed in a future version.
Instructions for updating:
Queue-based input pipelines have been replaced by `tf.data`. Use `tf.data.Dataset.from_tensor_slices(string_tensor).shuffle(tf.shape(input_tensor, out_type=tf.int64)[0]).repeat(num_epochs)`. If `shuffle=False`, omit the `.shuffle(...)`.
Create reader with filename_queue
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue) #Gets the images in the queue one by one
print(key,value)
WARNING:tensorflow:From <ipython-input-53-9f6a10a31bd1>:1: WholeFileReader.__init__ (from tensorflow.python.ops.io_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Queue-based input pipelines have been replaced by `tf.data`. Use `tf.data.Dataset.map(tf.read_file)`.
Decode the image
Can handle BMP, GIF, JPEG, or PNG
image_tensor = tf.image.decode_png(value)
print(image_tensor)
Run threads
Note:
Running the image_tensor every time, under a session will pull the next image in queue
!rm tensorboard_logs/*
#Without allow_growth set to true, this program will block the entire gou resource
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
with tf.Session(config=config) as sess:
writer = tf.summary.FileWriter('./tensorboard_logs/',sess.graph)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
for i in range(len(filenames)):
image_array,summary_str = sess.run([image_tensor,tf.summary.image("image_" + str(i) ,tf.expand_dims(image_tensor,0))])
print(image_array.shape)
#NOTE: Running this line separately will pull the next image in queue
#summary_str = sess.run(tf.summary.image("image_" + str(i) ,tf.expand_dims(image_tensor,0)))
writer.add_summary(summary_str)
writer.close()
coord.request_stop()
coord.join(threads)
!tensorboard --logdir=tensorboard_logs/
WARNING:tensorflow:From <ipython-input-57-bae544162e8f>:3: start_queue_runners (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
Wrong way to work with queues
def run_tf(x):
#Without allow_growth set to true, this program will block the entire gou resource
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
with tf.Session(config=config) as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
out = sess.run(x)
coord.request_stop()
coord.join(threads)
return out
Running the image_tensor in different sessions will result in the same image being pulled!
image_array = run_tf(image_tensor)
print(image_array.shape)
image_array = run_tf(image_tensor)
print(image_array.shape)
image_array = run_tf(image_tensor)
print(image_array.shape)
##Keeps pulling the same image because the scope of tf.Session is new for every run
To convert a list of (H,W,C) images to a 4D tensor, use tf.stack
[(H,W,C), (H,W,C), ...] >> [N,H,W,C]Used to pass tensors in batch. tf.summary.image can also handle batch of images
l = [[1,2],[2,3],[3,4],[4.1,5]]
tf.stack(l)