Search code examples
pythonarraysgoogle-colaboratorytraining-datapredict

How can I predict model from Handwriting numbers?


I collected Handwriting number and train model with VGG16 and made model is Sequential

from keras import models
from keras import layers
from keras import optimizers

model = models.Sequential()
model.add(layers.Dense(1024, activation='relu', input_dim=7 * 7 * 512))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(63, activation='softmax'))
model.compile(optimizer= tensorflow.keras.optimizers.RMSprop(learning_rate=2e-4),
              loss='categorical_crossentropy',
              metrics=['acc'])

I save in name modeldata.h5 and load it.

# load the handwriting OCR model
from keras.models import load_model
args = {}
args["model"] = '/content/drive/MyDrive/project/modeldata.h5'
model = load_model(args["model"] )

This is my model.summary()

enter image description here

Then I want to predict Image of number 8.

from tensorflow.keras.utils import load_img
from google.colab.patches import cv2_imshow
from matplotlib import pyplot as plt

img = '/content/drive/MyDrive/project/Dataset/DATA3/78/78_10.png'
image= cv2.imread(img)
cv2_imshow(image)

enter image description here And predict it

plt.imshow(image.reshape(224, 224),cmap='Greys')
pred = model.predict(image.reshape(1, 224, 224, 1))
print("From which the max choice is:", pred.argmax())

This is an error enter image description here

please Help me to fix it! I have model but I don't know to predict the numbers. I want resault is my model can predicted this picture.


Solution

  • Here actually there are many problems. The 2 major problems are:

    1. Why is your input_dim=7 * 7 * 512? I see you want to reshape your image to 224, 224 then your input_dim should be 224*224.
    2. You can't just reshape your image to 224x224, you need to resize it. You can do that with cv2.resize, here are some examples: https://www.geeksforgeeks.org/image-resizing-using-opencv-python/

    Edit: Adding solution.
    In your case you can do something like this:

    image = cv2.imread(img)
    image = cv2.resize(image, (224, 224))
    image = image[None, ...]
    
    features = vgg_conv.predict(image)
    features = np.reshape(features,(1,7*7*512))
    result = model.predict(features)