test_individual_infer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <memory>
  22. #include <string>
  23. #include <thread>
  24. #include <vector>
  25. #include "opencv2/highgui/highgui.hpp"
  26. #include "opencv2/imgproc/imgproc.hpp"
  27. #if (CV_MAJOR_VERSION >= 3)
  28. #include "opencv2/imgcodecs/imgcodecs.hpp"
  29. #endif
  30. #include "device/mlu_context.h"
  31. #include "easyinfer/mlu_memory_op.h"
  32. #include "easyinfer/model_loader.h"
  33. #include "cnstream_frame_va.hpp"
  34. #include "inferencer.hpp"
  35. #include "postproc.hpp"
  36. #include "preproc.hpp"
  37. #include "util/cnstream_queue.hpp"
  38. #include "test_base.hpp"
  39. namespace cnstream {
  40. static const char *g_func_name = "subnet0";
  41. static const char *g_postproc_name = "PostprocClassification";
  42. static constexpr int g_dev_id = 0;
  43. static constexpr int g_channel_id = 0;
  44. static std::string GetModelPath() {
  45. edk::MluContext ctx;
  46. edk::CoreVersion core_ver = ctx.GetCoreVersion();
  47. std::string model_path = "";
  48. switch (core_ver) {
  49. case edk::CoreVersion::MLU220:
  50. model_path = "../../data/models/resnet18_b4c4_bgra_mlu220.cambricon";
  51. break;
  52. case edk::CoreVersion::MLU270:
  53. default:
  54. model_path = "../../data/models/resnet50_b16c16_bgra_mlu270.cambricon";
  55. break;
  56. }
  57. return model_path;
  58. }
  59. class InferObserver : public IModuleObserver {
  60. public:
  61. void notify(std::shared_ptr<CNFrameInfo> data) override {
  62. output_frame_queue_.Push(data);
  63. }
  64. std::shared_ptr<CNFrameInfo> GetOutputFrame() {
  65. std::shared_ptr<CNFrameInfo> output_frame = nullptr;
  66. output_frame_queue_.WaitAndTryPop(output_frame, std::chrono::milliseconds(100));
  67. return output_frame;
  68. }
  69. private:
  70. ThreadSafeQueue<std::shared_ptr<CNFrameInfo>> output_frame_queue_;
  71. };
  72. void GetResult(std::shared_ptr<InferObserver> observer) {
  73. uint32_t i = 0;
  74. while (1) {
  75. auto data = observer->GetOutputFrame();
  76. if (data != nullptr) {
  77. if (!data->IsEos()) {
  78. CNDataFramePtr frame = data->collection.Get<CNDataFramePtr>(kCNDataFrameTag);
  79. EXPECT_EQ(frame->frame_id, i);
  80. i++;
  81. std::cout << "Got data, frame id = " << frame->frame_id << std::endl;
  82. } else {
  83. std::cout << "**********Got EOS *********" << std::endl;
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. TEST(Inferencer, Demo) {
  90. std::string model_path = GetExePath() + GetModelPath();
  91. std::shared_ptr<Module> infer = std::make_shared<Inferencer>("test_infer");
  92. std::shared_ptr<InferObserver> observer = std::make_shared<InferObserver>();
  93. infer->SetObserver(reinterpret_cast<IModuleObserver *>(observer.get()));
  94. std::thread th = std::thread(&GetResult, observer);
  95. ModuleParamSet param;
  96. param["model_path"] = model_path;
  97. param["func_name"] = g_func_name;
  98. param["postproc_name"] = g_postproc_name;
  99. param["device_id"] = std::to_string(g_dev_id);
  100. param["batching_timeout"] = "30";
  101. // Open
  102. ASSERT_TRUE(infer->Open(param));
  103. const int width = 1280, height = 720;
  104. size_t nbytes = width * height * sizeof(uint8_t) * 3 / 2;
  105. size_t boundary = 1 << 16;
  106. nbytes = (nbytes + boundary - 1) & ~(boundary - 1); // align to 64kb
  107. // fake data vector
  108. std::vector<void *> frame_data_vec;
  109. edk::MluMemoryOp mem_op;
  110. // test nv12
  111. for (uint32_t i = 0; i < 32; i++) {
  112. // fake data
  113. void *frame_data = nullptr;
  114. frame_data = mem_op.AllocMlu(nbytes);
  115. frame_data_vec.push_back(frame_data);
  116. void *planes[CN_MAX_PLANES] = {nullptr, nullptr};
  117. planes[0] = frame_data; // y plane
  118. planes[1] = reinterpret_cast<void *>(reinterpret_cast<int64_t>(frame_data) + width * height); // uv plane
  119. auto data = cnstream::CNFrameInfo::Create(std::to_string(g_channel_id));
  120. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  121. data->collection.Add(kCNDataFrameTag, frame);
  122. data->collection.Add(kCNInferObjsTag, std::make_shared<CNInferObjs>());
  123. frame->frame_id = i;
  124. data->timestamp = i;
  125. frame->width = width;
  126. frame->height = height;
  127. void *ptr_mlu[2] = {planes[0], planes[1]};
  128. frame->stride[0] = frame->stride[1] = width;
  129. frame->ctx.ddr_channel = g_channel_id;
  130. frame->ctx.dev_id = g_dev_id;
  131. frame->ctx.dev_type = DevContext::DevType::MLU;
  132. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV12;
  133. frame->dst_device_id = g_dev_id;
  134. frame->CopyToSyncMem(ptr_mlu, true);
  135. int ret = infer->Process(data);
  136. EXPECT_EQ(ret, 1);
  137. }
  138. // eos frame
  139. auto data = cnstream::CNFrameInfo::Create(std::to_string(g_channel_id), true);
  140. int ret = infer->Process(data);
  141. EXPECT_EQ(ret, 1);
  142. if (th.joinable()) {
  143. th.join();
  144. }
  145. ASSERT_NO_THROW(infer->Close());
  146. for (auto it : frame_data_vec) {
  147. mem_op.FreeMlu(it);
  148. it = nullptr;
  149. }
  150. }
  151. } // namespace cnstream