Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,6 +56,7 @@ def resize(img, target_res=224, resize=True, to_pil=True, edge=False, sampling_f
|
|
| 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') \
|
| 61 |
.permute(0,1,3,2) \
|
|
@@ -68,7 +69,7 @@ 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 |
def get_sim(
|
|
@@ -106,7 +107,6 @@ def draw_point(img_arr: np.ndarray, x: int, y: int, size: int, color=(255,0,0))
|
|
| 106 |
return np.array(pil)
|
| 107 |
|
| 108 |
# ─── Feature‐updating callback ───────────────────────────────────
|
| 109 |
-
@spaces.GPU
|
| 110 |
def update_features(
|
| 111 |
img: Image,
|
| 112 |
num_patches,
|
|
@@ -117,7 +117,6 @@ def update_features(
|
|
| 117 |
1) the same PIL image (so it can be displayed)
|
| 118 |
2) its DINO descriptor tensor, stored in a gr.State
|
| 119 |
"""
|
| 120 |
-
torch.cuda.empty_cache()
|
| 121 |
if img is None:
|
| 122 |
return None, None, None
|
| 123 |
img = resize(img, target_res=target_res, resize=True, to_pil=True)
|
|
@@ -245,7 +244,5 @@ with gr.Blocks() as demo:
|
|
| 245 |
outputs=[src,tgt]
|
| 246 |
)
|
| 247 |
|
| 248 |
-
# Add examples=[["What are you doing?"], ["Where should we time travel to?"]], (https://huggingface.co/learn/llm-course/chapter9/4?fw=pt)
|
| 249 |
-
|
| 250 |
if __name__ == "__main__":
|
| 251 |
demo.launch()
|
|
|
|
| 56 |
# ─── Feature extraction ──────────────────────────────────────────
|
| 57 |
@spaces.GPU
|
| 58 |
def get_processed_features_dino(num_patches, img,use_dummy):
|
| 59 |
+
torch.cuda.empty_cache()
|
| 60 |
batch = extractor_vit.preprocess_pil(img)
|
| 61 |
features_dino = extractor_vit.extract_descriptors(batch.to(extractor_vit.device), layer=11, facet='token') \
|
| 62 |
.permute(0,1,3,2) \
|
|
|
|
| 69 |
desc = aggre_net(features_dino)
|
| 70 |
norms = torch.linalg.norm(desc, dim=1, keepdim=True)
|
| 71 |
desc = desc / (norms + 1e-8)
|
| 72 |
+
return desc.cpu() # shape [1, C, num_patches, num_patches]
|
| 73 |
|
| 74 |
# ─── Similarity computation ───────────────────────────────────────
|
| 75 |
def get_sim(
|
|
|
|
| 107 |
return np.array(pil)
|
| 108 |
|
| 109 |
# ─── Feature‐updating callback ───────────────────────────────────
|
|
|
|
| 110 |
def update_features(
|
| 111 |
img: Image,
|
| 112 |
num_patches,
|
|
|
|
| 117 |
1) the same PIL image (so it can be displayed)
|
| 118 |
2) its DINO descriptor tensor, stored in a gr.State
|
| 119 |
"""
|
|
|
|
| 120 |
if img is None:
|
| 121 |
return None, None, None
|
| 122 |
img = resize(img, target_res=target_res, resize=True, to_pil=True)
|
|
|
|
| 244 |
outputs=[src,tgt]
|
| 245 |
)
|
| 246 |
|
|
|
|
|
|
|
| 247 |
if __name__ == "__main__":
|
| 248 |
demo.launch()
|