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

Function random_crop

slowfast/datasets/transform.py:117–150  ·  view source on GitHub ↗

Perform random spatial crop on the given images and corresponding boxes. Args: images (tensor): images to perform random crop. The dimension is `num frames` x `channel` x `height` x `width`. size (int): the size of height and width to crop on the image. b

(images, size, boxes=None)

Source from the content-addressed store, hash-verified

115
116
117def random_crop(images, size, boxes=None):
118 """
119 Perform random spatial crop on the given images and corresponding boxes.
120 Args:
121 images (tensor): images to perform random crop. The dimension is
122 `num frames` x `channel` x `height` x `width`.
123 size (int): the size of height and width to crop on the image.
124 boxes (ndarray or None): optional. Corresponding boxes to images.
125 Dimension is `num boxes` x 4.
126 Returns:
127 cropped (tensor): cropped images with dimension of
128 `num frames` x `channel` x `size` x `size`.
129 cropped_boxes (ndarray or None): the cropped boxes with dimension of
130 `num boxes` x 4.
131 """
132 if images.shape[2] == size and images.shape[3] == size:
133 return images
134 height = images.shape[2]
135 width = images.shape[3]
136 y_offset = 0
137 if height > size:
138 y_offset = int(np.random.randint(0, height - size))
139 x_offset = 0
140 if width > size:
141 x_offset = int(np.random.randint(0, width - size))
142 cropped = images[
143 :, :, y_offset : y_offset + size, x_offset : x_offset + size
144 ]
145
146 cropped_boxes = (
147 crop_boxes(boxes, x_offset, y_offset) if boxes is not None else None
148 )
149
150 return cropped, cropped_boxes
151
152
153def horizontal_flip(prob, images, boxes=None):

Callers

nothing calls this directly

Calls 1

crop_boxesFunction · 0.70

Tested by

no test coverage detected