• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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""" test conv """
16import numpy as np
17import pytest
18
19import mindspore.nn as nn
20from mindspore import Tensor
21from ..ut_filter import non_graph_engine
22
23weight = Tensor(np.ones([2, 2]))
24in_channels = 3
25out_channels = 64
26
27
28class Net(nn.Cell):
29    """ Net definition """
30
31    def __init__(self,
32                 cin,
33                 cout,
34                 kernel_size,
35                 stride=1,
36                 pad_mode="valid",
37                 padding=0,
38                 dilation=1,
39                 group=1,
40                 has_bias=True,
41                 weight_init='normal',
42                 bias_init='zeros'):
43        super(Net, self).__init__()
44        self.conv = nn.Conv2d(cin,
45                              cout,
46                              kernel_size,
47                              stride,
48                              pad_mode,
49                              padding,
50                              dilation,
51                              group,
52                              has_bias,
53                              weight_init,
54                              bias_init)
55
56    def construct(self, input_x):
57        return self.conv(input_x)
58
59
60@non_graph_engine
61def test_compile():
62    net = Net(3, 64, 3, bias_init='zeros')
63    input_data = Tensor(np.ones([1, 3, 16, 50], np.float32))
64    net(input_data)
65
66
67def test_compile_nobias():
68    net = Net(3, 64, 4, has_bias=False, weight_init='normal')
69    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
70    net(input_data)
71
72
73def test_compile_nobias2():
74    net = Net(3, 64, (3, 5), has_bias=False, weight_init='normal')
75    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
76    net(input_data)
77
78
79def test_compile_pad_same():
80    net = Net(3, 64, (3, 5), pad_mode="same", padding=0, has_bias=False, weight_init='normal')
81    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
82    net(input_data)
83
84
85def test_compile_pad_valid():
86    net = Net(3, 64, (3, 5), pad_mode="valid", padding=0, has_bias=False, weight_init='normal')
87    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
88    net(input_data)
89
90
91def test_compile_pad_pad():
92    net = Net(3, 64, (3, 5), pad_mode="pad", padding=1, has_bias=False, weight_init='normal')
93    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
94    net(input_data)
95
96
97def test_conv_group_error():
98    with pytest.raises(ValueError):
99        nn.Conv2d(6, 8, 3, group=3)
100    with pytest.raises(ValueError):
101        nn.Conv2d(6, 9, 3, group=2)
102
103
104def test_conv_check():
105    """ test_conv_check """
106    with pytest.raises(ValueError):
107        Net(3, 64, 4, pad_mode='sane')
108
109    with pytest.raises(ValueError):
110        Net(3, 0, 4)
111
112    with pytest.raises(ValueError):
113        Net(3, 1, 4, group=-1)
114
115    with pytest.raises(ValueError):
116        Net(3, 1, 4, dilation=-1)
117
118    with pytest.raises(ValueError):
119        Net(3, 1, kernel_size=-1)
120
121    with pytest.raises(ValueError):
122        Net(3, 1, 4, stride=0)
123
124    with pytest.raises(ValueError):
125        Net(0, 1, 4)
126
127
128class NetConv2dTranspose(nn.Cell):
129    def __init__(self,
130                 cin,
131                 cout,
132                 kernel_size,
133                 stride=1,
134                 pad_mode="same",
135                 padding=0,
136                 dilation=1,
137                 group=1,
138                 has_bias=False,
139                 weight_init='normal',
140                 bias_init='zeros'):
141        super(NetConv2dTranspose, self).__init__()
142        self.conv = nn.Conv2dTranspose(cin,
143                                       cout,
144                                       kernel_size,
145                                       stride,
146                                       pad_mode,
147                                       padding,
148                                       dilation,
149                                       group,
150                                       has_bias,
151                                       weight_init,
152                                       bias_init)
153
154    def construct(self, input_x):
155        return self.conv(input_x)
156
157
158def test_compile_transpose():
159    net = NetConv2dTranspose(3, 64, 4, weight_init='normal')
160    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
161    net(input_data)
162
163
164def test_compile_transpose_bias():
165    net = NetConv2dTranspose(3, 64, 4, has_bias=True, weight_init='normal')
166    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
167    net(input_data)
168
169
170def test_compile_transpose_bias_init():
171    bias = Tensor(np.random.randn(64).astype(np.float32))
172    net = NetConv2dTranspose(3, 64, 4, has_bias=True, weight_init='normal', bias_init=bias)
173    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
174    net(input_data)
175
176
177def test_compile_transpose_valid():
178    net = NetConv2dTranspose(3, 64, 4, pad_mode='valid', weight_init='normal')
179    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
180    net(input_data)
181
182
183def test_compile_transpose_pad():
184    net = NetConv2dTranspose(3, 64, 4, pad_mode='pad', weight_init='normal')
185    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
186    net(input_data)
187
188
189def test_compile_transpose_stride2():
190    net = NetConv2dTranspose(3, 64, 4, stride=2, weight_init='normal')
191    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
192    net(input_data)
193
194
195def test_compile_transpose_dilation_2():
196    net = NetConv2dTranspose(3, 64, 4, stride=2, dilation=2, pad_mode='same', weight_init='normal')
197    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
198    net(input_data)
199
200
201def test_compile_transpose_dilation_2_pad_mode_pad():
202    net = NetConv2dTranspose(3, 64, 4, stride=2, dilation=2, pad_mode='pad', weight_init='normal')
203    input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
204    net(input_data)
205