Spaces:
Running
Running
File size: 5,747 Bytes
e97ac39 e81731d e97ac39 058024c e97ac39 058024c e97ac39 e81731d e97ac39 e81731d 058024c e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 058024c e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d e97ac39 e81731d 058024c e97ac39 058024c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
from pathlib import Path
from typing import Optional, Dict, List
import gradio as gr
import torch
import torch.nn.functional as F
from PIL import Image
import torchvision.transforms as T
from torchvision.models import resnet18
# -----------------------------
# Config
# -----------------------------
CIFAR10_CLASSES = [
"airplane", "automobile", "bird", "cat", "deer",
"dog", "frog", "horse", "ship", "truck"
]
CIFAR10_MEAN = (0.4914, 0.4822, 0.4465)
CIFAR10_STD = (0.2470, 0.2435, 0.2616)
EXAMPLES_DIR = Path("Examples")
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# If you know the exact checkpoint name, lock it here:
CKPT_PATH = Path("ast_cifar10_resnet18.pth")
# -----------------------------
# Model helpers
# -----------------------------
def build_model(num_classes: int = 10) -> torch.nn.Module:
m = resnet18(weights=None)
m.fc = torch.nn.Linear(m.fc.in_features, num_classes)
return m
def load_weights(model: torch.nn.Module, ckpt_path: Path) -> None:
ckpt = torch.load(ckpt_path, map_location="cpu")
if isinstance(ckpt, dict):
if "state_dict" in ckpt and isinstance(ckpt["state_dict"], dict):
state = ckpt["state_dict"]
elif "model" in ckpt and isinstance(ckpt["model"], dict):
state = ckpt["model"]
else:
state = ckpt
else:
raise ValueError(f"Unsupported checkpoint format: {type(ckpt)}")
# Remove "module." if saved from DDP
cleaned = {k.replace("module.", ""): v for k, v in state.items()}
missing, unexpected = model.load_state_dict(cleaned, strict=False)
if missing or unexpected:
print("[load_weights] Missing keys:", missing)
print("[load_weights] Unexpected keys:", unexpected)
# -----------------------------
# Preprocess
# -----------------------------
preprocess = T.Compose([
T.Resize((32, 32), interpolation=T.InterpolationMode.BILINEAR),
T.ToTensor(),
T.Normalize(mean=CIFAR10_MEAN, std=CIFAR10_STD),
])
STATE: Dict[str, Optional[torch.nn.Module]] = {"model": None}
def init():
if not CKPT_PATH.exists():
print(f"[init] Checkpoint not found: {CKPT_PATH}")
STATE["model"] = None
return
print(f"[init] Loading checkpoint: {CKPT_PATH}")
model = build_model(num_classes=len(CIFAR10_CLASSES))
load_weights(model, CKPT_PATH)
model.to(DEVICE).eval()
STATE["model"] = model
def get_examples() -> List[List[str]]:
if not EXAMPLES_DIR.exists():
return []
imgs = sorted([p for p in EXAMPLES_DIR.iterdir() if p.suffix.lower() in [".png", ".jpg", ".jpeg"]])
return [[str(p)] for p in imgs]
# -----------------------------
# Predict
# -----------------------------
def predict(img: Image.Image):
if img is None:
return None, {}, [["", ""], ["", ""], ["", ""]], ""
if STATE["model"] is None:
raise gr.Error("Model is not loaded. Ensure ast_cifar10_resnet18.pth exists in the repo root.")
# show the actual 32x32 that goes into model
img32 = img.convert("RGB").resize((32, 32), resample=Image.BILINEAR)
x = preprocess(img.convert("RGB")).unsqueeze(0).to(DEVICE) # [1,3,32,32]
with torch.inference_mode():
logits = STATE["model"](x)
probs = F.softmax(logits, dim=1).squeeze(0) # [10]
# label dict for gr.Label
label_dict = {cls: float(probs[i]) for i, cls in enumerate(CIFAR10_CLASSES)}
# top-3 table
topk = torch.topk(probs, k=3)
top3_rows = []
for j, idx in enumerate(topk.indices.tolist()):
top3_rows.append([CIFAR10_CLASSES[idx], f"{float(topk.values[j]) * 100:.2f}%"])
pred_name = CIFAR10_CLASSES[int(topk.indices[0])]
pred_conf = float(topk.values[0]) * 100.0
pred_text = f"**{pred_name}** ({pred_conf:.2f}%)"
return img32, label_dict, top3_rows, pred_text
def clear_all():
return None, None, {}, [["", ""], ["", ""], ["", ""]], ""
# -----------------------------
# App
# -----------------------------
init()
EXAMPLES = get_examples()
with gr.Blocks(title="AST CIFAR-10 Classifier") as demo:
gr.Markdown(
"# AST CIFAR-10 Classifier\n"
"ResNet18 fine-tuned with Adaptive Sparse Training (AST) on CIFAR-10.\n\n"
f"**Device:** `{DEVICE}`"
)
with gr.Row():
with gr.Column(scale=1):
img_in = gr.Image(type="pil", label="Upload CIFAR-like image")
img_32 = gr.Image(type="pil", label="Model input (32×32)")
with gr.Column(scale=1):
gr.Markdown("### Top-3 Predictions")
pred_label = gr.Label(num_top_classes=3, label="Probabilities")
top3_table = gr.Dataframe(
headers=["class", "confidence"],
datatype=["str", "str"],
row_count=3,
column_count=2, # <-- fixed (no deprecated col_count)
interactive=False,
label="Top-3"
)
pred_text = gr.Markdown()
with gr.Row():
submit = gr.Button("Submit", variant="primary")
clear = gr.Button("Clear")
# ✅ FIX: if cache_examples=True, you MUST provide fn and outputs
if EXAMPLES:
gr.Markdown("### Examples (from `Examples/` folder)")
gr.Examples(
examples=EXAMPLES,
inputs=[img_in],
outputs=[img_32, pred_label, top3_table, pred_text],
fn=predict,
cache_examples=True
)
submit.click(
fn=predict,
inputs=[img_in],
outputs=[img_32, pred_label, top3_table, pred_text]
)
clear.click(
fn=clear_all,
inputs=[],
outputs=[img_in, img_32, pred_label, top3_table, pred_text]
)
demo.queue()
if __name__ == "__main__":
demo.launch()
|