Moondra
Moondra

Reputation: 4511

InvalidArgumentError: Input to reshape is a tensor with 178802 values, but the requested shape has 89401

I'm running into another invalid argument error and not really sure what the reason is this time.

I created a TFRecord with images (mixed extensions as far I know) of shape [299,299].

I'm trying to load the images in batches, but I'm running into this error:

'InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 178802 values, but the requested shape has 89401
     [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](DecodeRaw, Reshape/shape)]]

Here is my code:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os

IMAGE_DIR =r'C:\Users\Moondra\Desktop\TF_FISH_PROJECT\FINAL_FISHES'

data_path = r'E:\TFRECORDS\normal_fish_conversion_2.tfrecords'  

with tf.Session() as sess:
    feature = {'train/image': tf.FixedLenFeature([], tf.string),
               'train/label': tf.FixedLenFeature([], tf.int64),
               'rows':  tf.FixedLenFeature([], tf.int64),
                'columns':  tf.FixedLenFeature([], tf.int64)}

    # Create a list of filenames and pass it to a queue
    filename_queue = tf.train.string_input_producer([data_path], num_epochs=1000)

    # Define a reader and read the next record
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)

    # Decode the record read by the reader
    features = tf.parse_single_example(serialized_example, features=feature)

    # Convert the image data from string back to the numbers
    image = tf.decode_raw(features['train/image'], tf.float32)

    # Cast label data into int32
    label = tf.cast(features['train/label'], tf.int32)

    # Reshape image data into the original shape
    image = tf.reshape(image, [299, 299])
    print(image.shape) #shape is printing out correctly


    # Creates batches by randomly shuffling tensors
    #images, labels = tf.train.shuffle_batch([image, label], batch_size=50, capacity=10000, num_threads=3, min_after_dequeue=2000)
    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    sess.run(init_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for batch_index in range(5):
            img  = sess.run([image])
            img = img.astype(np.uint8)
            print(img.shape)





    coord.request_stop()
    coord.join(threads)
    sess.close()

I'm not really sure how to debug this..

The first print statement( reshaped_image.shape) is printing out a (299,299) shape, so not sure what the problem is.

Thank you.

Upvotes: 5

Views: 4885

Answers (1)

tsveti_iko
tsveti_iko

Reputation: 7972

What I needed to do is decoding the image to JPEG, converting it to float, expanding its dimensions and then resizing it using bilinear interpolation like this:

image = tf.image.decode_jpeg(features['train/image'], channels=3)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, [299, 299], align_corners=False)

NOTE:

  • Your images should be already stored in JPEG format (when creating the TFRecords).
  • You can instead set the channels to 1 if your images are grayscale or save the number of channels for every image in your TFRecords and get it from there dynamically (different for every image).

Upvotes: 3

Related Questions