mindspore.nn.ImageGradients

class mindspore.nn.ImageGradients[source]

Returns two tensors, the first is along the height dimension and the second is along the width dimension.

Assume an image shape is \(h*w\). The gradients along the height and the width are \(dy\) and \(dx\), respectively.

\[ \begin{align}\begin{aligned}dy[i] = \begin{cases} image[i+1, :]-image[i, :], &if\ 0<=i<h-1 \cr 0, &if\ i==h-1\end{cases}\\dx[i] = \begin{cases} image[:, i+1]-image[:, i], &if\ 0<=i<w-1 \cr 0, &if\ i==w-1\end{cases}\end{aligned}\end{align} \]
Inputs:
  • images (Tensor) - The input image data, with format ‘NCHW’.

Outputs:
  • dy (Tensor) - vertical image gradients, the same type and shape as input.

  • dx (Tensor) - horizontal image gradients, the same type and shape as input.

Raises

ValueError – If length of shape of images is not equal to 4.

Supported Platforms:

Ascend GPU CPU

Examples

>>> net = nn.ImageGradients()
>>> image = Tensor(np.array([[[[1, 2], [3, 4]]]]), dtype=mindspore.int32)
>>> output = net(image)
>>> print(output)
(Tensor(shape=[1, 1, 2, 2], dtype=Int32, value=
[[[[2, 2],
   [0, 0]]]]), Tensor(shape=[1, 1, 2, 2], dtype=Int32, value=
[[[[1, 0],
   [1, 0]]]]))