(
image_size: Union[int, Tuple[int, int]],
rescale_factor: float = 1.0,
mean: Optional[Tuple[float, ...]] = None,
std: Optional[Tuple[float, ...]] = None,
random_flip = False,
)
| 59 | return Compose(transforms) |
| 60 | |
| 61 | def frame_transform( |
| 62 | image_size: Union[int, Tuple[int, int]], |
| 63 | rescale_factor: float = 1.0, |
| 64 | mean: Optional[Tuple[float, ...]] = None, |
| 65 | std: Optional[Tuple[float, ...]] = None, |
| 66 | random_flip = False, |
| 67 | ): |
| 68 | mean = mean or OPENAI_DATASET_MEAN |
| 69 | if not isinstance(mean, (list, tuple)): |
| 70 | mean = (mean,) * 3 |
| 71 | |
| 72 | std = std or OPENAI_DATASET_STD |
| 73 | if not isinstance(std, (list, tuple)): |
| 74 | std = (std,) * 3 |
| 75 | |
| 76 | if isinstance(image_size, int): |
| 77 | resize_size = (image_size, image_size) |
| 78 | crop_size = (image_size, image_size) |
| 79 | elif isinstance(image_size, (list, tuple)) and len(image_size) == 2: |
| 80 | resize_size = (image_size[0], image_size[1]) |
| 81 | crop_size = (image_size[0], image_size[1]) |
| 82 | else: |
| 83 | raise ValueError("image_size must be an int or a tuple of two ints.") |
| 84 | |
| 85 | normalize = Normalize(mean=mean, std=std) |
| 86 | |
| 87 | transforms = [ |
| 88 | ToPILImage(), |
| 89 | Resize(resize_size, interpolation=InterpolationMode.BICUBIC), |
| 90 | CenterCrop(resize_size), |
| 91 | RandomHorizontalFlip() if random_flip else Lambda(lambda x: x), |
| 92 | ] |
| 93 | transforms.extend([ |
| 94 | _convert_to_rgb, |
| 95 | ToTensor(), |
| 96 | normalize, |
| 97 | ]) |
| 98 | return Compose(transforms) |
| 99 | |
| 100 | def expand2square(pil_img, background_color=tuple(int(x*255) for x in OPENAI_DATASET_MEAN)): |
| 101 | width, height = pil_img.size |
no outgoing calls
no test coverage detected