When I tried to write a program that reads and processes images from Google Drive, an error occurred in a specific image.I was able to process other images without any problems, but when I run them with this image, the following error occurs.It seems that there is no problem with loading and tensorizing.
The language used is python and the running environment is Google collaboration.
The tensor flow version is 2.0.0.
How can I resolve this error?
当 I am not familiar with this site, so I would appreciate it if you could point it out if there is anything wrong with how to ask questions or write them down.
InvalidArgumentError Traceback (most recent call last)
<ipython-input-111-242afefc9006>in<module>()
1 pre_data = tf.image.resize (pre_data, [500,500])
---->2pre_data=tf.image.rgb_to_grayscale(pre_data)
3 pre_data = np.asarray(pre_data)
4 pre_data=np.squeeze(pre_data)
5pre_data.shape
-----------------------------------------------------------------------------------------------------------------------------------------
/usr/local/lib/python 3.6/dist-packages/six.py in raise_from (value, from_value)
InvalidArgumentError: Matrix size-incompatible: In[0]: [250000,4], In[1]: [3,1] [Op: MatMul] name: rgb_to_grayscale/Tensordot/MatMul/
< print()
is omitted.Also, in the original Colaboratory program, the cells are filled in and written (please let me know if there is a good way to write)
import tensorflow as tf
import numpy as np
path="/content/drive/My Drive/for_MikuChecker"
img=np.asarray(image.open(path+"/run_check/yes/48911351_p0.png"))
img=tf.convert_to_tensor(img)#TensorShape([992,622,4])
size = if h<welse w#w,h = (622,992)
pre_data=tf.image.crop_to_bounding_box(img, int((h-size)/2), int(w-size)/2), size, size)
pre_data=tf.image.resize(pre_data, [500,500])
pre_data=tf.image.rgb_to_grayscale(pre_data)
pre_data=np.asarray(pre_data)
pre_data=np.squeeze(pre_data)
pre_data.shape
Where the error occurred
pre_data=tf.image.rgb_to_grayscale(pre_data)
This image is reprinted from (Head Office)
python python3 tensorflow image google-colaboratory
According to the Tensorflow document, the tf.image.rgb_to_grayscale function requires the last dimension to be 3.
https://www.tensorflow.org/api_docs/python/tf/image/rgb_to_grayscale
Args:
images: The RGB tensor to convert. Last dimension must have size 3 and should contain RGB values.
But if you look at the source code in question,
img=tf.convert_to_tensor(img)#TensorShape([992,622,4])
The last dimension is 4, so I think there is an error.The image in question appears to be an RGBA image, not an RGB image. If you create a tensor using only three RGB channels using the above code as shown below, you will be able to avoid the time being.(If alpha channel is set, we need to deal with it separately.)
img=tf.convert_to_tensor(img[:,:,:3])
© 2024 OneMinuteCode. All rights reserved.