Problem description
When an array is sent to tensorflow training, the following errors are reported:
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
The array element is an array, and the shape of each array element is inconsistent. An example is as follows:
cropImg[0].shape = (13, 13, 3)
cropImg[1].shape = (14, 13, 3)
cropImg[2].shape = (12, 13, 3)
environment
python 3.7. 9
tensorflow 2.6. 0
keras 2.6. 0
Solution:
There are many similar error reports on stackoverflow, which roughly means that the data type is wrong. The converted data type is not the data type in brackets, such as:
Unsupported object type numpy.Ndarray
means that the cropimg array element is not numpy Ndarray type
Bloggers were puzzled and tried many methods. They all showed that the data type of the cropimg array element was numpy Ndarray, but the error always exists
Later, I suddenly realized that when generating the cropimg array, there was a warning:
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
cropImg_ar = np.array(img_list)
The cropimg array element is an array with inconsistent shapes, which indicates that the cropimg array element type is actually object
. Is it caused by tensorflow not accepting object type data
After converting the cropimg array elements to shape consistency, the problem is solved
Similar Posts:
- The Usage of Numpy.unravel_index() function
- How to Solve DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueErr…
- [Solved] TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
- Scipy ValueError: ‘arr’ does not have a suitable array shape for any mode.
- Python bug problems and solutions sorted out in recent days
- [Solved] tf.summary Error: tags and values not the same shape
- TypeError: Object of type ‘int32’ is not JSON serializable
- Tensorflow error due to uninitialized variable [How to Fix]
- Keras.utils.to in keras_ Categorical method
- [Vue warn]:Error in render: “TypeError: Cannot read property ‘0’ of undefined”