MCPcopy Create free account
hub / github.com/apple/ml-streambridge / image_transform

Function image_transform

streambridge/utils.py:175–207  ·  view source on GitHub ↗
(
        image_size: int,
        rescale_factor: float = 1.0,
        mean: Optional[Tuple[float, ...]] = None,
        std: Optional[Tuple[float, ...]] = None,
        random_flip = False,
)

Source from the content-addressed store, hash-verified

173INTERNVIDEO_STD = (0.229, 0.224, 0.225)
174
175def image_transform(
176 image_size: int,
177 rescale_factor: float = 1.0,
178 mean: Optional[Tuple[float, ...]] = None,
179 std: Optional[Tuple[float, ...]] = None,
180 random_flip = False,
181):
182 mean = mean or OPENAI_DATASET_MEAN
183 if not isinstance(mean, (list, tuple)):
184 mean = (mean,) * 3
185
186 std = std or OPENAI_DATASET_STD
187 if not isinstance(std, (list, tuple)):
188 std = (std,) * 3
189
190 if isinstance(image_size, (list, tuple)) and image_size[0] == image_size[1]:
191 # for square size, pass size as int so that Resize() uses aspect preserving shortest edge
192 image_size = image_size[0]
193
194 normalize = Normalize(mean=mean, std=std)
195
196 transforms = [
197 Resize(image_size, interpolation=InterpolationMode.BICUBIC),
198 CenterCrop(image_size),
199 RandomHorizontalFlip() if random_flip else Lambda(lambda x: x),
200 ]
201
202 transforms.extend([
203 _convert_to_rgb,
204 ToTensor(),
205 normalize,
206 ])
207 return Compose(transforms)
208
209def frame_transform(
210 image_size: Union[int, Tuple[int, int]],

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected