I am setting up keras ocr first time and would like to run example from the documentation.
I have installed ocr(0.9.3) and tensorflow(2.16.1) by pip Here is the code
import matplotlib.pyplot as plt
import keras_ocr
# keras-ocr will automatically download pretrained
# weights for the detector and recognizer.
pipeline = keras_ocr.pipeline.Pipeline()
# Get a set of three example images
images = [
keras_ocr.tools.read(url) for url in [
'https://upload.wikimedia.org/wikipedia/commons/b/bd/Army_Reserves_Recruitment_Banner_MOD_45156284.jpg',
'https://upload.wikimedia.org/wikipedia/commons/e/e8/FseeG2QeLXo.jpg',
'https://upload.wikimedia.org/wikipedia/commons/b/b4/EUBanana-500x112.jpg'
]
]
# Each list of predictions in prediction_groups is a list of
# (word, box) tuples.
prediction_groups = pipeline.recognize(images)
# Plot the predictions
fig, axs = plt.subplots(nrows=len(images), figsize=(20, 20))
for ax, image, predictions in zip(axs, images, prediction_groups):
keras_ocr.tools.drawAnnotations(image=image, predictions=predictions, ax=ax)
Here is the error
2024-03-12 12:24:06.949452: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-12 12:24:07.386269: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Looking for D:\users\xxx\.keras-ocr\craft_mlt_25k.h5
2024-03-12 12:24:08.845546: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:From D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras\src\backend\tensorflow\core.py:174: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
Traceback (most recent call last):
File "D:\users\xxx\PycharmProjects\pytorch\pytorch.py", line 7, in <module>
pipeline = keras_ocr.pipeline.Pipeline()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras_ocr\pipeline.py", line 22, in __init__
recognizer = recognition.Recognizer()
^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras_ocr\recognition.py", line 388, in __init__
) = build_model(alphabet=alphabet, **build_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras_ocr\recognition.py", line 277, in build_model
locnet_y = keras.layers.Dense(
^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras\src\layers\core\dense.py", line 85, in __init__
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras\src\layers\layer.py", line 265, in __init__
raise ValueError(
ValueError: Unrecognized keyword arguments passed to Dense: {'weights': [array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
I tried to google stuff but, as far as I see I should not have issues according to version. I dont expect wrong code from the pypi example. Any idea what possible mistakes I might took during environment setup?
I faced the same issue with keras-ocr not working as expected on my system, particularly when using the newest version of TensorFlow - 2.16.1 (released Mar 9, 2024). After extensive troubleshooting, I discovered that the problem seems to be related to how keras-ocr interacts with this version of TensorFlow.
I managed to resolve the issue by downgrading TensorFlow to its last stable version before the update:
pip install --force-reinstall -v "tensorflow==2.15.1"
This workaround should help until an official fix is released in either keras-ocr or TensorFlow. It's worth noting that TensorFlow officially only supports Python versions 3.9 to 3.11. If you're still encountering issues after this and are using a version outside this range, consider downgrading your Python version as well.