Reputation: 101
I am using a script for loading images to TensorFlow which apparently works for everyone, but when I try it, I end up with black image (zero matrix). I tried it with several image files, and it's always zero, and when I deliberately misspell the image location string, it reports an error (as it should). Size of the returned image tensor is correct (256,256,3)
. This is the script, does someone sees the error?
file_names = ['/home/marko/Data/train_27.jpg']
filename_queue = tf.train.string_input_producer(file_names)
image_reader = tf.WholeFileReader()
title, image_file = image_reader.read(filename_queue)
image = tf.image.decode_jpeg(image_file,channels=3)
with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
image_tensor = sess.run(image)
print(image_tensor)
print(image_tensor.shape)
coord.request_stop()
coord.join(threads)
Upvotes: 2
Views: 1252
Reputation: 79
Your code is correct. I had the same problem with images from Kaggle competition
Seems like Tensorflow detects colorspace incorrectly for these files, or the colorspace information encoded in the images is incorrect.
It seems like Tensorflow doesn't allow to enforce colorspace while decoding images. So probably the easiest way is to "fix" the images.
I've used 'convert' utility from ImageMagic toolkit:
ls train-jpg/ | \
parallel convert -colorspace sRGB train-jpg/{} fixed-train-jpg/{}
Upvotes: 2
Reputation: 409
I have another solution that does not require conversion via command line as suggested by Evgeny, rather uses Pillow for loading the images:
import numpy as np
from PIL import Image
def load_image(img_path, resize=[256, 256]):
pil_img = Image.open(img_path).convert("RGB")
img = np.asarray(pil_img) / 255
img_tensor = tf.convert_to_tensor(img)
img_final = tf.image.resize(img_tensor, resize)
return img_final
Upvotes: 1