本文整理汇总了Python中mxnet.nd.full方法的典型用法代码示例。如果您正苦于以下问题:Python nd.full方法的具体用法?Python nd.full怎么用?Python nd.full使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.nd
的用法示例。
在下文中一共展示了nd.full方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_pairwise_multi_position_embedding_nd
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import full [as 别名]
def extract_pairwise_multi_position_embedding_nd(position_mat, feat_dim, wave_length=1000):
""" Extract multi-class position embedding
Args:
position_mat: [num_fg_classes, num_rois, num_rois, 4]
feat_dim: dimension of embedding feature
wave_length:
Returns:
embedding: [num_fg_classes, num_rois, num_rois, feat_dim]
"""
feat_range = nd.arange(0, feat_dim / 8)
dim_mat = nd.broadcast_power(lhs=nd.full((1,), wave_length),
rhs=(8. / feat_dim) * feat_range)
dim_mat = nd.Reshape(dim_mat, shape=(1, 1, 1, 1, -1))
position_mat = nd.expand_dims(100.0 * position_mat, axis=4)
div_mat = nd.broadcast_div(lhs=position_mat, rhs=dim_mat)
sin_mat = nd.sin(data=div_mat)
cos_mat = nd.cos(data=div_mat)
# embedding, [num_fg_classes, num_rois, num_rois, 4, feat_dim/4]
embedding = nd.concat(sin_mat, cos_mat, dim=4)
embedding = nd.Reshape(embedding, shape=(0, 0, 0, feat_dim))
return embedding
示例2: extract_rank_embedding_nd
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import full [as 别名]
def extract_rank_embedding_nd(rank_dim, feat_dim, wave_length=1000):
rank_range = nd.arange(0, rank_dim)
feat_range = nd.arange(0, feat_dim / 2)
dim_mat = nd.broadcast_power(lhs=nd.full((1,), wave_length),
rhs=(2. / feat_dim) * feat_range)
dim_mat = nd.Reshape(dim_mat, shape=(1, -1))
rank_mat = nd.expand_dims(rank_range, axis=1)
div_mat = nd.broadcast_div(lhs=rank_mat, rhs=dim_mat)
sin_mat = nd.sin(data=div_mat)
cos_mat = nd.cos(data=div_mat)
embedding = nd.concat(sin_mat, cos_mat, dim=1)
return embedding
示例3: random_expand
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import full [as 别名]
def random_expand(src, max_ratio=4, fill=0, keep_ratio=True):
"""Random expand original image with borders, this is identical to placing
the original image on a larger canvas.
Parameters
----------
src : mxnet.nd.NDArray
The original image with HWC format.
max_ratio : int or float
Maximum ratio of the output image on both direction(vertical and horizontal)
fill : int or float or array-like
The value(s) for padded borders. If `fill` is numerical type, RGB channels
will be padded with single value. Otherwise `fill` must have same length
as image channels, which resulted in padding with per-channel values.
keep_ratio : bool
If `True`, will keep output image the same aspect ratio as input.
Returns
-------
mxnet.nd.NDArray
Augmented image.
tuple
Tuple of (offset_x, offset_y, new_width, new_height)
"""
if max_ratio <= 1:
return src, (0, 0, src.shape[1], src.shape[0])
h, w, c = src.shape
ratio_x = random.uniform(1, max_ratio)
if keep_ratio:
ratio_y = ratio_x
else:
ratio_y = random.uniform(1, max_ratio)
oh, ow = int(h * ratio_y), int(w * ratio_x)
off_y = random.randint(0, oh - h)
off_x = random.randint(0, ow - w)
# make canvas
if isinstance(fill, numeric_types):
dst = nd.full(shape=(oh, ow, c), val=fill, dtype=src.dtype)
else:
fill = nd.array(fill, dtype=src.dtype, ctx=src.context)
if not c == fill.size:
raise ValueError("Channel and fill size mismatch, {} vs {}".format(c, fill.size))
dst = nd.tile(fill.reshape((1, c)), reps=(oh * ow, 1)).reshape((oh, ow, c))
dst[off_y:off_y+h, off_x:off_x+w, :] = src
return dst, (off_x, off_y, ow, oh)
示例4: resize_contain
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import full [as 别名]
def resize_contain(src, size, fill=0):
"""Resize the image to fit in the given area while keeping aspect ratio.
If both the height and the width in `size` are larger than
the height and the width of input image, the image is placed on
the center with an appropriate padding to match `size`.
Otherwise, the input image is scaled to fit in a canvas whose size
is `size` while preserving aspect ratio.
Parameters
----------
src : mxnet.nd.NDArray
The original image with HWC format.
size : tuple
Tuple of length 2 as (width, height).
fill : int or float or array-like
The value(s) for padded borders. If `fill` is numerical type, RGB channels
will be padded with single value. Otherwise `fill` must have same length
as image channels, which resulted in padding with per-channel values.
Returns
-------
mxnet.nd.NDArray
Augmented image.
tuple
Tuple of (offset_x, offset_y, scaled_x, scaled_y)
"""
h, w, c = src.shape
ow, oh = size
scale_h = oh / h
scale_w = ow / w
scale = min(min(scale_h, scale_w), 1)
scaled_x = int(w * scale)
scaled_y = int(h * scale)
if scale < 1:
src = mx.image.imresize(src, scaled_x, scaled_y)
off_y = (oh - scaled_y) // 2 if scaled_y < oh else 0
off_x = (ow - scaled_x) // 2 if scaled_x < ow else 0
# make canvas
if isinstance(fill, numeric_types):
dst = nd.full(shape=(oh, ow, c), val=fill, dtype=src.dtype)
else:
fill = nd.array(fill, ctx=src.context)
if not c == fill.size:
raise ValueError("Channel and fill size mismatch, {} vs {}".format(c, fill.size))
dst = nd.repeat(fill, repeats=oh * ow).reshape((oh, ow, c))
dst[off_y:off_y+scaled_y, off_x:off_x+scaled_x, :] = src
return dst, (off_x, off_y, scaled_x, scaled_y)
示例5: resize_contain
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import full [as 别名]
def resize_contain(src, size, fill=0):
"""Resize the image to fit in the given area while keeping aspect ratio.
If both the height and the width in `size` are larger than
the height and the width of input image, the image is placed on
the center with an appropriate padding to match `size`.
Otherwise, the input image is scaled to fit in a canvas whose size
is `size` while preserving aspect ratio.
Parameters
----------
src : mxnet.nd.NDArray
The original image with HWC format.
size : tuple
Tuple of length 2 as (width, height).
fill : int or float or array-like
The value(s) for padded borders. If `fill` is numerical type, RGB channels
will be padded with single value. Otherwise `fill` must have same length
as image channels, which resulted in padding with per-channel values.
Returns
-------
mxnet.nd.NDArray
Augmented image.
tuple
Tuple of (offset_x, offset_y, scaled_x, scaled_y)
"""
h, w, c = src.shape
ow, oh = size
scale_h = oh / h
scale_w = oh / w
scale = min(min(scale_h, scale_w), 1)
scaled_x = int(w * scale)
scaled_y = int(h * scale)
if scale < 1:
src = mx.image.imresize(src, scaled_x, scaled_y)
off_y = (oh - scaled_y) // 2 if scaled_y < oh else 0
off_x = (ow - scaled_x) // 2 if scaled_x < ow else 0
# make canvas
if isinstance(fill, numeric_types):
dst = nd.full(shape=(oh, ow, c), val=fill, dtype=src.dtype)
else:
fill = nd.array(fill, ctx=src.context)
if not c == fill.size:
raise ValueError("Channel and fill size mismatch, {} vs {}".format(c, fill.size))
dst = nd.repeat(fill, repeats=oh * ow).reshape((oh, ow, c))
dst[off_y:off_y+scaled_y, off_x:off_x+scaled_x, :] = src
return dst, (off_x, off_y, scaled_x, scaled_y)