Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

tensorflow - OCR with CRNN. How to get prediction score

I have a CRNN fitted model with CTC loss output.

I have the prediction and I use keras.backend.ctc_decode to decode it. As written in documentation (https://code.i-harness.com/en/docs/tensorflow~python/tf/keras/backend/ctc_decode), the function will return a Tuple with the decoded result and a Tensor with the log probability of the prediction.

keras.backend.ctc_decode can accept multiple values for its prediction but I need to pass it once at time.

This is the code:

    def decode_single_prediction(pred, num_to_char):
       input_len = np.ones(pred.shape[0]) * pred.shape[1]
       # Use greedy search. For complex tasks, you can use beam search
       decoded = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)
       # decoded[0] is supposed to be the decoded result
       # decoded[1] is supposed to be it's log probability
       accuracy = float(decoded[1][0][0])
       # take the resultin encoded char until it gets -1
       result = decoded[0][0][:,: np.argmax(decoded[0][0] == -1)]
       output_text = tf.strings.reduce_join(num_to_char(result)).numpy().decode("utf-8")
       return (output_text, accuracy)

    for image in images:
       pred = prediction_model.predict(image)
       # num_to_char is the mapping from number to char
       pred_texts, acc = decode_single_prediction(pred, num_to_char)
       print("True value: " + <true_result> + " prediction: " + pred_texts + " acc: " + str(acc))

Output:

True value: test0, prediction: test0, acc: 1.841524362564087
True value: test1, prediction: test1, acc: 0.9661365151405334
True value: test2, prediction: test2, acc: 1.0634151697158813
True value: test3, prediction: test3, acc: 2.471940755844116
True value: test4, prediction: test4, acc: 1.4866207838058472
True value: test5, prediction: test5, acc: 0.7630811333656311
True value: test6, prediction: test6, acc: 0.35642576217651367
True value: test7, prediction: test7, acc: 1.5693446397781372
True value: test8, prediction: test8, acc: 0.9700028896331787
True value: test9, prediction: test9, acc: 1.4783780574798584

The prediction is always correct. However what I think it's the probability seems not to be what I expect. They looks like completely random numbers, even grater than 1 or 2! What am I doing wrong??

Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...