如何获取模型中某些层的输出并使用它来计算最终损失?

时间:2019-09-02 15:59:15

标签: python-3.x tensorflow keras deep-learning tensorflow2.0

我正在尝试使用tensorflow 2.0实现SFSnet(https://images.app.goo.gl/pweUBKkrwyDVWeG48),在那儿模型的损失取决于normal_conv,light_estimator,albedo_conv和recon_image的输出,

final_loss = 0.5 * (normal_loss + albedo_loss + recon_loss) + 0.1 * light_loss 

现在要计算normal_loss,albedo_loss,light_loss和recon_loss,我需要在自定义损失函数中调用这些层。

def custom_loss(lighting):

    def loss(y_target,y_predicted):

        # Defining our loss functions
        mae = tf.keras.losses.MeanAbsoluteError()
        mse = tf.keras.losses.MeanSquaredError()
        normal_pred, albedo_pred, lighting_pred, recon_pred = y_predicted
        normal_target, albedo_target, lighting_target, recon_target = y_target

        # Get normal loss : L1 loss
        normal_loss = mae(normal_target, normal_pred)
        # Get albedo loss : L1 loss
        albedo_loss = mae(albedo_target, albedo_pred)
        # Get lighting loss : L2 Loss
        light_loss = mse(lighting_target, lighting_pred)

        # Get reconstrucion loss : L1 loss
        recon_loss = mae(recon_target, recon_pred)


        l = 0.5 * (normal_loss + albedo_loss + recon_loss) + 0.1 * light_loss 
        return l

    return loss

那是行不通的,所以我们有什么方法可以使用模型之间的层的输出并计算它们的损失,并使用这些损失来计算最终损失并使用该损失训练模型。

0 个答案:

没有答案