Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
951e094d
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
951e094d
编写于
4月 03, 2020
作者:
Z
zhaozhenlong
提交者:
高东海
4月 10, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add api image gradients
上级
14c5c1b5
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
158 addition
and
2 deletion
+158
-2
mindspore/nn/layer/__init__.py
mindspore/nn/layer/__init__.py
+2
-2
mindspore/nn/layer/basic.py
mindspore/nn/layer/basic.py
+45
-0
tests/st/ops/davinci/test_image_gradients.py
tests/st/ops/davinci/test_image_gradients.py
+62
-0
tests/ut/python/nn/test_image_gradients.py
tests/ut/python/nn/test_image_gradients.py
+49
-0
未找到文件。
mindspore/nn/layer/__init__.py
浏览文件 @
951e094d
...
...
@@ -22,7 +22,7 @@ from .normalization import BatchNorm1d, BatchNorm2d, LayerNorm
from
.container
import
SequentialCell
,
CellList
from
.conv
import
Conv2d
,
Conv2dTranspose
from
.lstm
import
LSTM
from
.basic
import
Dropout
,
Flatten
,
Dense
,
ClipByNorm
,
Norm
,
OneHot
from
.basic
import
Dropout
,
Flatten
,
Dense
,
ClipByNorm
,
Norm
,
OneHot
,
ImageGradients
from
.embedding
import
Embedding
from
.pooling
import
AvgPool2d
,
MaxPool2d
...
...
@@ -31,7 +31,7 @@ __all__ = ['Softmax', 'LogSoftmax', 'ReLU', 'ReLU6', 'Tanh', 'GELU', 'Sigmoid',
'SequentialCell'
,
'CellList'
,
'Conv2d'
,
'Conv2dTranspose'
,
'LSTM'
,
'Dropout'
,
'Flatten'
,
'Dense'
,
'ClipByNorm'
,
'Norm'
,
'OneHot'
,
'Dropout'
,
'Flatten'
,
'Dense'
,
'ClipByNorm'
,
'Norm'
,
'OneHot'
,
'ImageGradients'
,
'Embedding'
,
'AvgPool2d'
,
'MaxPool2d'
,
]
mindspore/nn/layer/basic.py
浏览文件 @
951e094d
...
...
@@ -370,3 +370,48 @@ class OneHot(Cell):
def
construct
(
self
,
indices
):
return
self
.
onehot
(
indices
,
self
.
depth
,
self
.
on_value
,
self
.
off_value
)
class
ImageGradients
(
Cell
):
r
"""
Returns two tensors, the first is along the height dimension and the second is along the width dimension.
Assume an image shape is :math:`h*w`. The gradients along the height and the width are :math:`dy` and :math:`dx`,
respectively.
.. math::
dy[i] = \begin{cases} image[i+1, :]-image[i, :], &if\ 0<=i<h-1 \cr
0, &if\ i==h-1\end{cases}
dx[i] = \begin{cases} image[:, i+1]-image[:, i], &if\ 0<=i<w-1 \cr
0, &if\ i==w-1\end{cases}
Inputs:
- **images** (Tensor) - The input image data, with format 'NCHW'.
Outputs:
- **dy** (Tensor) - vertical image gradients, the same type and shape as input.
- **dx** (Tensor) - horizontal image gradients, the same type and shape as input.
Examples:
>>> net = nn.ImageGradients()
>>> image = Tensor(np.array([[[[1,2],[3,4]]]]), dtype=mstype.int32)
>>> net(image)
[[[[2,2]
[0,0]]]]
[[[[1,0]
[1,0]]]]
"""
def
__init__
(
self
):
super
(
ImageGradients
,
self
).
__init__
()
def
construct
(
self
,
images
):
batch_size
,
depth
,
height
,
width
=
P
.
Shape
()(
images
)
dy
=
images
[:,
:,
1
:,
:]
-
images
[:,
:,
:
height
-
1
,
:]
dy_last
=
P
.
Fill
()(
P
.
DType
()(
images
),
(
batch_size
,
depth
,
1
,
width
),
0
)
dy
=
P
.
Concat
(
2
)((
dy
,
dy_last
))
dx
=
images
[:,
:,
:,
1
:]
-
images
[:,
:,
:,
:
width
-
1
]
dx_last
=
P
.
Fill
()(
P
.
DType
()(
images
),
(
batch_size
,
depth
,
height
,
1
),
0
)
dx
=
P
.
Concat
(
3
)((
dx
,
dx_last
))
return
dy
,
dx
tests/st/ops/davinci/test_image_gradients.py
0 → 100644
浏览文件 @
951e094d
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import
numpy
as
np
import
mindspore.nn
as
nn
import
mindspore.context
as
context
import
mindspore.common.dtype
as
mstype
from
mindspore
import
Tensor
from
mindspore.common.api
import
ms_function
context
.
set_context
(
device_target
=
"Ascend"
)
class
Net
(
nn
.
Cell
):
def
__init__
(
self
):
super
(
Net
,
self
).
__init__
()
self
.
image_gradients
=
nn
.
ImageGradients
()
@
ms_function
def
construct
(
self
,
x
):
return
self
.
image_gradients
(
x
)
def
test_image_gradients
():
image
=
Tensor
(
np
.
array
([[[[
1
,
2
],[
3
,
4
]]]]),
dtype
=
mstype
.
int32
)
expected_dy
=
np
.
array
([[[[
2
,
2
],[
0
,
0
]]]]).
astype
(
np
.
int32
)
expected_dx
=
np
.
array
([[[[
1
,
0
],[
1
,
0
]]]]).
astype
(
np
.
int32
)
net
=
Net
()
dy
,
dx
=
net
(
image
)
assert
np
.
any
(
dx
.
asnumpy
()
-
expected_dx
)
==
False
assert
np
.
any
(
dy
.
asnumpy
()
-
expected_dy
)
==
False
def
test_image_gradients_multi_channel_depth
():
# 4 x 2 x 2 x 2
dtype
=
mstype
.
int32
image
=
Tensor
(
np
.
array
([[[[
1
,
2
],[
3
,
4
]],
[[
5
,
6
],[
7
,
8
]]],
[[[
3
,
5
],[
7
,
9
]],
[[
11
,
13
],[
15
,
17
]]],
[[[
5
,
10
],[
15
,
20
]],
[[
25
,
30
],[
35
,
40
]]],
[[[
10
,
20
],[
30
,
40
]],
[[
50
,
60
],[
70
,
80
]]]]),
dtype
=
dtype
)
expected_dy
=
Tensor
(
np
.
array
([[[[
2
,
2
],[
0
,
0
]],
[[
2
,
2
],[
0
,
0
]]],
[[[
4
,
4
],[
0
,
0
]],
[[
4
,
4
],[
0
,
0
]]],
[[[
10
,
10
],[
0
,
0
]],
[[
10
,
10
],[
0
,
0
]]],
[[[
20
,
20
],[
0
,
0
]],
[[
20
,
20
],[
0
,
0
]]]]),
dtype
=
dtype
)
expected_dx
=
Tensor
(
np
.
array
([[[[
1
,
0
],[
1
,
0
]],
[[
1
,
0
],[
1
,
0
]]],
[[[
2
,
0
],[
2
,
0
]],
[[
2
,
0
],[
2
,
0
]]],
[[[
5
,
0
],[
5
,
0
]],
[[
5
,
0
],[
5
,
0
]]],
[[[
10
,
0
],[
10
,
0
]],
[[
10
,
0
],[
10
,
0
]]]]),
dtype
=
dtype
)
net
=
Net
()
dy
,
dx
=
net
(
image
)
assert
np
.
any
(
dx
.
asnumpy
()
-
expected_dx
.
asnumpy
())
==
False
assert
np
.
any
(
dy
.
asnumpy
()
-
expected_dy
.
asnumpy
())
==
False
tests/ut/python/nn/test_image_gradients.py
0 → 100644
浏览文件 @
951e094d
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test loss """
import
numpy
as
np
import
mindspore.nn
as
nn
import
mindspore.context
as
context
import
mindspore.common.dtype
as
mstype
from
mindspore
import
Tensor
from
mindspore.common.api
import
_executor
from
mindspore.common.api
import
ms_function
context
.
set_context
(
device_target
=
"Ascend"
)
class
Net
(
nn
.
Cell
):
def
__init__
(
self
):
super
(
Net
,
self
).
__init__
()
self
.
image_gradients
=
nn
.
ImageGradients
()
@
ms_function
def
construct
(
self
,
x
):
return
self
.
image_gradients
(
x
)
def
test_compile
():
# input shape 1 x 1 x 2 x 2
image
=
Tensor
(
np
.
array
([[[[
1
,
2
],[
3
,
4
]]]]),
dtype
=
mstype
.
int32
)
net
=
Net
()
_executor
.
compile
(
net
,
image
)
def
test_compile_multi_channel
():
# input shape 4 x 2 x 2 x 2
dtype
=
mstype
.
int32
image
=
Tensor
(
np
.
array
([[[[
1
,
2
],[
3
,
4
]],
[[
5
,
6
],[
7
,
8
]]],
[[[
3
,
5
],[
7
,
9
]],
[[
11
,
13
],[
15
,
17
]]],
[[[
5
,
10
],[
15
,
20
]],
[[
25
,
30
],[
35
,
40
]]],
[[[
10
,
20
],[
30
,
40
]],
[[
50
,
60
],[
70
,
80
]]]]),
dtype
=
dtype
)
net
=
Net
()
_executor
.
compile
(
net
,
image
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录