首页 文章

使用keras预训练vgg16的感知损失输出图像颜色不正确

提问于
浏览
0

我是新学习和深思熟虑的人 . 我正在尝试使用keras训练一个感知损失的Unet . 我的输出图像颜色有问题 . 我的输入图像是彩色图像(RGB) .

如果我不预处理输入图像,这意味着输入是RGB为0~255 . 输出如下:output image(RGB with 0~255)它比label image更暗 .

我发现预训练的vgg16模型使用"caffe"权重 . 函数 keras.applications.vgg16.preprocess_input 会将RGB更改为BGR并减去平均值 . 所以我尝试使用 keras.applications.vgg16.preprocess_input 然后通过添加平均值然后更改回RGB来处理输出图像 . 但输出图像太白:output image(vgg16.preprocess_input)

然后我添加MSE损失与比率 - > 10:1(感知损失:MSE)输出没有不同output image(vgg16.preprocess_input)

我想知道这是一个感知损失的常见问题,或者我的代码中有什么问题?

这是我的代码

preprocess image:

img = load_img(datapath, grayscale = False)
img = img.resize( (resize_size, resize_size), Image.BILINEAR )
img = img_to_array(img)
img = preprocess_input(img)

deprocess image:

mean = [103.939, 116.779, 123.68]
img[..., 0] += mean[0]
img[..., 1] += mean[1]
img[..., 2] += mean[2]
img = img[..., ::-1]

Perceptual loss:

def perceptual_loss(y_true, y_pred): 
    vgg = VGG16(include_top=False, weights='imagenet', input_shape=(resize_size, resize_size, 3)) 
    loss_model = Model(inputs=vgg.input, 
    outputs=vgg.get_layer('block3_conv3').output) 
    loss_model.trainable = False
    return K.mean(K.square(loss_model(y_true) - loss_model(y_pred)))

如果您有任何想法,请告诉我 . 非常感谢!!!

1 回答

  • 0

    “你的”模型的输出与VGG,caffe等的任何内容都没有关系 .

    在创建模型时定义它的是“你” .

    因此,如果模型的输出必须介于0到255之间,则有一种可能性是将其最后一层设置为:

    Activation('sigmoid')   
    Lambda(lambda x: x*255)
    

    然后你需要在感知损失中使用 preprocess_input 函数:

    def perceptual_loss(y_true, y_pred): 
        y_true = preprocess_input(y_true)
        y_pred = preprocess_input(y_pred)
        vgg = VGG16(include_top=False, weights='imagenet', input_shape=(resize_size, resize_size, 3)) 
        loss_model = Model(inputs=vgg.input, 
        outputs=vgg.get_layer('block3_conv3').output) 
        loss_model.trainable = False
        return K.mean(K.square(loss_model(y_true) - loss_model(y_pred)))
    

    另一种可能性是对模型的输出进行后处理 . (但同样,输出的范围完全由你定义) .

相关问题