item_transform_random_erasing: Randomly erase a rectangular region of a dataset item

View source: R/item-transforms-random-geometry.R

item_transform_random_erasingR Documentation

Randomly erase a rectangular region of a dataset item

Description

Randomly selects a rectangular region in the image inside a dataset item and erases its pixel values with a given probability. Only the image pixels are modified: bounding boxes and masks are left unchanged.

Usage

item_transform_random_erasing(
  x,
  p = 0.5,
  scale = c(0.02, 0.33),
  ratio = c(0.3, 3.3),
  value = 0,
  inplace = FALSE
)

Arguments

x

A dataset item, typically an image_with_bounding_box or image_with_segmentation_mask object containing an image tensor and associated target data.

p

(numeric): Probability that the random erasing operation will be performed. Default is 0.5.

scale

(numeric vector of length 2): Range of proportion of erased area against input image.

ratio

(numeric vector of length 2): Range of aspect ratio of erased area.

value

(numeric vector or numeric or character): Erasing value. Default is 0. If a single numeric value, it is used to erase all pixels. If a numeric vector of length 3, it is used to erase R, G, B channels respectively. If the string "random", erasing each pixel with random values.

inplace

(logical): Boolean to make this transform inplace. Default set to FALSE.

Details

'Random Erasing Data Augmentation' by Zhong et al. See https://arxiv.org/abs/1708.04896

Value

A dataset item of the same class. With probability p, the image is randomly erased; otherwise it is returned unchanged.

See Also

Other item_random_transforms: item_transform_random_affine(), item_transform_random_crop(), item_transform_random_horizontal_flip(), item_transform_random_perspective(), item_transform_random_resize_crop(), item_transform_random_rotation(), item_transform_random_vertical_flip()

Examples

## Not run: 
url <- "https://upload.wikimedia.org/wikipedia/commons/b/b6/Felis_catus-cat_on_snow.jpg"
img <- base_loader(url) |> transform_to_tensor()

boxes <- torch_tensor(matrix(c(600, 200, 2880, 1860), ncol = 4), dtype = torch_float32())

before <- list(x = img, y = list(boxes = boxes, labels = "cat"))
class(before) <- c("image_with_bounding_box", "list")

after <- item_transform_random_erasing(before)

before_plot <- draw_bounding_boxes(before, colors = "blue", width = 10)
after_plot <- draw_bounding_boxes(after, colors = "red", width = 10)

tensor_image_browse(vision_make_grid(before_plot, after_plot, scale = TRUE))

## End(Not run)


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