To begin with I'm very very new to python and to ML, which means i don't understand many things written, but the idea was to take trained model from Kaggle and predict the expression on my test photo, When is works, then try to go deeper into understanding the code. But i suck here...
import tensorflow as tf
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten,BatchNormalization
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
file_name = 'kaggle_model.h5'
model_path = os.path.join('checkpoint',file_name)
final_model = tf.keras.models.load_model(model_path)
label_to_text = {0:'anger', 1:'disgust', 2:'fear', 3:'happiness', 4:'sadness', 5:'surprise', 6:'neutral'}
from tensorflow.keras.preprocessing import image
img_path='test2.jpg'
test_image=image.load_img(img_path,target_size=(48,48),color_mode='grayscale')
test_image=image.img_to_array(test_image)
print(test_image.shape)
plt.imshow(test_image)
plt.show()
test_image=np.expand_dims(test_image,0)
predicted_class = final_model.predict(test_image).argmax()
print(f'predicted lable is{label_to_text[predicted_class]}')
Output:
InvalidArgumentError: Computed output size would be negative: -1 [input_size: 1, effective_filter_size: 3, stride: 1]
[[node sequential/conv2d/Relu (defined at \AppData\Local\Temp\ipykernel_4280\3302287779.py:3) ]] [Op:__inference_predict_function_4813]
Function call stack:
predict_function
There are couple of posts with the same problem here in stack, but i couldn't figure out the solution on that basis. Many thanks in advance!
If i try to reshape the image such as
test_image=test_image.reshape(1,48,48,1)
then i get a different problem
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1, 48, 48, 1)
and this
test_image=test_image.reshape(48,48,1)
gives
InvalidArgumentError: input depth must be evenly divisible by filter depth: 1 vs 3
[[node sequential/conv2d/Relu (defined at \AppData\Local\Temp\ipykernel_4280\1071859005.py:1) ]] [Op:__inference_predict_function_6387]