本文整理汇总了Python中mxnet.nd.zeros_like方法的典型用法代码示例。如果您正苦于以下问题:Python nd.zeros_like方法的具体用法?Python nd.zeros_like怎么用?Python nd.zeros_like使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.nd
的用法示例。
在下文中一共展示了nd.zeros_like方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: heatmap_to_coord
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def heatmap_to_coord(heatmaps, bbox_list):
heatmap_height = heatmaps.shape[2]
heatmap_width = heatmaps.shape[3]
coords, maxvals = get_max_pred(heatmaps)
preds = nd.zeros_like(coords)
for i, bbox in enumerate(bbox_list):
x0 = bbox[0]
y0 = bbox[1]
x1 = bbox[2]
y1 = bbox[3]
w = (x1 - x0) / 2
h = (y1 - y0) / 2
center = np.array([x0 + w, y0 + h])
scale = np.array([w, h])
w_ratio = coords[i][:, 0] / heatmap_width
h_ratio = coords[i][:, 1] / heatmap_height
preds[i][:, 0] = scale[0] * 2 * w_ratio + center[0] - scale[0]
preds[i][:, 1] = scale[1] * 2 * h_ratio + center[1] - scale[1]
return preds, maxvals
示例2: hybrid_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def hybrid_forward(self, F, samples, matches, refs):
"""HybridBlock, handle multi batch correctly
Parameters
----------
samples: (B, N), value +1 (positive), -1 (negative), 0 (ignore)
matches: (B, N), value range [0, M)
refs: (B, M), value range [0, num_fg_class), excluding background
Returns
-------
targets: (B, N), value range [0, num_fg_class + 1), including background
"""
# samples (B, N) (+1, -1, 0: ignore), matches (B, N) [0, M), refs (B, M)
# reshape refs (B, M) -> (B, 1, M) -> (B, N, M)
refs = F.repeat(refs.reshape((0, 1, -1)), axis=1, repeats=matches.shape[1])
# ids (B, N, M) -> (B, N), value [0, M + 1), 0 reserved for background class
target_ids = F.pick(refs, matches, axis=2) + 1
# samples 0: set ignore samples to ignore_label
targets = F.where(samples > 0.5, target_ids, nd.ones_like(target_ids) * self._ignore_label)
# samples -1: set negative samples to 0
targets = F.where(samples < -0.5, nd.zeros_like(targets), targets)
return targets
示例3: forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def forward(self, samples, matches, anchors, refs):
"""Forward"""
F = nd
# TODO(zhreshold): batch_pick, take multiple elements?
ref_boxes = nd.repeat(refs.reshape((0, 1, -1, 4)), axis=1, repeats=matches.shape[1])
ref_boxes = nd.split(ref_boxes, axis=-1, num_outputs=4, squeeze_axis=True)
ref_boxes = nd.concat(*[F.pick(ref_boxes[i], matches, axis=2).reshape((0, -1, 1)) \
for i in range(4)], dim=2)
g = self.corner_to_center(ref_boxes)
a = self.corner_to_center(anchors)
t0 = ((g[0] - a[0]) / a[2] - self._means[0]) / self._stds[0]
t1 = ((g[1] - a[1]) / a[3] - self._means[1]) / self._stds[1]
t2 = (F.log(g[2] / a[2]) - self._means[2]) / self._stds[2]
t3 = (F.log(g[3] / a[3]) - self._means[3]) / self._stds[3]
codecs = F.concat(t0, t1, t2, t3, dim=2)
temp = F.tile(samples.reshape((0, -1, 1)), reps=(1, 1, 4)) > 0.5
targets = F.where(temp, codecs, F.zeros_like(codecs))
masks = F.where(temp, F.ones_like(temp), F.zeros_like(temp))
return targets, masks
示例4: hybrid_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def hybrid_forward(self, F, samples):
"""Encode class prediction labels for SigmoidCrossEntropy Loss.
Parameters
----------
samples : mxnet.nd.NDArray or mxnet.sym.Symbol
Sampling results with shape (B, N), 1:pos, 0:ignore, -1:negative
Returns
-------
(mxnet.nd.NDArray, mxnet.nd.NDArray)
(target, mask)
target is the output label with shape (B, N), 1: pos, 0: negative, -1: ignore
mask is the mask for label, -1(ignore) labels have mask 0, otherwise mask is 1.
"""
# notation from samples, 1:pos, 0:ignore, -1:negative
target = (samples + 1) / 2.
target = F.where(F.abs(samples) < 1e-5, F.ones_like(target) * -1, target)
# output: 1: pos, 0: negative, -1: ignore
mask = F.where(F.abs(samples) > 1e-5, F.ones_like(samples), F.zeros_like(samples))
return target, mask
示例5: hybrid_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def hybrid_forward(self, F, box_preds, gt_boxes):
"""Short summary.
Parameters
----------
F : mxnet.nd or mxnet.sym
`F` is mxnet.sym if hybridized or mxnet.nd if not.
box_preds : mxnet.nd.NDArray
Predicted bounding boxes.
gt_boxes : mxnet.nd.NDArray
Ground-truth bounding boxes.
Returns
-------
(tuple of) mxnet.nd.NDArray
objectness: 0 for negative, 1 for positive, -1 for ignore.
center_targets: regression target for center x and y.
scale_targets: regression target for scale x and y.
weights: element-wise gradient weights for center_targets and scale_targets.
class_targets: a one-hot vector for classification.
"""
with autograd.pause():
box_preds = box_preds.reshape((0, -1, 4))
objness_t = F.zeros_like(box_preds.slice_axis(axis=-1, begin=0, end=1))
center_t = F.zeros_like(box_preds.slice_axis(axis=-1, begin=0, end=2))
scale_t = F.zeros_like(box_preds.slice_axis(axis=-1, begin=0, end=2))
weight_t = F.zeros_like(box_preds.slice_axis(axis=-1, begin=0, end=2))
class_t = F.ones_like(objness_t.tile(reps=(self._num_class))) * -1
batch_ious = self._batch_iou(box_preds, gt_boxes) # (B, N, M)
ious_max = batch_ious.max(axis=-1, keepdims=True) # (B, N, 1)
objness_t = (ious_max > self._ignore_iou_thresh) * -1 # use -1 for ignored
return objness_t, center_t, scale_t, weight_t, class_t
示例6: hybrid_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def hybrid_forward(self, F, pred, label):
"""Compute loss"""
softmaxout = F.SoftmaxOutput(
pred, label.astype(pred.dtype), ignore_label=self._ignore_label,
multi_output=self._sparse_label,
use_ignore=True, normalization='valid' if self._size_average else 'null')
if self._sparse_label:
loss = -F.pick(F.log(softmaxout), label, axis=1, keepdims=True)
else:
label = _reshape_like(F, label, pred)
loss = -F.sum(F.log(softmaxout) * label, axis=-1, keepdims=True)
loss = F.where(label.expand_dims(axis=1) == self._ignore_label,
F.zeros_like(loss), loss)
return F.mean(loss, axis=self._batch_axis, exclude=True)
示例7: get_final_preds
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def get_final_preds(batch_heatmaps, center, scale):
coords, maxvals = get_max_pred(batch_heatmaps)
heatmap_height = batch_heatmaps.shape[2]
heatmap_width = batch_heatmaps.shape[3]
# post-processing
for n in range(coords.shape[0]):
for p in range(coords.shape[1]):
hm = batch_heatmaps[n][p]
px = int(nd.floor(coords[n][p][0] + 0.5).asscalar())
py = int(nd.floor(coords[n][p][1] + 0.5).asscalar())
if 1 < px < heatmap_width-1 and 1 < py < heatmap_height-1:
diff = nd.concat(hm[py][px+1] - hm[py][px-1],
hm[py+1][px] - hm[py-1][px],
dim=0)
coords[n][p] += nd.sign(diff) * .25
preds = nd.zeros_like(coords)
# Transform back
for i in range(coords.shape[0]):
preds[i] = transform_preds(coords[i], center[i], scale[i],
[heatmap_width, heatmap_height])
return preds, maxvals
示例8: hybrid_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def hybrid_forward(self, F, pred, label):
"""Compute loss"""
softmaxout = F.SoftmaxOutput(
pred, label.astype(pred.dtype), ignore_label=self._ignore_label,
multi_output=self._sparse_label,
use_ignore=True, normalization='valid' if self._size_average else 'null')
loss = -F.pick(F.log(softmaxout), label, axis=1, keepdims=True)
loss = F.where(label.expand_dims(axis=1) == self._ignore_label,
F.zeros_like(loss), loss)
return F.mean(loss, axis=self._batch_axis, exclude=True)
示例9: forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def forward(self, samples, matches, anchors, refs):
"""Not HybridBlock due to use of matches.shape
Parameters
----------
samples: (B, N) value +1 (positive), -1 (negative), 0 (ignore)
matches: (B, N) value range [0, M)
anchors: (B, N, 4) encoded in corner
refs: (B, M, 4) encoded in corner
Returns
-------
targets: (B, N, 4) transform anchors to refs picked according to matches
masks: (B, N, 4) only positive anchors has targets
"""
F = nd
# TODO(zhreshold): batch_pick, take multiple elements?
# refs [B, M, 4], anchors [B, N, 4], samples [B, N], matches [B, N]
# refs [B, M, 4] -> reshape [B, 1, M, 4] -> repeat [B, N, M, 4]
ref_boxes = F.repeat(refs.reshape((0, 1, -1, 4)), axis=1, repeats=matches.shape[1])
# refs [B, N, M, 4] -> 4 * [B, N, M]
ref_boxes = F.split(ref_boxes, axis=-1, num_outputs=4, squeeze_axis=True)
# refs 4 * [B, N, M] -> pick from matches [B, N, 1] -> concat to [B, N, 4]
ref_boxes = F.concat(*[F.pick(ref_boxes[i], matches, axis=2).reshape((0, -1, 1)) \
for i in range(4)], dim=2)
# transform based on x, y, w, h
# g [B, N, 4], a [B, N, 4] -> codecs [B, N, 4]
g = self.corner_to_center(ref_boxes)
a = self.corner_to_center(anchors)
t0 = ((g[0] - a[0]) / a[2] - self._means[0]) / self._stds[0]
t1 = ((g[1] - a[1]) / a[3] - self._means[1]) / self._stds[1]
t2 = (F.log(g[2] / a[2]) - self._means[2]) / self._stds[2]
t3 = (F.log(g[3] / a[3]) - self._means[3]) / self._stds[3]
codecs = F.concat(t0, t1, t2, t3, dim=2)
# samples [B, N] -> [B, N, 1] -> [B, N, 4] -> boolean
temp = F.tile(samples.reshape((0, -1, 1)), reps=(1, 1, 4)) > 0.5
# fill targets and masks [B, N, 4]
targets = F.where(temp, codecs, F.zeros_like(codecs))
masks = F.where(temp, F.ones_like(temp), F.zeros_like(temp))
return targets, masks
示例10: compute_rot_loss
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [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
示例11: forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def forward(self, cls_pred, box_pred, cls_target, box_target):
"""Compute loss in entire batch across devices."""
# require results across different devices at this time
cls_pred, box_pred, cls_target, box_target = [_as_list(x) \
for x in (cls_pred, box_pred, cls_target, box_target)]
# cross device reduction to obtain positive samples in entire batch
num_pos = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pos_samples = (ct > 0)
num_pos.append(pos_samples.sum())
num_pos_all = sum([p.asscalar() for p in num_pos])
if num_pos_all < 1:
# no positive samples found, return dummy losses
return nd.zeros((1,)), nd.zeros((1,)), nd.zeros((1,))
# compute element-wise cross entropy loss and sort, then perform negative mining
cls_losses = []
box_losses = []
sum_losses = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pred = nd.log_softmax(cp, axis=-1)
pos = ct > 0
cls_loss = -nd.pick(pred, ct, axis=-1, keepdims=False)
rank = (cls_loss * (pos - 1)).argsort(axis=1).argsort(axis=1)
hard_negative = rank < (pos.sum(axis=1) * self._negative_mining_ratio).expand_dims(-1)
# mask out if not positive or negative
cls_loss = nd.where((pos + hard_negative) > 0, cls_loss, nd.zeros_like(cls_loss))
cls_losses.append(nd.sum(cls_loss, axis=0, exclude=True) / num_pos_all)
bp = _reshape_like(nd, bp, bt)
box_loss = nd.abs(bp - bt)
box_loss = nd.where(box_loss > self._rho, box_loss - 0.5 * self._rho,
(0.5 / self._rho) * nd.square(box_loss))
# box loss only apply to positive samples
box_loss = box_loss * pos.expand_dims(axis=-1)
box_losses.append(nd.sum(box_loss, axis=0, exclude=True) / num_pos_all)
sum_losses.append(cls_losses[-1] + self._lambd * box_losses[-1])
return sum_losses, cls_losses, box_losses
示例12: build_graph_validate_gt_obj
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def build_graph_validate_gt_obj(img, gt_ids, bbox, spatial_feat,
bbox_improvement=True, overlap=False):
'''given ground truth bbox and label, build graph for validation'''
n_batch = img.shape[0]
img_size = img.shape[2:4]
bbox[:, :, 0] /= img_size[1]
bbox[:, :, 1] /= img_size[0]
bbox[:, :, 2] /= img_size[1]
bbox[:, :, 3] /= img_size[0]
ctx = img.context
g_batch = []
for btc in range(n_batch):
inds = np.where(bbox[btc].sum(1).asnumpy() > 0)[0].tolist()
if len(inds) == 0:
continue
n_nodes = len(inds)
g_pred = dgl.DGLGraph()
g_pred.add_nodes(n_nodes, {'pred_bbox': bbox[btc, inds],
'node_feat': spatial_feat[btc, inds],
'node_class_pred': gt_ids[btc, inds, 0],
'node_class_logit': nd.zeros_like(gt_ids[btc, inds, 0], ctx=ctx)})
edge_list = []
for i in range(n_nodes - 1):
for j in range(i + 1, n_nodes):
edge_list.append((i, j))
src, dst = tuple(zip(*edge_list))
g_pred.add_edges(src, dst)
g_pred.add_edges(dst, src)
n_nodes = g_pred.number_of_nodes()
n_edges = g_pred.number_of_edges()
if bbox_improvement:
g_pred.ndata['pred_bbox'] = bbox_improve(g_pred.ndata['pred_bbox'])
g_pred.edata['rel_bbox'] = extract_edge_bbox(g_pred)
g_pred.edata['batch_id'] = nd.zeros((n_edges, 1), ctx = ctx) + btc
g_batch.append(g_pred)
if len(g_batch) == 0:
return None
if len(g_batch) > 1:
return dgl.batch(g_batch)
return g_batch[0]
示例13: forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def forward(self, cls_pred, box_pred, cls_target, box_target):
"""Compute loss in entire batch across devices."""
# require results across different devices at this time
cls_pred, box_pred, cls_target, box_target = [_as_list(x) \
for x in (cls_pred, box_pred, cls_target, box_target)]
# cross device reduction to obtain positive samples in entire batch
num_pos = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pos_samples = (ct > 0)
num_pos.append(pos_samples.sum())
num_pos_all = sum([p.asscalar() for p in num_pos])
if num_pos_all < 1 and self._min_hard_negatives < 1:
# no positive samples and no hard negatives, return dummy losses
cls_losses = [nd.sum(cp * 0) for cp in cls_pred]
box_losses = [nd.sum(bp * 0) for bp in box_pred]
sum_losses = [nd.sum(cp * 0) + nd.sum(bp * 0) for cp, bp in zip(cls_pred, box_pred)]
return sum_losses, cls_losses, box_losses
# compute element-wise cross entropy loss and sort, then perform negative mining
cls_losses = []
box_losses = []
sum_losses = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pred = nd.log_softmax(cp, axis=-1)
pos = ct > 0
cls_loss = -nd.pick(pred, ct, axis=-1, keepdims=False)
rank = (cls_loss * (pos - 1)).argsort(axis=1).argsort(axis=1)
hard_negative = rank < nd.maximum(self._min_hard_negatives, pos.sum(axis=1)
* self._negative_mining_ratio).expand_dims(-1)
# mask out if not positive or negative
cls_loss = nd.where((pos + hard_negative) > 0, cls_loss, nd.zeros_like(cls_loss))
cls_losses.append(nd.sum(cls_loss, axis=0, exclude=True) / max(1., num_pos_all))
bp = _reshape_like(nd, bp, bt)
box_loss = nd.abs(bp - bt)
box_loss = nd.where(box_loss > self._rho, box_loss - 0.5 * self._rho,
(0.5 / self._rho) * nd.square(box_loss))
# box loss only apply to positive samples
box_loss = box_loss * pos.expand_dims(axis=-1)
box_losses.append(nd.sum(box_loss, axis=0, exclude=True) / max(1., num_pos_all))
sum_losses.append(cls_losses[-1] + self._lambd * box_losses[-1])
return sum_losses, cls_losses, box_losses
示例14: refine_bbox_nd
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros_like [as 别名]
def refine_bbox_nd(bbox, bbox_delta, im_info=None, means=None, stds=None):
xmin, ymin, xmax, ymax = nd.split(data=bbox, num_outputs=4, axis=1)
bbox_width = xmax - xmin + 1.
bbox_height = ymax - ymin + 1.
center_x = 0.5 * (xmin + xmax)
center_y = 0.5 * (ymin + ymax)
bbox_delta_reshape = nd.Reshape(data=bbox_delta, shape=(0, -1, 4))
dx, dy, dw, dh = nd.split(data=bbox_delta_reshape,
num_outputs=4, axis=2, squeeze_axis=1)
if (means is not None) and (stds is not None):
dx = dx * stds[0] + means[0]
dy = dy * stds[1] + means[1]
dw = dw * stds[2] + means[2]
dh = dh * stds[3] + means[3]
refine_center_x = nd.broadcast_add(lhs=center_x,
rhs=nd.broadcast_mul(lhs=bbox_width, rhs=dx))
refine_center_y = nd.broadcast_add(lhs=center_y,
rhs=nd.broadcast_mul(lhs=bbox_height, rhs=dy))
refined_width = nd.broadcast_mul(lhs=bbox_width, rhs=nd.exp(dw))
refined_height = nd.broadcast_mul(lhs=bbox_height, rhs=nd.exp(dh))
w_offset = 0.5 * (refined_width - 1.)
h_offset = 0.5 * (refined_height - 1.)
refined_xmin = nd.expand_dims(refine_center_x - w_offset, axis=1)
refined_ymin = nd.expand_dims(refine_center_y - h_offset, axis=1)
refined_xmax = nd.expand_dims(refine_center_x + w_offset, axis=1)
refined_ymax = nd.expand_dims(refine_center_y + h_offset, axis=1)
refined_bbox = nd.concat(refined_xmin, refined_ymin, refined_xmax, refined_ymax, dim=1)
if im_info is not None:
# assume im_info [[height, width, scale]] with shape (1,3)
im_hw = nd.slice_axis(im_info, axis=1, begin=0, end=2)
im_wh = nd.reverse(im_hw, axis=1)
im_wh = im_wh - 1.
im_wh = nd.tile(data=im_wh, reps=(1, 2))
im_wh = nd.Reshape(im_wh, shape=(1, 4, 1))
refined_bbox = nd.broadcast_minimum(lhs=refined_bbox, rhs=im_wh)
refined_bbox = nd.broadcast_maximum(lhs=refined_bbox,
rhs=nd.zeros_like(refined_bbox))
# print refined_bbox.debug_str()
return refined_bbox