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.

main.cc 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright 2021 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 <iostream>
  17. #include <cstring>
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "include/api/status.h"
  22. #include "include/api/context.h"
  23. #include "include/api/model.h"
  24. namespace mindspore {
  25. namespace lite {
  26. namespace {
  27. constexpr int kNumPrintOfOutData = 20;
  28. Status FillInputData(const std::vector<mindspore::MSTensor> &inputs) {
  29. for (auto tensor : inputs) {
  30. auto input_data = tensor.MutableData();
  31. if (input_data == nullptr) {
  32. std::cerr << "MallocData for inTensor failed.\n";
  33. return kLiteError;
  34. }
  35. std::vector<float> temp(tensor.ElementNum(), 1.0f);
  36. memcpy(input_data, temp.data(), tensor.DataSize());
  37. }
  38. return kSuccess;
  39. }
  40. } // namespace
  41. Status CompileAndRun(int argc, const char **argv) {
  42. if (argc < 2) {
  43. std::cerr << "Model file must be provided.\n";
  44. return kLiteError;
  45. }
  46. // generate context.
  47. auto context = std::make_shared<mindspore::Context>();
  48. if (context == nullptr) {
  49. std::cerr << "New context failed while running.\n";
  50. return kLiteError;
  51. }
  52. auto &device_list = context->MutableDeviceInfo();
  53. std::shared_ptr<CPUDeviceInfo> device_info = std::make_shared<CPUDeviceInfo>();
  54. device_info->SetProvider("Tutorial");
  55. device_info->SetProviderDevice("Tutorial");
  56. device_list.push_back(device_info);
  57. // build model.
  58. std::string model_file = std::string(argv[1]);
  59. mindspore::Model model;
  60. auto ret = model.Build(model_file, kMindIR, context);
  61. if (ret != kSuccess) {
  62. std::cerr << "build model failed.\n";
  63. return kLiteError;
  64. }
  65. // fill input data.
  66. auto inputs = model.GetInputs();
  67. ret = FillInputData(inputs);
  68. if (ret != kSuccess) {
  69. std::cerr << "Generate Random Input Data failed.\n";
  70. return ret;
  71. }
  72. // run model.
  73. std::vector<MSTensor> outputs;
  74. ret = model.Predict(inputs, &outputs);
  75. if (ret != kSuccess) {
  76. std::cerr << "run model failed.\n";
  77. return ret;
  78. }
  79. // display output result.
  80. for (auto tensor : outputs) {
  81. std::cout << "tensor name is:" << tensor.Name() << " tensor size is:" << tensor.DataSize()
  82. << " tensor elements num is:" << tensor.ElementNum() << std::endl;
  83. auto out_data = std::static_pointer_cast<const float>(tensor.Data());
  84. std::cout << "output data is:";
  85. for (int i = 0; i < tensor.ElementNum() && i <= kNumPrintOfOutData; i++) {
  86. std::cout << out_data.get()[i] << " ";
  87. }
  88. std::cout << std::endl;
  89. }
  90. return kSuccess;
  91. }
  92. } // namespace lite
  93. } // namespace mindspore
  94. int main(int argc, const char **argv) {
  95. auto ret = mindspore::lite::CompileAndRun(argc, argv);
  96. if (ret != mindspore::kSuccess) {
  97. std::cerr << "run failed.\n";
  98. return -1;
  99. }
  100. std::cout << "run success.\n";
  101. return 0;
  102. }