odunkel commited on
Commit
ea88e6c
Β·
verified Β·
1 Parent(s): a106d62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -7
app.py CHANGED
@@ -68,10 +68,11 @@ def get_processed_features_dino(num_patches, img,use_dummy):
68
  desc = aggre_net(features_dino)
69
  norms = torch.linalg.norm(desc, dim=1, keepdim=True)
70
  desc = desc / (norms + 1e-8)
 
 
71
  return desc # shape [1, C, num_patches, num_patches]
72
 
73
  # ─── Similarity computation ───────────────────────────────────────
74
- spaces.GPU(duration=20)
75
  def get_sim(
76
  coord: tuple[int,int],
77
  feat1: torch.Tensor,
@@ -96,9 +97,7 @@ def get_sim(
96
  # Cosine similarity along channel‐dim
97
  cos = nn.CosineSimilarity(dim=1)
98
  cos_map = cos(src_vec, trg_ft)[0] # [img_size, img_size]
99
- cos_map = cos_map.cpu()
100
- torch.cuda.empty_cache()
101
- return cos_map.numpy()
102
 
103
  # ─── Drawing helper ───────────────────────────────────────────────
104
  def draw_point(img_arr: np.ndarray, x: int, y: int, size: int, color=(255,0,0)) -> np.ndarray:
@@ -109,7 +108,6 @@ def draw_point(img_arr: np.ndarray, x: int, y: int, size: int, color=(255,0,0))
109
  return np.array(pil)
110
 
111
  # ─── Feature‐updating callback ───────────────────────────────────
112
- spaces.GPU(duration=20)
113
  def update_features(
114
  img: Image,
115
  num_patches,
@@ -124,7 +122,7 @@ def update_features(
124
  return None, None, None
125
  img = resize(img, target_res=target_res, resize=True, to_pil=True)
126
  feat = get_processed_features_dino(num_patches, img=img,use_dummy=use_dummy)
127
- return img, feat, Image.fromarray(np.array(img))
128
 
129
  # ─── Click handler ───────────────────────────────────────────────
130
  def on_select(
@@ -177,7 +175,7 @@ def reload_img(
177
 
178
 
179
  # ─── Configuration ───────────────────────────────────────────────
180
- num_patches = 45
181
  target_res = num_patches * 14
182
  ckpt_file = "./ckpts/dino_spair_0300.pth"
183
 
 
68
  desc = aggre_net(features_dino)
69
  norms = torch.linalg.norm(desc, dim=1, keepdim=True)
70
  desc = desc / (norms + 1e-8)
71
+ desc = desc.cpu()
72
+ torch.cuda.empty_cache()
73
  return desc # shape [1, C, num_patches, num_patches]
74
 
75
  # ─── Similarity computation ───────────────────────────────────────
 
76
  def get_sim(
77
  coord: tuple[int,int],
78
  feat1: torch.Tensor,
 
97
  # Cosine similarity along channel‐dim
98
  cos = nn.CosineSimilarity(dim=1)
99
  cos_map = cos(src_vec, trg_ft)[0] # [img_size, img_size]
100
+ return cos_map.cpu().numpy()
 
 
101
 
102
  # ─── Drawing helper ───────────────────────────────────────────────
103
  def draw_point(img_arr: np.ndarray, x: int, y: int, size: int, color=(255,0,0)) -> np.ndarray:
 
108
  return np.array(pil)
109
 
110
  # ─── Feature‐updating callback ───────────────────────────────────
 
111
  def update_features(
112
  img: Image,
113
  num_patches,
 
122
  return None, None, None
123
  img = resize(img, target_res=target_res, resize=True, to_pil=True)
124
  feat = get_processed_features_dino(num_patches, img=img,use_dummy=use_dummy)
125
+ return img, feat.cpu(), Image.fromarray(np.array(img))
126
 
127
  # ─── Click handler ───────────────────────────────────────────────
128
  def on_select(
 
175
 
176
 
177
  # ─── Configuration ───────────────────────────────────────────────
178
+ num_patches = 60
179
  target_res = num_patches * 14
180
  ckpt_file = "./ckpts/dino_spair_0300.pth"
181