You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2017/11/02 05:31:12 UTC

[GitHub] solin319 commented on a change in pull request #8423: Re-implement segnet in MXnet

solin319 commented on a change in pull request #8423: Re-implement segnet in MXnet
URL: https://github.com/apache/incubator-mxnet/pull/8423#discussion_r148445696
 
 

 ##########
 File path: example/segnet/common/contrib_metrics.py
 ##########
 @@ -0,0 +1,156 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import numpy
+import mxnet as mx
+
+class Accuracy(mx.metric.EvalMetric):
+    """Computes accuracy classification score.
+
+    The accuracy score is defined as
+
+    .. math::
+        \\text{accuracy}(y, \\hat{y}) = \\frac{1}{n} \\sum_{i=0}^{n-1}
+        \\text{1}(\\hat{y_i} == y_i)
+
+    Parameters
+    ----------
+    axis : int, default=1
+        The axis that represents classes
+    name : str
+        Name of this metric instance for display.
+    output_names : list of str, or None
+        Name of predictions that should be used when updating with update_dict.
+        By default include all predictions.
+    label_names : list of str, or None
+        Name of labels that should be used when updating with update_dict.
+        By default include all labels.
+    ignore_label : int
+        Number of label that should not be computed.
+
+    Examples
+    --------
+    >>> predicts = [mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
+    >>> labels   = [mx.nd.array([0, 1, 1])]
+    >>> acc = mx.metric.Accuracy()
+    >>> acc.update(preds = predicts, labels = labels)
+    >>> print acc.get()
+    ('accuracy', 0.6666666666666666)
+    """
+    def __init__(self, axis=1, name='accuracy',
+                 output_names=None, label_names=None, ignore_label=-1):
+        super(Accuracy, 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):
+        """Updates the internal evaluation result.
+
+        Parameters
+        ----------
+        labels : list of `NDArray`
+            The labels of the data.
+
+        preds : list of `NDArray`
+            Predicted values.
+        """
+        mx.metric.check_label_shapes(labels, preds)
+
+        for label, pred_label in zip(labels, preds):
+            if pred_label.shape != label.shape:
+                pred_label = mx.ndarray.argmax(pred_label, axis=self.axis)
+            pred_label = pred_label.asnumpy().astype('int32')
+            label = label.asnumpy().astype('int32')
+
+            mx.metric.check_label_shapes(label, pred_label)
+
+            self.sum_metric += (pred_label.flat == label.flat).sum()
 
 Review comment:
   ```
   elf.sum_metric -= len(pred_label[pred_label == self.ignore_label])
   ```
   This can solve the problem caused by ignore_label  located in pred_label.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services