MCPcopy Create free account
hub / github.com/OpenGVLab/UniFormerV2 / RandomResizedCropAndInterpolation

Class RandomResizedCropAndInterpolation

slowfast/datasets/transform.py:728–832  ·  view source on GitHub ↗

Crop the given PIL Image to random size and aspect ratio with random interpolation. A crop of random size (default: of 0.08 to 1.0) of the original size and a random aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop is finally resized to given size.

Source from the content-addressed store, hash-verified

726# contents with dependency from PyTorchVideo.
727# https://github.com/facebookresearch/pytorchvideo
728class RandomResizedCropAndInterpolation:
729 """Crop the given PIL Image to random size and aspect ratio with random interpolation.
730 A crop of random size (default: of 0.08 to 1.0) of the original size and a random
731 aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
732 is finally resized to given size.
733 This is popularly used to train the Inception networks.
734 Args:
735 size: expected output size of each edge
736 scale: range of size of the origin size cropped
737 ratio: range of aspect ratio of the origin aspect ratio cropped
738 interpolation: Default: PIL.Image.BILINEAR
739 """
740
741 def __init__(
742 self,
743 size,
744 scale=(0.08, 1.0),
745 ratio=(3.0 / 4.0, 4.0 / 3.0),
746 interpolation="bilinear",
747 ):
748 if isinstance(size, tuple):
749 self.size = size
750 else:
751 self.size = (size, size)
752 if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
753 print("range should be of kind (min, max)")
754
755 if interpolation == "random":
756 self.interpolation = _RANDOM_INTERPOLATION
757 else:
758 self.interpolation = _pil_interp(interpolation)
759 self.scale = scale
760 self.ratio = ratio
761
762 @staticmethod
763 def get_params(img, scale, ratio):
764 """Get parameters for ``crop`` for a random sized crop.
765 Args:
766 img (PIL Image): Image to be cropped.
767 scale (tuple): range of size of the origin size cropped
768 ratio (tuple): range of aspect ratio of the origin aspect ratio cropped
769 Returns:
770 tuple: params (i, j, h, w) to be passed to ``crop`` for a random
771 sized crop.
772 """
773 area = img.size[0] * img.size[1]
774
775 for _ in range(10):
776 target_area = random.uniform(*scale) * area
777 log_ratio = (math.log(ratio[0]), math.log(ratio[1]))
778 aspect_ratio = math.exp(random.uniform(*log_ratio))
779
780 w = int(round(math.sqrt(target_area * aspect_ratio)))
781 h = int(round(math.sqrt(target_area / aspect_ratio)))
782
783 if w <= img.size[0] and h <= img.size[1]:
784 i = random.randint(0, img.size[1] - h)
785 j = random.randint(0, img.size[0] - w)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected