pytorch-center-loss icon indicating copy to clipboard operation
pytorch-center-loss copied to clipboard

Dont know how to apply centerloss to my own project

Open 1653091 opened this issue 5 years ago • 2 comments

I want to use ResNet50 as my model and change Fully Connection layer to classify images.

#Create model: model = models.resnet50(pretrained=True) fc_inputs = model.fc.in_features class FClayer(nn.Module): def init(self): super(FClayer, self).init() self.fc1 = nn.Linear(fc_inputs, 2) self.relu1 = nn.ReLU() self.fc2 = nn.Linear(2, num_classes) def forward(self, x): x = self.relu1(self.fc1(x)) y = self.fc2(x) return x, y model.fc = FClayer() ... #In train function: features, outputs = model(inputs) alpha = 1 loss = loss_criterion(outputs, labels) + (loss_criterion_cent(features, labels) * alpha) optimizer.zero_grad() optimizer_cent.zero_grad() loss.backward() optimizer.step() for param in loss_criterion_cent.parameters(): param.grad.data *= (1./alpha) optimizer_cent.step()

I dont know why when i was training, at the very first epochs, the accuracy had increased, but soon later, nothing happended. Here is my accuracy plot curve chart: Sign_accuracy_curve

Do I have something wrong?

1653091 avatar Aug 30 '19 01:08 1653091

Your question is quite ambiguous. But I think it was probably caused by a high LR or a high loss weight which makes gradient descent optimization failed.

shijianjian avatar Oct 15 '19 09:10 shijianjian

Have you found a solution to your problem?

RaduFilip16 avatar Aug 20 '22 17:08 RaduFilip16