| 187 | return image_features |
| 188 | |
| 189 | def image_preprocess(self, img: torch.Tensor) -> torch.Tensor: |
| 190 | # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_vision_objects.CLIPImageProcessor` has no attribute `crop_size`. |
| 191 | target_h = self.image_processor.crop_size["height"] |
| 192 | target_w = self.image_processor.crop_size["width"] |
| 193 | # pad the image with median rgb value, to make a square |
| 194 | l_pad = (target_w - img.shape[2]) // 2 |
| 195 | t_pad = (target_h - img.shape[1]) // 2 |
| 196 | # ceil division |
| 197 | r_pad = -((target_w - img.shape[2]) // -2) |
| 198 | b_pad = -((target_h - img.shape[1]) // -2) |
| 199 | |
| 200 | torch._check(l_pad >= 0) |
| 201 | torch._check(t_pad >= 0) |
| 202 | torch._check(r_pad >= 0) |
| 203 | torch._check(b_pad >= 0) |
| 204 | |
| 205 | # This is different from the original implementation, due to export limitations. |
| 206 | resized = torch.nn.functional.pad( |
| 207 | img, |
| 208 | (l_pad, r_pad, t_pad, b_pad), |
| 209 | ) |
| 210 | # originally: |
| 211 | # resized = F.pad( |
| 212 | # img, |
| 213 | # padding=(l_pad, t_pad, r_pad, b_pad), |
| 214 | # fill=tuple(int(x * 255) for x in self.image_mean), |
| 215 | # ) |
| 216 | |
| 217 | # TODO: implement _upsample_bicubic_aa.out in portable kernel library. |
| 218 | # here padded shape should be max(h, w) x max(h, w) |
| 219 | # skipping resize for now due to missing _upsample_bicubic_aa kernel in portable |
| 220 | # resized = resize( |
| 221 | # padded, |
| 222 | # size=[ |
| 223 | # self.image_processor.crop_size["height"], |
| 224 | # self.image_processor.crop_size["width"], |
| 225 | # ], |
| 226 | # interpolation="bicubic", |
| 227 | # ) |
| 228 | # torch._check(resized.size(1) == self.config.crop_size["height"]) |
| 229 | # torch._check(resized.size(2) == self.config.crop_size["width"]) |
| 230 | # print(resized.shape) |
| 231 | # cropped = F.center_crop(img, output_size=[w, w]) |
| 232 | # print(cropped.shape) |
| 233 | # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_vision_objects.CLIPImageProcessor` has no attribute `rescale_factor`. |
| 234 | scaled = resized * self.image_processor.rescale_factor |
| 235 | # print(scaled) |
| 236 | normed = F.normalize( |
| 237 | scaled, |
| 238 | # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_vision_objects.CLIPImageProcessor` has no attribute `image_mean`. |
| 239 | self.image_processor.image_mean, |
| 240 | # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_vision_objects.CLIPImageProcessor` has no attribute `image_std`. |
| 241 | self.image_processor.image_std, |
| 242 | ) |
| 243 | # print(normed) |
| 244 | return normed.unsqueeze(0) |
| 245 | |
| 246 | def step( |