Souhail EL-GHAYAM
Souhail EL-GHAYAM

Reputation: 1

Save the OCR model for reading Captchas from Keras Author: A_K_Nain

I was working for OCR model applied to word mnist dataset from Kaggle in colab. I was inspiring by the model from ocr captcha with LSTM and CTC layer authored by A_K_Nain in Keras examples hosted in the site :https://keras.io/examples/vision/captcha_ocr/ I would to save the model but when I tried to load it for making prediction on unseen data. I got an error of unknown CTClayer. The problem that ctclayer is not defined inside the model but outside it so when I tried to load the model I gor the error. I found a solution to use custom model but nothing works for me. HOW can I save the model hosted in the following site: https://keras.io/examples/vision/captcha_ocr/

Upvotes: 0

Views: 950

Answers (2)

GSK
GSK

Reputation: 11

Here is how we can predict new image using author A_K_Nain code. Load related functions from same code.

test_img_path =['/path/to/test/image/117011.png']

validation_dataset = tf.data.Dataset.from_tensor_slices((test_img_path[0:1], ['']))
validation_dataset = (
    validation_dataset.map(
        encode_single_sample, num_parallel_calls=tf.data.experimental.AUTOTUNE
    )
    .batch(batch_size)
    .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
)

for batch in validation_dataset.take(1):
    #print(batch['image'])
    
    preds = reconstructed_model.predict(batch['image']) # reconstructed_model is saved trained model
    pred_texts = decode_batch_predictions(preds)

print(pred_texts)

Upvotes: 0

Divy Patel
Divy Patel

Reputation: 11

CTC layer is not used to make predictions, so you can save without the CTC layer like this :-

saving_model = keras.models.Model(model.get_layer(name="image").input, model.get_layer(name="dense2").output
)
saving_model.summary()
saving_model.save("model_tf")

Other than this you will have to make few changes to make this code work in the variables :-

max_length = max([len(label) for label in labels])
outfile = open("max_length",'wb')
pickle.dump(max_length,outfile)
outfile.close()
import string
chars = string.printable
chars = chars[:-5]
characters = [c for c in chars]

This will give defined set of characters which will help in predictions, therefore in prediction part you have to do :-

infile = open("max_length",'rb')
max_length = pickle.load(infile)
infile.close()

import string
chars = string.printable
chars = chars[:-5]
characters = [c for c in chars]

# Mapping characters to integers
char_to_num = layers.experimental.preprocessing.StringLookup(
    vocabulary=characters, mask_token=None
)

# Mapping integers back to original characters
num_to_char = layers.experimental.preprocessing.StringLookup(
    vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)
prediction_model = tf.keras.models.load_model('model_tf')

And then proceed further.

Upvotes: 1

Related Questions