Spaces:
Sleeping
Sleeping
Damaris Adora Cursor commited on
Commit ·
22045db
1
Parent(s): 2952455
Show overall test accuracy in the Gradio interface
Browse filesCo-authored-by: Cursor <cursoragent@cursor.com>
app.py
CHANGED
|
@@ -40,6 +40,11 @@ def load_deployed_model(weights_path: Path, device: str) -> torch.nn.Module:
|
|
| 40 |
|
| 41 |
DEPLOYED_MODEL = load_deployed_model(MODEL_PATH, DEVICE)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
EXAMPLES_DIRECTORY = Path(__file__).parent / "images"
|
| 44 |
EXAMPLE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
| 45 |
|
|
@@ -57,8 +62,12 @@ def collect_example_images(directory: Path) -> list[list[str]]:
|
|
| 57 |
EXAMPLE_INPUTS = collect_example_images(EXAMPLES_DIRECTORY)
|
| 58 |
|
| 59 |
|
| 60 |
-
def classify_pet(image: Image.Image | None) -> tuple[dict[str, float], str, float]:
|
| 61 |
-
"""Predict breed probabilities, species, and inference latency for one image.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if image is None:
|
| 63 |
raise gr.Error("Please upload a pet photo before running the classifier.")
|
| 64 |
|
|
@@ -74,7 +83,12 @@ def classify_pet(image: Image.Image | None) -> tuple[dict[str, float], str, floa
|
|
| 74 |
format_breed_name(breed): probability
|
| 75 |
for breed, probability in result["probabilities"].items()
|
| 76 |
}
|
| 77 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
demo = gr.Interface(
|
|
@@ -83,14 +97,16 @@ demo = gr.Interface(
|
|
| 83 |
outputs=[
|
| 84 |
gr.Label(num_top_classes=3, label="Top Predicted Breeds"),
|
| 85 |
gr.Textbox(label="Detected Species (Cat vs. Dog)"),
|
|
|
|
| 86 |
gr.Number(label="Inference Latency (seconds)"),
|
| 87 |
],
|
| 88 |
title="Pet Breed Classifier",
|
| 89 |
description=(
|
| 90 |
"Drag and drop a photograph of a cat or dog into the upload area, or select one of "
|
| 91 |
"the sample photos below. The model predicts the breed among the 37 Oxford-IIIT Pet "
|
| 92 |
-
"categories, infers the species from the predicted breed, and reports the "
|
| 93 |
-
"
|
|
|
|
| 94 |
),
|
| 95 |
examples=EXAMPLE_INPUTS or None,
|
| 96 |
cache_examples=False,
|
|
|
|
| 40 |
|
| 41 |
DEPLOYED_MODEL = load_deployed_model(MODEL_PATH, DEVICE)
|
| 42 |
|
| 43 |
+
# Breed-level accuracy of the deployed ResNet18 on the held-out Oxford-IIIT Pet test
|
| 44 |
+
# partition (3,669 images), as measured in notebooks/train.ipynb. Update this value
|
| 45 |
+
# if the model is retrained.
|
| 46 |
+
TEST_ACCURACY_PERCENT = 87.1
|
| 47 |
+
|
| 48 |
EXAMPLES_DIRECTORY = Path(__file__).parent / "images"
|
| 49 |
EXAMPLE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
| 50 |
|
|
|
|
| 62 |
EXAMPLE_INPUTS = collect_example_images(EXAMPLES_DIRECTORY)
|
| 63 |
|
| 64 |
|
| 65 |
+
def classify_pet(image: Image.Image | None) -> tuple[dict[str, float], str, float, float]:
|
| 66 |
+
"""Predict breed probabilities, species, test accuracy, and inference latency for one image.
|
| 67 |
+
|
| 68 |
+
The accuracy value is the model's overall estimated accuracy on the held-out test
|
| 69 |
+
set. It describes the model, not this particular prediction.
|
| 70 |
+
"""
|
| 71 |
if image is None:
|
| 72 |
raise gr.Error("Please upload a pet photo before running the classifier.")
|
| 73 |
|
|
|
|
| 83 |
format_breed_name(breed): probability
|
| 84 |
for breed, probability in result["probabilities"].items()
|
| 85 |
}
|
| 86 |
+
return (
|
| 87 |
+
breed_probabilities,
|
| 88 |
+
result["species"],
|
| 89 |
+
TEST_ACCURACY_PERCENT,
|
| 90 |
+
round(result["inference_seconds"], 6),
|
| 91 |
+
)
|
| 92 |
|
| 93 |
|
| 94 |
demo = gr.Interface(
|
|
|
|
| 97 |
outputs=[
|
| 98 |
gr.Label(num_top_classes=3, label="Top Predicted Breeds"),
|
| 99 |
gr.Textbox(label="Detected Species (Cat vs. Dog)"),
|
| 100 |
+
gr.Number(label="Estimated Model Accuracy on Test Set (%)"),
|
| 101 |
gr.Number(label="Inference Latency (seconds)"),
|
| 102 |
],
|
| 103 |
title="Pet Breed Classifier",
|
| 104 |
description=(
|
| 105 |
"Drag and drop a photograph of a cat or dog into the upload area, or select one of "
|
| 106 |
"the sample photos below. The model predicts the breed among the 37 Oxford-IIIT Pet "
|
| 107 |
+
"categories, infers the species from the predicted breed, and reports the model's "
|
| 108 |
+
"overall estimated accuracy on the held-out test set along with the forward-pass "
|
| 109 |
+
"latency for the single image."
|
| 110 |
),
|
| 111 |
examples=EXAMPLE_INPUTS or None,
|
| 112 |
cache_examples=False,
|