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.5 kB

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