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.

jni-example.cc 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <jni.h>
  17. #include <string>
  18. #include <fstream>
  19. #include <iostream>
  20. #include <memory>
  21. #include "minddata/dataset/include/datasets.h"
  22. #include "minddata/dataset/util/path.h"
  23. #if defined(__ANDROID__) || defined(ANDROID)
  24. #include <android/log.h>
  25. #include <android/asset_manager.h>
  26. #endif
  27. extern "C" JNIEXPORT jstring JNICALL Java_com_example_mindsporepredict_MainActivity_stringFromJNI(JNIEnv *env,
  28. jobject /* this */) {
  29. std::string hello = "Hello World!";
  30. MS_LOG(DEBUG) << hello;
  31. return env->NewStringUTF(hello.c_str());
  32. }
  33. using Dataset = mindspore::dataset::api::Dataset;
  34. using Iterator = mindspore::dataset::api::Iterator;
  35. using mindspore::dataset::Path;
  36. using mindspore::dataset::Tensor;
  37. using mindspore::dataset::api::Cifar10;
  38. using mindspore::dataset::api::RandomSampler;
  39. extern "C" JNIEXPORT void JNICALL Java_com_example_mindsporepredict_MainActivity_pathTest(JNIEnv *env,
  40. jobject /* this */,
  41. jstring path) {
  42. MS_LOG(WARNING) << env->GetStringUTFChars(path, 0);
  43. Path f(env->GetStringUTFChars(path, 0));
  44. MS_LOG(WARNING) << f.Exists() << f.IsDirectory() << f.ParentPath();
  45. // Print out the first few items in the directory
  46. auto dir_it = Path::DirIterator::OpenDirectory(&f);
  47. MS_LOG(WARNING) << dir_it.get();
  48. int i = 0;
  49. while (dir_it->hasNext()) {
  50. Path v = dir_it->next();
  51. MS_LOG(WARNING) << v.toString();
  52. i++;
  53. if (i > 5) break;
  54. }
  55. }
  56. extern "C" JNIEXPORT void JNICALL Java_com_example_mindsporepredict_MainActivity_TestCifar10Dataset(JNIEnv *env,
  57. jobject /* this */,
  58. jstring path) {
  59. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10Dataset.";
  60. // Create a Cifar10 Dataset
  61. std::string folder_path = env->GetStringUTFChars(path, 0);
  62. std::shared_ptr<Dataset> ds = Cifar10(folder_path, std::string(), RandomSampler(false, 10));
  63. // Create an iterator over the result of the above dataset
  64. // This will trigger the creation of the Execution Tree and launch it.
  65. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  66. // Iterate the dataset and get each row
  67. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  68. iter->GetNextRow(&row);
  69. uint64_t i = 0;
  70. while (row.size() != 0) {
  71. i++;
  72. auto image = row["image"];
  73. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  74. iter->GetNextRow(&row);
  75. }
  76. // Manually terminate the pipeline
  77. iter->Stop();
  78. }