1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15 16import numpy as np 17import pytest 18import mindspore.context as context 19from mindspore import Tensor 20from mindspore.nn import Cell 21import mindspore.ops as P 22 23context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 24 25 26class SqueezeNet(Cell): 27 def __init__(self): 28 super(SqueezeNet, self).__init__() 29 self.squeeze = P.Squeeze() 30 31 def construct(self, x): 32 return self.squeeze(x) 33 34 35@pytest.mark.level0 36@pytest.mark.platform_x86_cpu 37@pytest.mark.env_onecard 38def test_squeeze_shape_float32(): 39 x = np.ones(shape=[1, 2, 1, 1, 8, 3, 1]).astype(np.float32) 40 expect = np.ones(shape=[2, 8, 3]).astype(np.float32) 41 net = SqueezeNet() 42 result = net(Tensor(x)) 43 assert np.allclose(result.asnumpy(), expect, rtol=1.e-4, 44 atol=1.e-8, equal_nan=True) 45 46 47@pytest.mark.level0 48@pytest.mark.platform_x86_cpu 49@pytest.mark.env_onecard 50def test_squeeze_shape_int32(): 51 x = np.array([[7], [11]]).astype(np.int32) 52 expect = np.array([7, 11]).astype(np.int32) 53 net = SqueezeNet() 54 result = net(Tensor(x)) 55 assert np.allclose(result.asnumpy(), expect, rtol=1.e-4, 56 atol=1.e-8, equal_nan=True) 57 58 59@pytest.mark.level0 60@pytest.mark.platform_x86_cpu 61@pytest.mark.env_onecard 62def test_squeeze_shape_bool(): 63 x = np.array([[True], [False]]).astype(np.bool_) 64 expect = np.array([True, False]).astype(np.bool_) 65 net = SqueezeNet() 66 result = net(Tensor(x)) 67 assert np.allclose(result.asnumpy(), expect, rtol=1.e-4, 68 atol=1.e-8, equal_nan=True) 69 70 71@pytest.mark.level0 72@pytest.mark.platform_x86_cpu 73@pytest.mark.env_onecard 74def test_squeeze_shape_float64(): 75 x = np.random.random([1, 2, 1, 1, 8, 3, 1]).astype(np.float64) 76 expect = np.squeeze(x) 77 net = SqueezeNet() 78 result = net(Tensor(x)) 79 print(result.asnumpy()[0][0], expect[0][0]) 80 assert np.allclose(result.asnumpy(), expect, rtol=1.e-4, 81 atol=1.e-8, equal_nan=True) 82 83 84@pytest.mark.level0 85@pytest.mark.platform_x86_cpu 86@pytest.mark.env_onecard 87def test_squeeze_shape_uint16(): 88 x = np.random.random([1, 2, 1, 1, 8, 3, 1]).astype(np.uint16) 89 expect = np.squeeze(x) 90 net = SqueezeNet() 91 result = net(Tensor(x)) 92 print(result.asnumpy()[0][0], expect[0][0]) 93 assert np.allclose(result.asnumpy(), expect, rtol=1.e-4, 94 atol=1.e-8, equal_nan=True) 95