模型的Keras输出张量必须是Keras层的输出(因此保留了过去的层元数据)

时间:2019-03-13 01:29:40

标签: python tensorflow keras deep-learning

我正在尝试在Keras中实现释放面具。我有一个VGG编码器,可以输出特定的功能图(如relu5_1)和一系列解池掩码。

def VGG19(input_tensor=None, input_shape=None, target_layer=1):
    """
    VGG19, up to the target layer (1 for relu1_1, 2 for relu2_1, etc.)
    """
    if input_tensor is None:
        inputs = Input(shape=input_shape)
    else:
        inputs = Input(tensor=input_tensor, shape=input_shape)

    layer, unpooling_masks = vgg_layers(inputs, target_layer)

    model = Model(inputs, [layer, unpooling_masks], name='vgg19')
    load_weights(model)
    return model, unpooling_masks


def vgg_layers(inputs, target_layer):
    unpooling_masks = []
    # Block 1
    x_b1 = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(inputs)    
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x_b1)
    before_pooling = x     
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
    unpooling_masks.append(make_unpooling_mask(x, before_pooling))

    # Block 2
    x_b2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x_b2)
    before_pooling = x 
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
    unpooling_masks.append(make_unpooling_mask(x, before_pooling))

    # Block 3
    x_b3 = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)

    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x_b3)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv4')(x)
    before_pooling = x 
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
    unpooling_masks.append(make_unpooling_mask(x, before_pooling))

    # Block 4
    x_b4 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)

    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x_b4)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv4')(x)
    before_pooling = x 
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
    unpooling_masks.append(make_unpooling_mask(x, before_pooling))

    # Block 5
    x_b5 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)

    if target_layer == 5:
        return x_b5, unpooling_masks
    elif target_layer == 4:
        return x_b4, unpooling_masks
    elif target_layer == 3:
        return x_b3, unpooling_masks
    elif target_layer == 2:
        return x_b2, unpooling_masks
    elif target_layer == 1:
        return x_b1, unpooling_masks

这是分拆功能

def make_unpooling_mask(x, before_pooling):
    t = UpSampling2D()(x)

    mask = Lambda(lambda x: K.cast(K.greater(x[0],x[1]), dtype='float32'))([t, before_pooling])

    return mask 

我收到此错误

  

发生异常:ValueError输出张量必须为Model   Keras Layer的输出(因此保留了过去的图层元数据)。   Found: [<tf.Tensor 'lambda_1/Cast:0' shape=(?, 256, 256, 64) dtype=float32>, <tf.Tensor 'lambda_2/Cast:0' shape=(?, 128, 128, 128) dtype=float32>, <tf.Tensor 'lambda_3/Cast:0' shape=(?, 64, 64, 256) dtype=float32>, <tf.Tensor 'lambda_4/Cast:0' shape=(?, 32, 32, 512) dtype=float32>]

在编译模型model = Model(inputs, [layer, unpooling_masks], name='vgg19')的行上发生这种情况

该怎么办?

1 个答案:

答案 0 :(得分:1)

当调用Model API时,输出参数的值应为张量(或张量列表),在这种情况下,它是张量列表的列表,因此存在问题。只需在调用Model时解压缩unpooling_masks列表(* unpooling_masks)即可。

model = Model(inputs, [layer, *unpooling_masks], name='vgg19')