I'm working on face recognition app and prepared dataset that reads jpg images and then trains my neural network. In order to improve accuracy I decided to convert my images to grayscale (inside mapping function). Here is the way how I convert image to a tensor:
from PIL import Image
image_data = tf.io.decode_jpeg(
image_bytes_string,
channels=3
)
image_data = tf.reshape(image_data , (277, 370, 3))
label = parsed['label']
label = tf.reshape(label, (1,))
return image_data, label
The code how I display image:
image_data = dataset_item[0].numpy()
img = Image.fromarray(image_data)
img
After changing code to convert to a grayscale the code that shows image displays this error:
KeyError: ((1, 1, 1), '|u1')
During handling of the above exception, another exception occurred:
Cannot handle this data type
Grayscale code:
image_data = tf.io.decode_jpeg(
image_bytes_string,
channels=3
)
grayscale = tf.image.rgb_to_grayscale(
image_data
)
image_data = tf.reshape(grayscale, (277, 370, 1))
label = parsed['label']
label = tf.reshape(label, (1,))
return image_data, label
Is there problem with how I convert my image to a grayscale or a problem with PIL library?
UPD: Here is the colab with an example: https://colab.research.google.com/drive/1v72_C5i8HZzSLEy_p6d45EWq1byoF3fe