CTfLiteClass._cpp_old 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "CTfLiteClass.h"
  2. #include "bitmap_image.hpp"
  3. #include <sys/stat.h>
  4. float CTfLiteClass::GetOutputValue(int nr)
  5. {
  6. TfLiteTensor* output2 = this->interpreter->output(0);
  7. int numeroutput = output2->dims->data[1];
  8. if ((nr+1) > numeroutput)
  9. return -1000;
  10. return output2->data.f[nr];
  11. }
  12. int CTfLiteClass::GetClassFromImage(std::string _fn)
  13. {
  14. // printf("Before Load image %s\n", _fn.c_str());
  15. if (!LoadInputImage(_fn))
  16. return -1000;
  17. // printf("After Load image %s\n", _fn.c_str());
  18. Invoke();
  19. printf("After Invoke %s\n", _fn.c_str());
  20. return GetOutClassification();
  21. // return 0;
  22. }
  23. int CTfLiteClass::GetOutClassification()
  24. {
  25. TfLiteTensor* output2 = interpreter->output(0);
  26. float zw_max = 0;
  27. float zw;
  28. int zw_class = -1;
  29. if (output2 == NULL)
  30. return -1;
  31. int numeroutput = output2->dims->data[1];
  32. for (int i = 0; i < numeroutput; ++i)
  33. {
  34. zw = output2->data.f[i];
  35. if (zw > zw_max)
  36. {
  37. zw_max = zw;
  38. zw_class = i;
  39. }
  40. }
  41. // printf("Result Ziffer: %d\n", zw_class);
  42. return zw_class;
  43. }
  44. void CTfLiteClass::GetInputDimension(bool silent = false)
  45. {
  46. TfLiteTensor* input2 = this->interpreter->input(0);
  47. int numdim = input2->dims->size;
  48. if (!silent) printf("NumDimension: %d\n", numdim);
  49. int sizeofdim;
  50. for (int j = 0; j < numdim; ++j)
  51. {
  52. sizeofdim = input2->dims->data[j];
  53. if (!silent) printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  54. if (j == 1) im_height = sizeofdim;
  55. if (j == 2) im_width = sizeofdim;
  56. if (j == 3) im_channel = sizeofdim;
  57. }
  58. }
  59. void CTfLiteClass::GetOutPut()
  60. {
  61. TfLiteTensor* output2 = this->interpreter->output(0);
  62. int numdim = output2->dims->size;
  63. printf("NumDimension: %d\n", numdim);
  64. int sizeofdim;
  65. for (int j = 0; j < numdim; ++j)
  66. {
  67. sizeofdim = output2->dims->data[j];
  68. printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  69. }
  70. float fo;
  71. // Process the inference results.
  72. int numeroutput = output2->dims->data[1];
  73. for (int i = 0; i < numeroutput; ++i)
  74. {
  75. fo = output2->data.f[i];
  76. printf("Result %d: %f\n", i, fo);
  77. }
  78. }
  79. void CTfLiteClass::Invoke()
  80. {
  81. interpreter->Invoke();
  82. // printf("Invoke Done.\n");
  83. }
  84. bool CTfLiteClass::LoadInputImage(std::string _fn)
  85. {
  86. bitmap_image image(_fn);
  87. unsigned int w = image.width();
  88. unsigned int h = image.height();
  89. unsigned char red, green, blue;
  90. input_i = 0;
  91. float* input_data_ptr = (interpreter->input(0))->data.f;
  92. for (int y = 0; y < h; ++y)
  93. for (int x = 0; x < w; ++x)
  94. {
  95. red = image.red_channel(x, y);
  96. green = image.green_channel(x, y);
  97. blue = image.blue_channel(x, y);
  98. *(input_data_ptr) = (float) red;
  99. input_data_ptr++;
  100. *(input_data_ptr) = (float) green;
  101. input_data_ptr++;
  102. *(input_data_ptr) = (float) blue;
  103. input_data_ptr++;
  104. // printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
  105. }
  106. return true;
  107. }
  108. void CTfLiteClass::MakeAllocate()
  109. {
  110. /*
  111. this->micro_op_resolver.AddBuiltin(
  112. tflite::BuiltinOperator_RESHAPE,
  113. tflite::ops::micro::Register_RESHAPE());
  114. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
  115. tflite::ops::micro::Register_CONV_2D());
  116. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_FULLY_CONNECTED,
  117. tflite::ops::micro::Register_FULLY_CONNECTED());
  118. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_SOFTMAX,
  119. tflite::ops::micro::Register_SOFTMAX());
  120. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
  121. tflite::ops::micro::Register_DEPTHWISE_CONV_2D());
  122. this->interpreter = new tflite::MicroInterpreter(this->model, this->micro_op_resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  123. */
  124. static tflite::ops::micro::AllOpsResolver resolver;
  125. this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  126. TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
  127. if (allocate_status != kTfLiteOk) {
  128. TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
  129. this->GetInputDimension();
  130. return;
  131. }
  132. printf("Allocate Done.\n");
  133. }
  134. void CTfLiteClass::GetInputTensorSize(){
  135. float *zw = this->input;
  136. int test = sizeof(zw);
  137. printf("Input Tensor Dimension: %d\n", test);
  138. printf("Input Tensor Dimension: %d\n", test);
  139. }
  140. long CTfLiteClass::GetFileSize(std::string filename)
  141. {
  142. struct stat stat_buf;
  143. long rc = stat(filename.c_str(), &stat_buf);
  144. return rc == 0 ? stat_buf.st_size : -1;
  145. }
  146. unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
  147. {
  148. long size;
  149. size = this->GetFileSize(_fn);
  150. if (size == -1)
  151. {
  152. printf("\nFile existiert nicht.\n");
  153. return NULL;
  154. }
  155. unsigned char *result = (unsigned char*) malloc(size);
  156. if(result != NULL) {
  157. // printf("\nSpeicher ist reserviert\n");
  158. FILE* f = fopen(_fn.c_str(), "rb"); // vorher nur "r"
  159. fread(result, 1, size, f);
  160. fclose(f);
  161. }else {
  162. printf("\nKein freier Speicher vorhanden.\n");
  163. }
  164. return result;
  165. }
  166. void CTfLiteClass::LoadModel(std::string _fn){
  167. this->error_reporter = new tflite::MicroErrorReporter;
  168. unsigned char *rd;
  169. rd = this->ReadFileToCharArray(_fn.c_str());
  170. // printf("loadedfile: %d", (int) rd);
  171. this->model = tflite::GetModel(rd);
  172. free(rd);
  173. TFLITE_MINIMAL_CHECK(model != nullptr);
  174. printf("tfile Loaded.\n");
  175. }
  176. CTfLiteClass::CTfLiteClass()
  177. {
  178. // this->accessSD = _accessSD;
  179. this->model = nullptr;
  180. this->interpreter = nullptr;
  181. this->input = nullptr;
  182. this->output = nullptr;
  183. this->kTensorArenaSize = 600 * 1024;
  184. this->tensor_arena = new uint8_t[kTensorArenaSize];
  185. // micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
  186. // tflite::ops::micro::Register_CONV_2D());
  187. }
  188. CTfLiteClass::~CTfLiteClass()
  189. {
  190. delete this->tensor_arena;
  191. }