Exemple #1
0
    def construct(self, box_xy, box_wh, box_confidence, box_probs,
                  image_shape):
        batch_size = F.shape(box_xy)[0]
        x = box_xy[:, :, :, :, 0:1]
        y = box_xy[:, :, :, :, 1:2]
        box_yx = P.Concat(-1)((y, x))
        w = box_wh[:, :, :, :, 0:1]
        h = box_wh[:, :, :, :, 1:2]
        box_hw = P.Concat(-1)((h, w))

        new_shape = P.Round()(image_shape *
                              P.ReduceMin()(self.input_shape / image_shape))
        offset = (self.input_shape - new_shape) / 2.0 / self.input_shape
        scale = self.input_shape / new_shape
        box_yx = (box_yx - offset) * scale
        box_hw = box_hw * scale

        box_min = box_yx - box_hw / 2.0
        box_max = box_yx + box_hw / 2.0
        boxes = P.Concat(-1)(
            (box_min[:, :, :, :, 0:1], box_min[:, :, :, :, 1:2],
             box_max[:, :, :, :, 0:1], box_max[:, :, :, :, 1:2]))
        image_scale = P.Tile()(image_shape, (1, 2))
        boxes = boxes * image_scale
        boxes = F.reshape(boxes, (batch_size, -1, 4))
        boxes_scores = box_confidence * box_probs
        boxes_scores = F.reshape(boxes_scores,
                                 (batch_size, -1, self.num_classes))
        return boxes, boxes_scores
 def construct(self, x, query_hidden_state, attention_mask, layer_past=None):
     original_shape = F.shape(x)
     x = F.reshape(x, (-1, original_shape[-1]))
     query_hidden_state = F.reshape(query_hidden_state, (-1, original_shape[-1]))
     query = self.dense1(query_hidden_state)
     key = self.dense2(x)
     value = self.dense3(x)
     query = self.transpose(
         F.reshape(
             query,
             (-1, original_shape[1], self.n_head, self.size_per_head)),
         (0, 2, 1, 3))
     key = self.transpose(
         F.reshape(
             key, (-1, original_shape[1], self.n_head, self.size_per_head)),
         (0, 2, 3, 1))
     value = self.transpose(
         F.reshape(
             value,
             (-1, original_shape[1], self.n_head, self.size_per_head)),
         (0, 2, 1, 3))
     if self.use_past:
         past_value = layer_past[1]
         past_key = self.transpose(layer_past[0], (0, 1, 3, 2))
         key = self.concat_k((past_key, key))
         value = self.concat_v(past_value, value)
     layer_present = P.Pack()([self.transpose(key, (0, 1, 3, 2)), value])
     attention = self._attn(query, key, value, attention_mask)
     attention_merge = self.merge_heads(attention)
     output = self.projection(attention_merge)
     output = self.dropout(output)
     return output, layer_present
Exemple #3
0
    def construct(self, pred_label, gt_label, num_matched_boxes):
        gt_label = F.cast(gt_label, mstype.int32)
        mask = F.cast(self.less(0, gt_label), mstype.float32)
        gt_label_shape = F.shape(gt_label)
        pred_label = F.reshape(pred_label, (-1, self.num_classes))
        gt_label = F.reshape(gt_label, (-1, ))
        cross_entropy = self.cross_entropy(pred_label, gt_label)
        cross_entropy = F.reshape(cross_entropy, gt_label_shape)

        # Hard example mining
        num_matched_boxes = F.reshape(num_matched_boxes, (-1, ))
        neg_masked_cross_entropy = F.cast(cross_entropy * (1 - mask),
                                          mstype.float16)
        _, loss_idx = self.sort_descend(neg_masked_cross_entropy,
                                        self.num_boxes)
        _, relative_position = self.sort(F.cast(loss_idx, mstype.float16),
                                         self.num_boxes)
        num_neg_boxes = self.minimum(num_matched_boxes * self.neg_pre_positive,
                                     self.num_boxes)
        tile_num_neg_boxes = self.tile(self.expand_dims(num_neg_boxes, -1),
                                       (1, self.num_boxes))
        top_k_neg_mask = F.cast(
            self.less(relative_position, tile_num_neg_boxes), mstype.float32)
        class_loss = self.reduce_sum(cross_entropy * (mask + top_k_neg_mask),
                                     1)
        return self.reduce_mean(
            class_loss / F.cast(num_matched_boxes, mstype.float32), 0)
Exemple #4
0
    def construct(self, x, attention_mask, layer_past=None):
        """
        self-attention

        Inputs:
            x: output of previous layer
            attention_mask: the attention mask matrix with shape (batch_size, 1, seq_length, seq_length)
            layer_past: the previous feature map

        Returns:
            output: Tensor, the output logit of this layer
            layer_present: Tensor, the feature map of current layer
        """

        original_shape = F.shape(x)
        x = F.reshape(x, (-1, original_shape[-1]))
        query = self.dense1(x)
        key = self.dense2(x)
        value = self.dense3(x)
        query = self.transpose(F.reshape(query, (-1, original_shape[1], self.n_head, self.size_per_head)), (0, 2, 1, 3))
        key = self.transpose(F.reshape(key, (-1, original_shape[1], self.n_head, self.size_per_head)), (0, 2, 3, 1))
        value = self.transpose(F.reshape(value, (-1, original_shape[1], self.n_head, self.size_per_head)), (0, 2, 1, 3))
        if self.use_past:
            past_value = layer_past[1]
            past_key = self.transpose(layer_past[0], (0, 1, 3, 2))
            key = self.concat_k((past_key, key))
            value = self.concat_v(past_value, value)
        layer_present = P.Pack()([self.transpose(key, (0, 1, 3, 2)), value])
        attention = self._attn(query, key, value, attention_mask)
        attention_merge = self.merge_heads(attention)
        output = self.projection(attention_merge)
        output = self.dropout(output)
        return output, layer_present
Exemple #5
0
 def construct(self, x):
     tensor_shape = F.shape(x)
     input_feature = F.reshape(x, (tensor_shape[0] * tensor_shape[1], tensor_shape[2]))
     output = self.matmul(input_feature, self.weight)
     if self.has_bias:
         output = self.bias_add(output, self.bias)
     output = F.reshape(output, (tensor_shape[0], tensor_shape[1], self.out_channels))
     return output
Exemple #6
0
 def construct(self, inputs):
     output = ()
     batch_size = F.shape(inputs[0])[0]
     for x in inputs:
         x = self.transpose(x, (0, 2, 3, 1))
         output += (F.reshape(x, (batch_size, -1)), )
     res = self.concat(output)
     return F.reshape(res, (batch_size, self.num_ssd_boxes, -1))
Exemple #7
0
 def construct(self, x):
     output = ()
     for i in range(self.length):
         shape = F.shape(x[i])
         mid_shape = (shape[0], -1, self.num_default[i], self.sizes[i], self.sizes[i])
         final_shape = (shape[0], -1, self.num_default[i] * self.sizes[i] * self.sizes[i])
         output += (F.reshape(F.reshape(x[i], mid_shape), final_shape),)
     res = self.concat(output)
     return self.transpose(res, (0, 2, 1))
Exemple #8
0
    def _fspecial_gauss(self, filter_size, filter_sigma):
        """get gauss kernel"""
        filter_size, g = _gauss_kernel_helper(filter_size)

        square_sigma_scale = -0.5 / (filter_sigma * filter_sigma)
        g = g * square_sigma_scale
        g = F.reshape(g, (1, -1)) + F.reshape(g, (-1, 1))
        g = F.reshape(g, (1, -1))
        g = P.Softmax()(g)
        ret = F.reshape(g, (1, 1, filter_size, filter_size))
        return ret
Exemple #9
0
    def construct(self, x):
        shape = x.shape
        if self.do_reshape:
            x_shape = (-1, shape[-1])
            x = F.reshape(x, x_shape)

        y = self.dense(x)

        if self.do_reshape:
            y_shape = shape[:-1] + (-1, )
            y = F.reshape(y, y_shape)

        return y
Exemple #10
0
    def construct(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        if self.use_se and self.stride != 1:
            out = self.e2(out)
        else:
            out = self.conv2(out)
            out = self.bn2(out)
            out = self.relu(out)
        out = self.conv3(out)
        out = self.bn3(out)
        if self.se_block:
            out_se = out
            out = self.se_global_pool(out, (2, 3))
            out = self.se_dense_0(out)
            out = self.relu(out)
            out = self.se_dense_1(out)
            out = self.se_sigmoid(out)
            out = F.reshape(out, F.shape(out) + (1, 1))
            out = self.se_mul(out, out_se)

        if self.down_sample:
            identity = self.down_sample_layer(identity)

        out = out + identity
        out = self.relu(out)

        return out
Exemple #11
0
    def construct(self, distances):
        """Compute smeared-gaussian distance values.

        Args:
            distances (torch.Tensor): interatomic distance values of
                (N_b x N_at x N_nbh) shape.

        Returns:
            torch.Tensor: layer output of (N_b x N_at x N_nbh x N_g) shape.

        """
        ex_dis = F.expand_dims(distances, -1)
        if not self.centered:
            # compute width of Gaussian functions (using an overlap of 1 STDDEV)
            coeff = -0.5 / F.square(self.width)
            # Use advanced indexing to compute the individual components
            # ~ diff = distances[:, :, :, None] - offset[None, None, None, :]
            ex_offset = F.reshape(self.offset, (1, 1, 1, -1))
            diff = ex_dis - ex_offset
        else:
            # if Gaussian functions are centered, use offsets to compute widths
            coeff = -0.5 / F.square(self.offset)
            # if Gaussian functions are centered, no offset is subtracted
            diff = ex_dis
        # compute smear distance values
        exp = P.Exp()
        gauss = exp(coeff * F.square(diff))
        return gauss
Exemple #12
0
 def construct(self, input_x):
     if self.use_batch_norm and self.training:
         ones = P.Fill()(mstype.float32,
                         F.shape(input_x)[:self.begin_norm_axis], 1.0)
         zeros = P.Fill()(mstype.float32,
                          F.shape(input_x)[:self.begin_norm_axis], 0.0)
         shape_x = F.shape(input_x)
         norm_shape = get_shape_for_norm(shape_x, self.begin_norm_axis)
         input_x = F.reshape(input_x, norm_shape)
         output, _, _, _, _, _ = self.batch_norm(input_x, ones, zeros, None,
                                                 None)
         output = F.reshape(output, shape_x)
         y = output * self.gamma + self.beta
     else:
         y, _, _ = self.layer_norm(input_x, self.gamma, self.beta)
     return y
Exemple #13
0
    def construct(self, inputs, neighbors):
        # Construct auxiliary index vector
        ns = neighbors.shape

        # Get atomic positions of all neighboring indices

        if self.fixed_neigh:
            return F.gather(inputs, neighbors[0], -2)
        else:
            # [B, A, N] -> [B, A*N, 1]
            neigh_idx = F.reshape(neighbors, (ns[0], ns[1] * ns[2], -1))
            # [B, A*N, V] = [B, A*N, V] * [1, 1, V]
            neigh_idx = neigh_idx * self.broad_ones
            # [B, A*N, V] gather from [B, A, V]
            outputs = self.gatherd(inputs, 1, neigh_idx)
            # [B, A, N, V]
            return F.reshape(outputs, (ns[0], ns[1], ns[2], -1))
Exemple #14
0
 def construct(self, x):
     x = self.transpose(x, (3, 0, 2, 1))
     x = F.reshape(x, (-1, self.batch_size, self.input_size))
     y1, _, _, _, _ = self.lstm_1(x, self.h, self.c, self.w1)
     y2, _, _, _, _ = self.lstm_2(y1, self.h, self.c, self.w2)
     output = self.fc(
         y2
     )  # y2 shape: [time_step, bs, hidden_size] output shape: [time_step, bs, num_classes].
     return output
    def _attn(self, query, key, value, attention_mask):
        """
        Get the weighted score along the seq_length
        Inputs:
            query: the query matrix
            key: the key matrix
            value: the value matrix
            attention_mask: the attention mask matrix with shape (batch_size, 1, seq_length, seq_length)
        Returns:
            weighted_values: Tensor, the weighted sum scores
        """
        if not self.scale:
            query = query / F.cast(self.coeff, F.dtype(query))
            key = key / F.cast(self.coeff, F.dtype(key))

        score = self.batch_matmul(query, key)
        if self.scale:
            score = self.real_div(
                score,
                P.Cast()(self.scale_factor, P.DType()(score)))

        ori_dtype = P.DType()(score)
        score = P.Cast()(score, mstype.float32)
        multiplu_out = self.sub(
            P.Cast()(F.tuple_to_array((1.0,)), P.DType()(score)),
            P.Cast()(attention_mask, P.DType()(score)))

        adder = self.mul(multiplu_out, self.multiply_data)
        attention_scores = self.add(adder, score)

        shape = F.shape(attention_scores)
        attention_probs = self.softmax(
            F.reshape(attention_scores,
                      (shape[0], -1, shape[-1])))
        attention_probs = P.Cast()(attention_probs, ori_dtype)
        attention_probs = F.reshape(attention_probs, shape)

        attention_probs = self.prob_dropout(attention_probs)
        weighted_values = self.batch_matmul(attention_probs, value)
        return weighted_values
Exemple #16
0
    def construct(self, x):
        """Compute neural network output.

        Args:
            inputs (torch.Tensor): network input.

        Returns:
            torch.Tensor: network output.

        """
        shape = x.shape
        if self.do_reshape:
            x_shape = (-1, shape[-1])
            x = F.reshape(x, x_shape)

        y = self.mlp(x)

        if self.do_reshape:
            y_shape = shape[:-1] + (-1, )
            y = F.reshape(y, y_shape)

        return y
Exemple #17
0
 def construct(self, x):
     return F.reshape(x, (F.shape(x)[0], -1))
Exemple #18
0
def matmul(x1, x2, dtype=None):
    """
    Returns the matrix product of two arrays.

    Note:
        Numpy arguments `out`, `casting`, `order`, `subok`, `signature`, and `extobj` are
        not supported.
        On GPU, the supported dtypes are np.float16 and np.float32.
        On CPU, the supported dtypes are np.float16 and np.float32.

    Args:
        x1 (Tensor): Input tensor, scalar not allowed.
        x2 (Tensor): Input tensor, scalar not allowed.
        dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
            output Tensor.

    Returns:
        Tensor or scalar, the matrix product of the inputs. This is a scalar only
        when both `x1`, `x2` are 1-d vectors.

    Raises:
        ValueError: If the last dimension of `x1` is not the same size as the
            second-to-last dimension of `x2`, or if a scalar value is passed in.

    Supported Platforms:
        ``Ascend`` ``GPU`` ``CPU``

    Examples:
        >>> x1 = Tensor(np.arange(2*3*4).reshape(2, 3, 4), mindspore.float32)
        >>> x2 = Tensor(np.arange(4*5).reshape(4, 5), mindspore.float32)
        >>> output = ops.matmul(x1, x2)
        >>> print(output)
        [[[  70.   76.   82.   88.   94.]
        [ 190.  212.  234.  256.  278.]
        [ 310.  348.  386.  424.  462.]]
        [[ 430.  484.  538.  592.  646.]
        [ 550.  620.  690.  760.  830.]
        [ 670.  756.  842.  928. 1014.]]]
    """
    # performs type promotion
    dtype1 = F.dtype(x1)
    dtype2 = F.dtype(x2)
    if not _check_same_type(dtype1, dtype2):
        x1 = x1.astype(mstype.float32)
        x2 = x2.astype(mstype.float32)

    ndim1_orig, ndim2_orig = F.rank(x1), F.rank(x2)
    shape1_orig, shape2_orig = F.shape(x1), F.shape(x2)
    transpose_b = ndim2_orig == 1
    shape_backbone = _check_matmul_shapes(shape1_orig, shape2_orig)
    # infers the shape of the output
    shape_out = shape_backbone + _infer_shape_rem(
        shape1_orig, shape2_orig, ndim1_orig, ndim2_orig, transpose_b)

    x1 = _expand(x1, 2)
    x2 = _expand(x2, 2)
    if F.rank(x2) == 2:
        if F.rank(x1) > 2:
            x1 = F.reshape(x1, (-1, shape1_orig[-1]))
        res = P.MatMul(False, transpose_b)(x1, x2)
    else:
        # broadcasts x1.shape[:-2] with x2.shape[:-2]
        ndim_aligned = _max(ndim1_orig, ndim2_orig)
        x1 = _expand(x1, ndim_aligned)
        x2 = _expand(x2, ndim_aligned)
        shape1_aligned, shape2_aligned = F.shape(x1), F.shape(x2)
        x1 = _broadcast_to(x1, shape1_aligned[:-2], shape_backbone,
                           ndim_aligned)
        x2 = _broadcast_to(x2, shape2_aligned[:-2], shape_backbone,
                           ndim_aligned)
        res = P.BatchMatMul(False, transpose_b)(x1, x2)

    if dtype is not None:
        res = res.astype(dtype)
    return F.reshape(res, shape_out)
Exemple #19
0
def batch_dot(x1, x2, axes=None):
    """
    Computation of batch dot product between samples in two tensors containing batch dims.

    Inputs:
        - **x1** (Tensor) - First tensor in Batch Dot op with datatype float32
        - **x2** (Tensor) - Second tensor in Batch Dot op with datatype float32. x2's datatype should
          be same as x1's.
        - **axes** (Union[int, tuple(int), list(int)]) - Single value or tuple/list of length 2 with dimensions
          specified for `a` and `b` each. If single value `N` passed, automatically picks up last N dims from
          `a` input shape and last N dims from `b` input shape in order as axes for each respectively.

    Outputs:
        Tensor, batch dot product of x1 and x2. The Shape of output for input shapes (batch, d1, axes, d2) and
          (batch, d3, axes, d4) is (batch, d1, d2, d3, d4)

    .. math::
        output = x1[batch, :] * x2[batch, :]

    Raises:
        TypeError: If shapes of x1 and x2 are not the same.
        ValueError: If rank of x1 or x2 less than 2.
        ValueError: If batch dim used in axes.
        ValueError: If dtype of x1 or x2 is not float32.
        ValueError: If len(axes) less than 2.
        ValueError: If axes is not one of those: None, int, (int, int).
        ValueError: If axes value is too high for dimensions of input arrays.
        ValueError: If batch size of x1 and x2 are not the same.

    Supported Platforms:
        ``Ascend`` ``GPU`` ``CPU``

    Examples:
        >>> input_x1 = Tensor(np.ones(shape=[2, 2, 3]), mindspore.float32)
        >>> input_x2 = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32)
        >>> axes = (-1, -2)
        >>> output = C.batch_dot(input_x1, input_x2, axes)
        >>> print(output)
        [[[3. 3.]
          [3. 3.]]
         [[3. 3.]
          [3. 3.]]]
    """

    transpose_op = P.Transpose()
    batch_matmul_op = P.BatchMatMul()
    squeeze_one_op = P.Squeeze(1)
    squeeze_minus_one_op = P.Squeeze(-1)
    # input validity checks
    x1_shape = F.shape(x1)
    x2_shape = F.shape(x2)
    x1_dim_num = len(x1_shape)
    x2_dim_num = len(x2_shape)
    x1_type = F.dtype(x1)
    x2_type = F.dtype(x2)

    x1_batch_size, x2_batch_size = _get_batch_size(x1_shape, x2_shape)

    _typecheck_input_batch_dot(x1_type, x2_type)
    _check_batch_size(x1_batch_size, x2_batch_size)
    axes = _check_axes_for_batch_dot(x1_shape, x2_shape, axes)

    if x1_dim_num == 2:
        x1 = F.expand_dims(x1, 1)
        axes[0] += 1
    if x2_dim_num == 2:
        x2 = F.expand_dims(x2, 2)

    x1_shape = F.shape(x1)
    x2_shape = F.shape(x2)

    x1_reshape_fwd, x1_transpose_fwd, x1_ret = _calc_new_shape_batchdot(
        x1_shape, axes, 0)
    x2_reshape_fwd, x2_transpose_fwd, x2_ret = _calc_new_shape_batchdot(
        x2_shape, axes, 1)
    output_shape = _get_output_shape(x1_batch_size, x1_ret, x2_ret)

    x1_transposed = transpose_op(x1, x1_transpose_fwd)
    x2_transposed = transpose_op(x2, x2_transpose_fwd)
    x1_reshaped = F.reshape(x1_transposed, x1_reshape_fwd)
    x2_reshaped = F.reshape(x2_transposed, x2_reshape_fwd)

    # Batch matmal op part
    mul_result = batch_matmul_op(x1_reshaped, x2_reshaped)

    final_result = F.reshape(mul_result, output_shape)

    # if the original dims are expanded, restore them from 3 to 2
    if x1_dim_num == 2:
        final_result = squeeze_one_op(final_result)
    elif x2_dim_num == 2:
        final_result = squeeze_minus_one_op(final_result)

    return final_result
Exemple #20
0
 def construct(self, x):
     shape = F.shape(x)
     return F.reshape(self.ops(F.reshape(x, (1, -1, shape[2], shape[3]))),
                      shape)
Exemple #21
0
    def construct(self, query, key, value, cutoff=None, mask=None):
        r"""Compute multi-head attention.

        Args:
            query  (Mindspore.Tensor [B, A, 1, V]):
            key    (Mindspore.Tensor [B, A, N', V]):
            value  (Mindspore.Tensor [B, A, N', V]):
            cutoff (Mindspore.Tensor [B, A, 1, N'] or [B, A, 1, 1, N']):

        Returns:
            Mindspore.Tensor [B, A, V]: multi-head attention output.

        """
        if self.n_heads > 1:
            q_reshape = query.shape[:-1] + self.reshape_tail
            k_reshape = key.shape[:-1] + self.reshape_tail
            v_reshape = value.shape[:-1] + self.reshape_tail

            # [B, A, 1, h, v]
            Q = F.reshape(query, q_reshape)
            # [B, A, h, 1, v]
            Q = self.transpose(Q, self.trans_shape)

            # [B, A, N', h, v]
            K = F.reshape(key, k_reshape)
            # [B, A, h, N', v]
            K = self.transpose(K, self.trans_shape)

            # [B, A, N', h, v]
            V = F.reshape(value, v_reshape)
            # [B, A, h, N', v]
            V = self.transpose(V, self.trans_shape)

            # [B, A, h, 1, v] x [B, A, h, N', v]^T / \sqrt(v)
            # [B, A, h, 1, v] x [B, A, h, v, N'] = [B, A, h, 1, N']
            attention_scores = self.bmmt(Q, K)
            attention_scores = self.mul(attention_scores, self.scores_mul)

            if cutoff is None:
                attention_probs = self.softmax(attention_scores)
            else:
                # [B, A, 1, 1, N']
                exmask = F.expand_dims(F.expand_dims(mask, -2), -2)
                # [B, A, h, 1, N']
                mhmask = exmask * self.exones
                large_neg = F.ones_like(attention_scores) * -5e4
                attention_scores = F.select(mhmask > 0, attention_scores,
                                            large_neg)
                attention_probs = self.softmax(attention_scores)
                excut = F.expand_dims(F.expand_dims(cutoff, -2), -2)
                # [B, A, h, 1, N'] * [B, A, 1, 1, N']
                attention_probs = self.mul(attention_probs, excut)

            # [B, A, h, 1, N'] x [B, A, h, N', v] = [B, A, h, 1, v]
            context = self.bmm(attention_probs, V)
            # [B, A, 1, h, v]
            context = self.transpose(context, self.trans_shape)
            # [B, A, 1, V]
            context = F.reshape(context, query.shape)
        else:
            # [B, A, 1, V] x [B, A, N', V]^T / \sqrt(V)
            # [B, A, 1, V] x [B, A, V, N'] = [B, A, 1, N']
            attention_scores = self.bmmt(query, key) * self.scores_mul

            if cutoff is None:
                attention_probs = self.softmax(attention_scores)
            else:
                large_neg = F.ones_like(attention_scores) * -5e4
                attention_scores = F.select(mask, attention_scores, large_neg)
                attention_probs = self.softmax(attention_scores)
                # [B, A, 1, N'] * [B, A, 1, N']
                attention_probs = attention_probs * F.expand_dims(cutoff, -2)

            # [B, A, 1, N'] x [B, A, N', V] = [B, A, 1, V]
            context = self.bmm(attention_probs, value)

        # [B, A, V]
        context = self.squeeze(context)

        return self.output(context)
Exemple #22
0
    def construct(self, loc_data, loc_t, conf_data, conf_t, landm_data,
                  landm_t):

        # landm loss
        mask_pos1 = F.cast(self.less(0.0, F.cast(conf_t, mstype.float32)),
                           mstype.float32)

        N1 = self.maximum(self.reduce_sum(mask_pos1), 1)
        mask_pos_idx1 = self.tile(self.expand_dims(mask_pos1, -1), (1, 1, 10))
        loss_landm = self.reduce_sum(
            self.smooth_l1_loss(landm_data, landm_t) * mask_pos_idx1)
        loss_landm = loss_landm / N1

        # Localization Loss
        mask_pos = F.cast(self.notequal(0, conf_t), mstype.float32)
        conf_t = F.cast(mask_pos, mstype.int32)

        N = self.maximum(self.reduce_sum(mask_pos), 1)
        mask_pos_idx = self.tile(self.expand_dims(mask_pos, -1), (1, 1, 4))
        loss_l = self.reduce_sum(
            self.smooth_l1_loss(loc_data, loc_t) * mask_pos_idx)
        loss_l = loss_l / N

        # Conf Loss
        conf_t_shape = F.shape(conf_t)
        conf_t = F.reshape(conf_t, (-1, ))
        indices = self.concat((self.idx, F.reshape(conf_t, (-1, 1))))

        batch_conf = F.reshape(conf_data, (-1, self.num_classes))
        x_max = self.max(batch_conf)
        loss_c = self.log(self.reduce_sum2(self.exp(batch_conf - x_max),
                                           1)) + x_max
        loss_c = loss_c - F.reshape(self.gather(batch_conf, indices), (-1, 1))
        loss_c = F.reshape(loss_c, conf_t_shape)

        # hard example mining
        num_matched_boxes = F.reshape(self.reduce_sum(mask_pos, 1), (-1, ))
        neg_masked_cross_entropy = F.cast(loss_c * (1 - mask_pos),
                                          mstype.float32)

        _, loss_idx = self.sort_descend(neg_masked_cross_entropy,
                                        self.num_boxes)
        _, relative_position = self.sort(F.cast(loss_idx, mstype.float32),
                                         self.num_boxes)
        relative_position = F.cast(relative_position, mstype.float32)
        relative_position = relative_position[:, ::-1]
        relative_position = F.cast(relative_position, mstype.int32)

        num_neg_boxes = self.minimum(num_matched_boxes * self.neg_pre_positive,
                                     self.num_boxes - 1)
        tile_num_neg_boxes = self.tile(self.expand_dims(num_neg_boxes, -1),
                                       (1, self.num_boxes))
        top_k_neg_mask = F.cast(
            self.less(relative_position, tile_num_neg_boxes), mstype.float32)

        cross_entropy = self.cross_entropy(batch_conf, conf_t)
        cross_entropy = F.reshape(cross_entropy, conf_t_shape)

        loss_c = self.reduce_sum(cross_entropy *
                                 self.minimum(mask_pos + top_k_neg_mask, 1))

        loss_c = loss_c / N

        return loss_l, loss_c, loss_landm