“KerasTensor”对象不可调用

epfja78i  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(138)

我是新手。我只是想用oxford-iiit pet数据集创建图像分割模型层。
我想看U-NET模型摘要,但是我得到了一个错误
完整的错误是:文件“C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py“,第108行,在model = UNET()中文件“C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py”,第98行,在UNET u6 = upsample_block(bottleneck,f4,512)中文件“C:\Users\Kim Ye Rim\Desktop\segmentation\training model.py”,第82行,在upsample_block x = concat([x,conv_feature])(x)TypeError:“KerasTensor”对象不可调用
代码如下:

#Building blocks
#add 2 layers to extract feautres
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
# 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='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)
    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='same', activation='softmax')(u9)
    #u-net(keras functional API)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

model = UNET()
model.summary()

任何评论或建议都是高度赞赏的。谢谢你

rkue9o1l

rkue9o1l1#

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

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])

我希望这将是有帮助的:)另一个注意事项,请始终提供有关您的模型,数据,错误的更多信息,如模型摘要()输出,训练数据形状和类型...

相关问题