Skip to content

Feat: add item_transform_center_crop for detection and segmentation items - #370

Merged
cregouby merged 33 commits into
mlverse:mainfrom
DerrickUnleashed:feat/centerCropTransform
Aug 1, 2026
Merged

Feat: add item_transform_center_crop for detection and segmentation items#370
cregouby merged 33 commits into
mlverse:mainfrom
DerrickUnleashed:feat/centerCropTransform

Conversation

@DerrickUnleashed

Copy link
Copy Markdown
Contributor

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

Closes #348

DerrickUnleashed and others added 21 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
@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author
load_all()

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_center_crop(det_item, size = c(1700, 2000))

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_center_crop(ds_det, size = c(300, 300))
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  # note: [, cols] not [rows, ]

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_center_crop(seg_item, size = c(1700, 2000))

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_center_crop(ds_seg, size = c(300, 300))
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)

========== 1. DETECTION ITEM ==========

file3f6d65806cf2 file3f6d5270b7ed

========== 2. DETECTION DATASET ==========

file3f6d35d4fa51 file3f6d317df773

========== 3. SEGMENTATION ITEM ==========

file3f6d663b52e3 file3f6d40bee984

========== 4. SEGMENTATION DATASET ==========

file3f6d4500b867 file3f6d324a9595

@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 contribution.

Comment thread R/item-transforms-geometry.R
Comment thread R/item-transforms-geometry.R Outdated
Comment thread R/item-transforms-geometry.R
Comment thread R/item-transforms-geometry.R Outdated
Comment thread R/item-transforms-geometry.R Outdated
Comment thread tests/testthat/test-item-transforms-geometry.R
Comment thread tests/testthat/test-item-transforms-geometry.R
@cregouby
cregouby merged commit 1b9f1ae into mlverse:main Aug 1, 2026
3 checks passed
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_center_crop

2 participants