odunkel commited on
Commit
162c36b
Β·
verified Β·
1 Parent(s): 1510936

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
 
2
  import os
 
3
  import gradio as gr
4
  import numpy as np
5
  import torch
@@ -53,6 +54,7 @@ def resize(img, target_res=224, resize=True, to_pil=True, edge=False, sampling_f
53
  return canvas
54
 
55
  # ─── Feature extraction ──────────────────────────────────────────
 
56
  def get_processed_features_dino(num_patches, img,use_dummy):
57
  batch = extractor_vit.preprocess_pil(img)
58
  features_dino = extractor_vit.extract_descriptors(batch.to(extractor_vit.device), layer=11, facet='token') \
@@ -104,6 +106,7 @@ def draw_point(img_arr: np.ndarray, x: int, y: int, size: int, color=(255,0,0))
104
  return np.array(pil)
105
 
106
  # ─── Feature‐updating callback ───────────────────────────────────
 
107
  def update_features(
108
  img: Image,
109
  num_patches,
@@ -171,12 +174,13 @@ def reload_img(
171
 
172
 
173
  # ─── Configuration ───────────────────────────────────────────────
174
- num_patches = 30
175
  target_res = num_patches * 14
176
- ckpt_file = "ckpts/dino_spair_0300.pth"
177
 
178
  # ─── Model setup ─────────────────────────────────────────────────
179
- device = 'cpu' #'cuda' if torch.cuda.is_available() else 'cpu'
 
180
  aggre_net = AggregationNetwork(feature_dims=[768], projection_dim=768, device=device)
181
  aggre_net.load_pretrained_weights(torch.load(ckpt_file, map_location=device))
182
  aggre_net_dummy = DummyAggregationNetwork()
@@ -198,7 +202,6 @@ with gr.Blocks() as demo:
198
  Welcome to the DIY-SC demo!
199
  Upload two images and select a keypoint in the source image. This demo will compute and visualize the feature similarity map and a corresponding point in the target image.
200
  You can choose between the DIY-SC (DINOv2) or the DINOv2 feature extractor.
201
- The demo is currently only running on CPUs. For this reason the run time is longer and a lower resolution is used, which results in a performance drop. We will update this soon.
202
  """)
203
 
204
  # Image upload / display components
@@ -241,5 +244,6 @@ with gr.Blocks() as demo:
241
  outputs=[src,tgt]
242
  )
243
 
 
244
  if __name__ == "__main__":
245
- demo.launch(share=True)
 
1
 
2
  import os
3
+ import spaces
4
  import gradio as gr
5
  import numpy as np
6
  import torch
 
54
  return canvas
55
 
56
  # ─── Feature extraction ──────────────────────────────────────────
57
+ @spaces.GPU
58
  def get_processed_features_dino(num_patches, img,use_dummy):
59
  batch = extractor_vit.preprocess_pil(img)
60
  features_dino = extractor_vit.extract_descriptors(batch.to(extractor_vit.device), layer=11, facet='token') \
 
106
  return np.array(pil)
107
 
108
  # ─── Feature‐updating callback ───────────────────────────────────
109
+ @spaces.GPU
110
  def update_features(
111
  img: Image,
112
  num_patches,
 
174
 
175
 
176
  # ─── Configuration ───────────────────────────────────────────────
177
+ num_patches = 60
178
  target_res = num_patches * 14
179
+ ckpt_file = "./ckpts/dino_spair_0300.pth"
180
 
181
  # ─── Model setup ─────────────────────────────────────────────────
182
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
183
+ print(f"Using device: {device}")
184
  aggre_net = AggregationNetwork(feature_dims=[768], projection_dim=768, device=device)
185
  aggre_net.load_pretrained_weights(torch.load(ckpt_file, map_location=device))
186
  aggre_net_dummy = DummyAggregationNetwork()
 
202
  Welcome to the DIY-SC demo!
203
  Upload two images and select a keypoint in the source image. This demo will compute and visualize the feature similarity map and a corresponding point in the target image.
204
  You can choose between the DIY-SC (DINOv2) or the DINOv2 feature extractor.
 
205
  """)
206
 
207
  # Image upload / display components
 
244
  outputs=[src,tgt]
245
  )
246
 
247
+
248
  if __name__ == "__main__":
249
+ demo.launch()