| 80 | |
| 81 | |
| 82 | class DINOv2Encoder(Encoder): |
| 83 | def setup(self, arch=None, clean_resize: bool = False): |
| 84 | if arch is None: |
| 85 | arch = "vitl14" |
| 86 | |
| 87 | self.arch = arch |
| 88 | |
| 89 | arch_str = f"dinov2_{self.arch}" |
| 90 | |
| 91 | self.model = torch.hub.load("facebookresearch/dinov2", arch_str) |
| 92 | |
| 93 | def transform(self, image): |
| 94 | |
| 95 | imagenet_mean = np.array([0.485, 0.456, 0.406]) |
| 96 | imagenet_std = np.array([0.229, 0.224, 0.225]) |
| 97 | |
| 98 | image = TF.Compose( |
| 99 | [ |
| 100 | TF.Resize((224, 224), TF.InterpolationMode.BICUBIC), |
| 101 | ] |
| 102 | )(image) |
| 103 | image = image.to(torch.float) |
| 104 | |
| 105 | return TF.Normalize(imagenet_mean, imagenet_std)(image) |
| 106 | |
| 107 | |
| 108 | def _compute_fid(mu1: Tensor, sigma1: Tensor, mu2: Tensor, sigma2: Tensor) -> Tensor: |