Reshaping problem (Input to reshape is a tensor with 10 values, but the requested shape has 1)


I’m trying to recreate this work using my own dataset:

I’ve made some slight tweaks to the code to accommodate my data but I don’t think that is what is causing an issue here; it could be though of course.

My code:

IMAGE_SIZE = [180, 180]
CLASS_NAMES = np.array(['active', 'inactive'])

train_list_ds ='{WORKING}/train/*/*'))
val_list_ds ='{WORKING}/val/*/*'))
test_list_ds ='{WORKING}/test/*/*'))


def get_label(path):
  # switcher that converts the keywords to 1 or zero
  if tf.strings.split(path, os.path.sep) == "inactive":
    return 0 # inactive
    return 1 # active

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  resizedImage = tf.image.resize(img, IMAGE_SIZE)
  return resizedImage

def process_path(file_path):
    label = get_label(file_path)
    # load the raw data from the file as a string
    img =
    img = decode_img(img)
    return img, label

train_ds =, num_parallel_calls=AUTOTUNE)
val_ds =, num_parallel_calls=AUTOTUNE)
test_ds =, num_parallel_calls=AUTOTUNE)
test_ds = test_ds.batch(BATCH_SIZE)

image_batch, label_batch = next(iter(train_ds))

And the error:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-21-d0b00c7e96e2> in <module>
     68 test_ds = test_ds.batch(BATCH_SIZE)
---> 70 image_batch, label_batch = next(iter(train_ds))
     72 # Use buffered prefetching so we can yield data from disk without having I/O become blocking.

3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ in raise_from_not_ok_status(e, name)
   7184 def raise_from_not_ok_status(e, name):
   7185   e.message += (" name: " + name if name is not None else "")
-> 7186   raise core._status_to_exception(e) from None  # pylint: disable=protected-access

InvalidArgumentError: Input to reshape is a tensor with 10 values, but the requested shape has 1
     [[{{node Reshape}}]] [Op:IteratorGetNext]

I can gather from the error that I have a mismatch in resizing, I believe it is pointing to the wrong place though and it is something to do with the line resizedImage = tf.image.resize(img, IMAGE_SIZE)

I’ve looked through the docs and on SO but I can’t find anything that might help me. I’ve tried debugging as best I can by printing things out on screen. I also tried changing the IMAGE_SIZE but that made no difference.

On the images, they are different sizes on disk, in JPG form. I expected that not to matter as we can use this step tot resize them for processing by the model later.

Other useful information is that I’m working on the pro version of Google Collab and the files are stored in a Google drive location. Again I don’t think that’s a problem or cause of anything; I’m trying to write things pre-emptively that people might ask.

Finally there is a prepare_for_training function in Amy’s code on Kaggle that is called before the last line I provided in my code above. I can trigger the same error without calling that function so I omitted it intentionally to help keep the example code cleaner. If you want to see it, here is a quick link to it in the notebook:


May be the problem come from the get_label(path) method, since you are splitting path according the sep, it will return a list of many elements, make sure that you select only one element to perform test, try this :

tf.strings.split(path, os.path.sep)[-1] == "inactive"

i assume that the last element is the label, you just replace this index by the position of the label in this list.

Answered By – Mohamed El Amine BELLEBNA

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published