Spaces:
Configuration error
Configuration error
Upload 4 files
Browse files- .gitattributes +1 -0
- CC_net (1).pt +3 -0
- ResNet_for_CC.py +93 -0
- app (1).py +95 -0
- requirements.txt +7 -0
.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
CC_net[[:space:]](1).pt filter=lfs diff=lfs merge=lfs -text
|
CC_net (1).pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b61ad39bb8f2872cff371265b3ad4ecbf9c5a201d64225f92d6bcc937d9e112b
|
3 |
+
size 95648689
|
ResNet_for_CC.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torchvision.models as models
|
4 |
+
|
5 |
+
class ResClassifier(nn.Module):
|
6 |
+
"""
|
7 |
+
A classifier with two fully connected layers followed by a final linear layer.
|
8 |
+
Uses BatchNorm, ReLU activations, and Dropout for better generalization.
|
9 |
+
"""
|
10 |
+
def __init__(self, num_classes=14):
|
11 |
+
super(ResClassifier, self).__init__()
|
12 |
+
|
13 |
+
# First fully connected layer: reduces 128D features to 64D
|
14 |
+
self.fc1 = nn.Sequential(
|
15 |
+
nn.Linear(128, 64),
|
16 |
+
nn.BatchNorm1d(64, affine=True),
|
17 |
+
nn.ReLU(inplace=True),
|
18 |
+
nn.Dropout()
|
19 |
+
)
|
20 |
+
|
21 |
+
# Second fully connected layer: retains 64D features
|
22 |
+
self.fc2 = nn.Sequential(
|
23 |
+
nn.Linear(64, 64),
|
24 |
+
nn.BatchNorm1d(64, affine=True),
|
25 |
+
nn.ReLU(inplace=True),
|
26 |
+
nn.Dropout()
|
27 |
+
)
|
28 |
+
|
29 |
+
# Final classification layer mapping 64D features to class logits
|
30 |
+
self.fc3 = nn.Linear(64, num_classes)
|
31 |
+
|
32 |
+
def forward(self, x):
|
33 |
+
"""
|
34 |
+
Forward pass through the classifier.
|
35 |
+
Returns class logits after two hidden layers.
|
36 |
+
"""
|
37 |
+
x = self.fc1(x) # First FC layer
|
38 |
+
x = self.fc2(x) # Second FC layer
|
39 |
+
output = self.fc3(x) # Final classification layer
|
40 |
+
return output
|
41 |
+
|
42 |
+
|
43 |
+
class CC_model(nn.Module):
|
44 |
+
"""
|
45 |
+
Clothing Classification Model based on ResNet50.
|
46 |
+
Extracts deep features and uses two independent classifiers for predictions.
|
47 |
+
"""
|
48 |
+
def __init__(self, num_classes1=14, num_classes2=None):
|
49 |
+
super(CC_model, self).__init__()
|
50 |
+
|
51 |
+
# If num_classes2 is not specified, default to num_classes1
|
52 |
+
num_classes2 = num_classes2 if num_classes2 else num_classes1
|
53 |
+
assert num_classes1 == num_classes2 # Ensure both classifiers predict the same categories
|
54 |
+
|
55 |
+
self.num_classes = num_classes1
|
56 |
+
|
57 |
+
# Load a pretrained ResNet-50 model as the feature extractor
|
58 |
+
self.model_resnet = models.resnet50(weights='ResNet50_Weights.DEFAULT')
|
59 |
+
|
60 |
+
# Remove ResNet's original classification layer to use as a feature extractor
|
61 |
+
num_ftrs = self.model_resnet.fc.in_features
|
62 |
+
self.model_resnet.fc = nn.Identity() # Identity layer keeps feature dimensions
|
63 |
+
|
64 |
+
# Additional transformation layer reducing feature size to 128D
|
65 |
+
self.dr = nn.Linear(num_ftrs, 128)
|
66 |
+
|
67 |
+
# Two independent classifiers
|
68 |
+
self.fc1 = ResClassifier(num_classes1)
|
69 |
+
self.fc2 = ResClassifier(num_classes1)
|
70 |
+
|
71 |
+
def forward(self, x, detach_feature=False):
|
72 |
+
"""
|
73 |
+
Forward pass through the model.
|
74 |
+
Extracts deep features from ResNet and processes them through classifiers.
|
75 |
+
"""
|
76 |
+
with torch.no_grad():
|
77 |
+
# Extract deep features using ResNet-50 (without its original classification head)
|
78 |
+
feature = self.model_resnet(x)
|
79 |
+
|
80 |
+
# Generate transformed features (128D) using the custom linear layer
|
81 |
+
dr_feature = self.dr(feature)
|
82 |
+
|
83 |
+
if detach_feature:
|
84 |
+
dr_feature = dr_feature.detach() # Detach feature for non-trainable forward pass
|
85 |
+
|
86 |
+
# Pass features through two independent classifiers
|
87 |
+
out1 = self.fc1(dr_feature)
|
88 |
+
out2 = self.fc2(dr_feature)
|
89 |
+
|
90 |
+
# Compute the mean prediction from both classifiers
|
91 |
+
output_mean = (out1 + out2) / 2
|
92 |
+
|
93 |
+
return dr_feature, output_mean # Returning feature embeddings and final prediction
|
app (1).py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
import torchvision.transforms as transforms
|
6 |
+
from PIL import Image
|
7 |
+
from ResNet_for_CC import CC_model # Import the model
|
8 |
+
|
9 |
+
# Set device (CPU/GPU)
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
|
12 |
+
# Load the trained CC_model
|
13 |
+
model_path = "CC_net.pt"
|
14 |
+
model = CC_model(num_classes1=14)
|
15 |
+
|
16 |
+
# Load model weights
|
17 |
+
state_dict = torch.load(model_path, map_location=device)
|
18 |
+
model.load_state_dict(state_dict, strict=False)
|
19 |
+
model.to(device)
|
20 |
+
model.eval()
|
21 |
+
|
22 |
+
# Clothing1M Class Labels
|
23 |
+
class_labels = [
|
24 |
+
"T-Shirt", "Shirt", "Knitwear", "Chiffon", "Sweater", "Hoodie",
|
25 |
+
"Windbreaker", "Jacket", "Downcoat", "Suit", "Shawl", "Dress",
|
26 |
+
"Vest", "Underwear"
|
27 |
+
]
|
28 |
+
|
29 |
+
# ✅ **Updated Image Preprocessing Function**
|
30 |
+
def preprocess_image(image):
|
31 |
+
"""Applies necessary transformations to the input image."""
|
32 |
+
transform = transforms.Compose([
|
33 |
+
transforms.Resize(256),
|
34 |
+
transforms.CenterCrop(224),
|
35 |
+
transforms.ToTensor(),
|
36 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
37 |
+
])
|
38 |
+
return transform(image).unsqueeze(0).to(device)
|
39 |
+
|
40 |
+
# ✅ **Classification Function**
|
41 |
+
def classify_image(image):
|
42 |
+
"""Processes the input image and returns the predicted clothing category."""
|
43 |
+
print("\n[INFO] Received image for classification.")
|
44 |
+
|
45 |
+
try:
|
46 |
+
image = Image.fromarray(image) # Ensure conversion to PIL format
|
47 |
+
image = preprocess_image(image) # Apply transformations
|
48 |
+
print("[INFO] Image transformed and moved to device.")
|
49 |
+
|
50 |
+
with torch.no_grad():
|
51 |
+
output = model(image)
|
52 |
+
|
53 |
+
# ✅ Ensure output is a tensor (handle tuple case)
|
54 |
+
if isinstance(output, tuple):
|
55 |
+
output = output[1] # Extract the actual output tensor
|
56 |
+
|
57 |
+
print(f"[DEBUG] Model output shape: {output.shape}")
|
58 |
+
print(f"[DEBUG] Model output values: {output}")
|
59 |
+
|
60 |
+
if output.shape[1] != 14:
|
61 |
+
return f"[ERROR] Model output mismatch! Expected 14 but got {output.shape[1]}."
|
62 |
+
|
63 |
+
# Convert logits to probabilities
|
64 |
+
probabilities = F.softmax(output, dim=1)
|
65 |
+
print(f"[DEBUG] Softmax probabilities: {probabilities}")
|
66 |
+
|
67 |
+
# Get predicted class index
|
68 |
+
predicted_class = torch.argmax(probabilities, dim=1).item()
|
69 |
+
print(f"[INFO] Predicted class index: {predicted_class} (Class: {class_labels[predicted_class]})")
|
70 |
+
|
71 |
+
# Validate and return the prediction
|
72 |
+
if 0 <= predicted_class < len(class_labels):
|
73 |
+
predicted_label = class_labels[predicted_class]
|
74 |
+
confidence = probabilities[0][predicted_class].item() * 100
|
75 |
+
return f"Predicted Class: {predicted_label} (Confidence: {confidence:.2f}%)"
|
76 |
+
else:
|
77 |
+
return "[ERROR] Model returned an invalid class index."
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
print(f"[ERROR] Exception during classification: {e}")
|
81 |
+
return "Error in classification. Check console for details."
|
82 |
+
|
83 |
+
# ✅ **Gradio Interface**
|
84 |
+
interface = gr.Interface(
|
85 |
+
fn=classify_image,
|
86 |
+
inputs=gr.Image(type="numpy"),
|
87 |
+
outputs="text",
|
88 |
+
title="Clothing1M Image Classifier",
|
89 |
+
description="Upload a clothing image, and the model will classify it into one of the 14 categories."
|
90 |
+
)
|
91 |
+
|
92 |
+
# ✅ **Run the Interface**
|
93 |
+
if __name__ == "__main__":
|
94 |
+
print("[INFO] Launching Gradio interface...")
|
95 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
clip==0.2.0
|
2 |
+
numpy==1.23.4
|
3 |
+
openai_clip==1.0.1
|
4 |
+
Pillow==9.4.0
|
5 |
+
torch==2.6.0
|
6 |
+
torchvision==0.21.0
|
7 |
+
tqdm==4.64.1
|