metric.py
Go to the documentation of this file.
00001 # --------------------------------------------------------
00002 # Deformable Convolutional Networks
00003 # Copyright (c) 2016 by Contributors
00004 # Copyright (c) 2017 Microsoft
00005 # Licensed under The Apache-2.0 License [see LICENSE for details]
00006 # Modified by Yuwen Xiong
00007 # --------------------------------------------------------
00008 
00009 import mxnet as mx
00010 import numpy as np
00011 
00012 
00013 def get_rpn_names():
00014     pred = ['rpn_cls_prob', 'rpn_bbox_loss']
00015     label = ['rpn_label', 'rpn_bbox_target', 'rpn_bbox_weight']
00016     return pred, label
00017 
00018 
00019 def get_rcnn_names(cfg):
00020     pred = ['rcnn_cls_prob', 'rcnn_bbox_loss']
00021     label = ['rcnn_label', 'rcnn_bbox_target', 'rcnn_bbox_weight']
00022     if cfg.TRAIN.ENABLE_OHEM or cfg.TRAIN.END2END:
00023         pred.append('rcnn_label')
00024     if cfg.TRAIN.END2END:
00025         rpn_pred, rpn_label = get_rpn_names()
00026         pred = rpn_pred + pred
00027         label = rpn_label
00028     return pred, label
00029 
00030 
00031 class RPNAccMetric(mx.metric.EvalMetric):
00032     def __init__(self):
00033         super(RPNAccMetric, self).__init__('RPNAcc')
00034         self.pred, self.label = get_rpn_names()
00035 
00036     def update(self, labels, preds):
00037         pred = preds[self.pred.index('rpn_cls_prob')]
00038         label = labels[self.label.index('rpn_label')]
00039 
00040         # pred (b, c, p) or (b, c, h, w)
00041         pred_label = mx.ndarray.argmax_channel(pred).asnumpy().astype('int32')
00042         pred_label = pred_label.reshape((pred_label.shape[0], -1))
00043         # label (b, p)
00044         label = label.asnumpy().astype('int32')
00045 
00046         # filter with keep_inds
00047         keep_inds = np.where(label != -1)
00048         pred_label = pred_label[keep_inds]
00049         label = label[keep_inds]
00050 
00051         self.sum_metric += np.sum(pred_label.flat == label.flat)
00052         self.num_inst += len(pred_label.flat)
00053 
00054 
00055 class RCNNAccMetric(mx.metric.EvalMetric):
00056     def __init__(self, cfg):
00057         super(RCNNAccMetric, self).__init__('RCNNAcc')
00058         self.e2e = cfg.TRAIN.END2END
00059         self.ohem = cfg.TRAIN.ENABLE_OHEM
00060         self.pred, self.label = get_rcnn_names(cfg)
00061 
00062     def update(self, labels, preds):
00063         pred = preds[self.pred.index('rcnn_cls_prob')]
00064         if self.ohem or self.e2e:
00065             label = preds[self.pred.index('rcnn_label')]
00066         else:
00067             label = labels[self.label.index('rcnn_label')]
00068 
00069         last_dim = pred.shape[-1]
00070         pred_label = pred.asnumpy().reshape(-1, last_dim).argmax(axis=1).astype('int32')
00071         label = label.asnumpy().reshape(-1,).astype('int32')
00072 
00073         # filter with keep_inds
00074         keep_inds = np.where(label != -1)
00075         pred_label = pred_label[keep_inds]
00076         label = label[keep_inds]
00077 
00078         self.sum_metric += np.sum(pred_label.flat == label.flat)
00079         self.num_inst += len(pred_label.flat)
00080 
00081 
00082 class RPNLogLossMetric(mx.metric.EvalMetric):
00083     def __init__(self):
00084         super(RPNLogLossMetric, self).__init__('RPNLogLoss')
00085         self.pred, self.label = get_rpn_names()
00086 
00087     def update(self, labels, preds):
00088         pred = preds[self.pred.index('rpn_cls_prob')]
00089         label = labels[self.label.index('rpn_label')]
00090 
00091         # label (b, p)
00092         label = label.asnumpy().astype('int32').reshape((-1))
00093         # pred (b, c, p) or (b, c, h, w) --> (b, p, c) --> (b*p, c)
00094         pred = pred.asnumpy().reshape((pred.shape[0], pred.shape[1], -1)).transpose((0, 2, 1))
00095         pred = pred.reshape((label.shape[0], -1))
00096 
00097         # filter with keep_inds
00098         keep_inds = np.where(label != -1)[0]
00099         label = label[keep_inds]
00100         cls = pred[keep_inds, label]
00101 
00102         cls += 1e-14
00103         cls_loss = -1 * np.log(cls)
00104         cls_loss = np.sum(cls_loss)
00105         self.sum_metric += cls_loss
00106         self.num_inst += label.shape[0]
00107 
00108 
00109 class RCNNLogLossMetric(mx.metric.EvalMetric):
00110     def __init__(self, cfg):
00111         super(RCNNLogLossMetric, self).__init__('RCNNLogLoss')
00112         self.e2e = cfg.TRAIN.END2END
00113         self.ohem = cfg.TRAIN.ENABLE_OHEM
00114         self.pred, self.label = get_rcnn_names(cfg)
00115 
00116     def update(self, labels, preds):
00117         pred = preds[self.pred.index('rcnn_cls_prob')]
00118         if self.ohem or self.e2e:
00119             label = preds[self.pred.index('rcnn_label')]
00120         else:
00121             label = labels[self.label.index('rcnn_label')]
00122 
00123         last_dim = pred.shape[-1]
00124         pred = pred.asnumpy().reshape(-1, last_dim)
00125         label = label.asnumpy().reshape(-1,).astype('int32')
00126 
00127         # filter with keep_inds
00128         keep_inds = np.where(label != -1)[0]
00129         label = label[keep_inds]
00130         cls = pred[keep_inds, label]
00131 
00132         cls += 1e-14
00133         cls_loss = -1 * np.log(cls)
00134         cls_loss = np.sum(cls_loss)
00135         self.sum_metric += cls_loss
00136         self.num_inst += label.shape[0]
00137 
00138 
00139 class RPNL1LossMetric(mx.metric.EvalMetric):
00140     def __init__(self):
00141         super(RPNL1LossMetric, self).__init__('RPNL1Loss')
00142         self.pred, self.label = get_rpn_names()
00143 
00144     def update(self, labels, preds):
00145         bbox_loss = preds[self.pred.index('rpn_bbox_loss')].asnumpy()
00146 
00147         # calculate num_inst (average on those kept anchors)
00148         label = labels[self.label.index('rpn_label')].asnumpy()
00149         num_inst = np.sum(label != -1)
00150 
00151         self.sum_metric += np.sum(bbox_loss)
00152         self.num_inst += num_inst
00153 
00154 
00155 class RCNNL1LossMetric(mx.metric.EvalMetric):
00156     def __init__(self, cfg):
00157         super(RCNNL1LossMetric, self).__init__('RCNNL1Loss')
00158         self.e2e = cfg.TRAIN.END2END
00159         self.ohem = cfg.TRAIN.ENABLE_OHEM
00160         self.pred, self.label = get_rcnn_names(cfg)
00161 
00162     def update(self, labels, preds):
00163         bbox_loss = preds[self.pred.index('rcnn_bbox_loss')].asnumpy()
00164         if self.ohem:
00165             label = preds[self.pred.index('rcnn_label')].asnumpy()
00166         else:
00167             if self.e2e:
00168                 label = preds[self.pred.index('rcnn_label')].asnumpy()
00169             else:
00170                 label = labels[self.label.index('rcnn_label')].asnumpy()
00171 
00172         # calculate num_inst (average on those kept anchors)
00173         num_inst = np.sum(label != -1)
00174 
00175         self.sum_metric += np.sum(bbox_loss)
00176         self.num_inst += num_inst


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30