Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
933188aa
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
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看板
提交
933188aa
编写于
4月 19, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(functional/nn): support F.warp_perspective with `mat_idx`
GitOrigin-RevId: 66910c8bd8ed96f888f50653e8cfc4326b12cfe8
上级
8585aa61
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
64 addition
and
24 deletion
+64
-24
imperative/python/megengine/functional/vision.py
imperative/python/megengine/functional/vision.py
+38
-24
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+26
-0
未找到文件。
imperative/python/megengine/functional/vision.py
浏览文件 @
933188aa
...
@@ -300,18 +300,18 @@ def remap(
...
@@ -300,18 +300,18 @@ def remap(
def
warp_affine
(
def
warp_affine
(
inp
:
Tensor
,
inp
:
Tensor
,
weigh
t
:
Tensor
,
ma
t
:
Tensor
,
out_shape
,
out_shape
:
Union
[
Tuple
[
int
,
int
],
int
,
Tensor
]
,
border_mode
=
"replicate"
,
border_mode
:
str
=
"replicate"
,
border_val
=
0
,
border_val
:
float
=
0.
0
,
format
=
"NHWC"
,
format
:
str
=
"NHWC"
,
i
mode
=
"linear"
,
i
nterp_mode
:
str
=
"linear"
,
):
)
->
Tensor
:
"""
"""
Batched affine transform on 2D images.
Batched affine transform on 2D images.
:param inp: input image.
:param inp: input image.
:param
weight: weight tensor
.
:param
mat: `(batch, 2, 3)` transformation matrix
.
:param out_shape: output tensor shape.
:param out_shape: output tensor shape.
:param border_mode: pixel extrapolation method.
:param border_mode: pixel extrapolation method.
Default: "wrap". Currently "constant", "reflect",
Default: "wrap". Currently "constant", "reflect",
...
@@ -319,30 +319,35 @@ def warp_affine(
...
@@ -319,30 +319,35 @@ def warp_affine(
:param border_val: value used in case of a constant border. Default: 0
:param border_val: value used in case of a constant border. Default: 0
:param format: "NHWC" as default based on historical concerns,
:param format: "NHWC" as default based on historical concerns,
"NCHW" is also supported. Default: "NHWC".
"NCHW" is also supported. Default: "NHWC".
:param imode: interpolation methods. Could be "linear", "nearest", "cubic", "area".
:param i
nterp_
mode: interpolation methods. Could be "linear", "nearest", "cubic", "area".
Default: "linear".
Default: "linear".
:return: output tensor.
:return: output tensor.
.. note::
.. note::
Here all available options for params are listed,
Here all available options for params are listed,
however it does not mean that you can use all the combinations.
however it does not mean that you can use all the combinations.
On different platforms, different combinations are supported.
On different platforms, different combinations are supported.
"""
"""
op
=
builtin
.
WarpAffine
(
op
=
builtin
.
WarpAffine
(
border_mode
=
border_mode
,
border_val
=
border_val
,
format
=
format
,
imode
=
imode
border_mode
=
border_mode
,
border_val
=
border_val
,
format
=
format
,
imode
=
interp_mode
,
)
)
out_shape
=
utils
.
astensor1d
(
out_shape
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
out_shape
=
utils
.
astensor1d
(
out_shape
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
(
result
,)
=
apply
(
op
,
inp
,
weigh
t
,
out_shape
)
(
result
,)
=
apply
(
op
,
inp
,
ma
t
,
out_shape
)
return
result
return
result
def
warp_perspective
(
def
warp_perspective
(
inp
:
Tensor
,
inp
:
Tensor
,
M
:
Tensor
,
mat
:
Tensor
,
dsize
:
Union
[
Tuple
[
int
,
int
],
int
,
Tensor
],
out_shape
:
Union
[
Tuple
[
int
,
int
],
int
,
Tensor
],
mat_idx
:
Optional
[
Union
[
Iterable
[
int
],
Tensor
]]
=
None
,
border_mode
:
str
=
"replicate"
,
border_mode
:
str
=
"replicate"
,
border_val
:
float
=
0.0
,
border_val
:
float
=
0.0
,
format
:
str
=
"NCHW"
,
interp_mode
:
str
=
"linear"
,
interp_mode
:
str
=
"linear"
,
)
->
Tensor
:
)
->
Tensor
:
r
"""
r
"""
...
@@ -356,20 +361,25 @@ def warp_perspective(
...
@@ -356,20 +361,25 @@ def warp_perspective(
\frac{M_{10}h + M_{11}w + M_{12}}{M_{20}h + M_{21}w + M_{22}}
\frac{M_{10}h + M_{11}w + M_{12}}{M_{20}h + M_{21}w + M_{22}}
\right)
\right)
Optionally, we can set `mat_idx` to assign different transformations to the same image,
otherwise the input images and transformations should be one-to-one correnspondence.
:param inp: input image.
:param inp: input image.
:param M: `(batch, 3, 3)` transformation matrix.
:param mat: `(batch, 3, 3)` transformation matrix.
:param dsize: `(h, w)` size of the output image.
:param out_shape: `(h, w)` size of the output image.
:param mat_idx: `(batch, )` image batch idx assigned to each matrix. Default: None
:param border_mode: pixel extrapolation method.
:param border_mode: pixel extrapolation method.
Default: "replicate". Currently also support "constant", "reflect",
Default: "replicate". Currently also support "constant", "reflect",
"reflect_101", "wrap".
"reflect_101", "wrap".
:param border_val: value used in case of a constant border. Default: 0
:param border_val: value used in case of a constant border. Default: 0
:param format: "NHWC" is also supported. Default: "NCHW".
:param interp_mode: interpolation methods.
:param interp_mode: interpolation methods.
Default: "linear". Currently only support "linear" mode.
Default: "linear". Currently only support "linear" mode.
:return: output tensor.
:return: output tensor.
Note
:
.. note:
:
The transformation matrix is the inverse of that used by `cv2.warpPerspective`.
The transformation matrix is the inverse of that used by `cv2.warpPerspective`.
Examples:
Examples:
...
@@ -398,11 +408,15 @@ def warp_perspective(
...
@@ -398,11 +408,15 @@ def warp_perspective(
"""
"""
op
=
builtin
.
WarpPerspective
(
op
=
builtin
.
WarpPerspective
(
imode
=
interp_mode
,
bmode
=
border_mode
,
format
=
"NCHW"
,
border_val
=
border_val
imode
=
interp_mode
,
bmode
=
border_mode
,
format
=
format
,
border_val
=
border_val
)
)
inp
,
M
=
utils
.
convert_inputs
(
inp
,
M
)
inp
,
mat
=
utils
.
convert_inputs
(
inp
,
mat
)
dsize
=
astensor1d
(
dsize
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
out_shape
=
astensor1d
(
out_shape
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
(
result
,)
=
apply
(
op
,
inp
,
M
,
dsize
)
if
mat_idx
is
not
None
:
mat_idx
=
astensor1d
(
mat_idx
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
(
result
,)
=
apply
(
op
,
inp
,
mat
,
mat_idx
,
out_shape
)
return
result
(
result
,)
=
apply
(
op
,
inp
,
mat
,
out_shape
)
return
result
return
result
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
933188aa
...
@@ -370,6 +370,32 @@ def test_warp_perspective():
...
@@ -370,6 +370,32 @@ def test_warp_perspective():
)
)
def
test_warp_perspective_mat_idx
():
inp_shape
=
(
2
,
1
,
4
,
4
)
x
=
tensor
(
np
.
arange
(
32
,
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
)
)
M
=
F
.
concat
([
M
,]
*
4
,
0
)
outp
=
F
.
vision
.
warp_perspective
(
x
,
M
,
(
2
,
2
),
mat_idx
=
[
0
,
1
,
1
,
0
])
np
.
testing
.
assert_equal
(
outp
.
numpy
(),
np
.
array
(
[
[[[
5.0
,
6.0
],
[
9.0
,
10.0
]]],
[[[
21.0
,
22.0
],
[
25.0
,
26.0
]]],
[[[
21.0
,
22.0
],
[
25.0
,
26.0
]]],
[[[
5.0
,
6.0
],
[
9.0
,
10.0
]]],
],
dtype
=
np
.
float32
,
),
)
def
test_warp_affine
():
def
test_warp_affine
():
inp_shape
=
(
1
,
3
,
3
,
3
)
inp_shape
=
(
1
,
3
,
3
,
3
)
x
=
tensor
(
np
.
arange
(
27
,
dtype
=
np
.
float32
).
reshape
(
inp_shape
))
x
=
tensor
(
np
.
arange
(
27
,
dtype
=
np
.
float32
).
reshape
(
inp_shape
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录