Upload 3 files
Browse files- CC_net (1).pt +3 -0
- ResNet_for_CC.py +93 -0
- requirements.txt +7 -0
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
|
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
|