|
|
|
@ -20,6 +20,8 @@ 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__()
|
|
|
|
@ -31,32 +33,32 @@ class Net(nn.Cell):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
assert np.any(dx.asnumpy() - expected_dx.asnumpy()) == False
|
|
|
|
|
assert np.any(dy.asnumpy() - expected_dy.asnumpy()) == False
|
|
|
|
|