Skip to content

Features: Implemented ChannelFirst2d#354

Closed
Pwhsky wants to merge 20 commits intogv/final/featuresfrom
AL/features-functions
Closed

Features: Implemented ChannelFirst2d#354
Pwhsky wants to merge 20 commits intogv/final/featuresfrom
AL/features-functions

Conversation

@Pwhsky
Copy link
Collaborator

@Pwhsky Pwhsky commented Jul 2, 2025

This PR implements ChannelFirst2d extended to handle torch tensors, as well as adding unit tests for torch tensors.

@Pwhsky Pwhsky requested a review from giovannivolpe July 2, 2025 20:38
@Pwhsky Pwhsky changed the title Implemented ChannelFirst2d Features: Implemented ChannelFirst2d Jul 2, 2025
@Pwhsky Pwhsky mentioned this pull request Jul 2, 2025
@Pwhsky Pwhsky changed the base branch from gv/final/features to develop July 3, 2025 12:42
@Pwhsky Pwhsky changed the base branch from develop to gv/final/features July 3, 2025 12:42
@Pwhsky Pwhsky requested review from JChonpca and mirjagranfors July 4, 2025 08:18
def __init__(
self: Feature,
axis: int = -1,
axis: PropertyLike[int] = -1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should axis be PropertyLike[int]? I don't really see why it would ever be something other than a fixed integer.

warnings.warn(
"ChannelFirst2d is deprecated and may be removed in a future release. "
"The current implementation is not guaranteed to be exactly "
"equivalent to prior implementations. "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that a comma is missing after the string. Without it I get an error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a line goes beyond 79 characters

@@ -6147,19 +6147,21 @@ class ChannelFirst2d(Feature):

This feature rearranges the axes of a 3D image so that the specified axis
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some lines have blank spaces at the end. Check it and remove the spaces.

self: Feature,
image: np.ndarray,
image: NDArray | torch.Tensor,
axis: int,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If "axis = -1" in the init, shouldn't it also be so here in the "get"? Or remove it in both cases.


input_image_3d = torch.rand(10, 20, 3)
output_image_3d = channel_first_feature.get(input_image_3d,
axis=-1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for you? When I try to run this in my script I get an error when using a 3d torch tensor and axis=-1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out, I have added a fix for this that passes the tests properly.

output_image_3d = channel_first_feature.get(input_image_3d,
axis=-1)
self.assertEqual(tuple(output_image_3d.shape), (3, 10, 20))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be nice to add some test with the actual values of the arrays and tensors. They can of course be small (e.g. [[1, 2, 3], [4, 5, 6]])

@@ -6287,7 +6305,7 @@ class Upscale(Feature):

Methods
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it becomes easier to review if you only put one class in each PR, so a separate one for the "Upscale" would have been nicer

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it doesn't seem like "Upscale" is ready yet. Let me know when you want a review on it

Methods
-------
`get(image: np.ndarray | Image, factor: int | tuple[int, int, int], **kwargs) -> np.ndarray`
`get(image: np.ndarray | Image | torch.tensor, factor: int | tuple[int, int, int], **kwargs) -> np.ndarray | torch.tensor`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line, and also some other line, is longer than 79 characters

image: np.ndarray
image: NDArray | torch.Tensor
The input image to process. Can be 2D or 3D.
axis: int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add that "axis" doesn't affect anything for 2D images

self: Feature,
image: np.ndarray,
image: np.ndarray | torch.tensor,
factor: int | tuple[int, int, int],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is the default in init, schouldn't we have the same default value here?

@Pwhsky Pwhsky closed this Jul 7, 2025
@Pwhsky Pwhsky deleted the AL/features-functions branch July 7, 2025 09:52
@Pwhsky Pwhsky mentioned this pull request Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants