ABHIMANYU PRASAD commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# 1. Define the EXACT same architecture from your notebook
|
| 8 |
+
class VehicleClassifier(nn.Module):
|
| 9 |
+
def __init__(self):
|
| 10 |
+
super(VehicleClassifier, self).__init__()
|
| 11 |
+
self.conv_layers = nn.Sequential(
|
| 12 |
+
nn.Conv2d(3, 16, kernel_size=3, padding=1),
|
| 13 |
+
nn.BatchNorm2d(16),
|
| 14 |
+
nn.ReLU(),
|
| 15 |
+
nn.MaxPool2d(2, 2),
|
| 16 |
+
nn.Conv2d(16, 32, kernel_size=3, padding=1),
|
| 17 |
+
nn.BatchNorm2d(32),
|
| 18 |
+
nn.ReLU(),
|
| 19 |
+
nn.MaxPool2d(2, 2),
|
| 20 |
+
nn.Conv2d(32, 64, kernel_size=3, padding=1),
|
| 21 |
+
nn.BatchNorm2d(64),
|
| 22 |
+
nn.ReLU(),
|
| 23 |
+
nn.MaxPool2d(2, 2)
|
| 24 |
+
)
|
| 25 |
+
self.fc_layers = nn.Sequential(
|
| 26 |
+
nn.Linear(64 * 28 * 28, 512),
|
| 27 |
+
nn.ReLU(),
|
| 28 |
+
nn.Dropout(0.5),
|
| 29 |
+
nn.Linear(512, 8)
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def forward(self, x):
|
| 33 |
+
x = self.conv_layers(x)
|
| 34 |
+
x = x.view(x.size(0), -1)
|
| 35 |
+
x = self.fc_layers(x)
|
| 36 |
+
return x
|
| 37 |
+
|
| 38 |
+
# 2. Setup Device and Load Model
|
| 39 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 40 |
+
model = VehicleClassifier().to(device)
|
| 41 |
+
|
| 42 |
+
# Replace with your actual repo and filename
|
| 43 |
+
path = "abhiprd20/vehicle-classification-utd"
|
| 44 |
+
model.load_state_dict(torch.hub.load_state_dict_from_url(
|
| 45 |
+
f"https://huggingface.co/{path}/resolve/main/model.pth",
|
| 46 |
+
map_location=device
|
| 47 |
+
))
|
| 48 |
+
model.eval()
|
| 49 |
+
|
| 50 |
+
# 3. Prediction Logic
|
| 51 |
+
classes = ['Bicycle', 'Bus', 'Car', 'Motorcycle', 'NonVehicles', 'Taxi', 'Truck', 'Van']
|
| 52 |
+
data_transforms = transforms.Compose([
|
| 53 |
+
transforms.Resize((224, 224)),
|
| 54 |
+
transforms.ToTensor(),
|
| 55 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 56 |
+
])
|
| 57 |
+
|
| 58 |
+
def predict(img):
|
| 59 |
+
img = data_transforms(img).unsqueeze(0).to(device)
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
outputs = model(img)
|
| 62 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=0)
|
| 63 |
+
confidences = {classes[i]: float(probs[i]) for i in range(8)}
|
| 64 |
+
return confidences
|
| 65 |
+
|
| 66 |
+
# 4. Interface
|
| 67 |
+
gr.Interface(
|
| 68 |
+
fn=predict,
|
| 69 |
+
inputs=gr.Image(type="pil"),
|
| 70 |
+
outputs=gr.Label(num_top_classes=3),
|
| 71 |
+
title="Autonomous Vehicle Perception Demo",
|
| 72 |
+
description="Custom CNN Model for UT Dallas AI Safety Lab. Target: 78.54% Accuracy."
|
| 73 |
+
).launch()
|