Keras Unet+VGG16预测都是sam

2024-10-01 17:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Keras用VGG16(译码器部分)训练U-Net。这个模型训练得很好,并且正在学习——我看到了验证集上的gradua-tol改进。在

但是,当我试图对图像调用predict时,我收到的矩阵的所有值都相同。在

模型如下:

class Gray2VGGInput(Layer):
    """Custom conversion layer"""
    def build(self, x):
        self.image_mean = K.variable(value=np.array([103.939, 116.779, 123.68]).reshape([1,1,1,3]).astype('float32'), 
                                     dtype='float32', 
                                     name='imageNet_mean' )
        self.built = True
        return
    def call(self, x):
        rgb_x = K.concatenate([x,x,x], axis=-1 )
        norm_x = rgb_x - self.image_mean
        return norm_x

    def compute_output_shape(self, input_shape):
        return input_shape[:3] + (3,)


def UNET1_VGG16(img_rows=864, img_cols=1232):
    ''' 
    UNET with pretrained layers from VGG16
    '''
    def upsampleLayer(in_layer, concat_layer, input_size):
        '''
        Upsampling (=Decoder) layer building block
        Parameters
        ----------
        in_layer: input layer
        concat_layer: layer with which to concatenate
        input_size: input size fot convolution
        '''
        upsample = Conv2DTranspose(input_size, (2, 2), strides=(2, 2), padding='same')(in_layer)    
        upsample = concatenate([upsample, concat_layer])
        conv = Conv2D(input_size, (1, 1), activation='relu', kernel_initializer='he_normal', padding='same')(upsample)
        conv = BatchNormalization()(conv)
        conv = Dropout(0.2)(conv)
        conv = Conv2D(input_size, (1, 1), activation='relu', kernel_initializer='he_normal', padding='same')(conv)
        conv = BatchNormalization()(conv)
        return conv

    #--------
    #INPUT
    #--------
    #batch, height, width, channels
    inputs_1 = Input((img_rows, img_cols, 1))

    #-----------------------
    #INPUT CONVERTER & VGG16
    #-----------------------
    inputs_3 = Gray2VGGInput(name='gray_to_rgb')(inputs_1)  #shape=(img_rows, img_cols, 3)
    base_VGG16 = VGG16(include_top=False, weights='imagenet', input_tensor=inputs_3)

    #--------
    #DECODER
    #--------
    c1 = base_VGG16.get_layer("block1_conv2").output #(None, 864, 1232, 64)
    c2 = base_VGG16.get_layer("block2_conv2").output #(None, 432, 616, 128) 
    c3 = base_VGG16.get_layer("block3_conv2").output #(None, 216, 308, 256) 
    c4 = base_VGG16.get_layer("block4_conv2").output #(None, 108, 154, 512) 

    #--------
    #BOTTLENECK
    #--------
    c5 = base_VGG16.get_layer("block5_conv2").output #(None, 54, 77, 512)

    #--------
    #ENCODER
    #--------    
    c6 = upsampleLayer(in_layer=c5, concat_layer=c4, input_size=512)
    c7 = upsampleLayer(in_layer=c6, concat_layer=c3, input_size=256)
    c8 = upsampleLayer(in_layer=c7, concat_layer=c2, input_size=128)
    c9 = upsampleLayer(in_layer=c8, concat_layer=c1, input_size=64)

    #--------
    #DENSE OUTPUT
    #--------
    outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)

    model = Model(inputs=inputs_1, outputs=outputs)

    #Freeze layers
    for layer in model.layers[:16]:
        layer.trainable = False

    print(model.summary())

    model.compile(optimizer='adam', 
                  loss=fr.diceCoefLoss, 
                  metrics=[fr.diceCoef])

    return model

然后,我加载模型并调用predict

^{pr2}$

但是,结果如下:

[[0.4567569 0.4567569 0.4567569 ... 0.4567569 0.4567569 0.4567569]
 [0.4567569 0.4567569 0.4567569 ... 0.4567569 0.4567569 0.4567569]
 [0.4567569 0.4567569 0.4567569 ... 0.4567569 0.4567569 0.4567569]
 ...
 [0.4567569 0.4567569 0.4567569 ... 0.4567569 0.4567569 0.4567569]
 [0.4567569 0.4567569 0.4567569 ... 0.4567569 0.4567569 0.4567569]
 [0.4567569 0.4567569 0.4567569 ... 0.4567569 0.4567569 0.4567569]]

我使用相同的程序与其他型号没有VGG16和一切工作良好。因此,我认为与VGG16相关的东西是错误的。可能是输入层,我正在转换为“假”RGB图像?在


Tags: inselflayerimginputoutputbasesize
2条回答

如果你用一些特定的约束条件训练你的网络(例如,你减去平均值),当你测试你的网络时(因为通过测试你也可以向前通过网络),你需要减去平均值(就像在训练中一样)。在

这也许能解决你的问题。在

问题在于VGG冻结层。如果你的数据集与imagenet大不相同,也许你应该对整个模型进行端到端的训练。而且,很明显,如果你冻结BatchNormalization层,它们可能会行为怪异。如需参考,请参阅discussion。在

相关问题 更多 >

    热门问题