Skip to main content
 首页 » 编程设计

python之自定义 Keras 损失函数中的 conv2d

2024年09月07日13bjzhanghao

我正在尝试基于两幅图像的拉普拉斯算子在 Keras 中使用 TF 后端实现自定义损失函数。

def blur_loss(y_true, y_pred): 
    #weighting of blur loss 
    alpha = 1 
    mae = losses.mean_absolute_error(y_true, y_pred) 
    lapKernel = K.constant([0, 1, 0, 1, -4, 1, 0, 1, 0],shape = [3, 3]) 
 
    trueLap = K.conv2d(y_true, lapKernel) 
    predLap = K.conv2d(y_pred, lapKernel) 
    trueBlur = K.var(trueLap) 
    predBlur = K.var(predLap) 
    blurLoss = alpha * K.abs(trueBlur - predBlur) 
    loss = (1-alpha) * mae + alpha * blurLoss 
    return loss 

当我尝试编译模型时出现此错误

Traceback (most recent call last): 
  File "kitti_train.py", line 65, in <module> 
    model.compile(loss='mean_absolute_error', optimizer='adam', metrics=[blur_loss]) 
  File "/home/ubuntu/.virtualenvs/dl4cv/lib/python3.5/site-packages/keras/engine/training.py", line 924, in compile 
    handle_metrics(output_metrics) 
  File "/home/ubuntu/.virtualenvs/dl4cv/lib/python3.5/site-packages/keras/engine/training.py", line 921, in handle_metrics 
    mask=masks[i]) 
  File "/home/ubuntu/.virtualenvs/dl4cv/lib/python3.5/site-packages/keras/engine/training.py", line 450, in weighted 
    score_array = fn(y_true, y_pred) 
  File "/home/ubuntu/prednet/blur_loss.py", line 14, in blur_loss 
    trueLap = K.conv2d(y_true, lapKernel) 
  File "/home/ubuntu/.virtualenvs/dl4cv/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3164, in conv2d 
    data_format='NHWC') 
  File "/home/ubuntu/.virtualenvs/dl4cv/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 655, in convolution 
    num_spatial_dims, strides, dilation_rate) 
  File "/home/ubuntu/.virtualenvs/dl4cv/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 483, in _get_strides_and_dilation_rate 
    (len(dilation_rate), num_spatial_dims)) 
ValueError: len(dilation_rate)=2 but should be 0 

阅读其他问题后,我的理解是这个问题源于对 y_true 和 y_pred 使用占位符张量的编译。我试过检查输入是否为占位符并将它们替换为零张量,但这给了我其他错误。

如何在我的损失函数中使用卷积(图像处理函数,而不是层)而不会出现这些错误?

请您参考如下方法:

这里的问题是对 conv2d 函数的误解,它不仅仅是一个二维卷积。它是多个 channel 的批量二维卷积。因此,虽然您可能期望 *2d 函数接受二维张量,但输入实际上应该是 4 维(batch_size、高度、宽度、 channel ),过滤器也应该是 4 维(filter_height、filter_width、input_channels、output_channels)。详情可以在 TF docs 中找到