本文整理匯總了Python中mxnet.nd.topk方法的典型用法代碼示例。如果您正苦於以下問題:Python nd.topk方法的具體用法?Python nd.topk怎麽用?Python nd.topk使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mxnet.nd
的用法示例。
在下文中一共展示了nd.topk方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _topk
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import topk [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
示例2: symbolic_topk
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import topk [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
示例3: get_attribute
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import topk [as 別名]
def get_attribute(self, image):
"""Face attribute predictor.
Parameters
----------
image: NDArray.
The NDArray data format for MXNet to process, such as (H, W, C).
Returns
-------
type: tuple
Results of Face Attribute Predict:
(str(gender), int(age), str(expression)).
"""
img = transform_eval(image, resize_short=self._image_size, crop_size=self._image_size)
img = img.as_in_context(self.ctx[0])
tic = time.time()
pred = self.net(img)
toc = time.time() - tic
print('Attribute inference time: %fms' % (toc*1000))
topK = 1
topK_age = 6
topK_exp = 2
age = 0
ind_1 = nd.topk(pred[0], k=topK)[0].astype('int')
ind_2 = nd.topk(pred[1], k=topK_age)[0].astype('int')
ind_3 = nd.topk(pred[2], k=topK_exp)[0].astype('int')
for i in range(topK_age):
age += int(nd.softmax(pred[1])[0][ind_2[i]].asscalar() * self.attribute_map2[1][ind_2[i].asscalar()])
gender = self.attribute_map2[0][ind_1[0].asscalar()]
if nd.softmax(pred[2])[0][ind_3[0]].asscalar() < 0.45:
expression = self.attribute_map2[2][7]
else:
expression_1 = self.attribute_map2[2][ind_3[0].asscalar()]
expression_2 = self.attribute_map2[2][ind_3[1].asscalar()]
return (gender, age, (expression_1, expression_2))
示例4: _topk_channel
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import topk [as 別名]
def _topk_channel(scores, K=40):
batch, cat, height, width = scores.shape
[topk_scores, topk_inds] = nd.topk(scores.reshape((batch, cat, -1)), ret_typ = "both", k= K)
#[topk_score, topk_ind] = nd.topk(nd.reshape(topk_scores, (batch, -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')
topk_ys = (topk_inds / width).astype('int64').astype('float32')
topk_xs = (topk_inds % width).astype('int64').astype('float32')
return topk_scores, topk_inds, topk_ys, topk_xs
示例5: symbolic_topk_channel
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import topk [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