• 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 eval"""
16import numpy as np
17
18import mindspore as ms
19import mindspore.nn as nn
20from mindspore import Tensor
21from mindspore import context
22from mindspore.common.api import _cell_graph_executor
23from ..ut_filter import non_graph_engine
24
25
26class Net(nn.Cell):
27    """Net definition"""
28
29    def __init__(self,
30                 cin,
31                 cout,
32                 kernel_size,
33                 stride=1,
34                 pad_mode='pad',
35                 padding=0,
36                 dilation=1,
37                 group=1,
38                 has_bias=False,
39                 weight_init='normal',
40                 bias_init='zeros'):
41        super(Net, self).__init__()
42        Tensor(np.ones([6, 3, 3, 3]).astype(np.float32) * 0.01)
43        self.conv = nn.Conv2d(cin,
44                              cout,
45                              kernel_size,
46                              stride,
47                              pad_mode,
48                              padding,
49                              dilation,
50                              group,
51                              has_bias,
52                              weight_init,
53                              bias_init)
54
55    def construct(self, input_x):
56        return self.conv(input_x)
57
58
59@non_graph_engine
60def test_compile_train_eval():
61    """test_compile_train_eval"""
62    net = Net(3, 1, (3, 3), bias_init='zeros')
63    train_input_data = Tensor(np.ones([1, 3, 32, 32]).astype(np.float32) * 0.01)
64    context.set_context(mode=context.GRAPH_MODE)
65
66    ms_executor = _cell_graph_executor
67
68    ms_executor.init_dataset("train", 1, 1, [ms.float32], [[1, 3, 32, 32]], (), 'dataset')
69
70    ms_executor.compile(net, train_input_data, phase='train')
71    ms_executor(net, train_input_data, phase='train')
72
73    ms_executor.init_dataset("eval", 1, 1, [ms.float32], [[1, 3, 32, 32]], (), phase='eval_dataset')
74
75    valid_input_data = Tensor(np.ones([1, 3, 32, 32]).astype(np.float32) * 0.01)
76    ms_executor.compile(net, valid_input_data, phase='eval')
77    ms_executor(net, valid_input_data, phase='eval')
78