Reputation: 143
I've had a lot of difficulty for the past few days trying to import images from my desktop into Tensorflow. I've looked at the API and searched for some online tutorials, however, my luck has been quite low and I haven't been able to understand much of what I've read. My attempts to create a function to import images from what little I've understood has been unsurprisingly unsuccessful. The following is what I've done so far. After running it through a debugger, I see that the program is getting stuck on the first line. The Tensorflow API says that I must pass a file pattern or a 1D tensor of file patterns. Isn't the file path I've given a file pattern?
def import_data():
image_path = tf.train.match_filenames_once("C:\Users\ratno\Desktop\honest chaos\*.JPG")
filename_queue = tf.train.string_input_producer(image_path)
reader = tf.WholeFileReader()
_, content = reader.read(filename_queue)
image = tf.image.decode_jpeg(content, channels=1)
cropped_image = tf.image.resize_image_with_crop_or_pad(image, 3000, 3000)
reduced_image = tf.image.resize_images(cropped_image, [100, 100])
modified_image = tf.transpose(tf.reshape(reduced_image, [10000, 1]))
return modified_image
The code is supposed to take in a slew of jpgs from a folder on my desktop and convert them from RGB jpgs to grayscale jpgs. Afterwards, it'll take the grayscale jpgs, and crop them to 3000x3000 pixel sizes, and use tf.resize_images to further reduce them to 100x100 pixel images. Finally, it'll return everything in the form of a 1x10000 shaped tensor.
Thanks in advance. And if there are any suggestions for other parts of the code, I'd be very grateful.
Upvotes: 3
Views: 2200
Reputation: 311
def _parse_function(filename):
image_string = tf.read_file(filename)
image_decoded = tf.cond(
tf.image.is_jpeg(image_string),
lambda: tf.image.decode_jpeg(image_string, channels=3),
lambda: tf.image.decode_png(image_string, channels=3))
image_resized = tf.image.resize_images(image_decoded, [90, 90])
return image_resized
filenames = ["/var/data/image1.jpg", "/var/data/image2.jpg", ...]
labels = [0, 37, 29, 1, ...]
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.map(_parse_function)
dataset = dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
Upvotes: 1
Reputation: 5206
To read multiple images in a single call to session.run simply call reader.read() many times. Alternatively, you can call it once and use tf.train.batch once you're done processing to get a minibatch of images.
Upvotes: 1