sk_1995
sk_1995

Reputation: 125

testing model by providing test image to model?

i had made a model for image classification,now i am testing model by providing test image to model but i facing some error.I delete this image = image.reshape((1, image.shape[0])) but facing the same issue and resize the shape by providing the (360,360) but this problem is not being resolved what should i need to change it in my code? The code i write is this :

import tensorflow as tf
import keras
#from keras.models import load_model
from keras.models import load_model
import argparse
import pickle
import cv2
import os
from sklearn.preprocessing import LabelBinarizer
lb = LabelBinarizer()
f = open("simple_multiclass_classifcation_lb.pickle", "wb")
f.write(pickle.dumps(lb))
f.close()

test_image_path = r"E:\classification\test\test\pan26.jpg"

model_path = r"PanModel.model.h5"

label_binarizer_path = "E:\API\simple_multiclass_classifcation_lb.pickle"

image = cv2.imread(test_image_path)
output = image.copy()
image = cv2.resize(image, (32,32))

 #scale the pixel values to [0, 1]
image = image.astype("float") / 255.0
image = image.flatten()
print ("image after flattening",len(image))
print ("image--reshape",image.shape)

# load the model and label binarizer
print("[INFO] loading network and label binarizer...")
model = tf.keras.models.load_model('PanModel.model.h5')
#model = load_model("PanModel.model.h5")
lb = pickle.loads(open(label_binarizer_path, "rb").read())

# make a prediction on the image
print (image.shape)
preds = model.predict(image)

# find the class label index with the largest corresponding
# probability
print ("preds.argmax(axis=1)",preds.argmax(axis=1))
i = preds.argmax(axis=1)[0]
print (i)
label = lb.classes_[i]
# draw the class label + probability on the output image
text = "{}: {:.2f}%".format(label, preds[0][i] * 100)
cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

# show the output image
cv2.imshow("Image", output)
cv2.waitKey(0)

error:

Traceback (most recent call last):
  File "modeltest.py", line 40, in <module>
    preds = model.predict(image)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 915, in predict
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 462, in predict
    steps=steps, callbacks=callbacks, **kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 396, in _model_iteration
    distribution_strategy=strategy)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 593, in _process_inputs
    steps=steps)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2437, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 573, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected conv2d_input to have shape (360, 360, 3) but got array with shape (32, 32, 3)

Upvotes: 1

Views: 1553

Answers (1)

Khimraj Suthar
Khimraj Suthar

Reputation: 26

Your model requires input with shape (360,360,3) but you are giving input with shape (32,32,3). So change line

image = cv2.resize(image, (32,32))

to

image = cv2.resize(image, (360,360))

Upvotes: 1

Related Questions