Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
import torchvision.transforms as T
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from matplotlib import colormaps
|
| 10 |
+
|
| 11 |
+
from model import SceneCNN, grad_cam
|
| 12 |
+
|
| 13 |
+
ROOT = Path(__file__).parent
|
| 14 |
+
|
| 15 |
+
# ---------- load model ----------
|
| 16 |
+
meta = json.loads((ROOT / "cnn_intel_meta.json").read_text())
|
| 17 |
+
CLASSES = meta["class_names"]
|
| 18 |
+
IMG_SIZE = meta["img_size"]
|
| 19 |
+
|
| 20 |
+
model = SceneCNN(base=meta["base"], num_classes=len(CLASSES))
|
| 21 |
+
model.load_state_dict(torch.load(ROOT / "cnn_intel.pth", map_location="cpu"))
|
| 22 |
+
model.eval()
|
| 23 |
+
|
| 24 |
+
preprocess = T.Compose([
|
| 25 |
+
T.Resize((IMG_SIZE, IMG_SIZE)),
|
| 26 |
+
T.ToTensor(),
|
| 27 |
+
T.Normalize(meta["mean"], meta["std"]),
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
# ---------- prediction ----------
|
| 31 |
+
def predict(img):
|
| 32 |
+
if img is None:
|
| 33 |
+
return None, None
|
| 34 |
+
img = img.convert("RGB")
|
| 35 |
+
cam, probs = grad_cam(model, preprocess(img))
|
| 36 |
+
|
| 37 |
+
show = 384
|
| 38 |
+
base = np.asarray(img.resize((show, show))) / 255.0
|
| 39 |
+
cam_big = np.asarray(Image.fromarray((cam * 255).astype(np.uint8)).resize((show, show), Image.BILINEAR)) / 255.0
|
| 40 |
+
heat = colormaps["jet"](cam_big)[..., :3]
|
| 41 |
+
overlay = (0.55 * base + 0.45 * heat).clip(0, 1)
|
| 42 |
+
|
| 43 |
+
label = {c: float(p) for c, p in zip(CLASSES, probs)}
|
| 44 |
+
return label, (overlay * 255).astype(np.uint8)
|
| 45 |
+
|
| 46 |
+
# ---------- metrics ----------
|
| 47 |
+
def metrics_markdown():
|
| 48 |
+
p = ROOT / "metrics.json"
|
| 49 |
+
if not p.exists():
|
| 50 |
+
return "_metrics.json not found._"
|
| 51 |
+
m = json.loads(p.read_text())
|
| 52 |
+
out = [
|
| 53 |
+
f"**Test accuracy:** {m['test_accuracy']:.2%} Β· "
|
| 54 |
+
f"**Macro F1:** {m['macro_f1']:.3f} Β· "
|
| 55 |
+
f"**Best val accuracy:** {m['best_val_accuracy']:.2%} Β· "
|
| 56 |
+
f"**Parameters:** {m['params']:,}",
|
| 57 |
+
"",
|
| 58 |
+
"| Class | Precision | Recall | F1 | Test images |",
|
| 59 |
+
"|---|---|---|---|---|",
|
| 60 |
+
]
|
| 61 |
+
for c, r in m["per_class"].items():
|
| 62 |
+
out.append(f"| {c} | {r['precision']:.3f} | {r['recall']:.3f} | {r['f1-score']:.3f} | {int(r['support'])} |")
|
| 63 |
+
return "\n".join(out)
|
| 64 |
+
|
| 65 |
+
PLOTS = [
|
| 66 |
+
("10_curves.png", "Training curves"),
|
| 67 |
+
("11_confusion_matrix.png", "Confusion matrix"),
|
| 68 |
+
("12_per_class_accuracy.png", "Per-class accuracy"),
|
| 69 |
+
("13_confidence.png", "Confidence: correct vs wrong"),
|
| 70 |
+
("14_mistakes.png", "Most confident mistakes"),
|
| 71 |
+
("15_gradcam.png", "Grad-CAM per class"),
|
| 72 |
+
("16_gradcam_mistakes.png", "Grad-CAM on mistakes"),
|
| 73 |
+
("08_feature_maps_after.png", "Feature maps after training"),
|
| 74 |
+
("09_filters_after.png", "First-layer filters after training"),
|
| 75 |
+
("04_augmentation.png", "Augmentation"),
|
| 76 |
+
("01_samples.png", "Dataset samples"),
|
| 77 |
+
("02_class_distribution.png", "Class distribution"),
|
| 78 |
+
]
|
| 79 |
+
gallery_items = [(str(ROOT / "images" / f), cap) for f, cap in PLOTS if (ROOT / "images" / f).exists()]
|
| 80 |
+
examples = [[str(p)] for p in sorted((ROOT / "examples").glob("*.jpg"))] if (ROOT / "examples").exists() else []
|
| 81 |
+
|
| 82 |
+
# ---------- UI ----------
|
| 83 |
+
with gr.Blocks(title="Intel Scene CNN", theme=gr.themes.Soft()) as demo:
|
| 84 |
+
gr.Markdown(
|
| 85 |
+
"# Intel Scene Classifier: a CNN built from scratch\n"
|
| 86 |
+
"Upload a photo of a **building, forest, glacier, mountain, sea or street**. "
|
| 87 |
+
"The CNN predicts the scene, and Grad-CAM shows which pixels drove the decision. "
|
| 88 |
+
"No pretrained backbone: 4 conv blocks trained from scratch on ~14k images."
|
| 89 |
+
)
|
| 90 |
+
with gr.Row():
|
| 91 |
+
inp = gr.Image(type="pil", label="Upload a scene", height=384)
|
| 92 |
+
with gr.Column():
|
| 93 |
+
out_label = gr.Label(num_top_classes=6, label="Prediction")
|
| 94 |
+
out_cam = gr.Image(label="Grad-CAM (red = most important)", height=384)
|
| 95 |
+
inp.change(predict, inputs=inp, outputs=[out_label, out_cam])
|
| 96 |
+
if examples:
|
| 97 |
+
gr.Examples(examples=examples, inputs=inp, cache_examples=False)
|
| 98 |
+
|
| 99 |
+
gr.Markdown("> Note: the model always picks one of the 6 classes, even for a photo of a cat.")
|
| 100 |
+
|
| 101 |
+
gr.Markdown("## Test-set metrics (seg_test, 3,000 held-out images)")
|
| 102 |
+
gr.Markdown(metrics_markdown())
|
| 103 |
+
|
| 104 |
+
if gallery_items:
|
| 105 |
+
gr.Markdown("## Training and evaluation plots")
|
| 106 |
+
gr.Gallery(value=gallery_items, columns=2, height="auto", object_fit="contain")
|
| 107 |
+
|
| 108 |
+
gr.Markdown(
|
| 109 |
+
"## Architecture\n"
|
| 110 |
+
"`[Conv3x3-BN-ReLU] x2 β MaxPool β Dropout2d` Γ 4 blocks "
|
| 111 |
+
f"(channels {meta['base']}β{meta['base']*2}β{meta['base']*4}β{meta['base']*8}) "
|
| 112 |
+
"β Global Avg Pool β Dropout β FC 128 β ReLU β FC 6"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
demo.launch()
|