justinkay
commited on
Commit
·
d442533
1
Parent(s):
8a229fe
Remove hf zeroshot bioclip inference
Browse files- hf_zeroshot.py +55 -50
- iwildcam_demo.pt +2 -2
hf_zeroshot.py
CHANGED
|
@@ -48,8 +48,11 @@ MODELS = [
|
|
| 48 |
"google/siglip2-large-patch16-384",
|
| 49 |
"google/siglip2-large-patch16-512",
|
| 50 |
"google/siglip2-so400m-patch16-naflex",
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
"facebook/PE-Core-L14-336",
|
| 54 |
"laion/CLIP-ViT-L-14-laion2B-s32B-b82K"
|
| 55 |
]
|
|
@@ -73,67 +76,67 @@ def load_demo_annotations():
|
|
| 73 |
|
| 74 |
return image_metadata
|
| 75 |
|
| 76 |
-
def run_bioclip_inference(model_name, image_paths, class_names):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
def run_openclip_inference(model_name, image_paths, class_names):
|
| 139 |
"""Run zero-shot inference using OpenCLIP models."""
|
|
@@ -333,7 +336,9 @@ def main():
|
|
| 333 |
|
| 334 |
# Handle different models with appropriate methods
|
| 335 |
if model_name in ["imageomics/bioclip", "imageomics/bioclip-2"]:
|
| 336 |
-
results = run_bioclip_inference(model_name, image_paths, CLASS_NAMES)
|
|
|
|
|
|
|
| 337 |
elif model_name.startswith("google/siglip"):
|
| 338 |
results = run_siglip_inference(model_name, image_paths, CLASS_NAMES)
|
| 339 |
elif model_name in ["facebook/PE-Core-L14-336", "laion/CLIP-ViT-L-14-laion2B-s32B-b82K"]:
|
|
|
|
| 48 |
"google/siglip2-large-patch16-384",
|
| 49 |
"google/siglip2-large-patch16-512",
|
| 50 |
"google/siglip2-so400m-patch16-naflex",
|
| 51 |
+
|
| 52 |
+
# using bioclip codebase instead
|
| 53 |
+
# "imageomics/bioclip",
|
| 54 |
+
# "imageomics/bioclip-2",
|
| 55 |
+
|
| 56 |
"facebook/PE-Core-L14-336",
|
| 57 |
"laion/CLIP-ViT-L-14-laion2B-s32B-b82K"
|
| 58 |
]
|
|
|
|
| 76 |
|
| 77 |
return image_metadata
|
| 78 |
|
| 79 |
+
# def run_bioclip_inference(model_name, image_paths, class_names):
|
| 80 |
+
# """Run zero-shot inference using BioCLIP via OpenCLIP."""
|
| 81 |
+
# if not OPEN_CLIP_AVAILABLE:
|
| 82 |
+
# print("open_clip is not available. Please install it with: pip install open_clip_torch")
|
| 83 |
+
# return None
|
| 84 |
|
| 85 |
+
# print(f"Loading BioCLIP model: {model_name}")
|
| 86 |
+
# try:
|
| 87 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 88 |
|
| 89 |
+
# # Load model using OpenCLIP with hf-hub prefix
|
| 90 |
+
# model, _, preprocess = open_clip.create_model_and_transforms(f'hf-hub:{model_name}')
|
| 91 |
+
# model = model.to(device)
|
| 92 |
+
# model.eval()
|
| 93 |
+
# tokenizer = open_clip.get_tokenizer(f'hf-hub:{model_name}')
|
| 94 |
|
| 95 |
+
# # Prepare text prompts
|
| 96 |
+
# prompts = [f"a photo of a {class_name.lower()}" for class_name in class_names]
|
| 97 |
+
# text_tokens = tokenizer(prompts).to(device)
|
| 98 |
|
| 99 |
+
# results = {}
|
| 100 |
|
| 101 |
+
# with torch.no_grad():
|
| 102 |
+
# # Encode text once
|
| 103 |
+
# text_features = model.encode_text(text_tokens)
|
| 104 |
+
# text_features /= text_features.norm(dim=-1, keepdim=True)
|
| 105 |
|
| 106 |
+
# for i, image_path in enumerate(image_paths):
|
| 107 |
+
# if i % 10 == 0:
|
| 108 |
+
# print(f"Processing image {i+1}/{len(image_paths)}: {os.path.basename(image_path)}")
|
| 109 |
|
| 110 |
+
# try:
|
| 111 |
+
# image = Image.open(image_path).convert("RGB")
|
| 112 |
+
# image_tensor = preprocess(image).unsqueeze(0).to(device)
|
| 113 |
|
| 114 |
+
# # Encode image
|
| 115 |
+
# image_features = model.encode_image(image_tensor)
|
| 116 |
+
# image_features /= image_features.norm(dim=-1, keepdim=True)
|
| 117 |
|
| 118 |
+
# # Calculate similarity and convert to probabilities
|
| 119 |
+
# similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)
|
| 120 |
+
# probabilities = similarity.squeeze(0).cpu().numpy()
|
| 121 |
|
| 122 |
+
# scores = {}
|
| 123 |
+
# for j, class_name in enumerate(class_names):
|
| 124 |
+
# scores[class_name] = float(probabilities[j])
|
| 125 |
|
| 126 |
+
# results[os.path.basename(image_path)] = scores
|
| 127 |
|
| 128 |
+
# except Exception as e:
|
| 129 |
+
# print(f"Error processing {image_path}: {e}")
|
| 130 |
+
# uniform_prob = 1.0 / len(class_names)
|
| 131 |
+
# results[os.path.basename(image_path)] = {class_name: uniform_prob for class_name in class_names}
|
| 132 |
|
| 133 |
+
# return results
|
| 134 |
|
| 135 |
+
# except Exception as e:
|
| 136 |
+
# print(f"Error loading BioCLIP: {e}")
|
| 137 |
+
# import traceback
|
| 138 |
+
# traceback.print_exc()
|
| 139 |
+
# return None
|
| 140 |
|
| 141 |
def run_openclip_inference(model_name, image_paths, class_names):
|
| 142 |
"""Run zero-shot inference using OpenCLIP models."""
|
|
|
|
| 336 |
|
| 337 |
# Handle different models with appropriate methods
|
| 338 |
if model_name in ["imageomics/bioclip", "imageomics/bioclip-2"]:
|
| 339 |
+
# results = run_bioclip_inference(model_name, image_paths, CLASS_NAMES)
|
| 340 |
+
print("Use pybioclip!")
|
| 341 |
+
return
|
| 342 |
elif model_name.startswith("google/siglip"):
|
| 343 |
results = run_siglip_inference(model_name, image_paths, CLASS_NAMES)
|
| 344 |
elif model_name in ["facebook/PE-Core-L14-336", "laion/CLIP-ViT-L-14-laion2B-s32B-b82K"]:
|
iwildcam_demo.pt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8a9449ef5b30e49bdecca0101e45992795f1650e0bee183cd2bf03dcd0ecfaa5
|
| 3 |
+
size 127187
|