Delete app.py
Browse files
app.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch.nn.functional as F
|
| 3 |
-
import torchvision.transforms as transforms
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import torch
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
-
from models.resnet import SupCEResNet
|
| 10 |
-
|
| 11 |
-
CLASS_NAMES = [
|
| 12 |
-
"T-shirt", "Shirt", "Knitwear", "Chiffon", "Sweater", "Hoodie",
|
| 13 |
-
"Windbreaker", "Jacket", "Down Coat", "Suit", "Shawl", "Dress",
|
| 14 |
-
"Vest", "Underwear"
|
| 15 |
-
]
|
| 16 |
-
|
| 17 |
-
# Image normalization (matching Clothing1M training)
|
| 18 |
-
mean = (0.485, 0.456, 0.406)
|
| 19 |
-
std = (0.229, 0.224, 0.225)
|
| 20 |
-
|
| 21 |
-
# Define preprocessing for user-uploaded images
|
| 22 |
-
transform_input_image = transforms.Compose([
|
| 23 |
-
transforms.Resize(256),
|
| 24 |
-
transforms.CenterCrop(224),
|
| 25 |
-
transforms.ToTensor(),
|
| 26 |
-
transforms.Normalize(mean, std),
|
| 27 |
-
])
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def load_model_from_huggingface():
|
| 31 |
-
model_filename = "ckpt_clothing_resnet50.pth"
|
| 32 |
-
model_path = hf_hub_download(repo_id="austin-carnahan/c2d-clothing1M", filename=model_filename)
|
| 33 |
-
|
| 34 |
-
# Load checkpoint and check keys
|
| 35 |
-
checkpoint = torch.load(model_path, map_location=torch.device("cpu"), weights_only=False)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# Ensure the model state_dict is loaded properly
|
| 39 |
-
if "model" in checkpoint:
|
| 40 |
-
sd = {k.replace("module.", ""): v for k, v in checkpoint["model"].items()}
|
| 41 |
-
else:
|
| 42 |
-
sd = checkpoint # Try loading directly
|
| 43 |
-
|
| 44 |
-
model = SupCEResNet("resnet50", num_classes=14, pool=True)
|
| 45 |
-
model.load_state_dict(sd, strict=False)
|
| 46 |
-
model.eval()
|
| 47 |
-
|
| 48 |
-
print("✅ Model loaded successfully!")
|
| 49 |
-
return model
|
| 50 |
-
|
| 51 |
-
model = load_model_from_huggingface()
|
| 52 |
-
|
| 53 |
-
model = load_model_from_huggingface()
|
| 54 |
-
print("✅ Model loaded successfully from Hugging Face Hub!")
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def gradio_inference(image):
|
| 58 |
-
"""Process uploaded image, run inference, and return predicted class + confidence."""
|
| 59 |
-
|
| 60 |
-
image = image.convert("RGB")
|
| 61 |
-
input_tensor = transform_input_image(image).unsqueeze(0) # Add batch dimension
|
| 62 |
-
|
| 63 |
-
# Run inference
|
| 64 |
-
with torch.no_grad():
|
| 65 |
-
output = model(input_tensor)
|
| 66 |
-
probabilities = F.softmax(output, dim=1) # Convert logits to probabilities
|
| 67 |
-
|
| 68 |
-
predicted_class = torch.argmax(probabilities, dim=1).item()
|
| 69 |
-
confidence = probabilities[0, predicted_class].item()
|
| 70 |
-
|
| 71 |
-
# Get human-readable class name
|
| 72 |
-
class_name = CLASS_NAMES[predicted_class]
|
| 73 |
-
|
| 74 |
-
return f"Predicted: {class_name} ({confidence:.2%} confidence)"
|
| 75 |
-
|
| 76 |
-
# Define Gradio UI
|
| 77 |
-
interface = gr.Interface(
|
| 78 |
-
fn=gradio_inference,
|
| 79 |
-
inputs=gr.Image(type="pil"),
|
| 80 |
-
outputs="text",
|
| 81 |
-
title="Clothing Image Classifier",
|
| 82 |
-
description="Upload an image of clothing and get a predicted class with confidence score.",
|
| 83 |
-
examples=["images/tshirt.jpg", "images/pants.jpg"]
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
# Launch the interface
|
| 87 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|