Add eager tests for transform ops

Signed-off-by: alex-yuyue <yue.yu1@huawei.com>
This commit is contained in:
alex-yuyue 2021-04-29 11:47:13 -04:00
parent b2a0265784
commit 9ef4331f56
3 changed files with 90 additions and 4 deletions

View File

@ -2238,15 +2238,20 @@ TEST_F(MindDataTestPipeline, TestSliceSuccess5) {
u_int32_t curr_seed = GlobalContext::config_manager()->seed();
GlobalContext::config_manager()->set_seed(246);
std::shared_ptr<SchemaObj> schema = Schema();
ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {2, 2}));
// Generate a ds of 4 tensors, each tensor has 3 rows and 2 columns
ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {3, 2}));
std::shared_ptr<Dataset> ds = RandomData(4, schema);
EXPECT_NE(ds, nullptr);
ds = ds->SetNumWorkers(2);
EXPECT_NE(ds, nullptr);
// Apply Slice op on ds, get the first and third elements in each row.
// Apply two SliceOptions on ds which includes 4 tensors with shape 3*2
// The first SliceOption is to slice the first and the second row of each tensor
// The shape of result tensor changes to 2*2
std::vector<dsize_t> indices1 = {0, 1};
SliceOption slice_option1 = SliceOption(indices1);
// The second SliceOption is to slice the last column of each tensor
// The shape of result tensor changes to 2*1
std::vector<dsize_t> indices2 = {-1};
SliceOption slice_option2 = SliceOption(indices2);
@ -2270,7 +2275,7 @@ TEST_F(MindDataTestPipeline, TestSliceSuccess5) {
while (row.size() != 0) {
auto ind = row["col1"];
std::shared_ptr<Tensor> de_expected_tensor;
ASSERT_OK(Tensor::CreateFromVector(expected[i], TensorShape({1, 2}), &de_expected_tensor));
ASSERT_OK(Tensor::CreateFromVector(expected[i], TensorShape({2, 1}), &de_expected_tensor));
mindspore::MSTensor expected_tensor =
mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
EXPECT_MSTENSOR_EQ(ind, expected_tensor);

View File

@ -63,10 +63,13 @@ using mindspore::StatusCode;
} \
} while (false)
// Macro to compare 2 MSTensors; compare shape-size and data
// Macro to compare 2 MSTensors; compare shape-size, shape and data
#define EXPECT_MSTENSOR_EQ(_mstensor1, _mstensor2) \
do { \
EXPECT_EQ(_mstensor1.Shape().size(), _mstensor2.Shape().size()); \
for (int i = 0; i < _mstensor1.Shape().size(); i++) { \
EXPECT_EQ(_mstensor1.Shape()[i], _mstensor2.Shape()[i]); \
} \
EXPECT_EQ(_mstensor1.DataSize(), _mstensor2.DataSize()); \
EXPECT_EQ(std::memcmp((const void *)_mstensor1.Data().get(), (const void *)_mstensor2.Data().get(), \
_mstensor2.DataSize()), 0); \

View File

@ -0,0 +1,78 @@
# Copyright 2021 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.
# ==============================================================================
"""
Eager Tests for Transform Tensor ops
"""
import numpy as np
import mindspore.common.dtype as mstype
import mindspore.dataset.transforms.c_transforms as data_trans
def test_eager_concatenate():
"""
Test Concatenate op is callable
"""
prepend_tensor = np.array([1.4, 2., 3., 4., 4.5], dtype=np.float)
append_tensor = np.array([9., 10.3, 11., 12.], dtype=np.float)
concatenate_op = data_trans.Concatenate(0, prepend_tensor, append_tensor)
expected = np.array([1.4, 2., 3., 4., 4.5, 5., 6., 7., 8., 9., 10.3,
11., 12.])
assert np.array_equal(concatenate_op([5., 6., 7., 8.]), expected)
def test_eager_fill():
"""
Test Fill op is callable
"""
fill_op = data_trans.Fill(3)
expected = np.array([3, 3, 3, 3])
assert np.array_equal(fill_op([4, 5, 6, 7]), expected)
def test_eager_mask():
"""
Test Mask op is callable
"""
mask_op = data_trans.Mask(data_trans.Relational.EQ, 3, mstype.bool_)
expected = np.array([False, False, True, False, False])
assert np.array_equal(mask_op([1, 2, 3, 4, 5]), expected)
def test_eager_pad_end():
"""
Test PadEnd op is callable
"""
pad_end_op = data_trans.PadEnd([3], -1)
expected = np.array([1, 2, -1])
assert np.array_equal(pad_end_op([1, 2]), expected)
def test_eager_slice():
"""
Test Slice op is callable
"""
indexing = [[0], [0, 3]]
slice_op = data_trans.Slice(*indexing)
expected = np.array([[1, 4]])
assert np.array_equal(slice_op([[1, 2, 3, 4, 5]]), expected)
if __name__ == "__main__":
test_eager_concatenate()
test_eager_fill()
test_eager_mask()
test_eager_pad_end()
test_eager_slice()