| prepare_sahi_split | R Documentation |
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.
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)
x |
Image input ( |
size |
Integer vector of length 2: crop |
overlap_size_ratio |
Numeric vector of length 2: vertical and horizontal
overlap ratios |
y |
An |
sahi_split |
An object of class |
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. |
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()
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.