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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-2020 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{mask_src.layout.reshape({1, 1, mask_src.layout[0],
  38. mask_src.layout[1]}),
  39. dtype::Float32()}};
  40. for (size_t i = 0; i < src.layout.total_nr_elems(); ++i) {
  41. src_ptr[i] = static_cast<float>(mask_src.ptr<int>()[i]);
  42. }
  43. auto filter_ptr = static_cast<float*>(megdnn_malloc(
  44. handle(),
  45. mask_param.kernel_h * mask_param.kernel_w * sizeof(float)));
  46. auto filter = TensorND{
  47. static_cast<void*>(filter_ptr),
  48. TensorLayout{{1, 1, mask_param.kernel_h, mask_param.kernel_w},
  49. dtype::Float32()}};
  50. for (size_t i = 0; i < mask_param.kernel_h * mask_param.kernel_w; ++i) {
  51. filter_ptr[i] = 1.0;
  52. }
  53. TensorLayout dst_layout{dtype::Float32()};
  54. param::Convolution conv_param{
  55. param::Convolution::Mode::CROSS_CORRELATION,
  56. mask_param.pad_h,
  57. mask_param.pad_w,
  58. mask_param.stride_h,
  59. mask_param.stride_w,
  60. mask_param.dilate_h,
  61. mask_param.dilate_w};
  62. auto opr = handle()->create_operator<Convolution>();
  63. opr->param() = conv_param;
  64. opr->deduce_layout(src.layout, filter.layout, dst_layout);
  65. auto dst_ptr = static_cast<float*>(megdnn_malloc(
  66. handle(), mask_dst.layout.total_nr_elems() * sizeof(float)));
  67. auto dst = TensorND{dst_ptr, dst_layout};
  68. WorkspaceWrapper workspace{
  69. handle(), opr->get_workspace_in_bytes(src.layout, filter.layout,
  70. dst.layout, nullptr)};
  71. opr->exec(src, filter, dst, nullptr, workspace.workspace());
  72. for (size_t i = 0; i < dst.layout.total_nr_elems(); ++i) {
  73. mask_dst.ptr<int>()[i] = dst_ptr[i] > 0;
  74. }
  75. delete dst_ptr;
  76. delete filter_ptr;
  77. delete src_ptr;
  78. };
  79. Checker<MaskPropagate> checker(handle());
  80. auto rng = std::make_unique<BernoulliRNG>(0.5);
  81. checker.set_extra_opr_impl(mask_check)
  82. .set_dtype(0, dtype::Int32())
  83. .set_rng(0, rng.get());
  84. auto run = [&](size_t IH, size_t IW, size_t FH, size_t FW, size_t SH = 1,
  85. size_t SW = 1, size_t PH = 0, size_t PW = 0, size_t DH = 1,
  86. size_t DW = 1) {
  87. mask_param.kernel_h = FH;
  88. mask_param.kernel_w = FW;
  89. mask_param.pad_h = PH;
  90. mask_param.pad_w = PW;
  91. mask_param.stride_h = SH;
  92. mask_param.stride_w = SW;
  93. mask_param.dilate_h = DH;
  94. mask_param.dilate_w = DW;
  95. checker.set_param(mask_param);
  96. TensorShape src_shape{IH, IW}, dst_shape{};
  97. checker.execs({src_shape, dst_shape});
  98. };
  99. run(5, 5, 3, 2);
  100. run(5, 5, 2, 3, 2, 2);
  101. run(5, 5, 3, 3, 2, 2, 1, 2);
  102. run(5, 5, 3, 3, 2, 1, 1, 2);
  103. run(5, 5, 3, 3, 1, 2, 2, 2);
  104. run(24, 23, 4, 4, 1, 1, 3, 2);
  105. run(24, 23, 4, 4, 1, 1, 3, 2, 2, 2);
  106. run(24, 23, 4, 4, 1, 1, 3, 2, 2, 3);
  107. run(24, 23, 4, 4, 1, 1, 3, 2, 3, 3);
  108. }
  109. // vim: syntax=cpp.doxygen

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