|
25 | 25 | from torchvision import transforms as legacy_transforms |
26 | 26 | from torchvision._utils import sequence_to_str |
27 | 27 | from torchvision.prototype import features, transforms as prototype_transforms |
28 | | -from torchvision.prototype.transforms import functional as F |
| 28 | +from torchvision.prototype.transforms import functional as prototype_F |
29 | 29 | from torchvision.prototype.transforms._utils import query_spatial_size |
30 | 30 | from torchvision.prototype.transforms.functional import to_image_pil |
| 31 | +from torchvision.transforms import functional as legacy_F |
31 | 32 |
|
32 | 33 | DEFAULT_MAKE_IMAGES_KWARGS = dict(color_spaces=[features.ColorSpace.RGB], extra_dims=[(4,)]) |
33 | 34 |
|
@@ -985,7 +986,7 @@ def _transform(self, inpt, params): |
985 | 986 | return inpt |
986 | 987 |
|
987 | 988 | fill = self.fill[type(inpt)] |
988 | | - return F.pad(inpt, padding=params["padding"], fill=fill) |
| 989 | + return prototype_F.pad(inpt, padding=params["padding"], fill=fill) |
989 | 990 |
|
990 | 991 |
|
991 | 992 | class TestRefSegTransforms: |
@@ -1119,3 +1120,81 @@ def test_random_resize_eval(self, mocker): |
1119 | 1120 | t_ref = seg_transforms.RandomResize(min_size=base_size, max_size=base_size) |
1120 | 1121 |
|
1121 | 1122 | self.check_resize(mocker, t_ref, t) |
| 1123 | + |
| 1124 | + |
| 1125 | +@pytest.mark.parametrize( |
| 1126 | + ("legacy_dispatcher", "name_only_params"), |
| 1127 | + [ |
| 1128 | + (legacy_F.get_dimensions, {}), |
| 1129 | + (legacy_F.get_image_size, {}), |
| 1130 | + (legacy_F.get_image_num_channels, {}), |
| 1131 | + (legacy_F.to_tensor, {}), |
| 1132 | + (legacy_F.pil_to_tensor, {}), |
| 1133 | + (legacy_F.convert_image_dtype, {}), |
| 1134 | + (legacy_F.to_pil_image, {}), |
| 1135 | + (legacy_F.normalize, {}), |
| 1136 | + (legacy_F.resize, {}), |
| 1137 | + (legacy_F.pad, {"padding", "fill"}), |
| 1138 | + (legacy_F.crop, {}), |
| 1139 | + (legacy_F.center_crop, {}), |
| 1140 | + (legacy_F.resized_crop, {}), |
| 1141 | + (legacy_F.hflip, {}), |
| 1142 | + (legacy_F.perspective, {"startpoints", "endpoints", "fill"}), |
| 1143 | + (legacy_F.vflip, {}), |
| 1144 | + (legacy_F.five_crop, {}), |
| 1145 | + (legacy_F.ten_crop, {}), |
| 1146 | + (legacy_F.adjust_brightness, {}), |
| 1147 | + (legacy_F.adjust_contrast, {}), |
| 1148 | + (legacy_F.adjust_saturation, {}), |
| 1149 | + (legacy_F.adjust_hue, {}), |
| 1150 | + (legacy_F.adjust_gamma, {}), |
| 1151 | + (legacy_F.rotate, {"center", "fill"}), |
| 1152 | + (legacy_F.affine, {"angle", "translate", "center", "fill"}), |
| 1153 | + (legacy_F.to_grayscale, {}), |
| 1154 | + (legacy_F.rgb_to_grayscale, {}), |
| 1155 | + (legacy_F.to_tensor, {}), |
| 1156 | + (legacy_F.erase, {}), |
| 1157 | + (legacy_F.gaussian_blur, {}), |
| 1158 | + (legacy_F.invert, {}), |
| 1159 | + (legacy_F.posterize, {}), |
| 1160 | + (legacy_F.solarize, {}), |
| 1161 | + (legacy_F.adjust_sharpness, {}), |
| 1162 | + (legacy_F.autocontrast, {}), |
| 1163 | + (legacy_F.equalize, {}), |
| 1164 | + (legacy_F.elastic_transform, {"fill"}), |
| 1165 | + ], |
| 1166 | +) |
| 1167 | +def test_dispatcher_signature_consistency(legacy_dispatcher, name_only_params): |
| 1168 | + legacy_signature = inspect.signature(legacy_dispatcher) |
| 1169 | + legacy_params = list(legacy_signature.parameters.values())[1:] |
| 1170 | + |
| 1171 | + try: |
| 1172 | + prototype_dispatcher = getattr(prototype_F, legacy_dispatcher.__name__) |
| 1173 | + except AttributeError: |
| 1174 | + raise AssertionError( |
| 1175 | + f"Legacy dispatcher `F.{legacy_dispatcher.__name__}` has no prototype equivalent" |
| 1176 | + ) from None |
| 1177 | + |
| 1178 | + prototype_signature = inspect.signature(prototype_dispatcher) |
| 1179 | + prototype_params = list(prototype_signature.parameters.values())[1:] |
| 1180 | + |
| 1181 | + # Some dispatchers got extra parameters. This makes sure they have a default argument and thus are BC. We don't |
| 1182 | + # need to check if parameters were added in the middle rather than at the end, since that will be caught by the |
| 1183 | + # regular check below. |
| 1184 | + prototype_params, new_prototype_params = ( |
| 1185 | + prototype_params[: len(legacy_params)], |
| 1186 | + prototype_params[len(legacy_params) :], |
| 1187 | + ) |
| 1188 | + for param in new_prototype_params: |
| 1189 | + assert param.default is not param.empty |
| 1190 | + |
| 1191 | + # Some annotations were changed mostly to supersets of what was there before. Plus, some legacy dispatchers had no |
| 1192 | + # annotations. In these cases we simply drop the annotation and default argument from the comparison |
| 1193 | + for prototype_param, legacy_param in zip(prototype_params, legacy_params): |
| 1194 | + if legacy_param.name in name_only_params: |
| 1195 | + prototype_param._annotation = prototype_param._default = inspect.Parameter.empty |
| 1196 | + legacy_param._annotation = legacy_param._default = inspect.Parameter.empty |
| 1197 | + elif legacy_param.annotation is inspect.Parameter.empty: |
| 1198 | + prototype_param._annotation = inspect.Parameter.empty |
| 1199 | + |
| 1200 | + assert prototype_params == legacy_params |
0 commit comments