0

I am trying to implement a OCR project by Keras.So I try to learn from Keras OCR example.I have use my own train data to train a new model and get the .H5 modelfile. Now I want to test a new image to see my model performance,so I code a test.py like this:

from keras.models import Model
import cv2
from keras.preprocessing.image import img_to_array
import numpy as np
from keras.models import load_model
from keras import backend as K
from allNumList import alphabet

def labels_to_text(labels):
    ret = []
    for c in labels:
        if c == len(alphabet):  # CTC Blank
            ret.append("")
        else:
            ret.append(alphabet[c])
    return "".join(ret)

def decode_predict_ctc(out, top_paths = 1):
    results = []
    beam_width = 5
    if beam_width < top_paths:
      beam_width = top_paths
    for i in range(top_paths):
      lables = K.get_value(K.ctc_decode(out, input_length=np.ones(out.shape[0])*out.shape[1],
                           greedy=False, beam_width=beam_width, top_paths=top_paths)[0][i])[0]
      text = labels_to_text(lables)
      results.append(text)
    return results

def test(modelPath,testPicTest):
    img=cv2.imread(testPicTest)
    img=cv2.resize(img,(128,64))
    img=img_to_array(img)
    img=np.array(img,dtype='float')/255.0
    img=np.expand_dims(img, axis=0)
    img=img.swapaxes(1,2)   

    model=load_model(modelPath,custom_objects = {'<lambda>': lambda y_true, y_pred: y_pred})
    net_out_value = model.predict(img)
    top_pred_texts = decode_predict_ctc(net_out_value)
    return top_pred_texts

result=test(r'D:\code\testAndExperiment\py\KerasOcr\weights.h5',r'D:\code\testAndExperiment\py\KerasOcr\test\avo.jpg') 
print(result)  

but I get a error like this:

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 4 array(s), but instead got the following list of 1 arrays: [array([[[[1., 1., 1.],          [1., 1., 1.],          [1., 1., 1.],          ...,          [1., 1., 1.],          [1., 1., 1.],          [1., 1., 1.]],          [[1., 1., 1.],          [1., 1., 1.],...

I have references some material:
https://stackoverflow.com/a/49537697/10689350
https://www.dlology.com/blog/how-to-train-a-keras-model-to-recognize-variable-length-text/
How to predict the results for OCR using keras image_ocr example?

some answer show that we should use 4 inputs [input_data, labels, input_length, label_length] in training but besides input_data, everything else is information used only for calculating the loss,so in testing maybe use the input_data is enough.So I just use a picture without labels, input_length, label_length.But I get the error above.

I am confused about if the model needs 4 inputs or 1 in testing?
It doesn't seem reasonable to require 4 inputs during the testing process.and now I have model.h5,what should I do next?
Thanks in advance.

My code is Here:https://github.com/hqabcxyxz/KerasOCR/tree/master

  • You should include your model if you are asking about what the model's inputs should be. – Dr. Snoopy Nov 29 '18 at 10:56
  • I have been push my github,here is link:https://github.com/hqabcxyxz/KerasOCR/tree/master thanks for your reminder – CaptainSama Nov 29 '18 at 11:26
  • Did you see [this answer](https://stackoverflow.com/questions/44847446/how-can-i-use-the-keras-ocr-example/49537697?noredirect=1#comment101170963_49537697)? I believe it solves your problem. – Claudio Aug 04 '19 at 23:47

1 Answers1

0

maybe I know why.Because in the OCR example,we make a lambda layer to count CTC loss.This Layer need 4 inputs! The right way to do test is we make a model without this lambda layer during inference.Then load the model weight by name to do inference.After we get inference result,just use CTC decode it! I will update my code in github later.....