(
image_size: int,
is_train: bool,
mean: Optional[Tuple[float, ...]] = None,
std: Optional[Tuple[float, ...]] = None,
resize_longest_max: bool = False,
fill_color: int = 0,
val_keep_ratio: bool = True,
)
| 69 | |
| 70 | |
| 71 | def image_transform( |
| 72 | image_size: int, |
| 73 | is_train: bool, |
| 74 | mean: Optional[Tuple[float, ...]] = None, |
| 75 | std: Optional[Tuple[float, ...]] = None, |
| 76 | resize_longest_max: bool = False, |
| 77 | fill_color: int = 0, |
| 78 | val_keep_ratio: bool = True, |
| 79 | ): |
| 80 | |
| 81 | mean = mean or OPENAI_DATASET_MEAN |
| 82 | if not isinstance(mean, (list, tuple)): |
| 83 | mean = (mean,) * 3 |
| 84 | |
| 85 | std = std or OPENAI_DATASET_STD |
| 86 | if not isinstance(std, (list, tuple)): |
| 87 | std = (std,) * 3 |
| 88 | |
| 89 | if isinstance(image_size, (list, tuple)) and image_size[0] == image_size[1]: |
| 90 | # for square size, pass size as int so that Resize() uses aspect preserving shortest edge |
| 91 | image_size = image_size[0] |
| 92 | |
| 93 | normalize = Normalize(mean=mean, std=std) |
| 94 | if is_train: |
| 95 | return Compose([ |
| 96 | RandomResizedCrop(image_size, scale=(0.9, 1.0), |
| 97 | interpolation=InterpolationMode.BICUBIC), |
| 98 | _convert_to_rgb, |
| 99 | ToTensor(), |
| 100 | normalize, |
| 101 | ]) |
| 102 | else: |
| 103 | if resize_longest_max: |
| 104 | transforms = [ |
| 105 | ResizeMaxSize(image_size, fill=fill_color) |
| 106 | ] |
| 107 | else: |
| 108 | if val_keep_ratio: |
| 109 | transforms = [ |
| 110 | Resize(image_size, interpolation=InterpolationMode.BICUBIC), |
| 111 | CenterCrop(image_size), |
| 112 | ] |
| 113 | else: |
| 114 | transforms = [ |
| 115 | Resize((image_size, image_size), |
| 116 | interpolation=InterpolationMode.BICUBIC), |
| 117 | ] |
| 118 | transforms.extend([ |
| 119 | _convert_to_rgb, |
| 120 | ToTensor(), |
| 121 | normalize, |
| 122 | ]) |
| 123 | return Compose(transforms) |
no test coverage detected