Issue
I have a trained inceptionV3 model that I want to test on a new data set. However, i am getting TypeError concerning shape of image data. InceptionV3 model is a trained on 1500 image classification dataset.
from tensorflow import keras
import cv2
from tensorflow.keras.preprocessing import image
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
# dimensions of our images ----- are these then grayscale (black and white)?
img_width, img_height = 139, 139
# load the model we saved
model = load_model('/home/DEV/model_inception.h5', compile=False)
# Get test image ready
test_image = cv2.imread('/home/images/0b53daf814304dd0d74efb2fa052ef23_0.png')
test_image = np.array(test_image)
test_image = cv2.resize(test_image,(img_width,img_height))
test_image = test_image.reshape(1,img_width, img_height,3)
result = model.predict(test_image)
plt.imshow(result, cmap="gray")
plt.show()
The Type error that i am getting is
TypeError: Invalid shape (1, 3, 3, 2048) for image data
How can I correct my evaluation model and and test it
Here is the sample of model summary
Solution
what you want is for your input image to have shape(1,139,139,3)if this is what the image size was for the training images you used to train your model. Next question is was your model trained on RGB or BGR images? cv2 reads in images as BGR. If your model was trained on RGB images then you need to convert the image from BGR to RGB with
image_rgb=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
Next question were the images your model was trained on have the pixel values scaled? Usually they are scale with
scaled_image=image/255
If the training images were scaled you need to scale the input image. Finally to get the image into the right shape use
image=np.expand_dims(image, axis=0)
this adds the extra dimension needed by model.predict
Answered By - Gerry P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.