当前位置: 首页>>代码示例>>Python>>正文


Python nd.reshape方法代码示例

本文整理汇总了Python中mxnet.nd.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python nd.reshape方法的具体用法?Python nd.reshape怎么用?Python nd.reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mxnet.nd的用法示例。


在下文中一共展示了nd.reshape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _spectral_norm

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def _spectral_norm(self):
        """ spectral normalization """
        w = self.params.get('weight').data(self.ctx)
        w_mat = nd.reshape(w, [w.shape[0], -1])

        _u = self.u.data(self.ctx)
        _v = None

        for _ in range(POWER_ITERATION):
            _v = nd.L2Normalization(nd.dot(_u, w_mat))
            _u = nd.L2Normalization(nd.dot(_v, w_mat.T))

        sigma = nd.sum(nd.dot(_u, w_mat) * _v)
        if sigma == 0.:
            sigma = EPSILON

        self.params.setattr('u', _u)

        return w / sigma 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:21,代码来源:model.py

示例2: _convert_bbox

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def _convert_bbox(self, delta, anchor):
        """from loc to predict postion

        Parameters
        ----------
            delta : ndarray or np.ndarray
                network output
            anchor : np.ndarray
                generate anchor location

        Returns
        -------
            rejust predict postion though Anchor
        """
        delta = nd.transpose(delta, axes=(1, 2, 3, 0))
        delta = nd.reshape(delta, shape=(4, -1))
        delta = delta.asnumpy()
        delta[0, :] = delta[0, :] * anchor[:, 2] + anchor[:, 0]
        delta[1, :] = delta[1, :] * anchor[:, 3] + anchor[:, 1]
        delta[2, :] = np.exp(delta[2, :]) * anchor[:, 2]
        delta[3, :] = np.exp(delta[3, :]) * anchor[:, 3]
        return delta 
开发者ID:dmlc,项目名称:gluon-cv,代码行数:24,代码来源:siamrpn_tracker.py

示例3: _convert_score

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def _convert_score(self, score):
        """from cls to score

        Parameters
        ----------
            score : ndarray
                network output

        Returns
        -------
            get feature map score though softmax
        """
        score = nd.transpose(score, axes=(1, 2, 3, 0))
        score = nd.reshape(score, shape=(2, -1))
        score = nd.transpose(score, axes=(1, 0))
        score = nd.softmax(score, axis=1)
        score = nd.slice_axis(score, axis=1, begin=1, end=2)
        score = nd.squeeze(score, axis=1)
        return score.asnumpy() 
开发者ID:dmlc,项目名称:gluon-cv,代码行数:21,代码来源:siamrpn_tracker.py

示例4: _topk

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def _topk(scores, K=40):
    batch, cat, height, width = scores.shape

    [topk_scores, topk_inds] = nd.topk(nd.reshape(scores, (batch, cat, -1)), ret_typ='both', k=K)  # return both value and indices

    topk_inds = topk_inds % (height * width)
    topk_ys   = (topk_inds / width).astype('int32').astype('float32')
    topk_xs   = (topk_inds % width).astype('int32').astype('float32')

    [topk_score, topk_ind] = nd.topk(nd.reshape(topk_scores, (batch, -1)), ret_typ='both', k=K)
    topk_clses = (topk_ind / K).astype('int32')

    topk_inds = _gather_feat(nd.reshape(topk_inds, (batch, -1, 1)), topk_ind)
    topk_inds = nd.reshape(topk_inds, (batch, K))

    topk_ys = _gather_feat(nd.reshape(topk_ys, (batch, -1, 1)), topk_ind)
    topk_ys = nd.reshape(topk_ys, (batch, K))

    topk_xs = _gather_feat(nd.reshape(topk_xs, (batch, -1, 1)), topk_ind)
    topk_xs = nd.reshape(topk_xs, (batch, K))

    return topk_score, topk_inds, topk_clses, topk_ys, topk_xs 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:24,代码来源:decoder.py

示例5: symbolic_topk

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def symbolic_topk(F, scores, K=40):
    batch, cat, height, width = 1, 1, 128.0, 128.0

    [topk_scores, topk_inds] = F.topk(scores.reshape((batch, cat, -1)), ret_typ='both', k=K)  # return both value and indices

    topk_inds = topk_inds % (height * width)
    topk_ys   = (topk_inds / width).astype('int32').astype('float32')
    topk_xs   = (topk_inds % width).astype('int32').astype('float32')

    [topk_score, topk_ind] = F.topk(topk_scores.reshape((batch, -1)), ret_typ='both', k=K)
    topk_clses = (topk_ind / K).astype('int32')

    topk_inds = symbolic_gather_feat(F, topk_inds.reshape((batch, -1, 1)), topk_ind, K, attri=1)
    topk_inds = topk_inds.reshape((batch, K))

    topk_ys = symbolic_gather_feat(F, topk_ys.reshape((batch, -1, 1)), topk_ind, K, attri=1)
    topk_ys = topk_ys.reshape((batch, K))

    topk_xs = symbolic_gather_feat(F, topk_xs.reshape((batch, -1, 1)), topk_ind, K, attri=1)
    topk_xs = topk_xs.reshape((batch, K))

    return topk_score, topk_inds, topk_clses, topk_ys, topk_xs 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:24,代码来源:decoder.py

示例6: msg_edge

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def msg_edge(self, edge):
        state = nd.concat(edge.src['state'], edge.dst['state'], dim=-1)
        feature = nd.concat(edge.src['feature'], edge.dst['feature'], edge.data['dist'], dim=-1)

        # generate weight by meta-learner
        weight = self.w_mlp(feature)
        weight = nd.reshape(weight, shape=(-1, self.hidden_size * 2, self.hidden_size))

        # reshape state to [n, b * t, d] for batch_dot (currently mxnet only support batch_dot for 3D tensor)
        shape = state.shape
        state = nd.reshape(state, shape=(shape[0], -1, shape[-1]))

        alpha = nd.LeakyReLU(nd.batch_dot(state, weight))

        # reshape alpha to [n, b, t, d]
        alpha = nd.reshape(alpha, shape=shape[:-1] + (self.hidden_size,))
        return { 'alpha': alpha, 'state': edge.src['state'] } 
开发者ID:panzheyi,项目名称:ST-MetaNet,代码行数:19,代码来源:graph.py

示例7: forward

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def forward(self, feature, data, begin_state):
        n, b, length, _ = data.shape

        # reshape the data and states for rnn unroll
        data = nd.reshape(data, shape=(n * b, length, -1)) # [n * b, t, d]
        if begin_state is not None:
            begin_state = [
                nd.reshape(state, shape=(n * b, -1)) for state in begin_state
            ] # [n * b, d]
        
        # unroll the rnn
        data, state = self.cell.unroll(length, data, begin_state, merge_outputs=True)

        # reshape the data & states back
        data = nd.reshape(data, shape=(n, b, length, -1))
        state = [nd.reshape(s, shape=(n, b, -1)) for s in state]

        return data, state 
开发者ID:panzheyi,项目名称:ST-MetaNet,代码行数:20,代码来源:cell.py

示例8: forward

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def forward(self, feature, data):
        """ Forward process of a MetaDense layer

        Parameters
        ----------
        feature: NDArray with shape [n, d]
        data: NDArray with shape [n, b, input_hidden_size]

        Returns
        -------
        output: NDArray with shape [n, b, output_hidden_size]
        """
        weight = self.w_mlp(feature) # [n, input_hidden_size * output_hidden_size]
        weight = nd.reshape(weight, (-1, self.input_hidden_size, self.output_hidden_size))
        bias = nd.reshape(self.b_mlp(feature), shape=(-1, 1, 1)) # [n, 1, 1]
        return nd.batch_dot(data, weight) + bias 
开发者ID:panzheyi,项目名称:ST-MetaNet,代码行数:18,代码来源:basic_structure.py

示例9: deal_output

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def deal_output(y: nd.NDArray, s, b, c):
    """

    :param y:
    :param s:
    :param b:
    :param c:
    :return:
    """
    label = y[:, 0:s * s * c]
    preds = y[:, s * s * c: s * s * c + s * s * b]
    location = y[:, s * s * c + s * s * b:]
    label = nd.reshape(label, shape=(-1, s * s, c))
    location = nd.reshape(location, shape=(-1, s * s, b, 4))
    return label, preds, location 
开发者ID:MashiMaroLjc,项目名称:YOLO,代码行数:17,代码来源:utils.py

示例10: generate_anchor

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def generate_anchor(self, score_size):
        """
        generate score map anchors based on predefined configuration

        Parameters
        ----------
            score_size : int
                score map size

        Returns
        ----------
            score map anchor
        """
        anchors = Anchors(self.STRIDE,
                          self.RATIOS,
                          self.SCALES)
        anchor = anchors.anchors
        x_min, y_min, x_max, y_max = anchor[:, 0], anchor[:, 1], anchor[:, 2], anchor[:, 3]
        anchor = np.stack([(x_min+x_max)*0.5, (y_min+y_max)*0.5, x_max-x_min, y_max-y_min], 1)
        total_stride = anchors.stride
        anchor_num = anchor.shape[0]
        anchor = np.tile(anchor, score_size * score_size).reshape((-1, 4))
        ori = - (score_size // 2) * total_stride
        x_x, y_y = np.meshgrid([ori + total_stride * dx for dx in range(score_size)],
                               [ori + total_stride * dy for dy in range(score_size)])
        x_x, y_y = np.tile(x_x.flatten(), (anchor_num, 1)).flatten(), \
            np.tile(y_y.flatten(), (anchor_num, 1)).flatten()
        anchor[:, 0], anchor[:, 1] = x_x.astype(np.float32), y_y.astype(np.float32)
        return anchor 
开发者ID:dmlc,项目名称:gluon-cv,代码行数:31,代码来源:siamrpn_tracker.py

示例11: compute_rot_loss

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def compute_rot_loss(output, target_bin, target_res, mask):
    # output: (B, 128, 8) [bin1_cls[0], bin1_cls[1], bin1_sin, bin1_cos,
    #                 bin2_cls[0], bin2_cls[1], bin2_sin, bin2_cos]
    # target_bin: (B, 128, 2) [bin1_cls, bin2_cls]
    # target_res: (B, 128, 2) [bin1_res, bin2_res]
    # mask: (B, 128, 1)
    output = nd.reshape(output, (-1, 8))
    target_bin = nd.reshape(target_bin, (-1, 2))
    target_res = nd.reshape(target_res, (-1, 2))
    mask = nd.reshape(mask, (-1, 1))
    loss_bin1 = compute_bin_loss(output[:, 0:2], target_bin[:, 0], mask)
    loss_bin2 = compute_bin_loss(output[:, 4:6], target_bin[:, 1], mask)
    loss_res = nd.zeros_like(loss_bin1)

    mask1 = (target_bin[:, 0] > 0).astype('float32')
    if mask1.sum() > 0:
        valid_output1 = output
        valid_target_res1 = target_res

        loss_sin1 = compute_res_loss(valid_output1[:, 2], nd.sin(valid_target_res1[:, 0]), mask1)
        loss_cos1 = compute_res_loss(valid_output1[:, 3], nd.cos(valid_target_res1[:, 0]), mask1)
        loss_res = loss_res + loss_sin1 + loss_cos1

    mask2 = (target_bin[:, 1] > 0).astype('float32')
    if mask2.sum() > 0:
        valid_output2 = output
        valid_target_res2 = target_res

        loss_sin2 = compute_res_loss(valid_output2[:, 6], nd.sin(valid_target_res2[:, 1]), mask2)
        loss_cos2 = compute_res_loss(valid_output2[:, 7], nd.cos(valid_target_res2[:, 1]), mask2)
        loss_res = loss_res + loss_sin2 + loss_cos2
    #print("loss_bin1: {}, loss_bin2: {}, loss_sin1: {}, loss_sin2: {}, loss_cos1: {}, loss_cos2: {}".format(loss_bin1, loss_bin2, loss_sin1, loss_sin2, loss_cos1, loss_cos2))
    return loss_bin1 + loss_bin2 + loss_res 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:35,代码来源:losses.py

示例12: decode_centernet_3dod

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def decode_centernet_3dod(heat, rot, depth, dim, wh=None, reg=None, K=40):
    batch, cat, height, width = heat.shape
    # perform nms on heatmaps
    heat = _nms(heat)

    scores, inds, clses, ys, xs = _topk(heat, K=K)
    if reg is not None:
        reg = _tranpose_and_gather_feat(reg, inds)
        reg = nd.reshape(reg, (batch, K, 2))
        xs = nd.reshape(xs, (batch, K, 1)) + reg[:, :, 0:1]
        ys = nd.reshape(ys, (batch, K, 1)) + reg[:, :, 1:2]
    else:
        xs = nd.reshape(xs, (batch, K, 1)) + 0.5
        ys = nd.reshape(ys, (batch, K, 1)) + 0.5

    rot = _tranpose_and_gather_feat(rot, inds)
    rot = nd.reshape(rot, (batch, K, 8))
    depth = _tranpose_and_gather_feat(depth, inds)
    depth = nd.reshape(depth, (batch, K, 1))
    dim = _tranpose_and_gather_feat(dim, inds)
    dim = nd.reshape(dim, (batch, K, 3))

    clses  = nd.reshape(clses, (batch, K, 1)).astype('float32')
    scores = nd.reshape(scores, (batch, K, 1))
    xs = nd.reshape(xs, (batch, K, 1))
    ys = nd.reshape(ys, (batch, K, 1))

    if wh is not None:
        wh = _tranpose_and_gather_feat(wh, inds)
        wh = nd.reshape(wh, (batch, K, 2))
        detections = nd.concat(xs, ys, scores, rot, depth, dim, wh, clses, dim=2)
    else:
        detections = nd.concat(xs, ys, scores, rot, depth, dim, clses, dim=2)

    return detections 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:37,代码来源:decoder.py

示例13: symbolic_topk_channel

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def symbolic_topk_channel(F, scores, K=40):
    scores_shape = F.shape_array(scores)
    batch, cat, height, width = 1, 1, 128.0, 128.0

    [topk_scores, topk_inds] = F.topk(scores.reshape((batch, cat, -1)), ret_typ = "both", k= K)

    topk_inds = topk_inds % (height * width)
    topk_ys   = (topk_inds / width).astype('int32').astype('float32')
    topk_xs   = (topk_inds % width).astype('int32').astype('float32')

    return topk_scores, topk_inds, topk_ys, topk_xs 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:13,代码来源:decoder.py

示例14: _tranpose_and_gather_feat

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def _tranpose_and_gather_feat(feat, ind):
    feat = nd.transpose(feat, axes=(0, 2, 3, 1))
    feat = nd.reshape(feat, shape=(feat.shape[0], -1, feat.shape[3]))
    feat = _gather_feat(feat, ind)
    return feat 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:7,代码来源:tensor_utils.py

示例15: flip_lr

# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import reshape [as 别名]
def flip_lr(x, flip_idx):
  tmp = x.detach().cpu().numpy()[..., ::-1].copy()
  shape = tmp.shape
  for e in flip_idx:
    tmp[:, e[0], ...], tmp[:, e[1], ...] = \
      tmp[:, e[1], ...].copy(), tmp[:, e[0], ...].copy()
  return nd.array(tmp.reshape(shape)).to(x.device) 
开发者ID:Guanghan,项目名称:mxnet-centernet,代码行数:9,代码来源:tensor_utils.py


注:本文中的mxnet.nd.reshape方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。