How to use pred method in hypothesis

Best Python code snippet using hypothesis

iou_loss.py

Source:iou_loss.py Github

copy

Full Screen

1import tensorflow as tf2import tensorflow.keras as keras3import math4def compute_iou(mode='fciou'):5 @tf.function(jit_compile=True)6 def iou_(y_true, y_pred):7 y_true = tf.maximum(y_true, 0)8 pred_left = y_pred[:, :, 0]9 pred_top = y_pred[:, :, 1]10 pred_right = y_pred[:, :, 2]11 pred_bottom = y_pred[:, :, 3]12 pred_width = pred_left + pred_right13 pred_height = pred_top + pred_bottom14 # (num_pos, )15 target_left = y_true[:, :, 0]16 target_top = y_true[:, :, 1]17 target_right = y_true[:, :, 2]18 target_bottom = y_true[:, :, 3]19 target_width = target_left + target_right20 target_height = target_top + target_bottom21 target_area = (target_left + target_right) * (target_top + target_bottom)22 pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)23 w_intersect = tf.minimum(pred_left, target_left) + tf.minimum(pred_right, target_right)24 h_intersect = tf.minimum(pred_bottom, target_bottom) + tf.minimum(pred_top, target_top)25 area_intersect = w_intersect * h_intersect26 area_union = target_area + pred_area - area_intersect27 classic_iou = (area_intersect + 1e-7) / (area_union + 1e-7)28 if mode == 'iou':29 # (num_pos, )30 iou_loss = -tf.math.log(classic_iou)31 elif mode == 'giou':32 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)33 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)34 close_area = g_w_intersect * g_h_intersect35 g_iou = classic_iou - (close_area - area_union) / close_area36 iou_loss = 1 - g_iou37 elif mode == 'ciou':38 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))39 target_atan = tf.atan(target_width / (target_height + 1e-9))40 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)41 a = v / (1 - classic_iou + v)42 c_iou = classic_iou - 1.0 * a * v43 iou_loss = 1 - c_iou44 elif mode == 'fciou':45 target_center_x = target_width * 0.546 target_center_y = target_height * 0.547 pred_center_x = target_left - pred_left + pred_width * 0.548 pred_center_y = target_top - pred_top + pred_height * 0.549 intersect_diagonal = (target_center_x - pred_center_x) ** 2 + (target_center_y - pred_center_y) ** 250 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)51 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)52 max_diagonal = g_h_intersect ** 2 + g_w_intersect ** 253 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))54 target_atan = tf.atan(target_width / (target_height + 1e-9))55 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)56 a = v / (1 - classic_iou + v)57 c_iou = classic_iou - 1.0 * a * v - 1.0 * (intersect_diagonal / max_diagonal)58 iou_loss = 1 - c_iou59 else:60 iou_loss = -tf.math.log(classic_iou)61 # compute the normalizer: the number of positive anchors62 normalizer = tf.maximum(1, tf.reduce_prod(tf.shape(y_true)[0:2]))63 normalizer = tf.cast(normalizer, dtype=tf.float32)64 return tf.reduce_sum(iou_loss) / normalizer65 return iou_66def compute_iou_v2(mode='fciou'):67 @tf.function(jit_compile=True)68 def iou_(y_true, y_pred):69 y_true = tf.maximum(y_true, 0)70 pred_left = y_pred[:, :, 0]71 pred_top = y_pred[:, :, 1]72 pred_right = y_pred[:, :, 2]73 pred_bottom = y_pred[:, :, 3]74 pred_width = pred_left + pred_right75 pred_height = pred_top + pred_bottom76 # (num_pos, )77 target_left = y_true[:, :, 0]78 target_top = y_true[:, :, 1]79 target_right = y_true[:, :, 2]80 target_bottom = y_true[:, :, 3]81 target_width = target_left + target_right82 target_height = target_top + target_bottom83 target_area = (target_left + target_right) * (target_top + target_bottom)84 pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)85 w_intersect = tf.minimum(pred_left, target_left) + tf.minimum(pred_right, target_right)86 h_intersect = tf.minimum(pred_bottom, target_bottom) + tf.minimum(pred_top, target_top)87 area_intersect = w_intersect * h_intersect88 area_union = target_area + pred_area - area_intersect89 classic_iou = (area_intersect + 1e-7) / (area_union + 1e-7)90 if mode == 'iou':91 # (num_pos, )92 iou_loss = -tf.math.log(classic_iou)93 elif mode == 'giou':94 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)95 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)96 close_area = g_w_intersect * g_h_intersect97 g_iou = classic_iou - (close_area - area_union) / close_area98 iou_loss = 1 - g_iou99 elif mode == 'ciou':100 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))101 target_atan = tf.atan(target_width / (target_height + 1e-9))102 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)103 a = v / (1 - classic_iou + v)104 c_iou = classic_iou - 1.0 * a * v105 iou_loss = 1 - c_iou106 elif mode == 'fciou':107 target_center_x = target_width * 0.5108 target_center_y = target_height * 0.5109 pred_center_x = target_left - pred_left + pred_width * 0.5110 pred_center_y = target_top - pred_top + pred_height * 0.5111 intersect_diagonal = (target_center_x - pred_center_x) ** 2 + (target_center_y - pred_center_y) ** 2112 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)113 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)114 max_diagonal = g_h_intersect ** 2 + g_w_intersect ** 2115 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))116 target_atan = tf.atan(target_width / (target_height + 1e-9))117 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)118 a = v / (1 - classic_iou + v)119 c_iou = classic_iou - 1.0 * a * v - 1.0 * (intersect_diagonal / max_diagonal)120 iou_loss = 1 - c_iou121 else:122 iou_loss = -tf.math.log(classic_iou)123 return iou_loss124 return iou_125def iou_mask(mode='fciou', factor=1.0):126 def iou_mask_(inputs):127 # y_ture: (Batch, Anchor-points, 4 + 2)128 y_true, y_pred, soft_weight, mask = inputs[0][..., :4], inputs[1], inputs[0][..., 4], inputs[0][..., 5]129 y_true = tf.maximum(y_true, 0)130 pred_left = y_pred[:, :, 0]131 pred_top = y_pred[:, :, 1]132 pred_right = y_pred[:, :, 2]133 pred_bottom = y_pred[:, :, 3]134 pred_width = pred_left + pred_right135 pred_height = pred_top + pred_bottom136 target_left = y_true[:, :, 0]137 target_top = y_true[:, :, 1]138 target_right = y_true[:, :, 2]139 target_bottom = y_true[:, :, 3]140 target_width = target_left + target_right141 target_height = target_top + target_bottom142 target_area = (target_left + target_right) * (target_top + target_bottom)143 masked_target_area = tf.boolean_mask(target_area, mask)144 pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)145 masked_pred_area = tf.boolean_mask(pred_area, mask)146 w_intersect = tf.minimum(pred_left, target_left) + tf.minimum(pred_right, target_right)147 h_intersect = tf.minimum(pred_bottom, target_bottom) + tf.minimum(pred_top, target_top)148 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)149 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)150 close_area = g_w_intersect * g_h_intersect151 masked_close_area = tf.boolean_mask(close_area, mask)152 area_intersect = w_intersect * h_intersect153 masked_area_intersect = tf.boolean_mask(area_intersect, mask)154 masked_area_union = masked_target_area + masked_pred_area - masked_area_intersect155 masked_soft_weight = tf.boolean_mask(soft_weight, mask)156 # (B, N)157 masked_iou = (masked_area_intersect + 1e-7) / (masked_area_union + 1e-7)158 if mode == 'iou':159 masked_iou_loss = -tf.math.log(masked_iou) * masked_soft_weight160 elif mode == 'giou':161 masked_g_iou = masked_iou - (masked_close_area - masked_area_union) / masked_close_area162 masked_iou_loss = (1 - masked_g_iou) * masked_soft_weight163 elif mode == 'ciou':164 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))165 target_atan = tf.atan(target_width / (target_height + 1e-9))166 masked_pred_atan = tf.boolean_mask(pred_atan, mask)167 masked_target_atan = tf.boolean_mask(target_atan, mask)168 v = 4.0 * keras.backend.pow((masked_pred_atan - masked_target_atan), 2) / (math.pi ** 2)169 a = v / (1 - masked_iou + v)170 c_iou = masked_iou - 1.0 * a * v171 masked_iou_loss = (1 - c_iou) * masked_soft_weight172 elif mode == 'fciou':173 target_center_x = target_width * 0.5174 target_center_y = target_height * 0.5175 pred_center_x = target_left - pred_left + pred_width * 0.5176 pred_center_y = target_top - pred_top + pred_height * 0.5177 intersect_diagonal = (target_center_x - pred_center_x) ** 2 + (target_center_y - pred_center_y) ** 2178 max_diagonal = g_h_intersect ** 2 + g_w_intersect ** 2179 r_masked = tf.boolean_mask((intersect_diagonal / max_diagonal), mask)180 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))181 target_atan = tf.atan(target_width / (target_height + 1e-9))182 masked_pred_atan = tf.boolean_mask(pred_atan, mask)183 masked_target_atan = tf.boolean_mask(target_atan, mask)184 v = 4.0 * keras.backend.pow((masked_pred_atan - masked_target_atan), 2) / (math.pi ** 2)185 a = v / (1 - masked_iou + v)186 c_iou = masked_iou - 1.0 * a * v - 1.0 * r_masked187 masked_iou_loss = (1 - c_iou) * masked_soft_weight188 # masked_iou_loss = (1 - c_iou)189 else:190 masked_iou_loss = -tf.math.log(masked_iou) * masked_soft_weight191 # compute the normalizer: the number of positive location192 num_pos = tf.reduce_sum(mask * soft_weight)193 normalizer = keras.backend.maximum(1., num_pos)194 return tf.reduce_sum(masked_iou_loss) * factor / normalizer195 return iou_mask_196def iou_mask_v2(mode='fciou', factor=1.0):197 @tf.function(jit_compile=True)198 def iou_mask_(inputs):199 y_true, y_pred, soft_weight, mask = inputs[0][..., :4], inputs[1], inputs[0][..., 4], inputs[0][..., 5]200 y_true = tf.maximum(y_true, 0)201 iou_loss = iou_method(soft_weight, y_pred, y_true)202 # mask203 # masked_iou_loss = tf.boolean_mask(iou_loss, mask)204 # compute the normalizer: the number of positive location205 # num_pos = tf.reduce_sum(mask * soft_weight)206 normalizer = keras.backend.maximum(1., tf.reduce_sum(mask * soft_weight))207 # return factor * tf.reduce_sum(masked_iou_loss) / normalizer208 return factor * tf.reduce_sum(mask * iou_loss) / normalizer209 @tf.function(jit_compile=True) # only for tensorflow 2.6, sometimes should use experiment_compile210 def iou_method(soft_weight, y_pred, y_true):211 # pred212 pred_left = y_pred[:, :, 0]213 pred_top = y_pred[:, :, 1]214 pred_right = y_pred[:, :, 2]215 pred_bottom = y_pred[:, :, 3]216 pred_width = pred_left + pred_right217 pred_height = pred_top + pred_bottom218 pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)219 # target220 target_left = y_true[:, :, 0]221 target_top = y_true[:, :, 1]222 target_right = y_true[:, :, 2]223 target_bottom = y_true[:, :, 3]224 target_width = target_left + target_right225 target_height = target_top + target_bottom226 target_area = (target_left + target_right) * (target_top + target_bottom)227 # intersection between target and predict bboxes.228 w_intersect = tf.minimum(pred_left, target_left) + tf.minimum(pred_right, target_right)229 h_intersect = tf.minimum(pred_bottom, target_bottom) + tf.minimum(pred_top, target_top)230 area_intersect = w_intersect * h_intersect231 area_union = target_area + pred_area - area_intersect232 iou = (area_intersect + 1e-7) / (area_union + 1e-7)233 if mode == 'iou':234 iou_loss = -tf.math.log(iou) * soft_weight235 elif mode == 'giou':236 # max close area237 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)238 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)239 close_area = g_w_intersect * g_h_intersect240 g_iou = iou - (close_area - area_union) / close_area241 iou_loss = (1 - g_iou) * soft_weight242 elif mode == 'ciou':243 # without center fixing244 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))245 target_atan = tf.atan(target_width / (target_height + 1e-9))246 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)247 a = v / (1 - iou + v)248 c_iou = iou - 1.0 * a * v249 iou_loss = (1 - c_iou) * soft_weight250 elif mode == 'fciou':251 # full version of CIoU252 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)253 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)254 max_diagonal = g_h_intersect ** 2 + g_w_intersect ** 2255 target_center_x = target_width * 0.5256 target_center_y = target_height * 0.5257 pred_center_x = target_left - pred_left + pred_width * 0.5258 pred_center_y = target_top - pred_top + pred_height * 0.5259 intersect_diagonal = (target_center_x - pred_center_x) ** 2 + (target_center_y - pred_center_y) ** 2260 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))261 target_atan = tf.atan(target_width / (target_height + 1e-9))262 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)263 a = v / (1 - iou + v)264 c_iou = iou - 1.0 * a * v - 1.0 * (intersect_diagonal / max_diagonal)265 iou_loss = (1 - c_iou) * soft_weight266 else:267 iou_loss = -tf.math.log(iou) * soft_weight268 return iou_loss269 return iou_mask_270def iou_mask_v3(mode='fciou', factor=1.0):271 def iou_mask_(inputs):272 y_true, y_pred, soft_weight, mask = inputs[0][..., :4], inputs[1], inputs[0][..., 4], inputs[0][..., 5]273 y_true = tf.maximum(y_true, 0)274 fmn_out = inputs[2]275 soft_weight *= fmn_out[..., 0]276 iou_loss = iou_method(soft_weight, y_pred, y_true)277 # mask278 masked_iou_loss = tf.boolean_mask(iou_loss, mask)279 # compute the normalizer: the number of positive location280 normalizer = keras.backend.maximum(1., tf.reduce_sum(mask * soft_weight))281 return factor * tf.reduce_sum(masked_iou_loss) / normalizer282 @tf.function(jit_compile=True) # only for tensorflow 2.6, sometimes should use experiment_compile283 def iou_method(soft_weight, y_pred, y_true):284 # pred285 pred_left = y_pred[:, :, 0]286 pred_top = y_pred[:, :, 1]287 pred_right = y_pred[:, :, 2]288 pred_bottom = y_pred[:, :, 3]289 pred_width = pred_left + pred_right290 pred_height = pred_top + pred_bottom291 pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)292 # target293 target_left = y_true[:, :, 0]294 target_top = y_true[:, :, 1]295 target_right = y_true[:, :, 2]296 target_bottom = y_true[:, :, 3]297 target_width = target_left + target_right298 target_height = target_top + target_bottom299 target_area = (target_left + target_right) * (target_top + target_bottom)300 # intersection between target and predict bboxes.301 w_intersect = tf.minimum(pred_left, target_left) + tf.minimum(pred_right, target_right)302 h_intersect = tf.minimum(pred_bottom, target_bottom) + tf.minimum(pred_top, target_top)303 area_intersect = w_intersect * h_intersect304 area_union = target_area + pred_area - area_intersect305 iou = (area_intersect + 1e-7) / (area_union + 1e-7)306 if mode == 'iou':307 iou_loss = -tf.math.log(iou) * soft_weight308 elif mode == 'giou':309 # max close area310 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)311 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)312 close_area = g_w_intersect * g_h_intersect313 g_iou = iou - (close_area - area_union) / close_area314 iou_loss = (1 - g_iou) * soft_weight315 elif mode == 'ciou':316 # without center fixing317 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))318 target_atan = tf.atan(target_width / (target_height + 1e-9))319 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)320 a = v / (1 - iou + v)321 c_iou = iou - 1.0 * a * v322 iou_loss = (1 - c_iou) * soft_weight323 elif mode == 'fciou':324 # full version of CIoU325 g_w_intersect = tf.maximum(pred_left, target_left) + tf.maximum(pred_right, target_right)326 g_h_intersect = tf.maximum(pred_bottom, target_bottom) + tf.maximum(pred_top, target_top)327 max_diagonal = g_h_intersect ** 2 + g_w_intersect ** 2328 target_center_x = target_width * 0.5329 target_center_y = target_height * 0.5330 pred_center_x = target_left - pred_left + pred_width * 0.5331 pred_center_y = target_top - pred_top + pred_height * 0.5332 intersect_diagonal = (target_center_x - pred_center_x) ** 2 + (target_center_y - pred_center_y) ** 2333 pred_atan = tf.atan(pred_width / (pred_height + 1e-9))334 target_atan = tf.atan(target_width / (target_height + 1e-9))335 v = 4.0 * keras.backend.pow((pred_atan - target_atan), 2) / (math.pi ** 2)336 a = v / (1 - iou + v)337 c_iou = iou - 1.0 * a * v - 1.0 * (intersect_diagonal / max_diagonal)338 iou_loss = (1 - c_iou) * soft_weight339 else:340 iou_loss = -tf.math.log(iou) * soft_weight341 return iou_loss342 return iou_mask_343class IoULoss(keras.layers.Layer):344 def __init__(self, mode='fciou', factor=1.0, name='loc_loss', test=False, **kwargs):345 super(IoULoss, self).__init__(dtype='float32', name=name, **kwargs)346 self.mode = mode347 self.factor = factor348 if not test:349 self.fnc = iou_mask_v2(mode, factor)350 else:351 self.fnc = iou_mask_v3(mode, factor)352 def call(self, inputs, **kwargs):353 loss = self.fnc(inputs)354 self.add_loss(loss)355 self.add_metric(loss, name=self.name)356 return loss357 def get_config(self):358 cfg = super(IoULoss, self).get_config()359 cfg.update(360 {'mode': self.mode, 'factor': self.factor}361 )362 return cfg363if __name__ == '__main__':...

Full Screen

Full Screen

metric.py

Source:metric.py Github

copy

Full Screen

1# ---------------------------------------------------------------2# SNIPER: Efficient Multi-scale Training3# Licensed under The Apache-2.0 License [see LICENSE for details]4# Modified from https://github.com/msracver/Deformable-ConvNets5# Modified by Mahyar Najibi6# ---------------------------------------------------------------7import mxnet as mx8import numpy as np9import os10import pickle11def get_rpn_names():12 pred = ['rpn_cls_prob', 'rpn_bbox_loss']13 label = ['rpn_label', 'rpn_bbox_target', 'rpn_bbox_weight']14 return pred, label15def get_mask_names():16 pred = ['mask_prob', 'mask_targets']17 label = []18 return pred, label19def get_rcnn_names(cfg):20 pred = ['rcnn_cls_prob', 'rcnn_bbox_loss']21 label = ['rcnn_label', 'rcnn_bbox_target', 'rcnn_bbox_weight']22 if cfg.TRAIN.ENABLE_OHEM or cfg.TRAIN.END2END:23 pred.append('rcnn_label')24 if cfg.TRAIN.END2END:25 rpn_pred, rpn_label = get_rpn_names()26 pred = rpn_pred + pred27 label = rpn_label28 if cfg.TRAIN.WITH_MASK:29 mask_pred, mask_label = get_mask_names()30 pred += mask_pred31 label += mask_label32 return pred, label33def get_rcnn_names_4vis(cfg):34 pred,label = get_rcnn_names(cfg)35 pred += ['rcnn_bbox_pred', 'rois', 'rcnn_label']36 return pred, label37class RPNAccMetric(mx.metric.EvalMetric):38 def __init__(self):39 super(RPNAccMetric, self).__init__('RPNAcc')40 self.pred, self.label = get_rpn_names()41 def update(self, labels, preds):42 pred = preds[self.pred.index('rpn_cls_prob')]43 label = labels[self.label.index('rpn_label')]44 # pred (b, c, p) or (b, c, h, w)45 pred_label = mx.ndarray.argmax_channel(pred).asnumpy().astype('int32')46 pred_label = pred_label.reshape((pred_label.shape[0], -1))47 # label (b, p)48 label = label.asnumpy().astype('int32')49 # filter with keep_inds50 keep_inds = np.where(label != -1)51 pred_label = pred_label[keep_inds]52 label = label[keep_inds]53 self.sum_metric += np.sum(pred_label.flat == label.flat)54 self.num_inst += len(pred_label.flat)55class RCNNAccMetric(mx.metric.EvalMetric):56 def __init__(self, cfg):57 super(RCNNAccMetric, self).__init__('RCNNAcc')58 self.e2e = cfg.TRAIN.END2END59 self.ohem = cfg.TRAIN.ENABLE_OHEM60 self.pred, self.label = get_rcnn_names(cfg)61 def update(self, labels, preds):62 pred = preds[self.pred.index('rcnn_cls_prob')]63 if self.ohem or self.e2e:64 label = preds[self.pred.index('rcnn_label')]65 else:66 label = labels[self.label.index('rcnn_label')]67 last_dim = pred.shape[-1]68 pred_label = pred.asnumpy().reshape(-1, last_dim).argmax(axis=1).astype('int32')69 label = label.asnumpy().reshape(-1,).astype('int32')70 # filter with keep_inds71 keep_inds = np.where(label != -1)72 pred_label = pred_label[keep_inds]73 label = label[keep_inds]74 self.sum_metric += np.sum(pred_label.flat == label.flat)75 self.num_inst += len(pred_label.flat)76class RCNNAccFgMetric(mx.metric.EvalMetric):77 def __init__(self, cfg):78 super(RCNNAccFgMetric, self).__init__('RCNNFgAcc')79 self.e2e = cfg.TRAIN.END2END80 self.ohem = cfg.TRAIN.ENABLE_OHEM81 self.pred, self.label = get_rcnn_names(cfg)82 def update(self, labels, preds):83 pred = preds[3]84 label = preds[4]85 last_dim = pred.shape[-1]86 pred_label = pred.asnumpy().reshape(-1, last_dim).argmax(axis=1).astype('int32')87 label = label.asnumpy().reshape(-1,).astype('int32')88 # filter with keep_inds89 keep_inds = np.where(label != -1)90 pred_label = pred_label[keep_inds]91 label = label[keep_inds]92 self.sum_metric += np.sum(pred_label.flat == label.flat)93 self.num_inst += len(pred_label.flat)94class MaskLogLossMetric(mx.metric.EvalMetric):95 def __init__(self, config):96 super(MaskLogLossMetric, self).__init__('MaskLogLoss')97 self.pred, self.label = get_rcnn_names(config)98 def update(self, labels, preds):99 pred = preds[self.pred.index('mask_prob')]100 label = preds[self.pred.index('mask_targets')]101 # label (b, p)102 label = label.asnumpy().astype('int32').reshape((-1))103 # pred (b, c, p) or (b, c, h, w) --> (b, p, c) --> (b*p, c)104 pred = pred.asnumpy().reshape((pred.shape[0], pred.shape[1], -1)).transpose((0, 2, 1))105 pred = pred.reshape((label.shape[0], -1))106 # filter with keep_inds107 keep_inds = np.where(label != -1)[0]108 label = label[keep_inds]109 cls = pred[keep_inds, label]110 cls += 1e-14111 cls_loss = -1 * np.log(cls)112 cls_loss = np.sum(cls_loss)113 self.sum_metric += cls_loss114 self.num_inst += label.shape[0]115class RPNLogLossMetric(mx.metric.EvalMetric):116 def __init__(self):117 super(RPNLogLossMetric, self).__init__('RPNLogLoss')118 self.pred, self.label = get_rpn_names()119 def update(self, labels, preds):120 pred = preds[self.pred.index('rpn_cls_prob')]121 label = labels[self.label.index('rpn_label')]122 # label (b, p)123 label = label.asnumpy().astype('int32').reshape((-1))124 # pred (b, c, p) or (b, c, h, w) --> (b, p, c) --> (b*p, c)125 pred = pred.asnumpy().reshape((pred.shape[0], pred.shape[1], -1)).transpose((0, 2, 1))126 pred = pred.reshape((label.shape[0], -1))127 # filter with keep_inds128 keep_inds = np.where(label != -1)[0]129 label = label[keep_inds]130 cls = pred[keep_inds, label]131 cls += 1e-14132 cls_loss = -1 * np.log(cls)133 cls_loss = np.sum(cls_loss)134 self.sum_metric += cls_loss135 self.num_inst += label.shape[0]136class RCNNLogLossMetric(mx.metric.EvalMetric):137 def __init__(self, cfg):138 super(RCNNLogLossMetric, self).__init__('RCNNLogLoss')139 self.e2e = cfg.TRAIN.END2END140 self.ohem = cfg.TRAIN.ENABLE_OHEM141 self.pred, self.label = get_rcnn_names(cfg)142 def update(self, labels, preds):143 pred = preds[self.pred.index('rcnn_cls_prob')]144 if self.ohem or self.e2e:145 label = preds[self.pred.index('rcnn_label')]146 else:147 label = labels[self.label.index('rcnn_label')]148 last_dim = pred.shape[-1]149 pred = pred.asnumpy().reshape(-1, last_dim)150 label = label.asnumpy().reshape(-1,).astype('int32')151 # filter with keep_inds152 keep_inds = np.where(label != -1)[0]153 label = label[keep_inds]154 cls = pred[keep_inds, label]155 cls += 1e-14156 cls_loss = -1 * np.log(cls)157 cls_loss = np.sum(cls_loss)158 self.sum_metric += cls_loss159 self.num_inst += label.shape[0]160class RCNNFgLogLossMetric(mx.metric.EvalMetric):161 def __init__(self, cfg):162 super(RCNNFgLogLossMetric, self).__init__('RCNNFgLogLoss')163 self.e2e = cfg.TRAIN.END2END164 self.ohem = cfg.TRAIN.ENABLE_OHEM165 self.pred, self.label = get_rcnn_names(cfg)166 def update(self, labels, preds):167 pred = preds[3]168 label = preds[4]169 last_dim = pred.shape[-1]170 pred = pred.asnumpy().reshape(-1, last_dim)171 label = label.asnumpy().reshape(-1,).astype('int32')172 # filter with keep_inds173 keep_inds = np.where(label != -1)[0]174 label = label[keep_inds]175 cls = pred[keep_inds, label]176 cls += 1e-14177 cls_loss = -1 * np.log(cls)178 cls_loss = np.sum(cls_loss)179 self.sum_metric += cls_loss180 self.num_inst += label.shape[0]181class RPNL1LossMetric(mx.metric.EvalMetric):182 def __init__(self):183 super(RPNL1LossMetric, self).__init__('RPNL1Loss')184 self.pred, self.label = get_rpn_names()185 def update(self, labels, preds):186 bbox_loss = preds[self.pred.index('rpn_bbox_loss')].asnumpy()187 # calculate num_inst (average on those kept anchors)188 label = labels[self.label.index('rpn_label')].asnumpy()189 num_inst = np.sum(label != -1)190 self.sum_metric += np.sum(bbox_loss)191 self.num_inst += num_inst192class RCNNL1LossMetric(mx.metric.EvalMetric):193 def __init__(self, cfg):194 super(RCNNL1LossMetric, self).__init__('RCNNL1Loss')195 self.e2e = cfg.TRAIN.END2END196 self.ohem = cfg.TRAIN.ENABLE_OHEM197 self.pred, self.label = get_rcnn_names(cfg)198 def update(self, labels, preds):199 bbox_loss = preds[self.pred.index('rcnn_bbox_loss')].asnumpy()200 if self.ohem:201 label = preds[self.pred.index('rcnn_label')].asnumpy()202 else:203 if self.e2e:204 label = preds[self.pred.index('rcnn_label')].asnumpy()205 else:206 label = labels[self.label.index('rcnn_label')].asnumpy()207 # calculate num_inst (average on those kept anchors)208 num_inst = np.sum(label != -1)209 self.sum_metric += np.sum(bbox_loss)210 self.num_inst += num_inst211class RCNNL1LossCRCNNMetric(mx.metric.EvalMetric):212 def __init__(self, cfg):213 super(RCNNL1LossCRCNNMetric, self).__init__('RCNNL1LossCRCNN')214 self.e2e = cfg.TRAIN.END2END215 self.ohem = cfg.TRAIN.ENABLE_OHEM216 self.pred, self.label = get_rcnn_names(cfg)217 self.cfg = cfg218 def update(self, labels, preds):219 bbox_loss = preds[self.pred.index('rcnn_bbox_loss')].asnumpy()220 if self.ohem:221 label = preds[self.pred.index('rcnn_label')].asnumpy()222 else:223 if self.e2e:224 label = preds[self.pred.index('rcnn_label')].asnumpy()225 else:226 label = labels[self.label.index('rcnn_label')].asnumpy()227 num_inst = np.sum(label != -1)228 self.sum_metric += np.sum(bbox_loss)229 self.num_inst += num_inst230class VisMetric(mx.metric.EvalMetric):231 def __init__(self, cfg):232 super(VisMetric, self).__init__('Vis')233 self.freq = cfg.TRAIN.visualization_freq234 self.root_path = cfg.TRAIN.visualization_path235 self.pred, self.label = get_rcnn_names_4vis(cfg)236 self.nGPU = len(cfg.gpus.split(','))237 self.file_name = os.path.basename(os.path.normpath(cfg.output_path))238 def update(self, labels, preds):239 if self.num_inst % self.freq == 0 and self.num_inst%self.nGPU==0:240 pred = preds[self.pred.index('rcnn_cls_prob')].asnumpy()241 bbox_pred = preds[self.pred.index('rcnn_bbox_pred')].asnumpy()242 labels = preds[self.pred.index('rcnn_label')].asnumpy()243 rois = preds[self.pred.index('rois')].asnumpy()244 inds = np.where(rois[:, 0] == 0)[0]245 path = os.path.join(self.root_path, 'dump_preds_{}.pkl'.format(self.file_name))246 with open(path,'wb') as f:247 pickle.dump((pred[0], bbox_pred[inds], rois[inds], labels[inds]),f)248 249 self.num_inst += 1250 self.sum_metric = 0...

Full Screen

Full Screen

metrics.py

Source:metrics.py Github

copy

Full Screen

1"""2定义各类性能指标3"""4from sklearn.metrics import roc_auc_score5def mean(item: list) -> float:6 """7 计算列表中元素的平均值8 :param item: 列表对象9 :return:10 """11 res = sum(item) / len(item) if len(item) > 0 else 012 return res13def accuracy(pred_y, true_y):14 """15 计算二类和多类的准确率16 :param pred_y: 预测结果17 :param true_y: 真实结果18 :return:19 """20 if isinstance(pred_y[0], list):21 pred_y = [item[0] for item in pred_y]22 corr = 023 for i in range(len(pred_y)):24 if pred_y[i] == true_y[i]:25 corr += 126 acc = corr / len(pred_y) if len(pred_y) > 0 else 027 return acc28def binary_auc(pred_y, true_y):29 """30 二类别的auc值31 :param pred_y: 预测结果32 :param true_y: 真实结果33 :return:34 """35 auc = roc_auc_score(true_y, pred_y)36 return auc37def binary_precision(pred_y, true_y, positive=1):38 """39 二类的精确率计算40 :param pred_y: 预测结果41 :param true_y: 真实结果42 :param positive: 正例的索引表示43 :return:44 """45 corr = 046 pred_corr = 047 for i in range(len(pred_y)):48 if pred_y[i] == positive:49 pred_corr += 150 if pred_y[i] == true_y[i]:51 corr += 152 prec = corr / pred_corr if pred_corr > 0 else 053 return prec54def binary_recall(pred_y, true_y, positive=1):55 """56 二类的召回率57 :param pred_y: 预测结果58 :param true_y: 真实结果59 :param positive: 正例的索引表示60 :return:61 """62 corr = 063 true_corr = 064 for i in range(len(pred_y)):65 if true_y[i] == positive:66 true_corr += 167 if pred_y[i] == true_y[i]:68 corr += 169 rec = corr / true_corr if true_corr > 0 else 070 return rec71def binary_f_beta(pred_y, true_y, beta=1.0, positive=1):72 """73 二类的f beta值74 :param pred_y: 预测结果75 :param true_y: 真实结果76 :param beta: beta值77 :param positive: 正例的索引表示78 :return:79 """80 precision = binary_precision(pred_y, true_y, positive)81 recall = binary_recall(pred_y, true_y, positive)82 try:83 f_b = (1 + beta * beta) * precision * recall / (beta * beta * precision + recall)84 except:85 f_b = 086 return f_b87def multi_precision(pred_y, true_y, labels):88 """89 多类的精确率90 :param pred_y: 预测结果91 :param true_y: 真实结果92 :param labels: 标签列表93 :return:94 """95 if isinstance(pred_y[0], list):96 pred_y = [item[0] for item in pred_y]97 precisions = [binary_precision(pred_y, true_y, label) for label in labels]98 prec = mean(precisions)99 return prec100def multi_recall(pred_y, true_y, labels):101 """102 多类的召回率103 :param pred_y: 预测结果104 :param true_y: 真实结果105 :param labels: 标签列表106 :return:107 """108 if isinstance(pred_y[0], list):109 pred_y = [item[0] for item in pred_y]110 recalls = [binary_recall(pred_y, true_y, label) for label in labels]111 rec = mean(recalls)112 return rec113def multi_f_beta(pred_y, true_y, labels, beta=1.0):114 """115 多类的f beta值116 :param pred_y: 预测结果117 :param true_y: 真实结果118 :param labels: 标签列表119 :param beta: beta值120 :return:121 """122 if isinstance(pred_y[0], list):123 pred_y = [item[0] for item in pred_y]124 f_betas = [binary_f_beta(pred_y, true_y, beta, label) for label in labels]125 f_beta = mean(f_betas)126 return f_beta127def get_binary_metrics(pred_y, true_y, f_beta=1.0):128 """129 得到二分类的性能指标130 :param pred_y:131 :param true_y:132 :param f_beta:133 :return:134 """135 acc = accuracy(pred_y, true_y)136 auc = binary_auc(pred_y, true_y)137 recall = binary_recall(pred_y, true_y)138 precision = binary_precision(pred_y, true_y)139 f_beta = binary_f_beta(pred_y, true_y, f_beta)140 return acc, auc, recall, precision, f_beta141def get_multi_metrics(pred_y, true_y, labels, f_beta=1.0):142 """143 得到多分类的性能指标144 :param pred_y:145 :param true_y:146 :param labels:147 :param f_beta:148 :return:149 """150 acc = accuracy(pred_y, true_y)151 recall = multi_recall(pred_y, true_y, labels)152 precision = multi_precision(pred_y, true_y, labels)153 f_beta_ = multi_f_beta(pred_y, true_y, labels, f_beta)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful