(self, device: torch.device, path: str = MODEL_PATHS)
| 6 | |
| 7 | class CLIPScore(torch.nn.Module): |
| 8 | def __init__(self, device: torch.device, path: str = MODEL_PATHS): |
| 9 | super().__init__() |
| 10 | """Initialize the CLIPScore with a model and tokenizer. |
| 11 | |
| 12 | Args: |
| 13 | device (torch.device): The device to load the model on. |
| 14 | """ |
| 15 | self.device = device |
| 16 | |
| 17 | # Create model and transforms |
| 18 | self.model, _, self.preprocess_val = create_model_and_transforms( |
| 19 | "ViT-H-14", |
| 20 | # "laion2B-s32B-b79K", |
| 21 | pretrained=path.get("open_clip"), |
| 22 | precision="amp", |
| 23 | device=device, |
| 24 | jit=False, |
| 25 | force_quick_gelu=False, |
| 26 | force_custom_text=False, |
| 27 | force_patch_dropout=False, |
| 28 | force_image_size=None, |
| 29 | pretrained_image=False, |
| 30 | image_mean=None, |
| 31 | image_std=None, |
| 32 | light_augmentation=True, |
| 33 | aug_cfg={}, |
| 34 | output_dict=True, |
| 35 | with_score_predictor=False, |
| 36 | with_region_predictor=False, |
| 37 | ) |
| 38 | |
| 39 | # Initialize tokenizer |
| 40 | self.tokenizer = get_tokenizer("ViT-H-14", path["open_clip_bpe"]) |
| 41 | self.model = self.model.to(device) |
| 42 | self.model.eval() |
| 43 | |
| 44 | def _calculate_score(self, image: torch.Tensor, prompt: str) -> float: |
| 45 | """Calculate the CLIP score for a single image and prompt. |
nothing calls this directly
no test coverage detected