0

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]
NepI2
  • 1
  • 1

1 Answers1

0

There are some shape errors which are evident from the error log which bring us back to the basics of CNNs , filters/kernels and related topics more of which you can explore in this cool blog.

Coming to the code fixes lets see them one by one and find where we need correction.

1. InvalidArgumentError : Here the tensor shape specified is less than the filter hence the numerical representation of the image needs to be corrected.

2. ValueError - you can refer to this StackOverflow thread. However, enclosing the relevant snippet.

The below example here is the clarify about Conv2D

input_layer= layers.InputLayer(input_shape=(2,2,1)) conv1 = layers.Conv2D(3,(2,2)) X= np.ones((2,2)) X =X.reshape(1,X.shape[0],X.shape1,1) # shape of X is 4D, (1, 2, 2, 1) conv1(input_layer(X)) TL;DR

Now let's elaborating above codes

Line 1 input_layer was defined with the shape of 3D, but at line no.4 X was reshaped to 4D shape which is not matching the shape at all. However, in order to feed any input X to input_layer or Conv2D must pass with 4D shape.

3. InvalidArgumentError : This can be solved by making the input shape to be 4D.

TF_Chinmay
  • 86
  • 4