Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,65 @@
|
|
| 1 |
-
import
|
| 2 |
-
import torch
|
| 3 |
-
import
|
| 4 |
-
from
|
| 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 |
-
demo = gr.Interface(
|
| 68 |
-
fn=predict,
|
| 69 |
-
inputs=gr.Image(type="pil"),
|
| 70 |
-
outputs=gr.Label(num_top_classes=5),
|
| 71 |
-
title="π Car Brand Classifier",
|
| 72 |
-
description="Upload a car image and the model predicts the brand (Top-5)."
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
if __name__ == "__main__":
|
| 76 |
-
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# 1. Load your model
|
| 9 |
+
# -----------------------------
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
model = torch.load("best_model.pth", map_location=device) # update filename if needed
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# -----------------------------
|
| 15 |
+
# 2. Class mapping (from training)
|
| 16 |
+
# -----------------------------
|
| 17 |
+
class_to_idx = {
|
| 18 |
+
'Acura': 0, 'Alfa Romeo': 1, 'Aston Martin': 2, 'Audi': 3, 'BMW': 4, 'Bentley': 5, 'Bugatti': 6,
|
| 19 |
+
'Buick': 7, 'Cadillac': 8, 'Chevrolet': 9, 'Chrysler': 10, 'Citroen': 11, 'Daewoo': 12,
|
| 20 |
+
'Dodge': 13, 'Ferrari': 14, 'Fiat': 15, 'Ford': 16, 'GMC': 17, 'Genesis': 18, 'Honda': 19,
|
| 21 |
+
'Hudson': 20, 'Hyundai': 21, 'Infiniti': 22, 'Jaguar': 23, 'Jeep': 24, 'Kia': 25, 'Land Rover': 26,
|
| 22 |
+
'Lexus': 27, 'Lincoln': 28, 'MG': 29, 'Maserati': 30, 'Mazda': 31, 'Mercedes-Benz': 32, 'Mini': 33,
|
| 23 |
+
'Mitsubishi': 34, 'Nissan': 35, 'Oldsmobile': 36, 'Peugeot': 37, 'Pontiac': 38, 'Porsche': 39,
|
| 24 |
+
'Ram Trucks': 40, 'Renault': 41, 'Saab': 42, 'Studebaker': 43, 'Subaru': 44, 'Suzuki': 45,
|
| 25 |
+
'Tesla': 46, 'Toyota': 47, 'Volkswagen': 48, 'Volvo': 49
|
| 26 |
+
}
|
| 27 |
+
idx_to_class = {v: k for k, v in class_to_idx.items()}
|
| 28 |
+
|
| 29 |
+
# -----------------------------
|
| 30 |
+
# 3. Transform (inference version: no randomness)
|
| 31 |
+
# -----------------------------
|
| 32 |
+
transform = transforms.Compose([
|
| 33 |
+
transforms.Lambda(lambda x: x.convert("RGB")),
|
| 34 |
+
transforms.Resize((224,224)),
|
| 35 |
+
transforms.ToTensor(),
|
| 36 |
+
transforms.Normalize([0.5]*3, [0.5]*3)
|
| 37 |
+
])
|
| 38 |
+
|
| 39 |
+
# -----------------------------
|
| 40 |
+
# 4. Prediction function
|
| 41 |
+
# -----------------------------
|
| 42 |
+
def predict(img):
|
| 43 |
+
img = transform(img).unsqueeze(0).to(device)
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
outputs = model(img)
|
| 47 |
+
probs = torch.softmax(outputs, dim=1)[0]
|
| 48 |
+
|
| 49 |
+
top5_probs, top5_idx = torch.topk(probs, 5)
|
| 50 |
+
results = {idx_to_class[idx.item()]: float(top5_probs[i]) for i, idx in enumerate(top5_idx)}
|
| 51 |
+
return results
|
| 52 |
+
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# 5. Gradio UI
|
| 55 |
+
# -----------------------------
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn=predict,
|
| 58 |
+
inputs=gr.Image(type="pil"),
|
| 59 |
+
outputs=gr.Label(num_top_classes=5),
|
| 60 |
+
title="Car Brand Classifier",
|
| 61 |
+
description="Upload a car image to classify its brand"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|