CTfLiteClass.cpp 5.9 KB

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