target_transform_resize: Resize the bounding boxes of a detection target

View source: R/target-transforms-detection.R

target_transform_resizeR Documentation

Resize the bounding boxes of a detection target

Description

Adjusts the bounding box coordinates in a detection target to match a resized image. The target is expected to contain a boxes field with shape ⁠(N, 4)⁠ in xyxy format and an orig_size field with the original image dimensions ⁠(H, W)⁠.

Usage

target_transform_resize(target, size)

## S3 method for class 'object_detection_target'
target_transform_resize(target, size)

Arguments

target

An object_detection_target, as returned by the .getitem() method of an object detection dataset, or a bare list containing at least:

  • boxes — tensor of shape ⁠(N, 4)⁠ with bounding boxes in xyxy format

  • orig_size — integer vector or tensor of length 2 with ⁠(height, width)⁠

  • Other fields (labels, image_id, etc.) are preserved unchanged

size

Desired output size. If size is a integer vector of length 2 like c(h, w), output size will be matched to this. If size is a bare integer, longest edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to ⁠(size * height / width, size)⁠.

Details

This function should be composed with a transform_resize() in the transform pipeline having the same size to ensure resized bounding boxes remain aligned with the resized image.

Value

A list with the same structure as the input target, where:

  • boxes are rescaled according to the resize factors

  • orig_size is updated to the new dimensions

See Also

Other target_transforms_detection: prepare_sahi_split(), target_transform_affine(), target_transform_rotate()

Examples

## Not run: 
target <- list(
  boxes = torch_tensor(matrix(c(10, 20, 50, 60), ncol = 4)),
  labels = torch_tensor(1L, dtype = torch_long()),
  image_height  = 100L,
  image_width = 100L
)
class(target) <- c("object_detection_target", "list")

# Resize to fixed size
transform_fn <- target_transform_resize(c(200L, 200L))
new_target <- transform_fn(target)

# Resize with proportional scaling
transform_fn <- target_transform_resize(50L)
new_target <- transform_fn(target)

## End(Not run)


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