def sobel(self, input):
        """Using Sobel to compute gradient. Return the magnitude."""
        if not len(input.shape) == 4:
            raise ValueError("Invalid input shape, we expect NCHW, but it is ",
                             input.shape)

        n, c, h, w = input.shape

        input_pad = paddle.reshape(input, (n * c, 1, h, w))
        input_pad = F.pad(input_pad, pad=[1, 1, 1, 1], mode='replicate')

        grad_x = F.conv2d(input_pad, self.kernel_x, padding=0)
        grad_y = F.conv2d(input_pad, self.kernel_y, padding=0)

        mag = paddle.sqrt(grad_x * grad_x + grad_y * grad_y + self.eps)
        mag = paddle.reshape(mag, (n, c, h, w))

        return mag
Using Sobel to compute gradient. Return the magnitude.
Matting/ppmatting/models/losses/loss.py