Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,71 +1,52 @@
|
|
| 1 |
import torch
|
| 2 |
-
import torchvision.
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
from PIL import Image
|
| 6 |
-
from torchvision import transforms
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
# β
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
-
print(f"β
Using device: {device}")
|
| 17 |
-
|
| 18 |
-
model = models.resnet50(pretrained=False)
|
| 19 |
-
checkpoint = torch.load(model_path, map_location=device)
|
| 20 |
-
|
| 21 |
-
if "model" in checkpoint:
|
| 22 |
-
model.load_state_dict(checkpoint["model"], strict=False)
|
| 23 |
-
elif "state_dict" in checkpoint:
|
| 24 |
-
model.load_state_dict(checkpoint["state_dict"], strict=False)
|
| 25 |
-
else:
|
| 26 |
-
model.load_state_dict(checkpoint, strict=False)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
model
|
|
|
|
|
|
|
|
|
|
| 30 |
model.to(device)
|
| 31 |
model.eval()
|
| 32 |
|
| 33 |
-
# β
Define
|
| 34 |
-
class_labels = [
|
| 35 |
-
"T-shirt", "Shirt", "Knitwear", "Chiffon", "Sweater", "Hoodie", "Windbreaker",
|
| 36 |
-
"Jacket", "Downcoat", "Suits", "Shawl", "Dress", "Vest", "Underwear",
|
| 37 |
-
"Hat", "Sock", "Jeans", "Sweatpants", "Trousers", "Shorts", "Skirt"
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
# β
Image Preprocessing
|
| 41 |
def preprocess_image(image):
|
| 42 |
transform = transforms.Compose([
|
| 43 |
transforms.Resize((224, 224)),
|
| 44 |
transforms.ToTensor(),
|
| 45 |
-
transforms.Normalize(mean=[0.
|
| 46 |
])
|
| 47 |
-
|
| 48 |
-
return image
|
| 49 |
|
| 50 |
-
# β
|
| 51 |
-
def
|
| 52 |
image_tensor = preprocess_image(image)
|
| 53 |
with torch.no_grad():
|
| 54 |
output = model(image_tensor)
|
| 55 |
predicted_class_idx = output.argmax(dim=1).item()
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
return f"Predicted Class: Unknown (Index {predicted_class_idx} out of range)"
|
| 59 |
-
|
| 60 |
-
return f"Predicted Class: {class_labels[predicted_class_idx]}"
|
| 61 |
-
|
| 62 |
-
# β
Gradio Interface
|
| 63 |
interface = gr.Interface(
|
| 64 |
-
fn=
|
| 65 |
inputs=gr.Image(type="pil"),
|
| 66 |
outputs="text",
|
| 67 |
-
title="Clothing1M
|
| 68 |
-
description="Upload an image
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import torchvision.transforms as transforms
|
| 3 |
import gradio as gr
|
| 4 |
+
from torchvision import models
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
+
# Define Clothing1M class labels
|
| 8 |
+
clothing1m_classes = [
|
| 9 |
+
"T-shirt", "Shirt", "Knitwear", "Chiffon", "Sweater", "Hoodie", "Windbreaker",
|
| 10 |
+
"Jacket", "Down Coat", "Suits", "Shawl", "Dress", "Vest", "Underwear", "Shorts",
|
| 11 |
+
"Trousers", "Jeans", "Leather Shoes", "Casual Shoes", "Sport Shoes", "Sandals"
|
| 12 |
+
]
|
| 13 |
|
| 14 |
+
# β
Set device
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# β
Load model
|
| 18 |
+
model = models.resnet50(weights=None) # Ensure correct architecture
|
| 19 |
+
num_ftrs = model.fc.in_features
|
| 20 |
+
model.fc = torch.nn.Linear(num_ftrs, 21) # Match Clothing1M class count
|
| 21 |
+
model.load_state_dict(torch.load("model.pth", map_location=device)) # Load weights
|
| 22 |
model.to(device)
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
+
# β
Define image preprocessing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def preprocess_image(image):
|
| 27 |
transform = transforms.Compose([
|
| 28 |
transforms.Resize((224, 224)),
|
| 29 |
transforms.ToTensor(),
|
| 30 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 31 |
])
|
| 32 |
+
return transform(image).unsqueeze(0).to(device)
|
|
|
|
| 33 |
|
| 34 |
+
# β
Define inference function
|
| 35 |
+
def classify_image(image):
|
| 36 |
image_tensor = preprocess_image(image)
|
| 37 |
with torch.no_grad():
|
| 38 |
output = model(image_tensor)
|
| 39 |
predicted_class_idx = output.argmax(dim=1).item()
|
| 40 |
+
predicted_class_name = clothing1m_classes[predicted_class_idx] if predicted_class_idx < len(clothing1m_classes) else "Unknown"
|
| 41 |
+
return f"Predicted Class: {predicted_class_name}"
|
| 42 |
|
| 43 |
+
# β
Create Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
interface = gr.Interface(
|
| 45 |
+
fn=classify_image,
|
| 46 |
inputs=gr.Image(type="pil"),
|
| 47 |
outputs="text",
|
| 48 |
+
title="Clothing1M Classifier",
|
| 49 |
+
description="Upload an image of clothing and get the predicted category."
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|