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_de.cc 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <string>
  17. #include <vector>
  18. #include "common/common_test.h"
  19. #include "include/api/types.h"
  20. #include "minddata/dataset/include/minddata_eager.h"
  21. #include "minddata/dataset/include/vision.h"
  22. #include "minddata/dataset/kernels/tensor_op.h"
  23. #include "include/api/model.h"
  24. #include "include/api/serializations.h"
  25. #include "include/api/context.h"
  26. using namespace mindspore::api;
  27. using namespace mindspore::dataset::vision;
  28. static void SaveFile(int idx, Buffer buffer, int seq) {
  29. std::string path = "mnt/disk1/yolo_dvpp_result/result_Files/output" + std::to_string(idx) +
  30. "_in_YoloV3-DarkNet_coco_bs_dvpp_" + std::to_string(seq) + ".bin";
  31. FILE *output_file = fopen(path.c_str(), "wb");
  32. if (output_file == nullptr) {
  33. std::cout << "Write file" << path << "failed when fopen" << std::endl;
  34. return;
  35. }
  36. size_t wsize = fwrite(buffer.Data(), buffer.DataSize(), sizeof(int8_t), output_file);
  37. if (wsize == 0) {
  38. std::cout << "Write file" << path << " failed when fwrite." << std::endl;
  39. return;
  40. }
  41. fclose(output_file);
  42. std::cout << "Save file " << path << "length" << buffer.DataSize() << " success." << std::endl;
  43. }
  44. class TestDE : public ST::Common {
  45. public:
  46. TestDE() {}
  47. };
  48. TEST_F(TestDE, ResNetPreprocess) {
  49. std::vector<std::shared_ptr<Tensor>> images;
  50. MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",
  51. &images);
  52. MindDataEager Compose({Decode(), Resize({224, 224}),
  53. Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255}),
  54. HWC2CHW()});
  55. for (auto &img : images) {
  56. img = Compose(img);
  57. }
  58. ASSERT_EQ(images[0]->Shape().size(), 3);
  59. ASSERT_EQ(images[0]->Shape()[0], 3);
  60. ASSERT_EQ(images[0]->Shape()[1], 224);
  61. ASSERT_EQ(images[0]->Shape()[2], 224);
  62. }
  63. TEST_F(TestDE, TestDvpp) {
  64. std::vector<std::shared_ptr<Tensor>> images;
  65. MindDataEager::LoadImageFromDir("/root/Dvpp_Unit_Dev/val2014_test/", &images);
  66. MindDataEager Solo({DvppDecodeResizeCropJpeg({224, 224}, {256, 256})});
  67. for (auto &img : images) {
  68. img = Solo(img);
  69. ASSERT_EQ(images[0]->Shape().size(), 3);
  70. ASSERT_EQ(images[0]->Shape()[0], 224 * 224 * 1.5);
  71. ASSERT_EQ(images[0]->Shape()[1], 1);
  72. ASSERT_EQ(images[0]->Shape()[2], 1);
  73. }
  74. }
  75. TEST_F(TestDE, TestYoloV3_with_Dvpp) {
  76. std::vector<std::shared_ptr<Tensor>> images;
  77. MIndDataEager::LoadImageFromDir("/home/lizhenglong/val2014", &images);
  78. MindDataEager SingleOp({DvppDecodeResizeCropJpeg({416, 416}, {416, 416})});
  79. constexpr auto yolo_mindir_file = "/home/zhoufeng/yolov3/yolov3_darknet53.mindir";
  80. Context::Instance().SetDeviceTarget(kDeviceTypeAscend310).SetDeviceID(1);
  81. auto graph = Serialization::LoadModel(yolo_mindir_file, ModelType::kMindIR);
  82. Model yolov3((GraphCell(graph)));
  83. Status ret = yolov3.Build({{kMOdelOptionInsertOpCfgPath, "/mnt/disk1/yolo_dvpp_result/aipp_resnet50.cfg"}});
  84. ASSERT_TRUE(ret == SUCCESS);
  85. std::vector<std::string> names;
  86. std::vector<std::vector<int64_t>> shapes;
  87. std::vector<DataType> data_types;
  88. std::vector<size_t> mem_sizes;
  89. yolov3.GetOutputsInfo(&names, &shapes, &data_types, &mem_sizes);
  90. std::vector<Buffer> outputs;
  91. std::vector<Buffer> inputs;
  92. int64_t seq = 0;
  93. for (auto &img : images) {
  94. img = SingleOp(img);
  95. std::vector<float> input_shape = {416, 416};
  96. input.clear();
  97. inputs.emplace_back(img->data(), img->DataSize());
  98. inputs.emplace_back(input_shape.data(), input_shape.size() * sizeof(float));
  99. ret = yolov3.Predict(inputs, &outputs);
  100. for (size_t i = 0; i < outputs.size(); ++i) {
  101. SaveFile(i, outputs[i], seq);
  102. }
  103. seq++;
  104. ASSERT_TRUE(ret == SUCCESS);
  105. }
  106. }