R/sahi-split.R, R/target-transforms-detection.R, R/transforms-generics.R
transform_sahi_crop.RdThe SAHI transform pipeline consists of three functions:
prepare_sahi_split() precomputes overlapping crop windows
from an image, tensor, numeric dimensions, or dataset.
transform_sahi_crop() applies the crop windows to an image,
returning the same type as the input (stacked tensor or multi-frame
magick image).
target_transform_sahi_crop() adjusts detection targets
(bounding boxes and labels) to match the cropped images.
Use as transform / target_transform in image datasets.
Image input (torch_tensor or magick-image) for
transform_sahi_crop. For prepare_sahi_split it can also be
a numeric vector c(height, width) or a dataset.
Integer vector of length 2: crop c(height, width).
Numeric vector of length 2: vertical and horizontal
overlap ratios c(overlap_h, overlap_w).
An object_detection_target or a bare list containing boxes and
labels, with bounding boxes in (x1, y1, x2, y2) format, or a
list of such targets to transform as a batch.
An object of class sahi_split created by
prepare_sahi_split().
Numeric between 0 and 1. Minimum fraction of original bounding box area that must remain after clipping for the annotation to be retained.
An object of class sahi_split with
crop_windows, size, overlap_size_ratio,
image_height, and image_width. Crop window coordinates
are 1-based.
Same type as x:
torch_tensor in \(\rightarrow\) stacked \((N, C, H, W)\)
tensor; magick-image in \(\rightarrow\) multi-frame magick
image.
List of transformed targets, one per
crop window, each with the same structure as the input y except
is_crowd (if present) is removed.
Other target_transforms_detection:
target_transform_affine(),
target_transform_resize(),
target_transform_rotate()
Other combining_transforms:
transform_five_crop(),
transform_random_apply(),
transform_random_choice(),
transform_random_order(),
transform_resized_crop(),
transform_ten_crop()
if (FALSE) { # \dontrun{
# Dataset workflow: prepare SAHI split from a resized COCO dataset
ds <- coco_detection_dataset(
train = FALSE, year = "2017", download = TRUE,
transform = . %>%
transform_to_tensor() %>%
transform_resize(c(500, 400)),
target_transform = . %>%
target_transform_resize(c(500, 400))
)
# Computes crop windows matching the resized (500x400) image dimensions
sp <- prepare_sahi_split(ds, size = c(200L, 200L), overlap_size_ratio = c(0.2, 0.2))
ds_sahi <- coco_detection_dataset(
train = FALSE, year = "2017", download = FALSE,
transform = . %>%
transform_to_tensor() %>%
transform_resize(c(500, 400)) %>%
transform_sahi_crop(sp),
target_transform = . %>%
target_transform_resize(c(500, 400)) %>%
target_transform_sahi_crop(sp, min_area_ratio = 0.2)
)
# Visualize the SAHI crops for the first image
item <- ds_sahi[1]
crops <- item$x
targets <- item$y
preview <- lapply(1:dim(crops)[1], function(i) {
item <- list(x = crops[i, ..], y = targets[[i]])
class(item) <- "image_with_bounding_box"
draw_bounding_boxes(item, colors = "red")
})
grid <- vision_make_grid(torch_stack(preview), scale = FALSE, per_row = 3)
tensor_image_browse(grid)
} # }