def __init__(self): super(NetOneHot, self).__init__() self.on_value = 2.0 self.off_value = 3.0 self.depth_1 = 6 self.one_hot_1 = nn.OneHot(-1, self.depth_1, self.on_value, self.off_value) self.depth_2 = 4 self.one_hot_2 = nn.OneHot(0, self.depth_1, self.on_value, self.off_value) self.one_hot_3 = nn.OneHot(0, self.depth_2, self.on_value, self.off_value) self.one_hot_4 = nn.OneHot(1, self.depth_1, self.on_value, self.off_value)
def __init__(self, num_classes): super(Encoder, self).__init__() self.fc1 = nn.Dense(1024 + num_classes, 400) self.relu = nn.ReLU() self.flatten = nn.Flatten() self.concat = P.Concat(axis=1) self.one_hot = nn.OneHot(depth=num_classes)
def __init__(self, backbone, config): super(WithLossCell, self).__init__(auto_prefix=False) self._backbone = backbone self.batch_size = config.batch_size self.onehot = nn.OneHot(depth=config.ch_vocab_size) self._loss_fn = NLLLoss() self.max_len = config.max_seq_length self.squeeze = P.Squeeze() self.cast = P.Cast() self.argmax = P.ArgMaxWithValue(axis=1, keep_dims=True) self.print = P.Print()
def __init__(self, num, ignore_label): super(OhemLoss, self).__init__() self.mul = P.Mul() self.shape = P.Shape() self.one_hot = nn.OneHot(-1, num, 1.0, 0.0) self.squeeze = P.Squeeze() self.num = num self.cross_entropy = P.SoftmaxCrossEntropyWithLogits() self.mean = P.ReduceMean() self.select = P.Select() self.reshape = P.Reshape() self.cast = P.Cast() self.not_equal = P.NotEqual() self.equal = P.Equal() self.reduce_sum = P.ReduceSum(keep_dims=False) self.fill = P.Fill() self.transpose = P.Transpose() self.ignore_label = ignore_label self.loss_weight = 1.0
def __init__(self, length, depth, max_relative_position, initializer_range, use_one_hot_embeddings=False): super(RelaPosEmbeddingsGenerator, self).__init__() self.depth = depth self.vocab_size = max_relative_position * 2 + 1 self.use_one_hot_embeddings = use_one_hot_embeddings self.embeddings_table = Parameter(initializer( TruncatedNormal(initializer_range), [self.vocab_size, self.depth]), name='embeddings_for_position') self.relative_positions_matrix = RelaPosMatrixGenerator( length=length, max_relative_position=max_relative_position) self.reshape = P.Reshape() self.one_hot = nn.OneHot(depth=self.vocab_size) self.shape = P.Shape() self.gather = P.GatherV2() # index_select self.matmul = P.BatchMatMul()
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()