Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -209,7 +209,6 @@ def batch_similarity(files: List[str], model_name: str, pooling: str):
|
|
| 209 |
|
| 210 |
# ---------------------------
|
| 211 |
# Image Classification using DINOv3 embeddings
|
| 212 |
-
# Few shot nearest centroid on GPU
|
| 213 |
# ---------------------------
|
| 214 |
# State format:
|
| 215 |
# state = {
|
|
@@ -304,12 +303,10 @@ def predict_class(image: Image.Image, model_name: str, pooling: str, state: Dict
|
|
| 304 |
|
| 305 |
order = np.argsort(-probs)[: max(1, min(top_k, len(names)))]
|
| 306 |
result_dict = {names[i]: float(probs[i]) for i in order}
|
| 307 |
-
# Full table for display
|
| 308 |
full_table = "<ol>" + "".join(
|
| 309 |
f"<li>{names[i]} score {float(probs[i]):.4f}</li>" for i in order
|
| 310 |
) + "</ol>"
|
| 311 |
|
| 312 |
-
# gr.Label expects a dict of class to score for visualization
|
| 313 |
return {"top_k": top_k, "prediction": names[order[0]]}, result_dict, full_table
|
| 314 |
|
| 315 |
def clear_classes(_state: Dict):
|
|
@@ -339,15 +336,15 @@ with gr.Blocks() as app:
|
|
| 339 |
}
|
| 340 |
``` """)
|
| 341 |
|
| 342 |
-
# -------------
|
| 343 |
with gr.Tab("Embeddings"):
|
| 344 |
with gr.Row():
|
| 345 |
with gr.Column():
|
| 346 |
img = gr.Image(type="pil", label="Image", height=360)
|
| 347 |
model_dd = gr.Dropdown(choices=list(MODELS.keys()), value=DEFAULT_MODEL, label="Backbone")
|
| 348 |
pooling = gr.Radio(["CLS", "Mean of patch tokens"], value="CLS", label="Pooling")
|
| 349 |
-
overlay = gr.Checkbox(value=
|
| 350 |
-
run_btn = gr.Button("Extract
|
| 351 |
with gr.Column():
|
| 352 |
out_img = gr.Image(type="pil", label="Overlay or input", height=360)
|
| 353 |
preview = gr.Textbox(label="Embedding head", max_lines=2)
|
|
@@ -356,7 +353,6 @@ with gr.Blocks() as app:
|
|
| 356 |
|
| 357 |
run_btn.click(extract_embedding, [img, model_dd, pooling, overlay], [out_img, preview, meta, download])
|
| 358 |
|
| 359 |
-
# Optional examples if you add files under ./examples
|
| 360 |
ex_single = []
|
| 361 |
for p in sorted(glob.glob("examples/*.*"))[:6]:
|
| 362 |
ex_single.append([p, DEFAULT_MODEL, "CLS", False])
|
|
@@ -369,12 +365,12 @@ with gr.Blocks() as app:
|
|
| 369 |
|
| 370 |
# ------------- Similarity -------------
|
| 371 |
with gr.Tab("Similarity"):
|
| 372 |
-
gr.Markdown("Upload multiple images
|
| 373 |
files_in = gr.Files(label="Upload images", file_types=["image"], file_count="multiple", type="filepath")
|
| 374 |
gallery_preview = gr.Gallery(label="Preview", columns=4, height=300)
|
| 375 |
model_dd2 = gr.Dropdown(choices=list(MODELS.keys()), value=DEFAULT_MODEL, label="Backbone")
|
| 376 |
pooling2 = gr.Radio(["CLS", "Mean of patch tokens"], value="CLS", label="Pooling")
|
| 377 |
-
go = gr.Button("Compute cosine
|
| 378 |
table = gr.HTML(label="Cosine similarity")
|
| 379 |
csv = gr.File(label="cosine_similarity_matrix.csv")
|
| 380 |
|
|
@@ -403,13 +399,13 @@ with gr.Blocks() as app:
|
|
| 403 |
gr.Markdown("Build your labeled set by adding a few images per class.")
|
| 404 |
class_name = gr.Textbox(label="Class name")
|
| 405 |
class_files = gr.Files(label="Upload images for this class", file_types=["image"], type="filepath", file_count="multiple")
|
| 406 |
-
add_btn = gr.Button("Add class
|
| 407 |
clear_btn = gr.Button("Clear classes")
|
| 408 |
state_view = gr.JSON(label="Classifier state")
|
| 409 |
with gr.Column():
|
| 410 |
query_img = gr.Image(type="pil", label="Query image", height=360)
|
| 411 |
topk = gr.Slider(1, 10, value=3, step=1, label="Top K")
|
| 412 |
-
predict_btn = gr.Button("Predict
|
| 413 |
predicted = gr.Label(num_top_classes=3, label="Prediction")
|
| 414 |
scores_html = gr.HTML(label="Scores")
|
| 415 |
|
|
|
|
| 209 |
|
| 210 |
# ---------------------------
|
| 211 |
# Image Classification using DINOv3 embeddings
|
|
|
|
| 212 |
# ---------------------------
|
| 213 |
# State format:
|
| 214 |
# state = {
|
|
|
|
| 303 |
|
| 304 |
order = np.argsort(-probs)[: max(1, min(top_k, len(names)))]
|
| 305 |
result_dict = {names[i]: float(probs[i]) for i in order}
|
|
|
|
| 306 |
full_table = "<ol>" + "".join(
|
| 307 |
f"<li>{names[i]} score {float(probs[i]):.4f}</li>" for i in order
|
| 308 |
) + "</ol>"
|
| 309 |
|
|
|
|
| 310 |
return {"top_k": top_k, "prediction": names[order[0]]}, result_dict, full_table
|
| 311 |
|
| 312 |
def clear_classes(_state: Dict):
|
|
|
|
| 336 |
}
|
| 337 |
``` """)
|
| 338 |
|
| 339 |
+
# ------------- Embeddings -------------
|
| 340 |
with gr.Tab("Embeddings"):
|
| 341 |
with gr.Row():
|
| 342 |
with gr.Column():
|
| 343 |
img = gr.Image(type="pil", label="Image", height=360)
|
| 344 |
model_dd = gr.Dropdown(choices=list(MODELS.keys()), value=DEFAULT_MODEL, label="Backbone")
|
| 345 |
pooling = gr.Radio(["CLS", "Mean of patch tokens"], value="CLS", label="Pooling")
|
| 346 |
+
overlay = gr.Checkbox(value=True, label="Show overlay")
|
| 347 |
+
run_btn = gr.Button("Extract")
|
| 348 |
with gr.Column():
|
| 349 |
out_img = gr.Image(type="pil", label="Overlay or input", height=360)
|
| 350 |
preview = gr.Textbox(label="Embedding head", max_lines=2)
|
|
|
|
| 353 |
|
| 354 |
run_btn.click(extract_embedding, [img, model_dd, pooling, overlay], [out_img, preview, meta, download])
|
| 355 |
|
|
|
|
| 356 |
ex_single = []
|
| 357 |
for p in sorted(glob.glob("examples/*.*"))[:6]:
|
| 358 |
ex_single.append([p, DEFAULT_MODEL, "CLS", False])
|
|
|
|
| 365 |
|
| 366 |
# ------------- Similarity -------------
|
| 367 |
with gr.Tab("Similarity"):
|
| 368 |
+
gr.Markdown("Upload multiple images to compute a cosine similarity matrix and return a CSV.")
|
| 369 |
files_in = gr.Files(label="Upload images", file_types=["image"], file_count="multiple", type="filepath")
|
| 370 |
gallery_preview = gr.Gallery(label="Preview", columns=4, height=300)
|
| 371 |
model_dd2 = gr.Dropdown(choices=list(MODELS.keys()), value=DEFAULT_MODEL, label="Backbone")
|
| 372 |
pooling2 = gr.Radio(["CLS", "Mean of patch tokens"], value="CLS", label="Pooling")
|
| 373 |
+
go = gr.Button("Compute cosine")
|
| 374 |
table = gr.HTML(label="Cosine similarity")
|
| 375 |
csv = gr.File(label="cosine_similarity_matrix.csv")
|
| 376 |
|
|
|
|
| 399 |
gr.Markdown("Build your labeled set by adding a few images per class.")
|
| 400 |
class_name = gr.Textbox(label="Class name")
|
| 401 |
class_files = gr.Files(label="Upload images for this class", file_types=["image"], type="filepath", file_count="multiple")
|
| 402 |
+
add_btn = gr.Button("Add class")
|
| 403 |
clear_btn = gr.Button("Clear classes")
|
| 404 |
state_view = gr.JSON(label="Classifier state")
|
| 405 |
with gr.Column():
|
| 406 |
query_img = gr.Image(type="pil", label="Query image", height=360)
|
| 407 |
topk = gr.Slider(1, 10, value=3, step=1, label="Top K")
|
| 408 |
+
predict_btn = gr.Button("Predict")
|
| 409 |
predicted = gr.Label(num_top_classes=3, label="Prediction")
|
| 410 |
scores_html = gr.HTML(label="Scores")
|
| 411 |
|