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

Keras模型输入形状不匹配:显示与报错不符问题求助

阿拉伯手写OCR推理时的形状不匹配ValueError问题

运行阿拉伯手写OCR推理代码时,遇到如下ValueError:

ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 64, 32, 1), found shape=(32, 32, 1)

添加打印语句调试后,显示输入形状为(64, 32, 1),但程序仍报错。相关调试输出与报错栈信息如下:

Shape: (None, 64, 32, 1)
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 image (InputLayer)          [(None, 64, 32, 1)]       0

 Conv1 (Conv2D)              (None, 64, 32, 32)        320

 pool1 (MaxPooling2D)        (None, 32, 16, 32)        0

 batch_normalization (BatchN  (None, 32, 16, 32)       128
 ormalization)

 reshape (Reshape)           (None, 32, 512)           0

 dense2 (Dense)              (None, 32, 16)            8208

 batch_normalization_1 (Batc  (None, 32, 16)           64
 hNormalization)

 bidirectional (Bidirectiona  (None, 32, 256)          148480
 l)

 dense3 (Dense)              (None, 32, 42)            10794

=================================================================
Total params: 167,994
Trainable params: 167,898
Non-trainable params: 96
_________________________________________________________________
None
Input Shape: (64, 32, 1)
Traceback (most recent call last):
  File "D:\Arabic-Handwritten-OCR\Arabic-Handwritten-OCR\inference.py", line 156, in <module>
    preds = prediction_model.predict(batch_images)
  File "C:\Users\User\miniconda3\envs\tf\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\User\AppData\Local\Temp\__autograph_generated_file42woagrz.py", line 15, in tf__predict_function
    retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:  ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 64, 32, 1), found shape=(32, 32, 1)

困惑为何打印的输入形状与报错信息不符,请问这是什么原因?该如何解决这个问题?


原因分析

  • 模型要求批量维度:模型输入层的(None, 64, 32, 1)中,None代表批量大小维度。你打印的(64, 32, 1)是单张图片的形状,缺少批量维度,TensorFlow会自动将维度顺序误判——把原本的高度64当成了不存在的批量维度,导致实际传入的形状被解析为(32, 32, 1)。
  • 输入数据维度缺失:直接传入单张图片时,TensorFlow会把第一个维度默认为批量大小,引发形状错位,最终出现和模型预期不匹配的报错。

解决方法

1. 为输入添加批量维度

在传入模型前,用numpy.expand_dims()为单张图片增加批量维度:

import numpy as np

# 假设单张图片为img,形状(64,32,1)
batch_images = np.expand_dims(img, axis=0)
# 此时batch_images形状为(1,64,32,1),符合模型输入要求
preds = prediction_model.predict(batch_images)

2. 校验批量数据形状

如果batch_images是批量数据,确保它的形状为(N,64,32,1)(N为批量大小),可通过打印确认:

print("Batch Shape:", batch_images.shape)
# 正常输出应为类似(2,64,32,1)格式,第一个数值是批量大小

若形状错误,需调整数据维度顺序或重新预处理图片,保证高度为64、宽度为32。

3. 检查图片预处理流程

确认预处理代码是否颠倒了图片的高度和宽度——比如本该将图片调整为64×32,实际处理成了32×64,加上批量维度后变成(1,32,64,1),导致模型接收时形状不匹配。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 06:54:56