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.

mask_conv.cpp 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * \file dnn/test/cpu/mask_conv.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/cpu/fixture.h"
  12. #include "megdnn/oprs.h"
  13. #include "test/common/benchmarker.h"
  14. #include "test/common/checker.h"
  15. #include "test/common/mask_conv.h"
  16. #include "test/common/rng.h"
  17. #include "test/common/utils.h"
  18. using namespace megdnn;
  19. using namespace test;
  20. TEST_F(CPU, MASK_CONV) {
  21. mask_conv_test(handle());
  22. }
  23. #if MEGDNN_WITH_BENCHMARK
  24. TEST_F(CPU, MASK_CONV_BENCHMARK) {
  25. mask_conv_benchmark(handle());
  26. }
  27. #endif
  28. TEST_F(CPU, MASK_PROPAGATE) {
  29. param::MaskPropagate mask_param;
  30. auto mask_check = [&](const TensorNDArray& tensors) {
  31. auto mask_src = tensors[0];
  32. auto mask_dst = tensors[1];
  33. auto src_ptr = static_cast<float*>(megdnn_malloc(
  34. handle(), mask_src.layout.total_nr_elems() * sizeof(float)));
  35. auto src = TensorND{
  36. src_ptr,
  37. TensorLayout{
  38. mask_src.layout.reshape(
  39. {1, 1, mask_src.layout[0], mask_src.layout[1]}),
  40. dtype::Float32()}};
  41. for (size_t i = 0; i < src.layout.total_nr_elems(); ++i) {
  42. src_ptr[i] = static_cast<float>(mask_src.ptr<int>()[i]);
  43. }
  44. auto filter_ptr = static_cast<float*>(megdnn_malloc(
  45. handle(), mask_param.kernel_h * mask_param.kernel_w * sizeof(float)));
  46. auto filter = TensorND{
  47. static_cast<void*>(filter_ptr),
  48. TensorLayout{
  49. {1, 1, mask_param.kernel_h, mask_param.kernel_w},
  50. dtype::Float32()}};
  51. for (size_t i = 0; i < mask_param.kernel_h * mask_param.kernel_w; ++i) {
  52. filter_ptr[i] = 1.0;
  53. }
  54. TensorLayout dst_layout{dtype::Float32()};
  55. param::Convolution conv_param{
  56. param::Convolution::Mode::CROSS_CORRELATION,
  57. mask_param.pad_h,
  58. mask_param.pad_w,
  59. mask_param.stride_h,
  60. mask_param.stride_w,
  61. mask_param.dilate_h,
  62. mask_param.dilate_w};
  63. auto opr = handle()->create_operator<Convolution>();
  64. opr->param() = conv_param;
  65. opr->deduce_layout(src.layout, filter.layout, dst_layout);
  66. auto dst_ptr = static_cast<float*>(megdnn_malloc(
  67. handle(), mask_dst.layout.total_nr_elems() * sizeof(float)));
  68. auto dst = TensorND{dst_ptr, dst_layout};
  69. WorkspaceWrapper workspace{
  70. handle(), opr->get_workspace_in_bytes(
  71. src.layout, filter.layout, dst.layout, nullptr)};
  72. opr->exec(src, filter, dst, nullptr, workspace.workspace());
  73. for (size_t i = 0; i < dst.layout.total_nr_elems(); ++i) {
  74. mask_dst.ptr<int>()[i] = dst_ptr[i] > 0;
  75. }
  76. delete dst_ptr;
  77. delete filter_ptr;
  78. delete src_ptr;
  79. };
  80. Checker<MaskPropagate> checker(handle());
  81. auto rng = std::make_unique<BernoulliRNG>(0.5);
  82. checker.set_extra_opr_impl(mask_check)
  83. .set_dtype(0, dtype::Int32())
  84. .set_rng(0, rng.get());
  85. auto run = [&](size_t IH, size_t IW, size_t FH, size_t FW, size_t SH = 1,
  86. size_t SW = 1, size_t PH = 0, size_t PW = 0, size_t DH = 1,
  87. size_t DW = 1) {
  88. mask_param.kernel_h = FH;
  89. mask_param.kernel_w = FW;
  90. mask_param.pad_h = PH;
  91. mask_param.pad_w = PW;
  92. mask_param.stride_h = SH;
  93. mask_param.stride_w = SW;
  94. mask_param.dilate_h = DH;
  95. mask_param.dilate_w = DW;
  96. checker.set_param(mask_param);
  97. TensorShape src_shape{IH, IW}, dst_shape{};
  98. checker.execs({src_shape, dst_shape});
  99. };
  100. run(5, 5, 3, 2);
  101. run(5, 5, 2, 3, 2, 2);
  102. run(5, 5, 3, 3, 2, 2, 1, 2);
  103. run(5, 5, 3, 3, 2, 1, 1, 2);
  104. run(5, 5, 3, 3, 1, 2, 2, 2);
  105. run(24, 23, 4, 4, 1, 1, 3, 2);
  106. run(24, 23, 4, 4, 1, 1, 3, 2, 2, 2);
  107. run(24, 23, 4, 4, 1, 1, 3, 2, 2, 3);
  108. run(24, 23, 4, 4, 1, 1, 3, 2, 3, 3);
  109. }
  110. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台