Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras自定义损失函数中y_真值的大小_Python_Tensorflow_Keras_Loss Function_Unity3d Unet - Fatal编程技术网

Python Keras自定义损失函数中y_真值的大小

Python Keras自定义损失函数中y_真值的大小,python,tensorflow,keras,loss-function,unity3d-unet,Python,Tensorflow,Keras,Loss Function,Unity3d Unet,我试图为Keras中的U-net编写一个自定义损失函数,其目标不仅是计算预测图像和真实图像的均方误差(MSE),而且还计算其梯度的均方误差(MSE) 我不确定这是否正常,但我的自定义损失函数中的y_true的形状是(无,无,无,无),即使从以下情况来看,我希望y_true的大小与y_pred相同,在我的情况下,它的大小应该是:(batch_size,128,256,3) 我已经列出了我为自定义损失函数编写的代码,如果有人能提供任何建议,我将不胜感激 import tensorflow.keras

我试图为Keras中的U-net编写一个自定义损失函数,其目标不仅是计算预测图像和真实图像的均方误差(MSE),而且还计算其梯度的均方误差(MSE)

我不确定这是否正常,但我的自定义损失函数中的
y_true
的形状是(无,无,无,无),即使从以下情况来看,我希望y_true的大小与y_pred相同,在我的情况下,它的大小应该是:(batch_size,128,256,3)

我已经列出了我为自定义损失函数编写的代码,如果有人能提供任何建议,我将不胜感激

import tensorflow.keras.backend as K
# Encouraging the predicted image to match the label not only in image domain, but also in gradient domain
def keras_customized_loss(batch_size, lambda1 = 1.0, lambda2 = 0.05):
    def grad_x(image):
        out = K.zeros((batch_size,)+image.shape[1:4])
        out = K.abs(image[0:batch_size, 1:, :, :] - image[0:batch_size, :-1, :, :])
        return out

    def grad_y(image):
        out = K.zeros((batch_size,)+image.shape[1:4])
        out = K.abs(image[0:batch_size, :, 1:, :] - image[0:batch_size, :, :-1, :])
        return out

    #OBS: Now y_true has size: (None, None, None, None), figure out how to solve it
    def compute_loss(y_true, y_pred):
        pred_grad_x = grad_x(y_pred)
        pred_grad_y = grad_y(y_pred)
        true_grad_x = grad_x(y_true)
        true_grad_y = grad_y(y_true)
        loss1 = K.mean(K.square(y_pred-y_true)) 
        loss2 = K.mean(K.square(pred_grad_x-true_grad_x))
        loss3 = K.mean(K.square(pred_grad_y-true_grad_y))

        return (lambda1*loss1+lambda2*loss2+lambda2*loss3)

    return compute_loss

model.compile(optimizer='adam', loss = keras_customized_loss(BATCH_SIZE), metrics=['MeanAbsoluteError'])

None
表示它接受可变大小。
因此,您的自定义损失可以非常灵活

实际大小自然是传递给
fit

如果您的数据具有shape
(示例,128256,3)
,则无需担心

但是您的代码中有很多不必要的东西,您可以:

def keras_customized_loss(lambda1 = 1.0, lambda2 = 0.05):
    def grad_x(image):
        return K.abs(image[:, 1:] - image[:, :-1])

    def grad_y(image):
        return K.abs(image[:, :, 1:] - image[:, :, :-1])

    def compute_loss(y_true, y_pred):
        pred_grad_x = grad_x(y_pred)
        pred_grad_y = grad_y(y_pred)
        true_grad_x = grad_x(y_true)
        true_grad_y = grad_y(y_true)
        loss1 = K.mean(K.square(y_pred-y_true)) 
        loss2 = K.mean(K.square(pred_grad_x-true_grad_x))
        loss3 = K.mean(K.square(pred_grad_y-true_grad_y))

        return (lambda1*loss1+lambda2*loss2+lambda2*loss3)

    return compute_loss

None
表示它接受可变大小。
因此,您的自定义损失可以非常灵活

实际大小自然是传递给
fit

如果您的数据具有shape
(示例,128256,3)
,则无需担心

但是您的代码中有很多不必要的东西,您可以:

def keras_customized_loss(lambda1 = 1.0, lambda2 = 0.05):
    def grad_x(image):
        return K.abs(image[:, 1:] - image[:, :-1])

    def grad_y(image):
        return K.abs(image[:, :, 1:] - image[:, :, :-1])

    def compute_loss(y_true, y_pred):
        pred_grad_x = grad_x(y_pred)
        pred_grad_y = grad_y(y_pred)
        true_grad_x = grad_x(y_true)
        true_grad_y = grad_y(y_true)
        loss1 = K.mean(K.square(y_pred-y_true)) 
        loss2 = K.mean(K.square(pred_grad_x-true_grad_x))
        loss3 = K.mean(K.square(pred_grad_y-true_grad_y))

        return (lambda1*loss1+lambda2*loss2+lambda2*loss3)

    return compute_loss

大牛,谢谢你的回复!我刚刚测试了你的代码,它运行得很好。好的,“高博:”如果你认为这是正确回答你的问题,请把它标记为答案。嘿,丹尼尔,谢谢你的回复!我只是测试了你的代码,它工作得很好。“好,”高博:“如果你认为这是正确回答你的问题,请把它标记为回答。”