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

TensorFlow中YOLOv3模型报错:'list' object has no attribute 'shape'

问题:打印YOLOv3模型输出shape时报错'list' object has no attribute 'shape'

我使用TensorFlow编写代码时,尝试打印模型输出的shape,出现了'list' object has no attribute 'shape'的报错。

相关代码

推理代码

image_batch, label_batch = next(iter(train_ds))
feature_batch = train_model(image_batch)
print(feature_batch.shape)

YOLOv3模型构建与保存代码

# define the model
model = make_yolov3_model()

# load the model weights
# I have loaded the pretrained weights in a separate dataset
weight_reader = WeightReader('yolov3.weights')

# set the model weights into the model
weight_reader.load_weights(model)

# save the model to file
model.save('model.h5')

模型加载代码

# load yolov3 model
from keras.models import load_model
train_model = load_model('model.h5', compile=False)

报错信息

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4412\2773113981.py in <module>
1 image_batch, label_batch = next(iter(train_ds))
2 feature_batch = train_model(image_batch)
----> 3 print(feature_batch.shape)

AttributeError: 'list' object has no attribute 'shape'

原因与解决办法

原因

YOLOv3是多输出模型,它会生成3个不同尺度的检测输出(对应不同尺寸的锚框,用于检测大小不同的目标),因此调用train_model(image_batch)返回的不是单个张量,而是包含3个张量的列表,列表没有shape属性,所以触发报错。

解决办法

  • 查看所有输出的shape:遍历列表逐个打印
feature_batch = train_model(image_batch)
for idx, output_tensor in enumerate(feature_batch):
    print(f"第{idx+1}个输出的shape: {output_tensor.shape}")
  • 查看单个输出的shape:通过索引直接取对应位置的张量后打印
feature_batch = train_model(image_batch)
# 示例:取第一个输出的shape
print(feature_batch[0].shape)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:32:31