View source: R/item-transforms-random-geometry.R
| item_transform_random_crop | R Documentation |
Crops 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"
)
x |
A dataset item, typically an |
size |
Desired output size of the crop. If |
padding |
(int or vector, optional) Optional padding on each border
of the image, applied before cropping. Default is |
pad_if_needed |
(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. |
fill |
(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. |
padding_mode |
(str) Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant. |
If the image is smaller than the requested crop size, it can optionally be
padded first (see padding and pad_if_needed).
A dataset item of the same class with the image and target randomly cropped to the requested size.
Other item_random_transforms:
item_transform_random_affine(),
item_transform_random_erasing(),
item_transform_random_horizontal_flip(),
item_transform_random_perspective(),
item_transform_random_resize_crop(),
item_transform_random_rotation(),
item_transform_random_vertical_flip()
## 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_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))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.