Skip to content

Commit

Permalink
feat: add grid_sample method
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesTrain2001 committed Sep 3, 2023
1 parent b16bcad commit faa1ed4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ivy/functional/frontends/paddle/nn/functional/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,50 @@ def channel_shuffle(x, groups, data_format="NCHW", name=None):
return x


@to_ivy_arrays_and_back
def grid_sample(x, grid, data_format="NCHW"):
input_shape = ivy.shape(x)

if len(input_shape) != 4:
raise ValueError(
"grid_sample requires a 4D input, but it got input size {}".format(
input_shape
)
)

if data_format not in ["NCHW", "NHWC"]:
raise ValueError(
"The data_format should be 'NCHW' or 'NHWC', but received the following"
" data_format: {}".format(data_format)
)

b = input_shape[0]
c = input_shape[1] if data_format == "NCHW" else input_shape[3]
h = input_shape[2] if data_format == "NCHW" else input_shape[1]
w = input_shape[3] if data_format == "NCHW" else input_shape[2]

grid_shape = ivy.shape(grid)
if len(grid_shape) != 4 or grid_shape[3] != 2:
raise ValueError("The grid must be a 4D tensor with shape (N, H, W, 2)")

oc = c
oh, ow = grid_shape[1], grid_shape[2]

if data_format == "NCHW":
input_reshaped = ivy.reshape(x, (b, oc, h, w))
else:
input_reshaped = ivy.reshape(x, (b, h, w, oc))

interpolated = grid_sample(
input_reshaped, grid, mode="bilinear", padding_mode="zeros"
)

if data_format == "NCHW":
return ivy.reshape(interpolated, (b, oc, oh, ow))
else:
return ivy.reshape(interpolated, (b, oh, ow, oc))


@to_ivy_arrays_and_back
def pixel_shuffle(x, upscale_factor, data_format="NCHW"):
input_shape = ivy.shape(x)
Expand Down

0 comments on commit faa1ed4

Please sign in to comment.