(image_dir, candidates_json, device)
| 150 | |
| 151 | |
| 152 | def clipeval(image_dir, candidates_json, device): |
| 153 | image_paths = [os.path.join(image_dir, path) for path in os.listdir(image_dir) |
| 154 | if path.endswith(('.png', '.jpg', '.jpeg', '.tiff', '.JPG'))] |
| 155 | image_ids = [Path(path).stem for path in image_paths] |
| 156 | with open(candidates_json) as f: |
| 157 | candidates = json.load(f) |
| 158 | candidates = [candidates[cid] for cid in image_ids] |
| 159 | |
| 160 | model, _ = clip.load("ViT-B/32", device=device, jit=False) |
| 161 | model.eval() |
| 162 | |
| 163 | image_feats = extract_all_images( |
| 164 | image_paths, model, CLIPImageDataset, device, batch_size=64, num_workers=8) |
| 165 | |
| 166 | _, per_instance_image_text, _ = get_clip_score( |
| 167 | model, image_feats, candidates, device) |
| 168 | |
| 169 | scores = {image_id: {'CLIPScore': float(clipscore)} |
| 170 | for image_id, clipscore in |
| 171 | zip(image_ids, per_instance_image_text)} |
| 172 | print('CLIPScore: {:.4f}'.format( |
| 173 | np.mean([s['CLIPScore'] for s in scores.values()]))) |
| 174 | |
| 175 | return np.mean([s['CLIPScore'] for s in scores.values()]), np.std([s['CLIPScore'] for s in scores.values()]) |
| 176 | |
| 177 | |
| 178 | def clipeval_image(image_dir, image_dir_ref, device): |
no test coverage detected