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

U-Net图像分割模型报错:'KerasTensor' object is not callable 求助

问题解决:U-Net模型构建中的TypeError错误

错误根源

报错TypeError: 'KerasTensor' object is not callable的核心原因是**upsample_block函数中Concatenate层的写法错误**。你错误地将输入张量传入了Concatenate的构造函数,导致返回的是KerasTensor对象而非层实例,后续对该对象调用(x)自然触发不可调用的错误。

修正方案

将upsample_block中的拼接代码替换为以下两种正确写法之一:

写法1:标准层实例调用

x = tf.keras.layers.Concatenate()([x, conv_feature])

写法2:便捷函数调用(小写concatenate)

x = tf.keras.layers.concatenate([x, conv_feature])

完整修正代码

import tensorflow as tf

# Building blocks
# 双层卷积特征提取
def double_convolution_block(x, filters):
    x = tf.keras.layers.Conv2D(filters, 3, padding='same', activation='relu', kernel_initializer='he_normal')(x)
    x = tf.keras.layers.Conv2D(filters, 3, padding='same', activation='relu', kernel_initializer='he_normal')(x)
    return x

# 下采样模块
def downsample_block(x, filters):
    f = double_convolution_block(x, filters)
    p = tf.keras.layers.MaxPool2D(2)(f)
    p = tf.keras.layers.Dropout(0.3)(p)
    return f, p

# 上采样+特征拼接模块
def upsample_block(x, conv_feature, filters):
    x = tf.keras.layers.Conv2DTranspose(filters, 3, 2, padding='same')(x)
    # 修正拼接层写法
    x = tf.keras.layers.Concatenate()([x, conv_feature])
    x = tf.keras.layers.Dropout(0.3)(x)
    x = double_convolution_block(x, filters)
    return x

# U-Net模型构建
def UNET():
    # 输入层
    inputs = tf.keras.layers.Input(shape=(128, 128, 3))
    # 编码器(下采样)
    f1, p1 = downsample_block(inputs, 64)
    f2, p2 = downsample_block(p1, 128)
    f3, p3 = downsample_block(p2, 256)
    f4, p4 = downsample_block(p3, 512)
    # 瓶颈层
    bottleneck = double_convolution_block(p4, 1024)
    # 解码器(上采样)
    u6 = upsample_block(bottleneck, f4, 512)
    u7 = upsample_block(u6, f3, 256)
    u8 = upsample_block(u7, f2, 128)
    u9 = upsample_block(u8, f1, 64)
    # 输出层
    outputs = tf.keras.layers.Conv2D(3, 1, padding='same', activation='softmax')(u9)
    # 构建模型
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

model = UNET()
model.summary()

额外注意点

  • 你的输入尺寸为128×128,下采样四次后得到8×8的瓶颈层,上采样一次后恢复为16×16,与编码器阶段的f4尺寸匹配,拼接不会出现尺寸不兼容问题。
  • 输出层使用softmax激活对应3类分割任务(Oxford-IIIT Pet数据集的分割目标为背景、宠物主体、边界),设置合理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 15:07:47