Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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 |
-
|
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 =
|
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 |
|