You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次搭建keras-ocr运行PyPI示例遇ValueError问题求助

问题

首次搭建keras-ocr环境,通过pip安装了keras-ocr 0.9.3和TensorFlow 2.16.1,运行PyPI官方示例代码时触发ValueError。

示例代码

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)

错误信息

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.],

已确认官方示例代码无误,按版本匹配搜索后未找到对应问题,咨询环境配置可能存在的问题。

解决方案

  • 降级TensorFlow到2.15.x版本(该版本自带Keras 2.x,与keras-ocr 0.9.3兼容),执行命令:
    pip install tensorflow==2.15.0
    
  • 或者,指定安装Keras 2.x版本,覆盖TensorFlow自带的Keras 3.x:
    pip install keras==2.15.0
    
  • 若想保留TensorFlow 2.16.x,需等待keras-ocr更新适配Keras 3.x,或自行修改keras_ocr/recognition.py中构建Dense层的代码,将weights参数改为通过layer.set_weights()方法设置,而非在初始化时传入。

内容的提问来源于stack exchange,提问作者kurczakiiziemniaki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 03:24:54