Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
28dbadf7
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
28dbadf7
编写于
9月 29, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(imperative): add remap opr
GitOrigin-RevId: 1fa7e18d4b9c43dff91ef4c465a0d61416fe34cf
上级
280861ae
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
95 addition
and
4 deletion
+95
-4
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+64
-4
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+31
-0
未找到文件。
imperative/python/megengine/functional/nn.py
浏览文件 @
28dbadf7
...
...
@@ -57,6 +57,7 @@ __all__ = [
"max_pool2d"
,
"one_hot"
,
"prelu"
,
"remap"
,
"softmax"
,
"softplus"
,
"svd"
,
...
...
@@ -892,7 +893,7 @@ def warp_perspective(
border_mode
:
str
=
"REPLICATE"
,
border_val
:
float
=
0.0
,
interp_mode
:
str
=
"LINEAR"
,
):
)
->
Tensor
:
r
"""
Applies perspective transformation to batched 2D images.
...
...
@@ -907,9 +908,12 @@ def warp_perspective(
:param inp: input image.
:param M: `(batch, 3, 3)` transformation matrix.
:param dsize: `(h, w)` size of the output image.
:param border_mode: pixel extrapolation method. Default: "REPLICATE"
:param border_mode: pixel extrapolation method.
Default: "REPLICATE". Currently also support "CONSTANT", "REFLECT",
"REFLECT_101", "WRAP".
:param border_val: value used in case of a constant border. Default: 0
:param interp_mode: interpolation methods. Default: "LINEAR"
:param interp_mode: interpolation methods.
Default: "LINEAR". Currently only support "LINEAR" mode.
:return: output tensor.
Note:
...
...
@@ -951,6 +955,62 @@ def warp_perspective(
return
result
def
remap
(
inp
:
Tensor
,
map_xy
:
Tensor
,
border_mode
:
str
=
"REPLICATE"
,
scalar
:
float
=
0.0
,
interp_mode
:
str
=
"LINEAR"
,
)
->
Tensor
:
r
"""
Applies remap transformation to batched 2D images.
The input images are transformed to the output images by the tensor map_xy.
The output's H and W are same as map_xy's H and W.
:param inp: input image
:param map_xy: (batch, oh, ow, 2) transformation matrix
:param border_mode: pixel extrapolation method.
Default: "REPLICATE". Currently also support "CONSTANT", "REFLECT",
"REFLECT_101", "WRAP".
:param scalar: value used in case of a constant border. Default: 0
:param interp_mode: interpolation methods.
Default: "LINEAR". Currently only support "LINEAR" mode.
:return: output tensor.
Examples:
.. testcode::
import numpy as np
from megengine import tensor
import megengine.functional as F
inp_shape = (1, 1, 4, 4)
inp = tensor(np.arange(16, dtype=np.float32).reshape(inp_shape))
map_xy_shape = (1, 2, 2, 2)
map_xy = tensor(np.array([[[1., 0.],[0., 1.]],
[[0., 1.],[0., 1.]]],
dtype=np.float32).reshape(map_xy_shape))
out = F.remap(inp, map_xy)
print(out.numpy())
Outputs:
.. testoutput::
[[[[1. 4.]
[4. 4.]]]]
"""
op
=
builtin
.
Remap
(
imode
=
interp_mode
,
border_type
=
border_mode
,
format
=
"NCHW"
,
scalar
=
scalar
)
assert
isinstance
(
inp
,
(
Tensor
,
megbrain_graph
.
VarNode
)),
"inp must be Tensor type"
(
result
,)
=
apply
(
op
,
inp
,
map_xy
)
return
result
def
matmul
(
inp1
:
Tensor
,
inp2
:
Tensor
,
...
...
@@ -1534,7 +1594,7 @@ def nms(
:param boxes: tensor of shape `(N, 4)`; the boxes to perform nms on; each box is expected to be in `(x1, y1, x2, y2)` format.
:param iou_thresh: IoU threshold for overlapping.
:param scores: tensor of shape `(N,)`, the score of boxes.
:param max_output: the maximum number of boxes to keep; it is optional if this operator is not traced
:param max_output: the maximum number of boxes to keep; it is optional if this operator is not traced
otherwise it required to be specified; if it is not specified, all boxes are kept.
:return: indices of the elements that have been kept by NMS.
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
28dbadf7
...
...
@@ -308,6 +308,37 @@ def test_one_hot():
onehot_high_dimension
()
def
test_warp_perspective
():
inp_shape
=
(
1
,
1
,
4
,
4
)
x
=
tensor
(
np
.
arange
(
16
,
dtype
=
np
.
float32
).
reshape
(
inp_shape
))
M_shape
=
(
1
,
3
,
3
)
# M defines a translation: dst(1, 1, h, w) = rst(1, 1, h+1, w+1)
M
=
tensor
(
np
.
array
(
[[
1.0
,
0.0
,
1.0
],
[
0.0
,
1.0
,
1.0
],
[
0.0
,
0.0
,
1.0
]],
dtype
=
np
.
float32
).
reshape
(
M_shape
)
)
outp
=
F
.
warp_perspective
(
x
,
M
,
(
2
,
2
))
np
.
testing
.
assert_equal
(
outp
.
numpy
(),
np
.
array
([[[[
5.0
,
6.0
],
[
9.0
,
10.0
]]]],
dtype
=
np
.
float32
)
)
def
test_remap
():
inp_shape
=
(
1
,
1
,
4
,
4
)
inp
=
tensor
(
np
.
arange
(
16
,
dtype
=
np
.
float32
).
reshape
(
inp_shape
))
map_xy_shape
=
(
1
,
2
,
2
,
2
)
map_xy
=
tensor
(
np
.
array
(
[[[
1.0
,
0.0
],
[
0.0
,
1.0
]],
[[
0.0
,
1.0
],
[
0.0
,
1.0
]]],
dtype
=
np
.
float32
).
reshape
(
map_xy_shape
)
)
outp
=
F
.
remap
(
inp
,
map_xy
)
np
.
testing
.
assert_equal
(
outp
.
numpy
(),
np
.
array
([[[[
1.0
,
4.0
],
[
4.0
,
4.0
]]]],
dtype
=
np
.
float32
)
)
def
test_binary_cross_entropy
():
data1_shape
=
(
2
,
2
)
label1_shape
=
(
2
,
2
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录