Skip to content

Feat: add item_transform_crop for detection and segmentation items - #371

Open
DerrickUnleashed wants to merge 44 commits into
mlverse:mainfrom
DerrickUnleashed:feat/cropTransform
Open

Feat: add item_transform_crop for detection and segmentation items#371
DerrickUnleashed wants to merge 44 commits into
mlverse:mainfrom
DerrickUnleashed:feat/cropTransform

Conversation

@DerrickUnleashed

Copy link
Copy Markdown
Contributor

Added item_transform_crop for detection item, dataset and segmentation item, dataset

Closes #349

DerrickUnleashed and others added 30 commits July 27, 2026 15:08
Both item_transform_hflip.dataset and target_transform_rotate_box.dataset
need to unlock the R6 .getitem binding before reassigning it.
Co-authored-by: cregouby <cregouby@users.noreply.github.com>
- Rename test file to test-item-transforms-geometry.R (plural)
- Rename make_item → make_detection_item() in hflip tests
- Merge same-input tests for efficiency
- Add composition test for item_transform_hflip
- Add item_transform_hflip to main's R/item-transforms-geometry.R
- Resolve all merge conflicts
Co-authored-by: cregouby <cregouby@users.noreply.github.com>
Co-authored-by: cregouby <cregouby@users.noreply.github.com>
@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author
url <- "https://upload.wikimedia.org/wikipedia/commons/b/b6/Felis_catus-cat_on_snow.jpg"

# ========== 1. DETECTION ITEM ==========
img <- base_loader(url) |> transform_to_tensor()
boxes <- torch_tensor(matrix(c(600, 200, 2880, 1860), ncol = 4), dtype = torch_float32())
det_item <- list(x = img, y = list(boxes = boxes, labels = "CAT"))
class(det_item) <- c("image_with_bounding_box", "list")

det_cropped <- item_transform_crop(det_item, top = 100, left = 200, height = 500, width = 800)

p1 <- draw_bounding_boxes(det_item, colors = "blue", width = 10)$to(torch_float())$div(255)
p2 <- draw_bounding_boxes(det_cropped, colors = "red", width = 10)$to(torch_float())$div(255)
tensor_image_browse(p1)
tensor_image_browse(p2)

# ========== 2. DETECTION DATASET ==========
ds_det <- pascal_detection_dataset(year = "2007", split = "trainval",
                                   transform = transform_to_tensor, download = TRUE)
orig_det <- ds_det[1]
ds_det_cropped <- item_transform_crop(ds_det, top = 50, left = 100, height = 300, width = 400)
crop_det <- ds_det_cropped[1]

p3 <- draw_bounding_boxes(orig_det, colors = "blue", width = 5)$to(torch_float())$div(255)
p4 <- draw_bounding_boxes(crop_det, colors = "red", width = 5)$to(torch_float())$div(255)
tensor_image_browse(p3)
tensor_image_browse(p4)

# ========== 3. SEGMENTATION ITEM ==========
img2 <- base_loader(url) |> transform_to_tensor()
h <- img2$shape[2]; w <- img2$shape[3]

mask1 <- torch_zeros(h, w, dtype = torch_bool())
mask1[, 1:150] <- TRUE

mask2 <- torch_zeros(h, w, dtype = torch_bool())
mask2[, 350:500] <- TRUE

masks <- torch_stack(list(mask1, mask2))
seg_item <- list(x = img2, y = list(masks = masks, labels = torch_tensor(c(1L, 2L)),
                                    image_height = h, image_width = w))
class(seg_item) <- c("image_with_segmentation_mask", "list")

seg_cropped <- item_transform_crop(seg_item, top = 50, left = 100, height = 300, width = 400)

p5 <- draw_segmentation_masks(seg_item, alpha = 0.5, colors = c("red", "blue"))$to(torch_float())$div(255)
p6 <- draw_segmentation_masks(seg_cropped, alpha = 0.5, colors = c("red", "blue"))$to(torch_float())$div(255)
tensor_image_browse(p5)
tensor_image_browse(p6)

# ========== 4. SEGMENTATION DATASET ==========
ds_seg <- pascal_segmentation_dataset(year = "2007", split = "trainval",
                                      transform = transform_to_tensor, download = TRUE)
orig_seg <- ds_seg[1]
ds_seg_cropped <- item_transform_crop(ds_seg, top = 50, left = 100, height = 300, width = 400)
crop_seg <- ds_seg_cropped[1]

p7 <- draw_segmentation_masks(orig_seg, alpha = 0.5)$to(torch_float())$div(255)
p8 <- draw_segmentation_masks(crop_seg, alpha = 0.5)$to(torch_float())$div(255)
tensor_image_browse(p7)
tensor_image_browse(p8)

DETECTION ITEM

file46d5a3e844b file46d3e5f8636

DETECTION DATASET

file46d71646337 file46d75adea09

SEGMENTATION ITEM

file46d6ce0313a file46d72c00ead

SEGMENTATION DATASET

file46d1f03bab6 file46d2dd712a2

@DerrickUnleashed
DerrickUnleashed marked this pull request as ready for review July 31, 2026 06:31

@cregouby cregouby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Praise Thanks for this one !

Comment thread tests/testthat/test-item-transforms-geometry.R
Comment thread tests/testthat/test-item-transforms-geometry.R
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.

Add item_transform_crop

2 participants