Peijie commited on
Commit
f80b905
1 Parent(s): 791f525

cannot load to main process, load model directly at inference.

Browse files
Files changed (2) hide show
  1. app.py +13 -8
  2. utils/predict.py +4 -0
app.py CHANGED
@@ -10,19 +10,25 @@ from pathlib import Path
10
  from PIL import Image
11
 
12
  from plots import get_pre_define_colors
13
- from utils.load_model import load_xclip
14
  from utils.predict import xclip_pred
15
 
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
- XCLIP, OWLVIT_PRECESSOR = None, None
18
- def initialize_model():
19
- global XCLIP, OWLVIT_PRECESSOR
20
- if XCLIP is None or OWLVIT_PRECESSOR is None:
21
- XCLIP, OWLVIT_PRECESSOR = load_xclip(DEVICE)
 
 
 
 
 
 
 
22
 
23
  XCLIP_DESC_PATH = "data/jsons/bs_cub_desc.json"
24
  XCLIP_DESC = json.load(open(XCLIP_DESC_PATH, "r"))
25
- PREPROCESS = lambda x: OWLVIT_PRECESSOR(images=x, return_tensors='pt')
26
  IMAGES_FOLDER = "data/images"
27
  # XCLIP_RESULTS = json.load(open("data/jsons/xclip_org.json", "r"))
28
  IMAGE2GT = json.load(open("data/jsons/image2gt.json", 'r'))
@@ -198,7 +204,6 @@ PART_IMAGES_DICT = {file_name: load_part_images(file_name) for file_name in IMAG
198
 
199
  # --- Gradio Functions ---
200
  def update_selected_image(event: gr.SelectData):
201
- initialize_model()
202
  image_height = 400
203
  index = event.index
204
 
 
10
  from PIL import Image
11
 
12
  from plots import get_pre_define_colors
13
+ # from utils.load_model import load_xclip
14
  from utils.predict import xclip_pred
15
 
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
+ # def initialize_model():
18
+ # global XCLIP, OWLVIT_PRECESSOR
19
+ # if XCLIP is None or OWLVIT_PRECESSOR is None:
20
+ # XCLIP, OWLVIT_PRECESSOR = load_xclip(DEVICE)
21
+
22
+ #! Huggingface does not allow load model to main process, so we need to load the model when needed, it may not help in improve the speed of the app.
23
+ try:
24
+ import spaces
25
+ XCLIP, OWLVIT_PRECESSOR = None, None
26
+ except:
27
+ print(f"Not at Huggingface demo, load model to main process.")
28
+ XCLIP, OWLVIT_PRECESSOR = load_xclip(DEVICE)
29
 
30
  XCLIP_DESC_PATH = "data/jsons/bs_cub_desc.json"
31
  XCLIP_DESC = json.load(open(XCLIP_DESC_PATH, "r"))
 
32
  IMAGES_FOLDER = "data/images"
33
  # XCLIP_RESULTS = json.load(open("data/jsons/xclip_org.json", "r"))
34
  IMAGE2GT = json.load(open("data/jsons/image2gt.json", 'r'))
 
204
 
205
  # --- Gradio Functions ---
206
  def update_selected_image(event: gr.SelectData):
 
207
  image_height = 400
208
  index = event.index
209
 
utils/predict.py CHANGED
@@ -1,6 +1,7 @@
1
  try:
2
  import spaces
3
  gpu_decorator = spaces.GPU
 
4
  except ImportError:
5
  # Define a no-operation decorator as fallback
6
  def gpu_decorator(func):
@@ -52,6 +53,9 @@ def xclip_pred(new_desc: dict,
52
  cub_embeds: torch.Tensor = None,
53
  cub_idx2name: dict = None,
54
  descriptors: dict = None):
 
 
 
55
  # reorder the new description and the mask
56
  if new_class is not None:
57
  new_desc_ = {k: new_desc[k] for k in ORG_PART_ORDER}
 
1
  try:
2
  import spaces
3
  gpu_decorator = spaces.GPU
4
+ from .load_model import load_xclip
5
  except ImportError:
6
  # Define a no-operation decorator as fallback
7
  def gpu_decorator(func):
 
53
  cub_embeds: torch.Tensor = None,
54
  cub_idx2name: dict = None,
55
  descriptors: dict = None):
56
+ if model is None or owlvit_processor is None:
57
+ model, owlvit_processor = load_xclip(device=device)
58
+
59
  # reorder the new description and the mask
60
  if new_class is not None:
61
  new_desc_ = {k: new_desc[k] for k in ORG_PART_ORDER}