CTfLiteClass.cpp 5.7 KB

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