R/item-transforms-random-geometry.R
item_transform_random_crop.RdCrops the image inside a dataset item at a random location. When a crop
occurs, the same coordinate adjustments used by
item_transform_crop are applied to bounding boxes and masks so
that the targets stay aligned with the cropped image.
item_transform_random_crop(
x,
size,
padding = NULL,
pad_if_needed = FALSE,
fill = 0,
padding_mode = "constant"
)A dataset item, typically an image_with_bounding_box or
image_with_segmentation_mask object containing an image tensor
and associated target data.
Desired output size of the crop. If size is an
int instead of a sequence like c(h, w), a square crop
c(size, size) is made. If a sequence of length 1 is provided it is
interpreted as c(size[1], size[1]).
(int or vector, optional) Optional padding on each border
of the image, applied before cropping. Default is NULL. If a single
int is provided this is used to pad all borders. If a vector of length 2 is
provided this is the padding on left/right and top/bottom respectively. If
a vector of length 4 is provided this is the padding for the left, top,
right and bottom borders respectively.
(logical) It will pad the image if smaller than the desired size to avoid raising an exception. Since cropping is done after padding, the padding seems to be done at a random offset.
(numeric or vector) Pixel fill value for constant fill. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively. This value is only used when the padding_mode is constant.
(str) Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
A dataset item of the same class with the image and target randomly cropped to the requested size.
If the image is smaller than the requested crop size, it can optionally be
padded first (see padding and pad_if_needed).
if (FALSE) { # \dontrun{
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_crop(before, size = c(800, 1200))
# the crop will changes the image size, so resize is needed before vision_make_grid
before_plot <- before %>%
draw_bounding_boxes(colors = "blue", width = 10) %>%
transform_resize(c(600, 600))
after_plot <- after %>%
draw_bounding_boxes(colors = "red", width = 10) %>%
transform_resize(c(600, 600))
tensor_image_browse(vision_make_grid(before_plot, after_plot))
} # }