RandomScale
Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float16, float32
Resize by a random scale factor (scale_range). Output size differs from input; all targets scaled together. Useful for scale augmentation without cropping.
Arguments
scale_rangetuple[float, float] | dict[['x', 'y'], tuple[float, float]]
[-0.1, 0.1]
Scaling factor range.
- If a tuple
(low, high): A single scale factor is sampled per image from(1 + low, 1 + high)and applied uniformly to both width and height. - If a dict with keys
"x"and"y": Each entry must be a(low, high)tuple. Scale factors are sampled independently per axis. Both keys are required. Default:(-0.1, 0.1).
interpolation0 | 1 | 2 | 3 | 4 | 5 | 6
1
flag that is used to specify the interpolation algorithm. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR.
mask_interpolation0 | 1 | 2 | 3 | 4 | 5 | 6
0
flag that is used to specify the interpolation algorithm for mask. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST.
area_for_downscaleimage | image_mask |
Controls automatic use of INTER_AREA interpolation for downscaling. Options:
- None: No automatic interpolation selection, always use the specified interpolation method
- "image": Use INTER_AREA when downscaling images, retain specified interpolation for upscaling and masks
- "image_mask": Use INTER_AREA when downscaling both images and masks Default: None.
pfloat
0.5
probability of applying the transform. Default: 0.5.
Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Create sample data for demonstration
>>> image = np.zeros((100, 100, 3), dtype=np.uint8)
>>> # Add some shapes to visualize scaling effects
>>> cv2.rectangle(image, (25, 25), (75, 75), (255, 0, 0), -1) # Red square
>>> cv2.circle(image, (50, 50), 10, (0, 255, 0), -1) # Green circle
>>>
>>> # Create a mask for segmentation
>>> mask = np.zeros((100, 100), dtype=np.uint8)
>>> mask[25:75, 25:75] = 1 # Mask covering the red square
>>>
>>> # Create bounding boxes and keypoints
>>> bboxes = np.array([[25, 25, 75, 75]]) # Box around the red square
>>> bbox_labels = [1]
>>> keypoints = np.array([[50, 50]]) # Center of circle
>>> keypoint_labels = [0]
>>>
>>> # Example 1: Uniform scaling with tuple scale_range
>>> transform = A.Compose([
... A.RandomScale(
... scale_range=(-0.3, 0.5), # Uniform: scale between 0.7x and 1.5x
... interpolation=cv2.INTER_LINEAR,
... mask_interpolation=cv2.INTER_NEAREST,
... area_for_downscale="image", # Use INTER_AREA for image downscaling
... p=1.0 # Always apply
... )
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
... keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform to all targets
>>> result = transform(
... image=image,
... mask=mask,
... bboxes=bboxes,
... bbox_labels=bbox_labels,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed results
>>> scaled_image = result['image'] # Dimensions will be between 70-150 pixels
>>> scaled_mask = result['mask'] # Mask scaled proportionally to image
>>> scaled_bboxes = result['bboxes'] # Bounding boxes adjusted to new dimensions
>>> scaled_bbox_labels = result['bbox_labels'] # Labels remain unchanged
>>> scaled_keypoints = result['keypoints'] # Keypoints adjusted to new dimensions
>>> scaled_keypoint_labels = result['keypoint_labels'] # Labels remain unchanged
>>>
>>> # Example 2: Anisotropic scaling with dict scale_range
>>> transform2 = A.Compose([
... A.RandomScale(
... scale_range={"x": (-0.20, 0.30), "y": (-0.10, 0.15)}, # Independent axes
... interpolation=cv2.INTER_LINEAR,
... p=1.0
... )
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
... keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> result2 = transform2(
... image=image,
... mask=mask,
... bboxes=bboxes,
... bbox_labels=bbox_labels,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>> # Width and height will scale independentlyNotes
- The output image size is different from the input image size.
- Scale factor is sampled independently per image side (width and height).
- Bounding box coordinates are scaled accordingly.
- Keypoint coordinates are scaled accordingly.
- When area_for_downscale is set, INTER_AREA interpolation will be used automatically for downscaling (scale < 1.0), which provides better quality for size reduction.
- When
scale_rangeis a tuple, the same scale factor is applied to both width and height (uniform scaling). When it is a dict,scale_xandscale_yare sampled independently (anisotropic scaling).