-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add apply_to_images #2560
add apply_to_images #2560
Conversation
Reviewer's GuideThis PR refactors parameter handling and extends the Gaussian noise transform to support image batches and volumes, fixes noise broadcasting in add_noise, upgrades the albucore dependency, and dramatically expands generate_noise test coverage for various noise types, spatial modes, shapes, and failure scenarios. Entity relationship diagram for noise map application to images and volumeserDiagram
IMAGE ||--o{ NOISE_MAP : "applies"
IMAGES ||--o{ NOISE_MAP : "applies"
VOLUME ||--o{ NOISE_MAP : "applies"
VOLUMES ||--o{ NOISE_MAP : "applies"
Class diagram for extended Gaussian noise transform methodsclassDiagram
class GaussianNoiseTransform {
+apply(img, noise_map, **params)
+apply_to_images(images, noise_map, **params)
+apply_to_volume(volume, noise_map, **params)
+apply_to_volumes(volumes, noise_map, **params)
+get_params_dependent_on_data(params, data)
}
Class diagram for updated add_noise functionclassDiagram
class fpixel {
+add_noise(img: np.ndarray, noise: np.ndarray) np.ndarray
}
fpixel : add_noise broadcasts noise to match img shape
Class diagram for get_image_data parameter extractionclassDiagram
class get_image_data {
+get_image_data(data: dict) -> dict
"""Returns metadata: dtype, height, width, num_channels"""
}
class GaussianNoiseTransform {
+get_params_dependent_on_data(params, data)
}
GaussianNoiseTransform ..> get_image_data : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @trylinka - I've reviewed your changes - here's some feedback:
- The new apply_to_images/apply_to_volume/apply_to_volumes methods duplicate existing batch/single transform logic; consider leveraging the batch_transform decorator or a common helper to reduce code duplication and ensure consistency.
- Switching get_params_dependent_on_data to use params['shape'] and get_image_data(data) could break handling of different channel-order conventions—please verify that the shape and dtype extraction works correctly for both 2D images and 3D volumes.
- Bumping albucore to 0.0.27 introduces the get_image_data function—double-check that v0.0.27 is backward-compatible and provides the expected API for this transform.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new apply_to_images/apply_to_volume/apply_to_volumes methods duplicate existing batch/single transform logic; consider leveraging the batch_transform decorator or a common helper to reduce code duplication and ensure consistency.
- Switching get_params_dependent_on_data to use params['shape'] and get_image_data(data) could break handling of different channel-order conventions—please verify that the shape and dtype extraction works correctly for both 2D images and 3D volumes.
- Bumping albucore to 0.0.27 introduces the get_image_data function—double-check that v0.0.27 is backward-compatible and provides the expected API for this transform.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@sourcery-ai review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sourcery-ai review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @trylinka - I've reviewed your changes - here's some feedback:
- In functional.add_noise, consider using np.broadcast_to instead of manual stacking and reshaping to simplify the code and avoid extra memory copies.
- The three new methods apply_to_images, apply_to_volume, and apply_to_volumes all just call fpixel.add_noise—refactor them to share a single implementation or delegate to apply to eliminate duplication.
- Since generate_noise treats any 3D array as (H, W, C) rather than (D, H, W), add a clarifying note or validation to prevent confusion when users pass true volumetric data.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In functional.add_noise, consider using np.broadcast_to instead of manual stacking and reshaping to simplify the code and avoid extra memory copies.
- The three new methods apply_to_images, apply_to_volume, and apply_to_volumes all just call fpixel.add_noise—refactor them to share a single implementation or delegate to apply to eliminate duplication.
- Since generate_noise treats any 3D array as (H, W, C) rather than (D, H, W), add a clarifying note or validation to prevent confusion when users pass true volumetric data.
## Individual Comments
### Comment 1
<location> `albumentations/augmentations/pixel/functional.py:2304` </location>
<code_context>
"""
- return add(img, noise, inplace=False)
+ num_pixels_in_image = np.prod(img.shape)
+ num_pixels_in_noise = np.prod(noise.shape)
+
+ noise = np.stack([noise] * (num_pixels_in_image // num_pixels_in_noise)).reshape(img.shape)
+
+ return add_array(img, noise, inplace=False)
</code_context>
<issue_to_address>
Noise tiling may not handle all broadcasting scenarios robustly.
Tiling assumes noise shape divides evenly into image shape, which may not always be true. Consider using np.broadcast_to or validating shapes to handle all scenarios robustly.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary by Sourcery
Expand noise augmentation support by introducing batch and volume apply methods, improving metadata handling in noise transforms, ensuring correct noise broadcasting in the functional API, and reinforcing reliability with extensive generate_noise tests.
New Features:
Enhancements:
Build:
Tests: