Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
e3a3e0cd
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看板
提交
e3a3e0cd
编写于
1月 22, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/functional): add warp_affine
GitOrigin-RevId: 2b333ccd12c28e4c5a6bfd8060c43f0c8f03482a
上级
329306b0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
92 addition
and
0 deletion
+92
-0
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+40
-0
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+18
-0
imperative/src/impl/ops/warp_affine.cpp
imperative/src/impl/ops/warp_affine.cpp
+32
-0
src/core/include/megbrain/ir/ops.td
src/core/include/megbrain/ir/ops.td
+2
-0
未找到文件。
imperative/python/megengine/functional/nn.py
浏览文件 @
e3a3e0cd
...
...
@@ -62,6 +62,7 @@ __all__ = [
"softmax"
,
"softplus"
,
"svd"
,
"warp_affine"
,
"warp_perspective"
,
"conv1d"
,
]
...
...
@@ -914,6 +915,45 @@ def resize(
return
result
def
warp_affine
(
inp
:
Tensor
,
weight
:
Tensor
,
out_shape
,
border_mode
=
"REPLICATE"
,
border_val
=
0
,
format
=
"NHWC"
,
imode
=
"LINEAR"
,
):
"""
Batched affine transform on 2D images.
:param inp: input image.
:param weight: weight tensor.
:param out_shape: output tensor shape.
:param border_mode: pixel extrapolation method.
Default: "WRAP". Currently "CONSTANT", "REFLECT",
"REFLECT_101", "ISOLATED", "WRAP", "REPLICATE", "TRANSPARENT" are supported.
:param border_val: value used in case of a constant border. Default: 0
:param format: "NHWC" as default based on historical concerns,
"NCHW" is also supported. Default: "NCHW".
:param imode: interpolation methods. Could be "LINEAR", "NEAREST", "CUBIC", "AREA".
Default: "LINEAR".
:return: output tensor.
.. note::
Here all available options for params are listed,
however it does not mean that you can use all the combinations.
On different platforms, different combinations are supported.
"""
op
=
builtin
.
WarpAffine
(
border_mode
=
border_mode
,
border_val
=
border_val
,
format
=
format
,
imode
=
imode
)
out_shape
=
utils
.
astensor1d
(
out_shape
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
(
result
,)
=
apply
(
op
,
inp
,
weight
,
out_shape
)
return
result
def
warp_perspective
(
inp
:
Tensor
,
M
:
Tensor
,
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
e3a3e0cd
...
...
@@ -369,6 +369,24 @@ def test_warp_perspective():
)
def
test_warp_affine
():
inp_shape
=
(
1
,
3
,
3
,
3
)
x
=
tensor
(
np
.
arange
(
27
,
dtype
=
np
.
float32
).
reshape
(
inp_shape
))
weightv
=
[[[
1.26666667
,
0.6
,
-
83.33333333
],
[
-
0.33333333
,
1
,
66.66666667
]]]
outp
=
F
.
warp_affine
(
x
,
tensor
(
weightv
),
(
2
,
2
),
border_mode
=
"WRAP"
)
res
=
np
.
array
(
[
[
[[
7.875
,
8.875
,
9.875
],
[
8.90625
,
9.90625
,
10.90625
]],
[[
18.75
,
19.75
,
20.75
],
[
14.90625
,
15.90625
,
16.90625
]],
]
],
dtype
=
np
.
float32
,
)
if
not
is_cuda_available
():
np
.
testing
.
assert_almost_equal
(
outp
.
numpy
(),
res
,
5
)
def
test_remap
():
inp_shape
=
(
1
,
1
,
4
,
4
)
inp
=
tensor
(
np
.
arange
(
16
,
dtype
=
np
.
float32
).
reshape
(
inp_shape
))
...
...
imperative/src/impl/ops/warp_affine.cpp
0 → 100644
浏览文件 @
e3a3e0cd
/**
* \file imperative/src/impl/ops/warp_affine.cpp
* MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
*
* Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
#include "../op_trait.h"
#include "megbrain/imperative/ops/autogen.h"
#include "megbrain/opr/imgproc.h"
namespace
mgb
::
imperative
{
namespace
{
namespace
warp_affine
{
auto
apply_on_var_node
(
const
OpDef
&
def
,
const
VarNodeArray
&
inputs
)
{
mgb_assert
(
inputs
.
size
()
==
3
);
auto
&&
op
=
static_cast
<
const
WarpAffine
&>
(
def
);
return
opr
::
WarpAffine
::
make
(
inputs
[
0
],
inputs
[
1
],
inputs
[
2
],
op
.
param
());
}
OP_TRAIT_REG
(
WarpAffine
,
WarpAffine
)
.
apply_on_var_node
(
apply_on_var_node
)
.
fallback
();
}}
// warp_affine
}
// namespace mgb::imperative
src/core/include/megbrain/ir/ops.td
浏览文件 @
e3a3e0cd
...
...
@@ -74,6 +74,8 @@ def ROIAlign: MgbHashableOp<"ROIAlign", [ROIAlignParam]>;
def WarpPerspective: MgbHashableOp<"WarpPerspective", [WarpPerspectiveParam]>;
def WarpAffine: MgbHashableOp<"WarpAffine", [WarpAffineParam]>;
def Remap: MgbHashableOp<"Remap", [RemapParam]>;
def Resize: MgbHashableOp<"Resize", [ResizeParam]>;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录