Beispiel #1
0
    def __init__(self,
                 num_sampled,
                 num_classes,
                 num_true=1,
                 sampled_values=None,
                 remove_accidental_hits=True,
                 seed=0,
                 reduction='none'):
        super(SampledSoftmaxLoss, self).__init__(reduction)

        if num_true < 1:
            raise ValueError(f"num_true {num_true} is less than 1.")
        if seed < 0:
            raise ValueError(f"seed {seed} is less than 0.")
        if num_sampled > num_classes:
            raise ValueError(
                f"num_sampled {num_sampled} is great than num_classes {num_classes}."
            )
        if num_true > num_classes:
            raise ValueError(
                f"num_true {num_true} is great than num_classes {num_classes}."
            )
        if sampled_values is not None:
            if not isinstance(sampled_values, (list, tuple)):
                raise TypeError(
                    f"sampled_values {sampled_values} is not a list.")
            if len(sampled_values) != 3:
                raise ValueError(
                    f"sampled_values size {len(sampled_values)} is not 3.")

        self.num_sampled = num_sampled
        self.num_classes = num_classes
        self.num_true = num_true
        self.sampled_values = sampled_values
        self.remove_accidental_hits = remove_accidental_hits
        self.seed = seed
        self.sampler = P.LogUniformCandidateSampler(num_true, num_sampled,
                                                    True, num_classes, seed)
        self.cast = P.Cast()
        self.reshape = P.Reshape()
        self.shape = P.Shape()
        self.exp = P.Exp()
        self.log = P.Log()
        self.slice_op = P.Slice()
        self.matmul = P.MatMul(False, True)
        self.gather_v2 = P.Gather()
        self.reduce_max_true = P.ReduceMax(True)
        self.reduce_sum = P.ReduceSum()
        self.reduce_sum_true = P.ReduceSum(True)
        self.concat_dim0 = P.Concat(0)
        self.concat_dim1 = P.Concat(1)
        self.ones_like = P.OnesLike()
        self.zeros_like = P.ZerosLike()
        self.mul = P.Mul()
        self.expand_dims = P.ExpandDims()
        self.dtype = P.DType()
        self.compute_accidental_hits = P.ComputeAccidentalHits(num_true)
        self.scatter_nd = P.ScatterNd()
    def __init__(self, config, batch_size, num_bboxes, add_gt_as_proposals):
        super(BboxAssignSample, self).__init__()
        cfg = config
        self.batch_size = batch_size

        self.neg_iou_thr = Tensor(cfg.neg_iou_thr, mstype.float16)
        self.pos_iou_thr = Tensor(cfg.pos_iou_thr, mstype.float16)
        self.min_pos_iou = Tensor(cfg.min_pos_iou, mstype.float16)
        self.zero_thr = Tensor(0.0, mstype.float16)

        self.num_bboxes = num_bboxes
        self.num_gts = cfg.num_gts
        self.num_expected_pos = cfg.num_expected_pos
        self.num_expected_neg = cfg.num_expected_neg
        self.add_gt_as_proposals = add_gt_as_proposals

        if self.add_gt_as_proposals:
            self.label_inds = Tensor(np.arange(1, self.num_gts + 1))

        self.concat = P.Concat(axis=0)
        self.max_gt = P.ArgMaxWithValue(axis=0)
        self.max_anchor = P.ArgMaxWithValue(axis=1)
        self.sum_inds = P.ReduceSum()
        self.iou = P.IOU()
        self.greaterequal = P.GreaterEqual()
        self.greater = P.Greater()
        self.select = P.Select()
        self.gatherND = P.GatherNd()
        self.squeeze = P.Squeeze()
        self.cast = P.Cast()
        self.logicaland = P.LogicalAnd()
        self.less = P.Less()
        self.random_choice_with_mask_pos = P.RandomChoiceWithMask(self.num_expected_pos)
        self.random_choice_with_mask_neg = P.RandomChoiceWithMask(self.num_expected_neg)
        self.reshape = P.Reshape()
        self.equal = P.Equal()
        self.bounding_box_encode = BoundingBoxEncode()
        self.scatterNdUpdate = P.ScatterNdUpdate()
        self.scatterNd = P.ScatterNd()
        self.logicalnot = P.LogicalNot()
        self.tile = P.Tile()
        self.zeros_like = P.ZerosLike()

        self.assigned_gt_inds = Tensor(np.array(-1 * np.ones(num_bboxes), dtype=np.int32))
        self.assigned_gt_zeros = Tensor(np.array(np.zeros(num_bboxes), dtype=np.int32))
        self.assigned_gt_ones = Tensor(np.array(np.ones(num_bboxes), dtype=np.int32))
        self.assigned_gt_ignores = Tensor(np.array(-1 * np.ones(num_bboxes), dtype=np.int32))
        self.assigned_pos_ones = Tensor(np.array(np.ones(self.num_expected_pos), dtype=np.int32))

        self.check_neg_mask = Tensor(np.array(np.ones(self.num_expected_neg - self.num_expected_pos), dtype=np.bool))
        self.range_pos_size = Tensor(np.arange(self.num_expected_pos).astype(np.float16))
        self.check_gt_one = Tensor(np.array(-1 * np.ones((self.num_gts, 4)), dtype=np.float16))
        self.check_anchor_two = Tensor(np.array(-2 * np.ones((self.num_bboxes, 4)), dtype=np.float16))
        self.print = P.Print()
Beispiel #3
0
 def __init__(self,
              temperature=0.07,
              contrast_mode='all',
              base_temperature=0.07):
     super(SupConLoss, self).__init__()
     self.temperature = temperature
     self.contrast_mode = contrast_mode
     self.base_temperature = base_temperature
     self.normalize = P.L2Normalize(axis=2)
     self.eye = P.Eye()
     self.unbind = P.Unstack(axis=1)
     self.cat = P.Concat(axis=0)
     self.matmul = P.MatMul()
     self.div = P.Div()
     self.transpose = P.Transpose()
     self.maxes = P.ArgMaxWithValue(axis=1, keep_dims=True)
     self.tile = P.Tile()
     self.scatter = P.ScatterNd()
     self.oneslike = P.OnesLike()
     self.exp = P.Exp()
     self.sum = P.ReduceSum(keep_dims=True)
     self.log = P.Log()
     self.reshape = P.Reshape()
     self.mean = P.ReduceMean()
Beispiel #4
0
 def __init__(self):
     super(Net, self).__init__()
     self.scatternd = P.ScatterNd()
Beispiel #5
0
     'desc_inputs': [[256, 4], [256, 4]],
     'desc_bprop': [[256, 4]],
     'skip': ['backward']}),
 ('GatherNd', {
     'block': P.GatherNd(),
     'desc_inputs': (Tensor(np.ones((1, 3, 6, 6), np.float32)),
                     Tensor(np.ones((2, 4), np.int32))),
     'desc_bprop': [[2]]}),
 ('ScatterNdUpdate', {
     'block': P.ScatterNdUpdate(),
     'desc_inputs': (Tensor(np.ones((2, 3), np.float32)),
                     Tensor(np.ones((2, 2), np.int32)),
                     Tensor(np.ones((2,), np.float32))),
     'desc_bprop': [[2, 3]]}),
 ('ScatterNd', {
     'block': P.ScatterNd(),
     'desc_const': [(3, 3)],
     'desc_inputs': (Tensor(np.ones((2, 2), np.int32)),
                     Tensor(np.ones((2,), np.int32))),
     'desc_bprop': [[3, 3]]}),
 ('SmoothL1Loss', {
     'block': P.SmoothL1Loss(),
     'desc_inputs': [[256, 4], [256, 4]],
     'desc_bprop': [[256, 4]]}),
 ('IOU', {
     'block': P.IOU(),
     'desc_inputs': [Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16))],
     'desc_bprop': [[128, 256]]}),
 ('Summary', {
     'block': SummaryNet(),
     'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
Beispiel #6
0
 def __init__(self, _shape):
     super(Net, self).__init__()
     self.shape = _shape
     self.scatternd = P.ScatterNd()
Beispiel #7
0
     P.GatherNd(),
     'desc_inputs': (Tensor(np.ones(
         (1, 3, 6, 6), np.float32)), Tensor(np.ones((2, 4), np.int32))),
     'desc_bprop': [[2]]
 }),
 ('ScatterNdUpdate', {
     'block':
     P.ScatterNdUpdate(),
     'desc_inputs': (Tensor(np.ones(
         (2, 3), np.float32)), Tensor(np.ones(
             (2, 2), np.int32)), Tensor(np.ones((2, ), np.float32))),
     'desc_bprop': [[2, 3]]
 }),
 ('ScatterNd', {
     'block':
     P.ScatterNd(),
     'desc_const': [(3, 3)],
     'desc_inputs': (Tensor(np.ones(
         (2, 2), np.int32)), Tensor(np.ones((2, ), np.int32))),
     'desc_bprop': [[3, 3]]
 }),
 ('SmoothL1Loss', {
     'block': P.SmoothL1Loss(),
     'desc_inputs': [[256, 4], [256, 4]],
     'desc_bprop': [[256, 4]]
 }),
 ('IOU', {
     'block':
     P.IOU(),
     'desc_inputs': [
         Tensor(np.ones((256, 4), np.float16)),
Beispiel #8
0
    def __init__(self,
                 in_channel,
                 out_channel,
                 rate,
                 stride=1,
                 num=0,
                 index=None):
        super(ResidualBlock, self).__init__()

        channel = out_channel // self.expansion
        prune_channel = math.ceil(channel * rate)
        self.conv1 = _conv1x1(in_channel, prune_channel, stride=1)
        self.bn1 = _bn(prune_channel)

        self.conv2 = _conv3x3(prune_channel, prune_channel, stride=stride)
        self.bn2 = _bn(prune_channel)

        self.conv3 = _conv1x1(prune_channel,
                              math.ceil(channel * rate * self.expansion),
                              stride=1)
        self.bn3 = _bn_last(math.ceil(channel * rate * self.expansion))

        self.relu = nn.ReLU()

        self.down_sample = False

        if stride != 1 or in_channel != out_channel:
            self.down_sample = True
        self.down_sample_layer = None

        if self.down_sample:
            self.down_sample_layer = nn.SequentialCell(
                [_conv1x1(in_channel, out_channel, stride),
                 _bn(out_channel)])
        self.add = P.TensorAdd()

        self.op = P.ScatterNd()
        self.transpose = P.Transpose()
        self.idx = None
        self.shape = None
        if out_channel == 256:
            self.shape = (out_channel, 1, 56, 56)
            if num == 0:
                self.idx = index[0]
            elif num == 1:
                self.idx = index[1]
            elif num == 2:
                self.idx = index[2]
        elif out_channel == 512:
            self.shape = (out_channel, 1, 28, 28)
            if num == 0:
                self.idx = index[3]
            elif num == 1:
                self.idx = index[4]
            elif num == 2:
                self.idx = index[5]
            elif num == 3:
                self.idx = index[6]
        elif out_channel == 1024:
            self.shape = (out_channel, 1, 14, 14)
            if num == 0:
                self.idx = index[7]
            elif num == 1:
                self.idx = index[8]
            elif num == 2:
                self.idx = index[9]
            elif num == 3:
                self.idx = index[10]
            elif num == 4:
                self.idx = index[11]
            elif num == 5:
                self.idx = index[12]
        elif out_channel == 2048:
            self.shape = (out_channel, 1, 7, 7)
            if num == 0:
                self.idx = index[13]
            elif num == 1:
                self.idx = index[14]
            elif num == 2:
                self.idx = index[15]