karthik55 commited on
Commit
c06e143
·
verified ·
1 Parent(s): 0accf3f

Upload 9 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ examples/img3.jpg filter=lfs diff=lfs merge=lfs -text
37
+ examples/Screenshot[[:space:]]2025-03-14[[:space:]]at[[:space:]]5.29.03 PM.png filter=lfs diff=lfs merge=lfs -text
38
+ examples/Screenshot[[:space:]]2025-03-14[[:space:]]at[[:space:]]5.29.32 PM.png filter=lfs diff=lfs merge=lfs -text
App.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ import torchvision.transforms as transforms
7
+ from PIL import Image
8
+ from ResNet_for_CC import CC_model # Import fixed model
9
+
10
+ # Set device (CPU/GPU)
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+
13
+ # Load the trained CC_model
14
+ model_path = "CC_net.pt"
15
+ model = CC_model(num_classes=14)
16
+
17
+ # Load model weights
18
+ state_dict = torch.load(model_path, map_location=device)
19
+ model.load_state_dict(state_dict, strict=False)
20
+ model.to(device)
21
+ model.eval()
22
+
23
+ # Clothing1M Class Labels
24
+ class_labels = [
25
+ "T-Shirt", "Shirt", "Knitwear", "Chiffon", "Sweater", "Hoodie",
26
+ "Windbreaker", "Jacket", "Downcoat", "Suit", "Shawl", "Dress",
27
+ "Vest", "Underwear"
28
+ ]
29
+
30
+ # Define image transformations
31
+ transform = transforms.Compose([
32
+ transforms.Resize(256),
33
+ transforms.CenterCrop(224),
34
+ transforms.ToTensor(),
35
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
36
+ ])
37
+
38
+ # 🔹 **Dynamically Fetch All Example Images (Including .webp)**
39
+ def get_example_images():
40
+ examples_dir = "examples"
41
+ if not os.path.exists(examples_dir):
42
+ print("[WARNING] 'examples/' directory does not exist.")
43
+ return []
44
+
45
+ # Fetch all image files (including .webp)
46
+ image_files = sorted([
47
+ os.path.join(examples_dir, f) for f in os.listdir(examples_dir)
48
+ if f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
49
+ ])
50
+
51
+ if not image_files:
52
+ print("[WARNING] No images found in 'examples/' directory.")
53
+
54
+ print(f"[INFO] Found {len(image_files)} images in 'examples/'")
55
+ return image_files
56
+
57
+ # 🔹 **Classification Function**
58
+ def classify_image(image):
59
+ print("\n[DEBUG] Received image for classification.")
60
+
61
+ try:
62
+ image = transform(image).unsqueeze(0).to(device)
63
+ print("[DEBUG] Image transformed and moved to device.")
64
+
65
+ with torch.no_grad():
66
+ output = model(image)
67
+ print(f"[DEBUG] Model output shape: {output.shape}")
68
+ print(f"[DEBUG] Model output values: {output}")
69
+
70
+ if output.shape[1] != 14:
71
+ return f"[ERROR] Model output mismatch! Expected 14 but got {output.shape[1]}."
72
+
73
+ # Convert logits to probabilities
74
+ probabilities = F.softmax(output, dim=1)
75
+ print(f"[DEBUG] Softmax probabilities: {probabilities}")
76
+
77
+ # Print class predictions and probabilities
78
+ for i, prob in enumerate(probabilities[0].tolist()):
79
+ print(f"[INFO] {class_labels[i]}: {prob * 100:.2f}%")
80
+
81
+ # Get predicted class index
82
+ predicted_class = torch.argmax(probabilities, dim=1).item()
83
+ print(f"[DEBUG] Predicted class index: {predicted_class} (Class: {class_labels[predicted_class]})")
84
+
85
+ # Validate prediction
86
+ if 0 <= predicted_class < len(class_labels):
87
+ predicted_label = class_labels[predicted_class]
88
+ confidence = probabilities[0][predicted_class].item() * 100
89
+ return f"Predicted Class: {predicted_label} (Confidence: {confidence:.2f}%)"
90
+ else:
91
+ return "[ERROR] Model returned an invalid class index."
92
+
93
+ except Exception as e:
94
+ print(f"[ERROR] Exception during classification: {e}")
95
+ return "Error in classification. Check console for details."
96
+
97
+ # 🔹 **Create Gradio Interface with Dynamic Examples**
98
+ interface = gr.Interface(
99
+ fn=classify_image,
100
+ inputs=gr.Image(type="pil"),
101
+ outputs="text",
102
+ title="Clothing1M Image Classifier",
103
+ description="Upload a clothing image, or select an example below to classify it.",
104
+ examples=get_example_images() # Dynamically load all images including .webp
105
+ )
106
+
107
+ # Run the Interface
108
+ if __name__ == "__main__":
109
+ print("[INFO] Launching Gradio interface...")
110
+ interface.launch()
CC_net.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,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.models as models
4
+
5
+
6
+ class ResClassifier(nn.Module):
7
+ def __init__(self, class_num=14):
8
+ super(ResClassifier, self).__init__()
9
+ self.fc1 = nn.Sequential(
10
+ nn.Linear(128, 64),
11
+ nn.BatchNorm1d(64, affine=True),
12
+ nn.ReLU(inplace=True),
13
+ nn.Dropout()
14
+ )
15
+ self.fc2 = nn.Sequential(
16
+ nn.Linear(64, 64),
17
+ nn.BatchNorm1d(64, affine=True),
18
+ nn.ReLU(inplace=True),
19
+ nn.Dropout()
20
+ )
21
+ self.fc3 = nn.Linear(64, class_num)
22
+
23
+ def forward(self, x):
24
+ fc1_emb = self.fc1(x)
25
+ fc2_emb = self.fc2(fc1_emb)
26
+ logit = self.fc3(fc2_emb)
27
+ return logit
28
+
29
+
30
+ class CC_model(nn.Module):
31
+ def __init__(self, num_classes=14):
32
+ super(CC_model, self).__init__()
33
+ self.num_classes = num_classes
34
+ self.model_resnet = models.resnet50(weights='ResNet50_Weights.DEFAULT')
35
+
36
+ # Modify final layers
37
+ num_ftrs = self.model_resnet.fc.in_features
38
+ self.model_resnet.fc = nn.Identity() # Remove ResNet's default final layer
39
+ self.classification_fc = nn.Linear(num_ftrs, num_classes)
40
+ self.dr = nn.Linear(num_ftrs, 128) # Feature reduction (for embeddings)
41
+ self.fc1 = ResClassifier(num_classes)
42
+ self.fc2 = ResClassifier(num_classes)
43
+
44
+ def forward(self, x):
45
+ feature = self.model_resnet(x)
46
+ class_logits = self.classification_fc(feature) # Correct classification output
47
+ return class_logits # Ensure output shape is [batch_size, 14]
examples/Screenshot 2025-03-14 at 5.29.03 PM.png ADDED

Git LFS Details

  • SHA256: 40a23b2249a7060334da586e2320b85a89b01fda026420fe3e48b993a79f421d
  • Pointer size: 131 Bytes
  • Size of remote file: 179 kB
examples/Screenshot 2025-03-14 at 5.29.32 PM.png ADDED

Git LFS Details

  • SHA256: ffeac5d710c5c311b7a9b30640670593126a0329094afb0a0778e7416ef35de2
  • Pointer size: 131 Bytes
  • Size of remote file: 629 kB
examples/imag7.jpeg ADDED
examples/img1.webp ADDED
examples/img3.jpg ADDED

Git LFS Details

  • SHA256: b344c2f8877fad1e17627512a55fda6fe186eebf7789b6b410c557663a89514d
  • Pointer size: 131 Bytes
  • Size of remote file: 108 kB
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