You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ops_conv2d.cc 3.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <vector>
  17. #include <memory>
  18. #include "common/common_test.h"
  19. #include "ops/conv2d.h"
  20. #include "ir/dtype/type.h"
  21. #include "abstract/dshape.h"
  22. #include "utils/tensor_construct_utils.h"
  23. namespace mindspore {
  24. namespace ops {
  25. class TestConv2d : public UT::Common {
  26. public:
  27. TestConv2d() {}
  28. void SetUp() {}
  29. void TearDown() {}
  30. };
  31. TEST_F(TestConv2d, test_ops_conv2d) {
  32. auto conv_2d = std::make_shared<Conv2D>();
  33. conv_2d->Init(64, {7, 7});
  34. std::vector<int64_t> kernel_size = conv_2d->get_kernel_size();
  35. for (auto item : kernel_size) {
  36. EXPECT_EQ(item, 7);
  37. }
  38. std::vector<int64_t> stride = conv_2d->get_stride();
  39. for (auto item : stride) {
  40. EXPECT_EQ(item, 1);
  41. }
  42. std::vector<int64_t> dilation = conv_2d->get_dilation();
  43. for (auto item : dilation) {
  44. EXPECT_EQ(item, 1);
  45. }
  46. EXPECT_EQ(conv_2d->get_pad_mode(), VALID);
  47. std::vector<int64_t> pad = conv_2d->get_pad();
  48. for (auto item : pad) {
  49. EXPECT_EQ(item, 0);
  50. }
  51. EXPECT_EQ(conv_2d->get_mode(), 1);
  52. EXPECT_EQ(conv_2d->get_group(), 1);
  53. EXPECT_EQ(conv_2d->get_out_channel(), 64);
  54. EXPECT_EQ(conv_2d->get_format(), NCHW);
  55. auto tensor_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{32, 3, 224, 224});
  56. auto tensor_w = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{64, 3, 7, 7});
  57. MS_EXCEPTION_IF_NULL(tensor_x);
  58. MS_EXCEPTION_IF_NULL(tensor_w);
  59. auto conv_abstract = conv_2d->Infer({tensor_x->ToAbstract(), tensor_w->ToAbstract()});
  60. MS_EXCEPTION_IF_NULL(conv_abstract);
  61. EXPECT_EQ(conv_abstract->isa<abstract::AbstractTensor>(), true);
  62. auto shape_ptr = conv_abstract->BuildShape();
  63. MS_EXCEPTION_IF_NULL(shape_ptr);
  64. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  65. auto conv_shape = shape_ptr->cast<abstract::ShapePtr>();
  66. MS_EXCEPTION_IF_NULL(conv_shape);
  67. auto shape_vec = conv_shape->shape();
  68. auto type = conv_abstract->BuildType();
  69. MS_EXCEPTION_IF_NULL(type);
  70. EXPECT_EQ(type->isa<TensorType>(), true);
  71. auto tensor_type = type->cast<TensorTypePtr>();
  72. MS_EXCEPTION_IF_NULL(tensor_type);
  73. auto elem_type = tensor_type->element();
  74. EXPECT_EQ(elem_type->type_id(), kNumberTypeFloat32);
  75. EXPECT_EQ(shape_vec.size(), 4);
  76. EXPECT_EQ(shape_vec[0], 32);
  77. EXPECT_EQ(shape_vec[1], 64);
  78. EXPECT_EQ(shape_vec[2], 218);
  79. EXPECT_EQ(shape_vec[3], 218);
  80. }
  81. } // namespace ops
  82. } // namespace mindspore