Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added to_grayscale paddle function #26872

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ivy/functional/frontends/paddle/vision/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ def pad(img, padding, fill=0, padding_mode="constant"):
raise "Unsupported padding_mode"


@with_supported_dtypes(
{"2.5.1 and below": ("float32", "float64", "int32", "int64")}, "paddle"
)
@to_ivy_arrays_and_back
def to_grayscale(img, num_output_channels=1):
if num_output_channels == 1:
return ivy.mean(img, axis=-3, keepdims=True)
elif num_output_channels == 3:
return ivy.repeat(ivy.mean(img, axis=-3, keepdims=True), 3, axis=-3)
else:
raise ValueError("num_output_channels should be either 1 or 3")


@with_supported_dtypes(
{"2.5.1 and below": ("float32", "float64", "int32", "int64")}, "paddle"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,41 @@ def test_paddle_pad(
)


# to_grayscale
@handle_frontend_test(
fn_tree="paddle.vision.transforms.to_grayscale",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("valid"),
min_num_dims=3,
max_num_dims=3,
min_dim_size=3,
max_dim_size=3,
),
num_output_channels=st.sampled_from([1, 3]),
)
def test_paddle_to_grayscale(
*,
dtype_and_x,
num_output_channels,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
input_dtype, x = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
img=x[0],
num_output_channels=num_output_channels,
)


# to_tensor
@handle_frontend_test(
fn_tree="paddle.to_tensor",
Expand Down
Loading