View source: R/item-transforms-random-geometry.R
| item_transform_random_resize_crop | R Documentation |
Crops the image inside a dataset item to a random area and aspect ratio, then resizes that crop to the given size. For detection items, bounding boxes are cropped along with the image and their coordinates are rescaled to the output size: boxes falling outside the crop are dropped and boxes straddling its border are clipped to it. For segmentation items, the masks are cropped and resized alongside the image with nearest-neighbour sampling, so that they keep their discrete values.
item_transform_random_resize_crop(
x,
size,
scale = c(0.08, 1),
ratio = c(3/4, 4/3),
interpolation = 2
)
x |
A dataset item, typically an |
size |
(integer vector or integer): Desired output size. If |
scale |
(numeric vector of length 2): Range of size of the origin size cropped. |
ratio |
(numeric vector of length 2): Range of aspect ratio of the origin aspect ratio cropped. ratio cropped. |
interpolation |
(integer, optional): Desired interpolation. An integer
|
The crop is drawn again for every item, so that a dataset wrapped with this transform yields a different crop on each access.
The area field of a detection target, when present, is rescaled by the
area ratio between the crop and the output. For rotated boxes, a crop whose
aspect ratio differs from the output one maps the box to a parallelogram: the
enclosing box stays exact and the angle is that of the transformed box axis,
which is exact whenever both edges are scaled alike.
A dataset item of the same class with the image and target cropped and resized.
Other item_random_transforms:
item_transform_random_affine(),
item_transform_random_crop(),
item_transform_random_erasing(),
item_transform_random_horizontal_flip(),
item_transform_random_perspective(),
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_resize_crop(before, size = c(600, 800))
before_plot <- draw_bounding_boxes(before, colors = "blue", width = 10)
after_plot <- draw_bounding_boxes(after, colors = "red", width = 10)
tensor_image_browse(before_plot)
tensor_image_browse(after_plot)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.