‘KerasTensor’ 对象不可调用

huangapple go评论80阅读模式
英文:

'KerasTensor' object is not callable

问题

我是新手。
我只是尝试使用oxford-iiit宠物数据集创建图像分割模型的图层。

我想查看U-NET模型的摘要,但出现了错误。

完整的错误如下:

  File "C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py", line 108, in <module>
    model = UNET()
  File "C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py", line 98, in UNET
    u6 = upsample_block(bottleneck, f4, 512)
  File "C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py", line 82, in upsample_block
    x = concat([x, conv_feature])(x)
TypeError: 'KerasTensor' object is not callable

以下是代码:

# 构建模块
# 添加2个图层以提取特征
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)
    # U-Net(Keras函数式API)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

model = UNET()
model.summary()

欢迎提出任何评论或建议。谢谢!

英文:

i'am newbie.
I'm just trying to create layers of image segmentation model with the oxford-iiit pet datasets.

I want to see the U-NET model summary, but I got an error

The full error is :
File "C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py", line 108, in <module>
model = UNET()
File "C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py", line 98, in UNET
u6 = upsample_block(bottleneck, f4, 512)
File "C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py", line 82, in upsample_block
x = concat([x, conv_feature])(x)
TypeError: 'KerasTensor' object is not callable

here is the code :

#Building blocks
#add 2 layers to extract feautres
def double_convolution_block(x,filters):
    x = tf.keras.layers.Conv2D(filters, 3, padding=&#39;same&#39;, activation=&#39;relu&#39;,    kernel_initializer=&#39;he_normal&#39;)(x)
    x = tf.keras.layers.Conv2D(filters,3 ,padding =&#39;same&#39;, activation=&#39;relu&#39;, kernel_initializer=&#39;he_normal&#39;)(x)
    return x
# dowm sampling
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
# up sampling+ extract feature
def upsample_block(x, conv_feature, filters):
    x = tf.keras.layers.Conv2DTranspose(filters, 3,2, padding=&#39;same&#39;)(x)
    x = tf.keras.layers.Concatenate([x, conv_feature])(x)
    x = tf.keras.layers.Dropout(0.3)(x)
    x = double_convolution_block(x, filters)
    return x
#layer(U-NET)
def UNET():
    #inputs
    inputs = tf.keras.layers.Input(shape=(128, 128, 3))
    #encoder(downsample)
    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
    bottleneck = double_convolution_block(p4, 1024)
    #decorder(upsample)
    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
    outputs = tf.keras.layers.Conv2D(3, 1, padding=&#39;same&#39;, activation=&#39;softmax&#39;)(u9)
    #u-net(keras functional API)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

model = UNET()
model.summary()

Any commment or suggestion is highly appreciated. Thank you

答案1

得分: 1

所以,无论您的专业知识如何,您都可以随时在这里提出问题(最终我们都在这里学习)。
我在您的代码中发现了一个语法错误(也基于错误消息),您正在传递连接层的参数,然后应用它,这是错误的:

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)
    x = tf.keras.layers.Dropout(0.3)(x)
    x = double_convolution_block(x, filters)

所以在行尾删除 '(x)',应为:

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

我希望这对您有帮助:)
另外,请始终提供有关您的模型、数据和错误的更多信息,例如模型的 summary() 输出、训练数据的形状和类型...

英文:

So you are always welcome to ask questions here regardless of your expertise (at the end we all learning here).
I spotted a syntax error in your code (also based on the error message), you are passing the concatenate layer parameters and then applying it which is wrong:

def upsample_block(x, conv_feature, filters):
    x = tf.keras.layers.Conv2DTranspose(filters, 3,2, padding=&#39;same&#39;)(x)
    x = tf.keras.layers.Concatenate([x, conv_feature])(x)
    x = tf.keras.layers.Dropout(0.3)(x)
    x = double_convolution_block(x, filters)

So drop the '(x)' at the end of the line to be:

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

I hope this will be helpful ‘KerasTensor’ 对象不可调用
Another note please always provide more info about your model, data, error, like the model summary() output, train data shapes and types ...

huangapple
  • 本文由 发表于 2023年6月19日 16:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定