How to compute loss given formula?

Viewed 49

In this paper the method of depth estimation is proposed. There is a disparity smoothness loss enter image description here

where N is number of pixels, d is the disparity map, I is the image. I wonder if the following implementation in torch is correct, since I am getting poor results and want to exclude possibility of incorrect implementation.

import torch
from torch import nn
import torch.nn.functional as F
import torch.linalg as LA

class DisparitySmoothnessLoss(nn.Module):
    
    @staticmethod
    def x_grad(image):
        image = F.pad(image, (0, 1, 0, 0), mode='replicate')
        grad_x = image[:, :, :, :-1] - image[:, :, :, 1:]
        return grad_x

    @staticmethod
    def y_grad(image):
        image = F.pad(image, (0, 0, 0, 1), mode='replicate')
        grad_y = image[:, :, :-1, :] - image[:, :, 1:, :]
        return grad_y

    
    def forward(self, disparity_map, image):
        disparity_grad_x = self.x_grad(disparity_map)
        disparity_grad_y = self.y_grad(disparity_map) 
        image_grad_x = self.x_grad(image)
        image_grad_y = self.y_grad(image)
        
        return torch.mean(torch.abs(disparity_grad_x) * torch.exp(-LA.vector_norm(image_grad_x, dim=1, keepdim=True)) + \
                torch.abs(disparity_grad_y) * torch.exp(-LA.vector_norm(image_grad_y, dim=1, keepdim=True)))
1 Answers

I expect a list of grads into disparity grads like that implementation. On paper with code you can check many implementations of that article, you can also transform the TensorFlow ones (the official) to PyTorch if you do not trust into PyTorch one.

Related