transform_sahi_crop: Slicing Aid for Hyper Inference (SAHI)

prepare_sahi_splitR Documentation

Slicing Aid for Hyper Inference (SAHI)

Description

The 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.

Usage

prepare_sahi_split(x, size = c(512L, 512L), overlap_size_ratio = c(0.2, 0.2))

target_transform_sahi_crop(y, sahi_split, min_area_ratio = 0.1)

## S3 method for class 'object_detection_target'
target_transform_sahi_crop(y, sahi_split, min_area_ratio = 0.1)

transform_sahi_crop(x, sahi_split)

Arguments

x

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.

size

Integer vector of length 2: crop c(height, width).

overlap_size_ratio

Numeric vector of length 2: vertical and horizontal overlap ratios c(overlap_h, overlap_w).

y

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.

sahi_split

An object of class sahi_split created by prepare_sahi_split().

min_area_ratio

Numeric between 0 and 1. Minimum fraction of original bounding box area that must remain after clipping for the annotation to be retained.

Value

prepare_sahi_split

An object of class sahi_split with crop_windows, size, overlap_size_ratio, image_height, and image_width. Crop window coordinates are 1-based.

transform_sahi_crop

Same type as x: torch_tensor in \rightarrow stacked (N, C, H, W) tensor; magick-image in \rightarrow multi-frame magick image.

target_transform_sahi_crop

List of transformed targets, one per crop window, each with the same structure as the input y except is_crowd (if present) is removed.

See Also

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()

Examples

## Not run: 
# 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)

## End(Not run)


torchvision documentation built on Oct. 14, 2026, 1:09 a.m.