Planning on using Tensorflow Lite for image classification.
To test the model, using Fashion-MNIST database to create the model following a Tensorflow basic example from their website. Created and saved the model and want to test it in TF Lite.
I want to save an image from the mentioned database to predict its class in TF Lite. This is the code to check the images (some of them from Google search):
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
import tensorflow as tf
import base64
import io
from os.path import dirname, join
from PIL import Image
from matplotlib.pyplot import imread
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_X, train_y), (test_X, test_y) = fashion_mnist.load_data()
# Function to check image format
def check_mnist_image_format(image_array):
print(f"Image {index} shape: {image_array.shape}")
print(f"Image {index} data type: {image_array.dtype}")
print(f"Image {index} min pixel value: {np.min(image_array)}")
print(f"Image {index} max pixel value: {np.max(image_array)}")
# Check the format of the first training image
check_mnist_image_format(train_X[0], 0)
filename = join(dirname("/home/gachaconr/tf/"), 'image.unit8')
with open(filename, 'wb') as file:
file.write(train_X[0])
print("image saved")
# Open the image file
image_array = imread(filename)
check_mnist_image_format(image_array)
Saving the image with extension .unit8 since that the format of the image database given by the properties given by the function check_mnist_image_format
The image is saved as expected but the function imread cannot read it. This is the error:
Traceback (most recent call last):
File "/home/gachaconr/tf/imagemnistdatacheck.py", line 40, in <module>
image_array = imread(filename)
^^^^^^^^^^^^^^^^
File "/home/gachaconr/tf/lib/python3.12/site-packages/matplotlib/pyplot.py", line 2607, in imread
return matplotlib.image.imread(fname, format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/gachaconr/tf/lib/python3.12/site-packages/matplotlib/image.py", line 1512, in imread
with img_open(fname) as image:
^^^^^^^^^^^^^^^
File "/home/gachaconr/tf/lib/python3.12/site-packages/PIL/Image.py", line 3532, in open
raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file '/home/gachaconr/tf/image.unit8'
How can I accomplish the mentioned task? What am I doing wrong?
image.unit8supposed to be?