def __init__(self, config, batch_size, num_classes, target_means=(0., 0., 0., 0.), target_stds=(0.1, 0.1, 0.2, 0.2) ): super(RcnnMask, self).__init__() cfg = config self.rcnn_loss_mask_fb_weight = Tensor(np.array(cfg.rcnn_loss_mask_fb_weight).astype(np.float16)) self.rcnn_mask_out_channels = cfg.rcnn_mask_out_channels self.target_means = target_means self.target_stds = target_stds self.num_classes = num_classes self.in_channels = cfg.rcnn_in_channels self.fpn_mask = FpnMask(self.in_channels, self.rcnn_mask_out_channels, self.num_classes) self.logicaland = P.LogicalAnd() self.loss_mask = P.SigmoidCrossEntropyWithLogits() self.onehot = P.OneHot() self.greater = P.Greater() self.cast = P.Cast() self.sum_loss = P.ReduceSum() self.tile = P.Tile() self.expandims = P.ExpandDims() self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.num_bboxes = cfg.num_expected_pos_stage2 * batch_size rmv_first = np.ones((self.num_bboxes, self.num_classes)) rmv_first[:, 0] = np.zeros((self.num_bboxes,)) self.rmv_first_tensor = Tensor(rmv_first.astype(np.float16)) self.mean_loss = P.ReduceMean()
def __init__(self, config, batch_size, in_channels, feat_channels, num_anchors, cls_out_channels): super(RPN, self).__init__() cfg_rpn = config self.dtype = np.float32 self.ms_type = mstype.float32 self.num_bboxes = cfg_rpn.num_bboxes self.slice_index = () self.feature_anchor_shape = () self.slice_index += (0, ) index = 0 for shape in cfg_rpn.feature_shapes: self.slice_index += (self.slice_index[index] + shape[0] * shape[1] * num_anchors, ) self.feature_anchor_shape += (shape[0] * shape[1] * num_anchors * batch_size, ) index += 1 self.num_anchors = num_anchors self.batch_size = batch_size self.test_batch_size = cfg_rpn.test_batch_size self.num_layers = 5 self.real_ratio = Tensor(np.ones((1, 1)).astype(self.dtype)) self.rpn_convs_list = nn.layer.CellList( self._make_rpn_layer(self.num_layers, in_channels, feat_channels, num_anchors, cls_out_channels)) self.transpose = P.Transpose() self.reshape = P.Reshape() self.concat = P.Concat(axis=0) self.fill = P.Fill() self.placeh1 = Tensor(np.ones((1, )).astype(self.dtype)) self.trans_shape = (0, 2, 3, 1) self.reshape_shape_reg = (-1, 4) self.reshape_shape_cls = (-1, ) self.rpn_loss_reg_weight = Tensor( np.array(cfg_rpn.rpn_loss_reg_weight).astype(self.dtype)) self.rpn_loss_cls_weight = Tensor( np.array(cfg_rpn.rpn_loss_cls_weight).astype(self.dtype)) self.num_expected_total = Tensor( np.array(cfg_rpn.num_expected_neg * self.batch_size).astype( self.dtype)) self.num_bboxes = cfg_rpn.num_bboxes self.get_targets = BboxAssignSample(cfg_rpn, self.batch_size, self.num_bboxes, False) self.CheckValid = P.CheckValid() self.sum_loss = P.ReduceSum() self.loss_cls = P.SigmoidCrossEntropyWithLogits() self.loss_bbox = P.SmoothL1Loss(beta=1.0 / 9.0) self.squeeze = P.Squeeze() self.cast = P.Cast() self.tile = P.Tile() self.zeros_like = P.ZerosLike() self.loss = Tensor(np.zeros((1, )).astype(self.dtype)) self.clsloss = Tensor(np.zeros((1, )).astype(self.dtype)) self.regloss = Tensor(np.zeros((1, )).astype(self.dtype))
def __init__(self, network, l2_coef=1e-6): super(NetWithLossClass, self).__init__(auto_prefix=False) self.loss = P.SigmoidCrossEntropyWithLogits() self.network = network self.l2_coef = l2_coef self.Square = P.Square() self.ReduceMean_false = P.ReduceMean(keep_dims=False) self.ReduceSum_false = P.ReduceSum(keep_dims=False)
def __init__(self, network, config): super(NetWithLossClass, self).__init__(auto_prefix=False) self.network = network self.l2_coef = config.l2_coef self.loss = P.SigmoidCrossEntropyWithLogits() self.square = P.Square().set_strategy(((1, get_group_size()),)) self.reduceMean_false = P.ReduceMean(keep_dims=False) self.reduceSum_false = P.ReduceSum(keep_dims=False)
def __init__(self, config, batch_size, in_channels, feat_channels, num_anchors, cls_out_channels): super(RPN, self).__init__() cfg_rpn = config self.cfg = config self.num_bboxes = cfg_rpn.num_bboxes self.feature_anchor_shape = cfg_rpn.feature_shapes self.feature_anchor_shape = self.feature_anchor_shape[0] * \ self.feature_anchor_shape[1] * num_anchors * batch_size self.num_anchors = num_anchors self.batch_size = batch_size self.test_batch_size = cfg_rpn.test_batch_size self.num_layers = 1 self.real_ratio = Tensor(np.ones((1, 1)).astype(np.float16)) self.use_sigmoid_cls = config.use_sigmoid_cls if config.use_sigmoid_cls: self.reshape_shape_cls = (-1, ) self.loss_cls = P.SigmoidCrossEntropyWithLogits() cls_out_channels = 1 else: self.reshape_shape_cls = (-1, cls_out_channels) self.loss_cls = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="none") self.rpn_convs_list = self._make_rpn_layer(self.num_layers, in_channels, feat_channels,\ num_anchors, cls_out_channels) self.transpose = P.Transpose() self.reshape = P.Reshape() self.concat = P.Concat(axis=0) self.fill = P.Fill() self.placeh1 = Tensor(np.ones((1, )).astype(np.float16)) self.trans_shape = (0, 2, 3, 1) self.reshape_shape_reg = (-1, 4) self.softmax = nn.Softmax() self.rpn_loss_reg_weight = Tensor( np.array(cfg_rpn.rpn_loss_reg_weight).astype(np.float16)) self.rpn_loss_cls_weight = Tensor( np.array(cfg_rpn.rpn_loss_cls_weight).astype(np.float16)) self.num_expected_total = Tensor( np.array(cfg_rpn.num_expected_neg * self.batch_size).astype( np.float16)) self.num_bboxes = cfg_rpn.num_bboxes self.get_targets = BboxAssignSample(cfg_rpn, self.batch_size, self.num_bboxes, False) self.CheckValid = P.CheckValid() self.sum_loss = P.ReduceSum() self.loss_bbox = P.SmoothL1Loss(beta=1.0 / 9.0) self.squeeze = P.Squeeze() self.cast = P.Cast() self.tile = P.Tile() self.zeros_like = P.ZerosLike() self.loss = Tensor(np.zeros((1, )).astype(np.float16)) self.clsloss = Tensor(np.zeros((1, )).astype(np.float16)) self.regloss = Tensor(np.zeros((1, )).astype(np.float16)) self.print = P.Print()
def __init__(self, gamma=2.0, alpha=0.25): super(SigmoidFocalClassificationLoss, self).__init__() self.sigmiod_cross_entropy = P.SigmoidCrossEntropyWithLogits() self.sigmoid = P.Sigmoid() self.pow = P.Pow() self.onehot = P.OneHot() self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.gamma = gamma self.alpha = alpha
def __init__(self, config, batch_size, num_classes, target_means=(0., 0., 0., 0.), target_stds=(0.1, 0.1, 0.2, 0.2)): super(RcnnCls, self).__init__() cfg = config self.rcnn_loss_cls_weight = Tensor( np.array(cfg.rcnn_loss_cls_weight).astype(np.float16)) self.rcnn_loss_reg_weight = Tensor( np.array(cfg.rcnn_loss_reg_weight).astype(np.float16)) self.rcnn_fc_out_channels = cfg.rcnn_fc_out_channels self.target_means = target_means self.target_stds = target_stds self.num_classes = num_classes self.in_channels = cfg.rcnn_in_channels self.train_batch_size = batch_size self.test_batch_size = cfg.test_batch_size self.fpn_cls = FpnCls(self.in_channels, self.rcnn_fc_out_channels, self.num_classes, cfg.roi_layer["out_size"]) self.relu = P.ReLU() self.logicaland = P.LogicalAnd() self.loss_cls = P.SoftmaxCrossEntropyWithLogits() self.loss_bbox = P.SmoothL1Loss(beta=1.0) self.loss_mask = P.SigmoidCrossEntropyWithLogits() self.reshape = P.Reshape() self.onehot = P.OneHot() self.greater = P.Greater() self.cast = P.Cast() self.sum_loss = P.ReduceSum() self.tile = P.Tile() self.expandims = P.ExpandDims() self.gather = P.GatherNd() self.argmax = P.ArgMaxWithValue(axis=1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.value = Tensor(1.0, mstype.float16) self.num_bboxes = (cfg.num_expected_pos_stage2 + cfg.num_expected_neg_stage2) * batch_size rmv_first = np.ones((self.num_bboxes, self.num_classes)) rmv_first[:, 0] = np.zeros((self.num_bboxes, )) self.rmv_first_tensor = Tensor(rmv_first.astype(np.float16)) self.num_bboxes_test = cfg.rpn_max_num * cfg.test_batch_size
def __init__(self, network, config): super(NetWithLossClass, self).__init__(auto_prefix=False) host_device_mix = bool(config.host_device_mix) parameter_server = bool(config.parameter_server) parallel_mode = context.get_auto_parallel_context("parallel_mode") is_auto_parallel = parallel_mode in (ParallelMode.SEMI_AUTO_PARALLEL, ParallelMode.AUTO_PARALLEL) self.no_l2loss = (is_auto_parallel if (host_device_mix or config.field_slice) else parameter_server) self.network = network self.l2_coef = config.l2_coef self.loss = P.SigmoidCrossEntropyWithLogits() self.square = P.Square() self.reduceMean_false = P.ReduceMean(keep_dims=False) if is_auto_parallel: self.reduceMean_false.add_prim_attr("cross_batch", True) self.reduceSum_false = P.ReduceSum(keep_dims=False)
def __init__(self, scale, config): super(YoloLossBlock, self).__init__() self.config = config if scale == 's': idx = (0, 1, 2) elif scale == 'm': idx = (3, 4, 5) elif scale == 'l': idx = (6, 7, 8) else: raise KeyError("Invalid scale value for DetectionBlock") self.anchors = Tensor([self.config.anchor_scales[i] for i in idx], ms.float32) self.ignore_threshold = Tensor(self.config.ignore_threshold, ms.float32) self.concat = P.Concat(axis=-1) self.iou = Iou() self.cross_entropy = P.SigmoidCrossEntropyWithLogits() self.reduce_sum = P.ReduceSum() self.reduce_max = P.ReduceMax(keep_dims=False) self.input_shape = Tensor(tuple(config.img_shape[::-1]), ms.float32)
def __init__(self): super(NetSigmoidCrossEntropyWithLogits, self).__init__() self.loss = P.SigmoidCrossEntropyWithLogits()
def __init__(self, num_classes, anchors, anchors_mask, reduction=32, seen=0, coord_scale=1.0, no_object_scale=1.0, object_scale=1.0, class_scale=1.0, thresh=0.5, head_idx=0.0): super(YoloLoss, self).__init__() self.num_classes = num_classes self.num_anchors = len(anchors_mask) self.anchor_step = len(anchors[0]) # each scale has step anchors self.anchors = np.array(anchors, dtype=np.float32) / reduction # scale every anchor for every scale self.tensor_anchors = Tensor(self.anchors, mstype.float32) self.anchors_mask = anchors_mask anchors_w = [] anchors_h = [] for i in range(len(anchors_mask)): anchors_w.append(self.anchors[self.anchors_mask[i]][0]) anchors_h.append(self.anchors[self.anchors_mask[i]][1]) self.anchors_w = Tensor(np.array(anchors_w).reshape(len(self.anchors_mask), 1)) self.anchors_h = Tensor(np.array(anchors_h).reshape(len(self.anchors_mask), 1)) self.reduction = reduction self.seen = seen self.head_idx = head_idx self.zero = Tensor(0) self.coord_scale = coord_scale self.no_object_scale = no_object_scale self.object_scale = object_scale self.class_scale = class_scale self.thresh = thresh self.info = {'avg_iou': 0, 'class': 0, 'obj': 0, 'no_obj': 0, 'recall50': 0, 'recall75': 0, 'obj_cur': 0, 'obj_all': 0, 'coord_xy': 0, 'coord_wh': 0} self.shape = P.Shape() self.reshape = P.Reshape() self.sigmoid = P.Sigmoid() self.zeros_like = P.ZerosLike() self.concat0 = P.Concat(0) self.concat0_2 = P.Concat(0) self.concat0_3 = P.Concat(0) self.concat0_4 = P.Concat(0) self.concat1 = P.Concat(1) self.concat1_2 = P.Concat(1) self.concat1_3 = P.Concat(1) self.concat1_4 = P.Concat(1) self.concat2 = P.Concat(2) self.concat2_2 = P.Concat(2) self.concat2_3 = P.Concat(2) self.concat2_4 = P.Concat(2) self.tile = P.Tile() self.transpose = P.Transpose() self.cast = P.Cast() self.exp = P.Exp() self.sum = P.ReduceSum() self.smooth_l1_loss = P.SmoothL1Loss() self.bce = P.SigmoidCrossEntropyWithLogits() self.ce = P.SoftmaxCrossEntropyWithLogits() self.pt_linspace = PtLinspace() self.one_hot = nn.OneHot(-1, self.num_classes, 1.0, 0.0) self.squeeze_2 = P.Squeeze(2) self.reduce_sum = P.ReduceSum() self.select = P.Select() self.iou = P.IOU()
def __init__(self, mul_weight, strategy1=None, strategy2=None): super().__init__() self.mul = P.Mul().shard(strategy1) self.loss = P.SigmoidCrossEntropyWithLogits().shard(strategy2) self.mul_weight = Parameter(mul_weight, "w1")
def __init__(self): super(ClassLoss, self).__init__() self.cross_entropy = P.SigmoidCrossEntropyWithLogits() self.reduce_sum = P.ReduceSum()
def __init__(self): super(Loss, self).__init__() self.sigmoid_cross_entropy_with_logits = P.SigmoidCrossEntropyWithLogits( ) self.reduce_mean = P.ReduceMean(keep_dims=False)
def __init__(self): super(Net, self).__init__() self.sigmoid_cross_entropy_with_logits = P.SigmoidCrossEntropyWithLogits( )
'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))] }), ('Softmax', { 'block': P.Softmax(), 'desc_inputs': [[5, 5]], 'desc_bprop': [[5, 5]]}), ('DepthwiseConv2dNative_1', { 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2), 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]], 'desc_bprop': [[10, 32, 16, 16]]}), ('DepthwiseConv2dNative_2', { 'block': P.DepthwiseConv2dNative(1, (3, 3), pad_mode="same", pad=0, stride=1), 'desc_inputs': [[2592, 2048, 4, 4], [1, 2048, 3, 3]], 'desc_bprop': [[2592, 2048, 4, 4]]}), ('SigmoidCrossEntropyWithLogits', { 'block': P.SigmoidCrossEntropyWithLogits(), 'desc_inputs': [[128, 10], [128, 10]], 'desc_bprop': [[128, 10]]}), ('Pad', { 'block': P.Pad(((1, 2), (2, 3))), 'desc_inputs': [[7, 7]], 'desc_bprop': [[10, 12]]}), ('BinaryCrossEntropy', { 'block': P.BinaryCrossEntropy(), 'desc_inputs': [[1, 2, 3], [1, 2, 3], [1, 2, 3]], 'desc_bprop': []}), ('SparseApplyAdagrad', { 'block': P.SparseApplyAdagrad(0.5), 'desc_inputs': [[3, 3], [3, 3], [3, 3], Tensor(np.ones((3,), np.int32))], 'desc_bprop': [3, 3], 'skip': ['backward']}),
def __init__(self): super(SigmoidCrossEntropyWithLogits, self).__init__() self.cross_entropy = P.SigmoidCrossEntropyWithLogits()