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

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

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

Contributors (1)