Lovasz hinge loss function


(Imran) #1

Hi,

I am trying to implement “lovasz_hinge” loss for binary segmentation - given here (https://github.com/bermanmaxim/LovaszSoftmax/blob/master/pytorch/lovasz_losses.py) with fast ai, on carvana notebook from lesson 14.

However I am unable to do. Can someone direct me on how to do this?


(William Horton) #2

Does the code in that repository not work for you? What errors are you seeing?


(Imran) #3

For starters, I am getting this error even while trying to run lr_find

lovasz_hinge
    loss = mean(lovasz_hinge_flat(*flatten_binary_scores(log.unsqueeze(0), lab.unsqueeze(0), ignore))

NameError: name 'mean' is not defined

This is the code I am using fordef lovasz_loss(logits, labels):
def lovasz_loss(logits, labels):
labels, logits = labels.squeeze(1),logits.squeeze(1)
loss = lovasz_hinge(logits, labels, per_image = True, ignore = None)
return loss

def lovasz_hinge(logits, labels, per_image=True, ignore=None):
 if per_image:
  loss = mean(lovasz_hinge_flat(*flatten_binary_scores(log.unsqueeze(0), lab.unsqueeze(0), ignore))
        for log, lab in zip(logits, labels))
 else:
  loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels, ignore))
 return loss
 
def lovasz_hinge_flat(logits, labels):
 if len(labels) == 0:
  return logits.sum() * 0.
 signs = 2. * labels.float() - 1.
 errors = (1. - logits * Variable(signs))
 errors_sorted, perm = torch.sort(errors, dim=0, descending=True)
 perm = perm.data
 gt_sorted = labels[perm]
 grad = lovasz_grad(gt_sorted)
 loss = torch.dot(F.relu(errors_sorted), Variable(grad))
 return loss
 
def lovasz_grad(gt_sorted):
 p = len(gt_sorted)
 gts = gt_sorted.sum()
 intersection = gts - gt_sorted.float().cumsum(0)
 union = gts + (1 - gt_sorted).float().cumsum(0)
 jaccard = 1. - intersection / union
 if p > 1: # cover 1-pixel case
  jaccard[1:p] = jaccard[1:p] - jaccard[0:-1]
 return jaccard
 
def flatten_binary_scores(scores, labels, ignore=None):
 scores = scores.view(-1)
 labels = labels.view(-1)
 if ignore is None:
  return scores, labels
 valid = (labels != ignore)
 vscores = scores[valid]
 vlabels = labels[valid]
 return vscores, vlabels

And this is where I am getting the error

loss_func=lovasz_loss
optimizer=optim.SGD

learn = ConvLearner(md, models)
learn.opt_fn=optimizer
learn.crit=loss_func
learn.metrics=[accuracy_thresh(0.5),dice]

learn.freeze_to(1)
learn.lr_find()
learn.sched.plot()

And when I include the mean function by importing statistics, there is a different error.


(William Horton) #4

It looks like the authors of the code have a custom mean helper function defined in that same file, you should start by trying to use that.


(Imran) #5

Good catch thanks, that fixed it, was fumbling for 4 days, trying to make this work !


(James) #6

Hi, I guess you implemented lovasz hinge loss. I am aware this loss is used in image segmentation but then still I can’t understand how it works ?. Can you explain it to me, if you don’t mind please.