|
import torch |
|
import torch.nn as nn |
|
import torchvision.models as models |
|
|
|
class ResClassifier(nn.Module): |
|
""" |
|
A classifier with two fully connected layers followed by a final linear layer. |
|
Uses BatchNorm, ReLU activations, and Dropout for better generalization. |
|
""" |
|
def __init__(self, num_classes=14): |
|
super(ResClassifier, self).__init__() |
|
|
|
|
|
self.fc1 = nn.Sequential( |
|
nn.Linear(128, 64), |
|
nn.BatchNorm1d(64, affine=True), |
|
nn.ReLU(inplace=True), |
|
nn.Dropout() |
|
) |
|
|
|
|
|
self.fc2 = nn.Sequential( |
|
nn.Linear(64, 64), |
|
nn.BatchNorm1d(64, affine=True), |
|
nn.ReLU(inplace=True), |
|
nn.Dropout() |
|
) |
|
|
|
|
|
self.fc3 = nn.Linear(64, num_classes) |
|
|
|
def forward(self, x): |
|
""" |
|
Forward pass through the classifier. |
|
Returns class logits after two hidden layers. |
|
""" |
|
x = self.fc1(x) |
|
x = self.fc2(x) |
|
output = self.fc3(x) |
|
return output |
|
|
|
|
|
class CC_model(nn.Module): |
|
""" |
|
Clothing Classification Model based on ResNet50. |
|
Extracts deep features and uses two independent classifiers for predictions. |
|
""" |
|
def __init__(self, num_classes1=14, num_classes2=None): |
|
super(CC_model, self).__init__() |
|
|
|
|
|
num_classes2 = num_classes2 if num_classes2 else num_classes1 |
|
assert num_classes1 == num_classes2 |
|
|
|
self.num_classes = num_classes1 |
|
|
|
|
|
self.model_resnet = models.resnet50(weights='ResNet50_Weights.DEFAULT') |
|
|
|
|
|
num_ftrs = self.model_resnet.fc.in_features |
|
self.model_resnet.fc = nn.Identity() |
|
|
|
|
|
self.dr = nn.Linear(num_ftrs, 128) |
|
|
|
|
|
self.fc1 = ResClassifier(num_classes1) |
|
self.fc2 = ResClassifier(num_classes1) |
|
|
|
def forward(self, x, detach_feature=False): |
|
""" |
|
Forward pass through the model. |
|
Extracts deep features from ResNet and processes them through classifiers. |
|
""" |
|
with torch.no_grad(): |
|
|
|
feature = self.model_resnet(x) |
|
|
|
|
|
dr_feature = self.dr(feature) |
|
|
|
if detach_feature: |
|
dr_feature = dr_feature.detach() |
|
|
|
|
|
out1 = self.fc1(dr_feature) |
|
out2 = self.fc2(dr_feature) |
|
|
|
|
|
output_mean = (out1 + out2) / 2 |
|
|
|
return dr_feature, output_mean |
|
|