numanai commited on
Commit
f873b73
·
1 Parent(s): 8e2f002

Update the model weights loading method

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoModel, AutoTokenizer
 
4
  from PIL import Image
5
  import json
6
  import os
@@ -11,14 +12,29 @@ from tqdm import tqdm
11
  ASSETS_DIR = "assets"
12
  EXAMPLES_DIR = "examples"
13
  PATH_TEXT_PROMPTS = os.path.join(ASSETS_DIR, "prompt_fetal_view.json")
14
- MODEL_NAME = "numansaeed/fetalclip-model" # Replace with your actual model name
 
15
 
16
  def load_model():
17
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
 
19
- # Load model and tokenizer from Hugging Face
20
- model = AutoModel.from_pretrained(MODEL_NAME)
21
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  model = model.float()
24
  model.eval()
@@ -35,8 +51,8 @@ def load_model():
35
  for plane, prompts in tqdm(text_prompts.items()):
36
  list_plane.append(plane)
37
 
38
- prompts = tokenizer(prompts, return_tensors="pt", padding=True).to(device)
39
- text_features = model(**prompts).last_hidden_state.mean(dim=1)
40
  text_features /= text_features.norm(dim=-1, keepdim=True)
41
 
42
  text_features = text_features.mean(dim=0).unsqueeze(0)
@@ -45,10 +61,10 @@ def load_model():
45
  list_text_features.append(text_features)
46
  text_features = torch.stack(list_text_features)[:,0]
47
 
48
- return model, tokenizer, text_features, list_plane, device
49
 
50
  # Load model and text features at startup
51
- model, tokenizer, text_features, list_plane, device = load_model()
52
 
53
  def process_image(image, model_type, top_k):
54
  if image is None:
@@ -57,11 +73,11 @@ def process_image(image, model_type, top_k):
57
  try:
58
  # Preprocess image
59
  img = make_image_square_with_zero_padding(Image.fromarray(image))
60
- img = tokenizer(images=img, return_tensors="pt").to(device)
61
 
62
  # Get image features
63
  with torch.no_grad():
64
- image_features = model(**img).last_hidden_state.mean(dim=1)
65
  image_features /= image_features.norm(dim=-1, keepdim=True)
66
 
67
  # Calculate similarity scores
 
1
  import gradio as gr
2
  import torch
3
+ from huggingface_hub import hf_hub_download
4
+ import open_clip
5
  from PIL import Image
6
  import json
7
  import os
 
12
  ASSETS_DIR = "assets"
13
  EXAMPLES_DIR = "examples"
14
  PATH_TEXT_PROMPTS = os.path.join(ASSETS_DIR, "prompt_fetal_view.json")
15
+ PATH_FETALCLIP_CONFIG = os.path.join(ASSETS_DIR, "FetalCLIP_config.json")
16
+ MODEL_NAME = "numansaeed/fetalclip-model"
17
 
18
  def load_model():
19
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
20
 
21
+ # Load and register model configuration
22
+ with open(PATH_FETALCLIP_CONFIG, "r") as file:
23
+ config_fetalclip = json.load(file)
24
+ open_clip.factory._MODEL_CONFIGS["FetalCLIP"] = config_fetalclip
25
+
26
+ # Download model weights from Hugging Face Hub
27
+ weights_path = hf_hub_download(
28
+ repo_id=MODEL_NAME,
29
+ filename="FetalCLIP_weights.pt"
30
+ )
31
+
32
+ # Load the FetalCLIP model and preprocessing transforms
33
+ model, _, preprocess_test = open_clip.create_model_and_transforms(
34
+ "FetalCLIP",
35
+ pretrained=weights_path
36
+ )
37
+ tokenizer = open_clip.get_tokenizer("FetalCLIP")
38
 
39
  model = model.float()
40
  model.eval()
 
51
  for plane, prompts in tqdm(text_prompts.items()):
52
  list_plane.append(plane)
53
 
54
+ prompts = tokenizer(prompts).to(device)
55
+ text_features = model.encode_text(prompts)
56
  text_features /= text_features.norm(dim=-1, keepdim=True)
57
 
58
  text_features = text_features.mean(dim=0).unsqueeze(0)
 
61
  list_text_features.append(text_features)
62
  text_features = torch.stack(list_text_features)[:,0]
63
 
64
+ return model, preprocess_test, text_features, list_plane, device
65
 
66
  # Load model and text features at startup
67
+ model, preprocess_test, text_features, list_plane, device = load_model()
68
 
69
  def process_image(image, model_type, top_k):
70
  if image is None:
 
73
  try:
74
  # Preprocess image
75
  img = make_image_square_with_zero_padding(Image.fromarray(image))
76
+ img = preprocess_test(img).unsqueeze(0).to(device)
77
 
78
  # Get image features
79
  with torch.no_grad():
80
+ image_features = model.encode_image(img)
81
  image_features /= image_features.norm(dim=-1, keepdim=True)
82
 
83
  # Calculate similarity scores