2

I have two arrays of shape (600,) containing images and labels. When I try to pass them to Keras/Tensorflow in any form I get the error:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

As far as I understand the images are stored in an array of arrays. When observing the inner arrays (single images) they have the following properties:

Array of dtype=uint8 with shape: (x, 500, 3) where x is between 300 and 500.

I was able to apply tf layers on the array of images via pandas.apply in the hope the issue was in the inconsistent size of the images:

resize_and_rescale = tf.keras.Sequential([
  tf.keras.layers.experimental.preprocessing.Resizing(IMG_SIZE, IMG_SIZE),
  tf.keras.layers.experimental.preprocessing.Rescaling(1./255)
])
train_df.image = train_df.image.apply(resize_and_rescale)

This code executed successful, but the resulting eager tensors are still not compatible with tensorflow:

train_dataset = tf.data.Dataset.from_tensor_slices((train_df.image.values, train_df.label.values))

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor).

How can I load the array of images into tf?

I already tried the following load functions unsuccessfully:

NumpyArrayIterator

from_tensor_slices

ImageDataGenerator.flow

1
  • You can try converting your array to float32 but in case it doesn't work i recommend trying to look for empty values in your dataframe Commented May 19, 2021 at 17:19

1 Answer 1

1

Firstly you should try converting the input of from_tensor_slices into a list of 600 arrays. The function is currently consider it as only 1 sample, an array with the first shape of 600, thus creating the error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.