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.

execute.cc 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * Copyright 2020-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 <algorithm>
  17. #include <fstream>
  18. #include "minddata/dataset/include/execute.h"
  19. #include "minddata/dataset/core/de_tensor.h"
  20. #include "minddata/dataset/core/device_resource.h"
  21. #include "minddata/dataset/core/device_tensor.h"
  22. #include "minddata/dataset/core/tensor_row.h"
  23. #include "minddata/dataset/include/tensor.h"
  24. #include "minddata/dataset/include/type_id.h"
  25. #include "minddata/dataset/kernels/ir/tensor_operation.h"
  26. #include "minddata/dataset/kernels/tensor_op.h"
  27. #ifndef ENABLE_ANDROID
  28. #include "utils/log_adapter.h"
  29. #else
  30. #include "mindspore/lite/src/common/log_adapter.h"
  31. #endif
  32. #ifdef ENABLE_ACL
  33. #include "minddata/dataset/core/ascend_resource.h"
  34. #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h"
  35. #endif
  36. namespace mindspore {
  37. namespace dataset {
  38. using json = nlohmann::json;
  39. struct Execute::ExtraInfo {
  40. std::multimap<std::string, std::vector<uint32_t>> aipp_cfg_;
  41. };
  42. // FIXME - Temporarily overload Execute to support both TensorOperation and TensorTransform
  43. Execute::Execute(std::shared_ptr<TensorOperation> op, MapTargetDevice deviceType) {
  44. ops_.emplace_back(std::move(op));
  45. device_type_ = deviceType;
  46. info_ = std::make_shared<ExtraInfo>();
  47. #ifdef ENABLE_ACL
  48. if (device_type_ == MapTargetDevice::kAscend310) {
  49. device_resource_ = std::make_shared<AscendResource>();
  50. Status rc = device_resource_->InitResource();
  51. if (!rc.IsOk()) {
  52. device_resource_ = nullptr;
  53. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  54. }
  55. }
  56. #endif
  57. }
  58. Execute::Execute(std::shared_ptr<TensorTransform> op, MapTargetDevice deviceType) {
  59. // Convert op from TensorTransform to TensorOperation
  60. std::shared_ptr<TensorOperation> operation;
  61. info_ = std::make_shared<ExtraInfo>();
  62. if (deviceType == MapTargetDevice::kCpu) {
  63. operation = op->Parse();
  64. } else {
  65. operation = op->Parse(deviceType);
  66. }
  67. ops_.emplace_back(std::move(operation));
  68. device_type_ = deviceType;
  69. #ifdef ENABLE_ACL
  70. if (device_type_ == MapTargetDevice::kAscend310) {
  71. device_resource_ = std::make_shared<AscendResource>();
  72. Status rc = device_resource_->InitResource();
  73. if (!rc.IsOk()) {
  74. device_resource_ = nullptr;
  75. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  76. }
  77. }
  78. #endif
  79. }
  80. Execute::Execute(std::reference_wrapper<TensorTransform> op, MapTargetDevice deviceType) {
  81. // Convert op from TensorTransform to TensorOperation
  82. std::shared_ptr<TensorOperation> operation = op.get().Parse();
  83. ops_.emplace_back(std::move(operation));
  84. device_type_ = deviceType;
  85. #ifdef ENABLE_ACL
  86. if (device_type_ == MapTargetDevice::kAscend310) {
  87. device_resource_ = std::make_shared<AscendResource>();
  88. Status rc = device_resource_->InitResource();
  89. if (!rc.IsOk()) {
  90. device_resource_ = nullptr;
  91. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  92. }
  93. }
  94. #endif
  95. }
  96. // Execute function for the example case: auto decode(new vision::Decode());
  97. Execute::Execute(TensorTransform *op, MapTargetDevice deviceType) {
  98. // Convert op from TensorTransform to TensorOperation
  99. std::shared_ptr<TensorOperation> operation;
  100. info_ = std::make_shared<ExtraInfo>();
  101. if (deviceType == MapTargetDevice::kCpu) {
  102. operation = op->Parse();
  103. } else {
  104. operation = op->Parse(deviceType);
  105. }
  106. ops_.emplace_back(std::move(operation));
  107. device_type_ = deviceType;
  108. #ifdef ENABLE_ACL
  109. if (device_type_ == MapTargetDevice::kAscend310) {
  110. device_resource_ = std::make_shared<AscendResource>();
  111. Status rc = device_resource_->InitResource();
  112. if (!rc.IsOk()) {
  113. device_resource_ = nullptr;
  114. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  115. }
  116. }
  117. #endif
  118. }
  119. Execute::Execute(std::vector<std::shared_ptr<TensorOperation>> ops, MapTargetDevice deviceType)
  120. : ops_(std::move(ops)), device_type_(deviceType) {
  121. info_ = std::make_shared<ExtraInfo>();
  122. #ifdef ENABLE_ACL
  123. if (device_type_ == MapTargetDevice::kAscend310) {
  124. device_resource_ = std::make_shared<AscendResource>();
  125. Status rc = device_resource_->InitResource();
  126. if (!rc.IsOk()) {
  127. device_resource_ = nullptr;
  128. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  129. }
  130. }
  131. #endif
  132. }
  133. Execute::Execute(std::vector<std::shared_ptr<TensorTransform>> ops, MapTargetDevice deviceType) {
  134. // Convert ops from TensorTransform to TensorOperation
  135. info_ = std::make_shared<ExtraInfo>();
  136. if (deviceType == MapTargetDevice::kCpu) {
  137. (void)std::transform(ops.begin(), ops.end(), std::back_inserter(ops_),
  138. [](std::shared_ptr<TensorTransform> operation) -> std::shared_ptr<TensorOperation> {
  139. return operation->Parse();
  140. });
  141. } else {
  142. for (auto &op : ops) {
  143. ops_.emplace_back(op->Parse(deviceType));
  144. }
  145. }
  146. device_type_ = deviceType;
  147. #ifdef ENABLE_ACL
  148. if (device_type_ == MapTargetDevice::kAscend310) {
  149. device_resource_ = std::make_shared<AscendResource>();
  150. Status rc = device_resource_->InitResource();
  151. if (!rc.IsOk()) {
  152. device_resource_ = nullptr;
  153. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  154. }
  155. }
  156. #endif
  157. }
  158. Execute::Execute(const std::vector<std::reference_wrapper<TensorTransform>> ops, MapTargetDevice deviceType) {
  159. // Convert ops from TensorTransform to TensorOperation
  160. info_ = std::make_shared<ExtraInfo>();
  161. if (deviceType == MapTargetDevice::kCpu) {
  162. (void)std::transform(
  163. ops.begin(), ops.end(), std::back_inserter(ops_),
  164. [](TensorTransform &operation) -> std::shared_ptr<TensorOperation> { return operation.Parse(); });
  165. } else {
  166. for (auto &op : ops) {
  167. ops_.emplace_back(op.get().Parse(deviceType));
  168. }
  169. }
  170. device_type_ = deviceType;
  171. #ifdef ENABLE_ACL
  172. if (device_type_ == MapTargetDevice::kAscend310) {
  173. device_resource_ = std::make_shared<AscendResource>();
  174. Status rc = device_resource_->InitResource();
  175. if (!rc.IsOk()) {
  176. device_resource_ = nullptr;
  177. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  178. }
  179. }
  180. #endif
  181. }
  182. // Execute function for the example vector case: auto decode(new vision::Decode());
  183. Execute::Execute(std::vector<TensorTransform *> ops, MapTargetDevice deviceType) {
  184. // Convert ops from TensorTransform to TensorOperation
  185. info_ = std::make_shared<ExtraInfo>();
  186. if (deviceType == MapTargetDevice::kCpu) {
  187. (void)std::transform(
  188. ops.begin(), ops.end(), std::back_inserter(ops_),
  189. [](TensorTransform *operation) -> std::shared_ptr<TensorOperation> { return operation->Parse(); });
  190. } else {
  191. for (auto &op : ops) {
  192. ops_.emplace_back(op->Parse(deviceType));
  193. }
  194. }
  195. device_type_ = deviceType;
  196. #ifdef ENABLE_ACL
  197. if (device_type_ == MapTargetDevice::kAscend310) {
  198. device_resource_ = std::make_shared<AscendResource>();
  199. Status rc = device_resource_->InitResource();
  200. if (!rc.IsOk()) {
  201. device_resource_ = nullptr;
  202. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  203. }
  204. }
  205. #endif
  206. }
  207. Execute::~Execute() {
  208. #ifdef ENABLE_ACL
  209. if (device_type_ == MapTargetDevice::kAscend310) {
  210. if (device_resource_) {
  211. device_resource_->FinalizeResource();
  212. } else {
  213. MS_LOG(ERROR) << "Device resource is nullptr which is illegal under case Ascend310";
  214. }
  215. }
  216. #endif
  217. }
  218. Status Execute::operator()(const mindspore::MSTensor &input, mindspore::MSTensor *output) {
  219. // Validate input tensor
  220. CHECK_FAIL_RETURN_UNEXPECTED(input.DataSize() > 0, "Input Tensor has no data");
  221. CHECK_FAIL_RETURN_UNEXPECTED(!ops_.empty(), "Input TensorOperation should be provided");
  222. CHECK_FAIL_RETURN_UNEXPECTED(validate_device_(), "Device Type should be 'Ascend310' or 'CPU'");
  223. // Validate and build runtime ops
  224. std::vector<std::shared_ptr<TensorOp>> transforms; // record the transformations
  225. std::map<MapTargetDevice, std::string> env_list = {
  226. {MapTargetDevice::kCpu, "kCpu"}, {MapTargetDevice::kGpu, "kGpu"}, {MapTargetDevice::kAscend310, "kAscend310"}};
  227. for (int32_t i = 0; i < ops_.size(); i++) {
  228. if (ops_[i] == nullptr) {
  229. MS_LOG(ERROR) << "Input TensorOperation["
  230. << std::to_string(i) + "] is unsupported on your input device:" << env_list.at(device_type_);
  231. }
  232. CHECK_FAIL_RETURN_UNEXPECTED(ops_[i] != nullptr, "Input TensorOperation[" + std::to_string(i) + "] is null");
  233. RETURN_IF_NOT_OK(ops_[i]->ValidateParams());
  234. transforms.emplace_back(ops_[i]->Build());
  235. }
  236. if (device_type_ == MapTargetDevice::kCpu) {
  237. // Convert mindspore::Tensor to dataset::Tensor
  238. std::shared_ptr<dataset::Tensor> de_tensor;
  239. Status rc = dataset::Tensor::CreateFromMemory(dataset::TensorShape(input.Shape()),
  240. MSTypeToDEType(static_cast<TypeId>(input.DataType())),
  241. (const uchar *)(input.Data().get()), input.DataSize(), &de_tensor);
  242. if (rc.IsError()) {
  243. MS_LOG(ERROR) << rc;
  244. return rc;
  245. }
  246. // Apply transforms on tensor
  247. for (auto &t : transforms) {
  248. std::shared_ptr<dataset::Tensor> de_output;
  249. Status rc_ = t->Compute(de_tensor, &de_output);
  250. if (rc_.IsError()) {
  251. MS_LOG(ERROR) << rc_;
  252. return rc_;
  253. }
  254. // For next transform
  255. de_tensor = std::move(de_output);
  256. }
  257. // Convert dataset::Tensor to mindspore::Tensor
  258. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  259. *output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
  260. } else { // Ascend310 case, where we must set Ascend resource on each operators
  261. #ifdef ENABLE_ACL
  262. CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310");
  263. // Sink data from host into device
  264. std::shared_ptr<mindspore::dataset::DeviceTensor> device_input;
  265. RETURN_IF_NOT_OK(device_resource_->Sink(input, &device_input));
  266. for (auto &t : transforms) {
  267. // Initialize AscendResource for each operators
  268. std::shared_ptr<DeviceTensor> device_output;
  269. RETURN_IF_NOT_OK(t->SetAscendResource(device_resource_));
  270. RETURN_IF_NOT_OK(t->Compute(device_input, &device_output));
  271. // For next transform
  272. device_input = std::move(device_output);
  273. }
  274. CHECK_FAIL_RETURN_UNEXPECTED(device_input->HasDeviceData(), "Apply transform failed, output tensor has no data");
  275. std::shared_ptr<mindspore::dataset::Tensor> host_output;
  276. // TODO(lizhenglong) waiting for computing department development, hence we pop data onto host temporarily.
  277. RETURN_IF_NOT_OK(device_resource_->Pop(device_input, &host_output));
  278. *output = mindspore::MSTensor(std::make_shared<DETensor>(host_output));
  279. // *output = mindspore::MSTensor(std::make_shared<DETensor>(device_input, true)); Use in the future
  280. #endif
  281. }
  282. return Status::OK();
  283. }
  284. Status Execute::operator()(const std::vector<MSTensor> &input_tensor_list, std::vector<MSTensor> *output_tensor_list) {
  285. // Validate input tensor
  286. CHECK_FAIL_RETURN_UNEXPECTED(!input_tensor_list.empty(), "Input Tensor is not valid");
  287. for (auto &tensor : input_tensor_list) {
  288. CHECK_FAIL_RETURN_UNEXPECTED(tensor.DataSize() > 0, "Input Tensor has no data");
  289. }
  290. CHECK_FAIL_RETURN_UNEXPECTED(!ops_.empty(), "Input TensorOperation should be provided");
  291. CHECK_FAIL_RETURN_UNEXPECTED(validate_device_(), "Device Type should be 'Ascend310' or 'CPU'");
  292. std::map<MapTargetDevice, std::string> env_list = {
  293. {MapTargetDevice::kCpu, "kCpu"}, {MapTargetDevice::kGpu, "kGpu"}, {MapTargetDevice::kAscend310, "kAscend310"}};
  294. // Validate and build runtime ops
  295. std::vector<std::shared_ptr<TensorOp>> transforms;
  296. for (int32_t i = 0; i < ops_.size(); i++) {
  297. if (ops_[i] == nullptr) {
  298. MS_LOG(ERROR) << "Input TensorOperation["
  299. << std::to_string(i) + "] is unsupported on your input device:" << env_list.at(device_type_);
  300. }
  301. CHECK_FAIL_RETURN_UNEXPECTED(ops_[i] != nullptr, "Input TensorOperation[" + std::to_string(i) + "] is null");
  302. RETURN_IF_NOT_OK(ops_[i]->ValidateParams());
  303. transforms.emplace_back(ops_[i]->Build());
  304. }
  305. if (device_type_ == MapTargetDevice::kCpu) { // Case CPU
  306. TensorRow de_tensor_list;
  307. for (auto &tensor : input_tensor_list) {
  308. std::shared_ptr<dataset::Tensor> de_tensor;
  309. Status rc = dataset::Tensor::CreateFromMemory(
  310. dataset::TensorShape(tensor.Shape()), MSTypeToDEType(static_cast<TypeId>(tensor.DataType())),
  311. (const uchar *)(tensor.Data().get()), tensor.DataSize(), &de_tensor);
  312. if (rc.IsError()) {
  313. MS_LOG(ERROR) << rc;
  314. RETURN_IF_NOT_OK(rc);
  315. }
  316. de_tensor_list.emplace_back(std::move(de_tensor));
  317. }
  318. // Apply transforms on tensor
  319. for (auto &t : transforms) {
  320. TensorRow de_output_list;
  321. RETURN_IF_NOT_OK(t->Compute(de_tensor_list, &de_output_list));
  322. // For next transform
  323. de_tensor_list = std::move(de_output_list);
  324. }
  325. for (auto &tensor : de_tensor_list) {
  326. CHECK_FAIL_RETURN_UNEXPECTED(tensor->HasData(), "Apply transform failed, output tensor has no data");
  327. auto ms_tensor = mindspore::MSTensor(std::make_shared<DETensor>(tensor));
  328. output_tensor_list->emplace_back(ms_tensor);
  329. }
  330. CHECK_FAIL_RETURN_UNEXPECTED(!output_tensor_list->empty(), "Output Tensor is not valid");
  331. } else { // Case Ascend310
  332. #ifdef ENABLE_ACL
  333. CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310");
  334. for (auto &input_tensor : input_tensor_list) {
  335. // Sink each data from host into device
  336. std::shared_ptr<dataset::DeviceTensor> device_input;
  337. RETURN_IF_NOT_OK(device_resource_->Sink(input_tensor, &device_input));
  338. for (auto &t : transforms) {
  339. std::shared_ptr<DeviceTensor> device_output;
  340. RETURN_IF_NOT_OK(t->SetAscendResource(device_resource_));
  341. RETURN_IF_NOT_OK(t->Compute(device_input, &device_output));
  342. // For next transform
  343. device_input = std::move(device_output);
  344. }
  345. CHECK_FAIL_RETURN_UNEXPECTED(device_input->HasDeviceData(), "Apply transform failed, output tensor has no data");
  346. // Due to the limitation of Ascend310 memory, we have to pop every data onto host memory
  347. // So the speed of this batch method is slower than solo mode
  348. std::shared_ptr<mindspore::dataset::Tensor> host_output;
  349. RETURN_IF_NOT_OK(device_resource_->Pop(device_input, &host_output));
  350. auto ms_tensor = mindspore::MSTensor(std::make_shared<DETensor>(host_output));
  351. output_tensor_list->emplace_back(ms_tensor);
  352. // Release the data on the device because we have copied one piece onto host
  353. RETURN_IF_NOT_OK(device_resource_->DeviceDataRelease());
  354. }
  355. CHECK_FAIL_RETURN_UNEXPECTED(!output_tensor_list->empty(), "Output Tensor vector is empty");
  356. #endif
  357. }
  358. return Status::OK();
  359. }
  360. std::vector<uint32_t> AippSizeFilter(const std::vector<uint32_t> &resize_para, const std::vector<uint32_t> &crop_para) {
  361. std::vector<uint32_t> aipp_size;
  362. // Special condition where (no Crop and no Resize) or (no Crop and resize with fixed ratio) will lead to dynamic input
  363. if ((resize_para.size() == 0 || resize_para.size() == 1) && crop_para.size() == 0) {
  364. aipp_size = {0, 0};
  365. MS_LOG(WARNING) << "Dynamic input shape is not supported, incomplete aipp config file will be generated. Please "
  366. "checkout your TensorTransform input, both src_image_size_h and src_image_size will be 0";
  367. return aipp_size;
  368. }
  369. if (resize_para.size() == 0) { // If only Crop operator exists
  370. aipp_size = crop_para;
  371. } else if (crop_para.size() == 0) { // If only Resize operator with 2 parameters exists
  372. aipp_size = resize_para;
  373. } else { // If both of them exist
  374. if (resize_para.size() == 1) {
  375. aipp_size = crop_para;
  376. } else {
  377. aipp_size =
  378. *min_element(resize_para.begin(), resize_para.end()) < *min_element(crop_para.begin(), crop_para.end())
  379. ? resize_para
  380. : crop_para;
  381. }
  382. }
  383. return aipp_size;
  384. }
  385. std::vector<uint32_t> AippMeanFilter(const std::vector<uint32_t> &normalize_para) {
  386. std::vector<uint32_t> aipp_mean;
  387. if (normalize_para.size() == 6) { // If Normalize operator exist
  388. std::transform(normalize_para.begin(), normalize_para.begin() + 3, std::back_inserter(aipp_mean),
  389. [](uint32_t i) { return static_cast<uint32_t>(i / 10000); });
  390. } else {
  391. aipp_mean = {0, 0, 0};
  392. }
  393. return aipp_mean;
  394. }
  395. std::vector<float> AippStdFilter(const std::vector<uint32_t> &normalize_para) {
  396. std::vector<float> aipp_std;
  397. if (normalize_para.size() == 6) { // If Normalize operator exist
  398. auto zeros = std::find(std::begin(normalize_para), std::end(normalize_para), 0);
  399. if (zeros == std::end(normalize_para)) {
  400. std::transform(normalize_para.begin() + 3, normalize_para.end(), std::back_inserter(aipp_std),
  401. [](uint32_t i) { return 10000 / static_cast<float>(i); });
  402. } else { // If 0 occurs in std vector
  403. MS_LOG(WARNING) << "Detect 0 in std vector, please verify your input";
  404. aipp_std = {1.0, 1.0, 1.0};
  405. }
  406. } else {
  407. aipp_std = {1.0, 1.0, 1.0};
  408. }
  409. return aipp_std;
  410. }
  411. Status AippInfoCollection(std::map<std::string, std::string> *aipp_options, const std::vector<uint32_t> &aipp_size,
  412. const std::vector<uint32_t> &aipp_mean, const std::vector<float> &aipp_std) {
  413. // Several aipp config parameters
  414. aipp_options->insert(std::make_pair("related_input_rank", "0"));
  415. aipp_options->insert(std::make_pair("src_image_size_w", std::to_string(aipp_size[1])));
  416. aipp_options->insert(std::make_pair("src_image_size_h", std::to_string(aipp_size[0])));
  417. aipp_options->insert(std::make_pair("crop", "false"));
  418. aipp_options->insert(std::make_pair("input_format", "YUV420SP_U8"));
  419. aipp_options->insert(std::make_pair("aipp_mode", "static"));
  420. aipp_options->insert(std::make_pair("csc_switch", "true"));
  421. aipp_options->insert(std::make_pair("rbuv_swap_switch", "false"));
  422. // Y = AX + b, this part is A
  423. std::vector<int32_t> color_space_matrix = {256, 0, 359, 256, -88, -183, 256, 454, 0};
  424. int count = 0;
  425. for (int i = 0; i < 3; i++) {
  426. for (int j = 0; j < 3; j++) {
  427. std::string key_word = "matrix_r" + std::to_string(i) + "c" + std::to_string(j);
  428. aipp_options->insert(std::make_pair(key_word, std::to_string(color_space_matrix[count])));
  429. ++count;
  430. }
  431. }
  432. // This part is b
  433. std::vector<uint32_t> color_space_bias = {0, 128, 128};
  434. for (int i = 0; i < 3; i++) {
  435. std::string key_word = "input_bias_" + std::to_string(i);
  436. aipp_options->insert(std::make_pair(key_word, std::to_string(color_space_bias[i])));
  437. }
  438. // Y = (X - mean - min) * [std^(-1)], this part is mean
  439. for (int i = 0; i < aipp_mean.size(); i++) {
  440. std::string key_word = "mean_chn_" + std::to_string(i);
  441. aipp_options->insert(std::make_pair(key_word, std::to_string(aipp_mean[i])));
  442. }
  443. // This part is min
  444. for (int i = 0; i < aipp_mean.size(); i++) {
  445. std::string key_word = "min_chn_" + std::to_string(i);
  446. aipp_options->insert(std::make_pair(key_word, "0.0"));
  447. }
  448. // This part is std^(-1)
  449. for (int i = 0; i < aipp_std.size(); i++) {
  450. std::string key_word = "var_reci_chn_" + std::to_string(i);
  451. aipp_options->insert(std::make_pair(key_word, std::to_string(aipp_std[i])));
  452. }
  453. return Status::OK();
  454. }
  455. std::string Execute::AippCfgGenerator() {
  456. std::string config_location = "./aipp.cfg";
  457. #ifdef ENABLE_ACL
  458. std::vector<uint32_t> paras; // Record the parameters value of each Ascend operators
  459. for (int32_t i = 0; i < ops_.size(); i++) {
  460. // Validate operator ir
  461. json ir_info;
  462. if (ops_[i] == nullptr) {
  463. MS_LOG(ERROR) << "Input TensorOperation[" + std::to_string(i) + "] is null";
  464. return "";
  465. }
  466. if (ops_[i]->ValidateParams() != Status::OK()) {
  467. MS_LOG(ERROR) << "Input TensorOperation[" + std::to_string(i) + "] has wrong parameters";
  468. return "";
  469. }
  470. // Define map between operator name and parameter name
  471. ops_[i]->to_json(&ir_info);
  472. std::multimap<std::string, std::string> op_list = {{vision::kDvppCropJpegOperation, "size"},
  473. {vision::kDvppDecodeResizeOperation, "size"},
  474. {vision::kDvppDecodeResizeCropOperation, "crop_size"},
  475. {vision::kDvppDecodeResizeCropOperation, "resize_size"},
  476. {vision::kDvppNormalizeOperation, "mean"},
  477. {vision::kDvppNormalizeOperation, "std"},
  478. {vision::kDvppResizeJpegOperation, "size"}};
  479. // Collect the information of operators
  480. for (auto pos = op_list.equal_range(ops_[i]->Name()); pos.first != pos.second; ++pos.first) {
  481. auto paras_key_word = pos.first->second;
  482. paras = ir_info[paras_key_word].get<std::vector<uint32_t>>();
  483. info_->aipp_cfg_.insert(std::make_pair(ops_[i]->Name(), paras));
  484. }
  485. }
  486. std::ofstream outfile;
  487. outfile.open(config_location, std::ofstream::out);
  488. if (!outfile.is_open()) {
  489. MS_LOG(ERROR) << "Fail to open Aipp config file, please verify your system config(including authority)"
  490. << "We will return empty string which represent the location of Aipp config file in this case";
  491. std::string except = "";
  492. return except;
  493. }
  494. if (device_type_ == MapTargetDevice::kAscend310) {
  495. // Process resize parameters and crop parameters to find out the final size of input data
  496. std::vector<uint32_t> resize_paras;
  497. std::vector<uint32_t> crop_paras;
  498. // Find resize parameters
  499. std::map<std::string, std::vector<uint32_t>>::iterator iter;
  500. if (info_->aipp_cfg_.find(vision::kDvppResizeJpegOperation) != info_->aipp_cfg_.end()) {
  501. iter = info_->aipp_cfg_.find(vision::kDvppResizeJpegOperation);
  502. resize_paras = iter->second;
  503. } else if (info_->aipp_cfg_.find(vision::kDvppDecodeResizeOperation) != info_->aipp_cfg_.end()) {
  504. iter = info_->aipp_cfg_.find(vision::kDvppDecodeResizeOperation);
  505. resize_paras = iter->second;
  506. }
  507. // Find crop parameters
  508. if (info_->aipp_cfg_.find(vision::kDvppCropJpegOperation) != info_->aipp_cfg_.end()) {
  509. iter = info_->aipp_cfg_.find(vision::kDvppCropJpegOperation);
  510. crop_paras = iter->second;
  511. } else if (info_->aipp_cfg_.find(vision::kDvppDecodeResizeCropOperation) != info_->aipp_cfg_.end()) {
  512. iter = info_->aipp_cfg_.find(vision::kDvppDecodeResizeCropOperation);
  513. crop_paras = iter->second;
  514. }
  515. if (crop_paras.size() == 1) {
  516. crop_paras.emplace_back(crop_paras[0]);
  517. }
  518. std::vector<uint32_t> aipp_size = AippSizeFilter(resize_paras, crop_paras);
  519. // Process normalization parameters to find out the final normalization parameters for Aipp module
  520. std::vector<uint32_t> normalize_paras;
  521. if (info_->aipp_cfg_.find(vision::kDvppNormalizeOperation) != info_->aipp_cfg_.end()) {
  522. for (auto pos = info_->aipp_cfg_.equal_range(vision::kDvppNormalizeOperation); pos.first != pos.second;
  523. ++pos.first) {
  524. auto mean_or_std = pos.first->second;
  525. normalize_paras.insert(normalize_paras.end(), mean_or_std.begin(), mean_or_std.end());
  526. }
  527. }
  528. std::vector<uint32_t> aipp_mean = AippMeanFilter(normalize_paras);
  529. std::vector<float> aipp_std = AippStdFilter(normalize_paras);
  530. std::map<std::string, std::string> aipp_options;
  531. AippInfoCollection(&aipp_options, aipp_size, aipp_mean, aipp_std);
  532. std::string tab_char(4, ' ');
  533. outfile << "aipp_op {" << std::endl;
  534. for (auto &option : aipp_options) {
  535. outfile << tab_char << option.first << " : " << option.second << std::endl;
  536. }
  537. outfile << "}";
  538. outfile.close();
  539. }
  540. #endif
  541. return config_location;
  542. }
  543. Status Execute::validate_device_() {
  544. if (device_type_ != MapTargetDevice::kCpu && device_type_ != MapTargetDevice::kAscend310) {
  545. std::string err_msg = "Your input device is not supported. (Option: CPU or Ascend310)";
  546. MS_LOG(ERROR) << err_msg;
  547. RETURN_STATUS_UNEXPECTED(err_msg);
  548. }
  549. return Status::OK();
  550. }
  551. Status Execute::DeviceMemoryRelease() {
  552. CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310");
  553. Status rc = device_resource_->DeviceDataRelease();
  554. if (rc.IsError()) {
  555. std::string err_msg = "Error in device data release";
  556. MS_LOG(ERROR) << err_msg;
  557. RETURN_STATUS_UNEXPECTED(err_msg);
  558. }
  559. return Status::OK();
  560. }
  561. } // namespace dataset
  562. } // namespace mindspore