Failed To Convert A Numpy Array To A Tensor (unsupported Object Type Numpy.ndarray) On Imagedatagenerator In Keras
Solution 1:
I have succeeded in reproducing the error.
The error appears when the line np.array([cv2.imread(file_name).astype(np.int) for file_name in batch_x])
in My_Custom_Generator.__getitem__
does not create a multi-dimensional array, but instead it creates an array with dtype=np.object
that is just a list of arrays.
That happens when the shapes of the arrays in the list (i.e. the shapes of the images) are not identical, as you can see from the example below.
np.array([np.zeros((4, 4, 3)), np.zeros((5, 5, 3))])
>>> array([array([[[0., 0., 0.], ..., [0., 0., 0.]]]),
array([[[0., 0., 0.], ..., [0., 0., 0.]]])], dtype=object)
Running the code in the example the following deprecation warning is visualized: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. np.array([np.zeros((4, 4, 3)), np.zeros((5, 5, 3))]).
In general is not a good practice to stack arrays in a list through the function np.array
, it is recommended to use np.stack
that allows to catch this type of errors in advance.
Replacing np.array([cv2.imread(file_name).astype(np.int) for file_name in batch_x])
with np.stack([cv2.imread(file_name).astype(np.int) for file_name in batch_x], axis=0)
, you should see a much more meaningful error stack trace.
To conclude, to solve the problem you should check the shapes of your images to find the image that breaks your code.
Post a Comment for "Failed To Convert A Numpy Array To A Tensor (unsupported Object Type Numpy.ndarray) On Imagedatagenerator In Keras"