Skip to content

Feat: add item_transform_perspective for detection and segmentation items - #377

Open
DerrickUnleashed wants to merge 7 commits into
mlverse:mainfrom
DerrickUnleashed:feat/perspectiveTransform
Open

Feat: add item_transform_perspective for detection and segmentation items#377
DerrickUnleashed wants to merge 7 commits into
mlverse:mainfrom
DerrickUnleashed:feat/perspectiveTransform

Conversation

@DerrickUnleashed

Copy link
Copy Markdown
Contributor

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

Closes #353

@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author
# perspective corners covering the full image
startpoints <- function(img) {
  h <- img$shape[2]; w <- img$shape[3]
  list(c(0, 0), c(w - 1, 0), c(w - 1, h - 1), c(0, h - 1))  # TL, TR, BR, BL
}
# warp: pinch the top edge inward, push the bottom edge down/out
endpoints <- function(img) {
  h <- img$shape[2]; w <- img$shape[3]
  list(c(150, 60), c(w - 151, 40), c(w - 1, h - 1), c(0, h - 1))
}

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_warped <- item_transform_perspective(det_item, startpoints(img), endpoints(img))

p1 <- draw_bounding_boxes(det_item, colors = "blue", width = 10)$to(torch_float())$div(255)
p2 <- draw_bounding_boxes(det_warped, colors = "red", width = 10)$to(torch_float())$div(255)
grid1 <- vision_make_grid(torch_stack(list(p1, p2)), scale = TRUE)
tensor_image_browse(grid1)

# ========== 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_warped <- item_transform_perspective(ds_det, startpoints(orig_det$x), endpoints(orig_det$x))
warp_det <- ds_det_warped[1]

p3 <- draw_bounding_boxes(orig_det, colors = "blue", width = 5)$to(torch_float())$div(255)
p4 <- draw_bounding_boxes(warp_det, colors = "red", width = 5)$to(torch_float())$div(255)
grid2 <- vision_make_grid(torch_stack(list(p3, p4)), scale = TRUE)
tensor_image_browse(grid2)

# ========== 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_warped <- item_transform_perspective(seg_item, startpoints(img2), endpoints(img2))

p5 <- draw_segmentation_masks(seg_item, alpha = 0.5, colors = c("red", "blue"))$to(torch_float())$div(255)
p6 <- draw_segmentation_masks(seg_warped, alpha = 0.5, colors = c("red", "blue"))$to(torch_float())$div(255)
grid3 <- vision_make_grid(torch_stack(list(p5, p6)), scale = TRUE)
tensor_image_browse(grid3)

# ========== 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_warped <- item_transform_perspective(ds_seg, startpoints(orig_seg$x), endpoints(orig_seg$x))
warp_seg <- ds_seg_warped[1]

p7 <- draw_segmentation_masks(orig_seg, alpha = 0.5)$to(torch_float())$div(255)
p8 <- draw_segmentation_masks(warp_seg, alpha = 0.5)$to(torch_float())$div(255)
grid4 <- vision_make_grid(torch_stack(list(p7, p8)), scale = TRUE)
tensor_image_browse(grid4)

DETECTION ITEM

file33968f942b4

DETECTION DATASET

file339448a6d43

SEGMENTATION ITEM

file3395c0b6cdc

SEGMENTATION DATASET

file33972180eb9

@DerrickUnleashed
DerrickUnleashed marked this pull request as ready for review August 1, 2026 20:02
@cregouby

cregouby commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

praise @DerrickUnleashed, this looks nice.
todo please resolve merge conflicts.

@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author

praise @DerrickUnleashed, this looks nice. todo please resolve merge conflicts.

Thanks @cregouby , I have resolved the conflicts

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_perspective

2 participants