Commit 2990a673 authored by Julian Treiber's avatar Julian Treiber

added metric AccuracyWithIgnoreLabel

parent 02624b4a
......@@ -87,6 +87,33 @@ class DiceLoss(gluon.loss.Loss):
diceloss = self.dice_loss(F, pred, label)
return F.mean(loss, axis=self._batch_axis, exclude=True) + diceloss
@mx.metric.register
class ACCURACY_IGNORE_LABEL(mx.metric.EvalMetric):
def __init__(self, axis=1, ignore_label=255, name='accuracy',
output_names=None, label_names=None):
super(ACCURACY_IGNORE_LABEL, self).__init__(
name, axis=axis,
output_names=output_names, label_names=label_names)
self.axis = axis
self.ignore_label = ignore_label
def update(self, labels, preds):
mx.metric.check_label_shapes(labels, preds)
for label, pred_label in zip(labels, preds):
if pred_label.shape != label.shape:
pred_label = mx.nd.argmax(pred_label, axis=self.axis, keepdims=True)
label = label.astype('int32')
pred_label = pred_label.astype('int32').as_in_context(label.context)
mx.metric.check_label_shapes(label, pred_label)
correct = mx.nd.sum( (label == pred_label) * (label != self.ignore_label) ).asscalar()
total = mx.nd.sum( (label != self.ignore_label) ).asscalar()
self.sum_metric += correct
self.num_inst += total
@mx.metric.register
class BLEU(mx.metric.EvalMetric):
N = 4
......
......@@ -66,6 +66,12 @@ if __name__ == "__main__":
eval_metric_params={
<#if (config.evalMetric.exclude)??>
'exclude': [<#list config.evalMetric.exclude as value>${value}<#sep>, </#list>],
</#if>
<#if (config.evalMetric.exclude)??>
'axis': ${config.evalMetric.axis},
</#if>
<#if (config.evalMetric.exclude)??>
'ignore_label': ${config.evalMetric.ignore_label},
</#if>
},
</#if>
......
......@@ -65,7 +65,10 @@ if __name__ == "__main__":
preprocessing=${config.preprocessor?string("True","False")},
</#if>
<#if (config.evalMetric)??>
eval_metric='${config.evalMetric}',
eval_metric='${config.evalMetric.name}',
eval_metric_params={
<#if (config.evalMetric.exclude)??>
'exclude': [<#list config.evalMetric.exclude as value>${value}<#sep>, </#list>],
</#if>
<#if (config.configuration.optimizer)??>
optimizer='${config.optimizerName}',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment