Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the CustomNonLinearClsHead when the batch_size is set to 1 #2569

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@

def forward_train(self, cls_score, gt_label):
"""Forward_train fuction of CustomNonLinearHead class."""
# NOTE, when the batch_size is set to 1, this raise an error at BatchNorm1d
# To avoid this, use simple trick to pretend batch size as 2.
# If loss type isn't IBLoss, the output loss will be same
bs = cls_score.shape[0]
if bs == 1 and self.loss_type != "IBLoss":
cls_score = torch.cat([cls_score, cls_score], dim=0)
gt_label = torch.cat([gt_label, gt_label], dim=0)

Check warning on line 51 in src/otx/algorithms/classification/adapters/mmcls/models/heads/custom_cls_head.py

View check run for this annotation

Codecov / codecov/patch

src/otx/algorithms/classification/adapters/mmcls/models/heads/custom_cls_head.py#L50-L51

Added lines #L50 - L51 were not covered by tests
logit = self.classifier(cls_score)
losses = self.loss(logit, gt_label, feature=cls_score)
return losses
Expand Down