R/item-transforms-random-geometry.R
item_transform_random_affine.RdDraws a random rotation, translation, scale and shear inside the given ranges
and applies the resulting affine transformation to the dataset item with
item_transform_affine, keeping the image size unchanged. Image
and target share the same draw, so that they stay aligned.
item_transform_random_affine(
x,
degrees,
translate = NULL,
scale = NULL,
shear = NULL,
interpolation = 0,
fill = NULL,
center = NULL
)A dataset item, typically an image_with_bounding_box,
image_with_rotated_box or image_with_segmentation_mask object
containing an image tensor and associated target data.
(numeric vector of length 2 or numeric): Range of degrees to
select from. If degrees is a bare number instead of a numeric vector of
length 2 like c(min, max), the range of degrees will be
(-degrees, +degrees).
(numeric vector of length 2, optional): Numeric vector of
maximum absolute fraction for horizontal and vertical translations. For
example translate = c(a, b), then horizontal shift is randomly sampled
in the range -img_width * a < dx < img_width * a and vertical shift is
randomly sampled in the range
-img_height * b < dy < img_height * b. Will not translate by default.
(numeric vector of length 2, optional): Scaling factor
interval, e.g. c(a, b), then scale is randomly sampled from the range
a <= scale <= b. Will keep original scale by default.
(numeric vector or numeric, optional): Range of degrees to
select from. If shear is a bare number, a shear parallel to the x axis
in the range (-shear, +shear) will be applied. Else if shear is a
numeric vector of length 2, a shear parallel to the x axis in the range
(shear[1], shear[2]) will be applied. Else if shear is a numeric
vector of length 4, a x-axis shear in (shear[1], shear[2]) and y-axis
shear in (shear[3], shear[4]) will be applied. Will not apply shear by
default.
(integer or character, optional): Interpolation mode.
Supported values are 0 / "nearest" and 2 / "bilinear". Default
is 0.
Fill color for the area outside the transform. Default is
NULL.
(numeric vector of length 2, optional): Optional center of
rotation, c(x, y). Default is image center.
A dataset item with the image and target transformed. Detection items
are returned as image_with_rotated_box; segmentation items keep their
class.
The transformation is drawn again for every item, so that a dataset wrapped with this transform yields a different transformation on each access.
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",
image_height = img$shape[2], image_width = img$shape[3]))
class(before) <- c("image_with_bounding_box", "list")
after <- item_transform_random_affine(before, degrees = 30, translate = c(0.1, 0.1),
scale = c(0.8, 1.2), shear = 10)
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)
} # }