faustomorales / keras-ocr

A packaged and flexible version of the CRAFT text detector and Keras CRNN recognition model.
https://keras-ocr.readthedocs.io/
MIT License
1.36k stars 347 forks source link

Loading trained model doesn't work in new session - shape mismatch error. #214

Closed andriykutsevol closed 1 year ago

andriykutsevol commented 1 year ago

Ubuntu 20.04. pip install git+https://github.com/faustomorales/keras-ocr.git#egg=keras-ocr

keras_ocr.version '0.9.0-post.2+40bb9bd'


I train the model exactly as described in the documentation: https://keras-ocr.readthedocs.io/en/latest/examples/end_to_end_training.html

The single thing I changed is I set the "epochs" parameter to 10, instead of 1000. And the pipeline.recognize() works in the session where you train the model.


Trying to load the model in the new session:

import keras_ocr

detector = keras_ocr.detection.Detector() detector.model.load_weights("./keras_ocr/data/detector.h5")

recognizer = keras_ocr.recognition.Recognizer() recognizer.model.load_weights("./keras_ocr/data/recognizer.h5")

pipeline = keras_ocr.pipeline.Pipeline(detector=detector, recognizer=recognizer)


Getting the error:

recognizer.model.load_weights("./keras_ocr/data/recognizer.h5") File "/media/andriy/NVME/venvs/opencv/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/media/andriy/NVME/venvs/opencv/lib/python3.8/site-packages/keras/backend.py", line 4029, in batch_set_value x.assign(np.asarray(value, dtype=dtype_numpy(x))) ValueError: Cannot assign value to variable ' fc_12/kernel:0': Shape mismatch.The variable shape (256, 37), and the assigned value shape (256, 41) are incompatible.