justinkay commited on
Commit
d442533
·
1 Parent(s): 8a229fe

Remove hf zeroshot bioclip inference

Browse files
Files changed (2) hide show
  1. hf_zeroshot.py +55 -50
  2. 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
- "imageomics/bioclip",
52
- "imageomics/bioclip-2",
 
 
 
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
- """Run zero-shot inference using BioCLIP via OpenCLIP."""
78
- if not OPEN_CLIP_AVAILABLE:
79
- print("open_clip is not available. Please install it with: pip install open_clip_torch")
80
- return None
81
 
82
- print(f"Loading BioCLIP model: {model_name}")
83
- try:
84
- device = "cuda" if torch.cuda.is_available() else "cpu"
85
 
86
- # Load model using OpenCLIP with hf-hub prefix
87
- model, _, preprocess = open_clip.create_model_and_transforms(f'hf-hub:{model_name}')
88
- model = model.to(device)
89
- model.eval()
90
- tokenizer = open_clip.get_tokenizer(f'hf-hub:{model_name}')
91
 
92
- # Prepare text prompts
93
- prompts = [f"a photo of a {class_name.lower()}" for class_name in class_names]
94
- text_tokens = tokenizer(prompts).to(device)
95
 
96
- results = {}
97
 
98
- with torch.no_grad():
99
- # Encode text once
100
- text_features = model.encode_text(text_tokens)
101
- text_features /= text_features.norm(dim=-1, keepdim=True)
102
 
103
- for i, image_path in enumerate(image_paths):
104
- if i % 10 == 0:
105
- print(f"Processing image {i+1}/{len(image_paths)}: {os.path.basename(image_path)}")
106
 
107
- try:
108
- image = Image.open(image_path).convert("RGB")
109
- image_tensor = preprocess(image).unsqueeze(0).to(device)
110
 
111
- # Encode image
112
- image_features = model.encode_image(image_tensor)
113
- image_features /= image_features.norm(dim=-1, keepdim=True)
114
 
115
- # Calculate similarity and convert to probabilities
116
- similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)
117
- probabilities = similarity.squeeze(0).cpu().numpy()
118
 
119
- scores = {}
120
- for j, class_name in enumerate(class_names):
121
- scores[class_name] = float(probabilities[j])
122
 
123
- results[os.path.basename(image_path)] = scores
124
 
125
- except Exception as e:
126
- print(f"Error processing {image_path}: {e}")
127
- uniform_prob = 1.0 / len(class_names)
128
- results[os.path.basename(image_path)] = {class_name: uniform_prob for class_name in class_names}
129
 
130
- return results
131
 
132
- except Exception as e:
133
- print(f"Error loading BioCLIP: {e}")
134
- import traceback
135
- traceback.print_exc()
136
- return None
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:f31fb9889d96fcecb5899a12e03102cbebac1d02a1689770ca734d748333286c
3
- size 272
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a9449ef5b30e49bdecca0101e45992795f1650e0bee183cd2bf03dcd0ecfaa5
3
+ size 127187