Skip to content

Commit

Permalink
Support default target_weight as None in losses (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezra-Yu authored Aug 3, 2021
1 parent 3ee909c commit 92fd1c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion mmpose/models/losses/classfication_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, use_target_weight=False, loss_weight=1.):
self.use_target_weight = use_target_weight
self.loss_weight = loss_weight

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -29,6 +29,7 @@ def forward(self, output, target, target_weight):
"""

if self.use_target_weight:
assert target_weight is not None
loss = self.criterion(output, target, reduction='none')
if target_weight.dim() == 1:
target_weight = target_weight[:, None]
Expand Down
18 changes: 12 additions & 6 deletions mmpose/models/losses/regression_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, use_target_weight=False, loss_weight=1.):
self.use_target_weight = use_target_weight
self.loss_weight = loss_weight

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -38,6 +38,7 @@ def forward(self, output, target, target_weight):
Weights across different joint types.
"""
if self.use_target_weight:
assert target_weight is not None
loss = self.criterion(output * target_weight,
target * target_weight)
else:
Expand Down Expand Up @@ -91,7 +92,7 @@ def criterion(self, pred, target):
self.omega * torch.log(1.0 + delta / self.epsilon), delta - self.C)
return torch.mean(torch.sum(losses, dim=[1, 2]), dim=0)

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -106,6 +107,7 @@ def forward(self, output, target, target_weight):
Weights across different joint types.
"""
if self.use_target_weight:
assert target_weight is not None
loss = self.criterion(output * target_weight,
target * target_weight)
else:
Expand All @@ -129,7 +131,7 @@ def __init__(self, use_target_weight=False, loss_weight=1.):
self.use_target_weight = use_target_weight
self.loss_weight = loss_weight

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -145,6 +147,7 @@ def forward(self, output, target, target_weight):
"""

if self.use_target_weight:
assert target_weight is not None
loss = torch.mean(
torch.norm((output - target) * target_weight, dim=-1))
else:
Expand All @@ -163,7 +166,7 @@ def __init__(self, use_target_weight=False, loss_weight=1.):
self.use_target_weight = use_target_weight
self.loss_weight = loss_weight

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -177,6 +180,7 @@ def forward(self, output, target, target_weight):
Weights across different joint types.
"""
if self.use_target_weight:
assert target_weight is not None
loss = self.criterion(output * target_weight,
target * target_weight)
else:
Expand All @@ -195,7 +199,7 @@ def __init__(self, use_target_weight=False, loss_weight=1.):
self.use_target_weight = use_target_weight
self.loss_weight = loss_weight

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -209,6 +213,7 @@ def forward(self, output, target, target_weight):
Weights across different joint types.
"""
if self.use_target_weight:
assert target_weight is not None
loss = self.criterion(output * target_weight,
target * target_weight)
else:
Expand Down Expand Up @@ -239,7 +244,7 @@ def __init__(self, joint_parents, use_target_weight=False, loss_weight=1.):
if i != self.joint_parents[i]:
self.non_root_indices.append(i)

def forward(self, output, target, target_weight):
def forward(self, output, target, target_weight=None):
"""Forward function.
Note:
Expand All @@ -260,6 +265,7 @@ def forward(self, output, target, target_weight):
target - target[:, self.joint_parents, :],
dim=-1)[:, self.non_root_indices]
if self.use_target_weight:
assert target_weight is not None
loss = torch.mean(
torch.abs((output_bone * target_weight).mean(dim=0) -
(target_bone * target_weight).mean(dim=0)))
Expand Down

0 comments on commit 92fd1c7

Please sign in to comment.