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

使用TensorFlow裁剪频谱图时出现形状广播不匹配错误的问题求助

TensorFlow裁剪频谱图时出现形状广播不匹配错误的问题求助

我手头有一批要输入CNN模型的频谱图,所有图片的尺寸统一是高400、宽1000,这类图都带有白色边框和轴标签区域。我只想把核心的频谱图部分喂给模型,不想包含那些空白区域和轴说明,所以打算用Keras的ImageDataGenerator的预处理函数来实现裁剪。

我写了下面这段代码:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
import tensorflow as tf

path_images = os.path.join('..', 'Datasets', 'spectrums')

def crop_image(image):
    cropped_image = tf.image.crop_to_bounding_box(
        image,
        offset_height = 35, 
        offset_width = 89, 
        target_height = 307, 
        target_width = 717
    )
    return cropped_image

image_generator = ImageDataGenerator(
    rescale=1.0/255,
    preprocessing_function=crop_image
)

train_generator = image_generator.flow_from_directory(
    directory=path_images,
    target_size=(400, 1000),
    batch_size=32,
    class_mode='categorical'
)

但运行时遇到了形状广播不匹配的错误,想请教各位大佬:这个裁剪逻辑哪里出问题了?是不是preprocessing_function里的张量处理和ImageDataGenerator的输出形状不兼容?比如裁剪后的图像形状和target_size设置的尺寸不一致导致的?

备注:内容来源于stack exchange,提问作者Snessub

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 14:25:28