CTfLiteClass.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "CTfLiteClass.h"
  2. #include "ClassLogFile.h"
  3. #include "Helper.h"
  4. #include <sys/stat.h>
  5. // #define DEBUG_DETAIL_ON
  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::GetClassFromImageBasis(CImageBasis *rs)
  15. {
  16. if (!LoadInputImageBasis(rs))
  17. return -1000;
  18. Invoke();
  19. return GetOutClassification();
  20. }
  21. int CTfLiteClass::GetOutClassification(int _von, int _bis)
  22. {
  23. TfLiteTensor* output2 = interpreter->output(0);
  24. float zw_max;
  25. float zw;
  26. int zw_class;
  27. if (output2 == NULL)
  28. return -1;
  29. int numeroutput = output2->dims->data[1];
  30. //printf("\n number output neurons: %d\n\n", numeroutput);
  31. if (_bis == -1)
  32. _bis = numeroutput;
  33. if (_von == -1)
  34. _von = 0;
  35. if (_bis > numeroutput)
  36. {
  37. printf("ANZAHL OUTPUT NEURONS passt nicht zu geforderter Classifizierung!");
  38. return -1;
  39. }
  40. zw_max = output2->data.f[_von];
  41. zw_class = _von;
  42. for (int i = _von+1; i <= _bis; ++i)
  43. {
  44. zw = output2->data.f[i];
  45. if (zw > zw_max)
  46. {
  47. zw_max = zw;
  48. zw_class = i;
  49. }
  50. }
  51. return (zw_class - _von);
  52. }
  53. /*
  54. int CTfLiteClass::GetOutClassification()
  55. {
  56. TfLiteTensor* output2 = interpreter->output(0);
  57. float zw_max = 0;
  58. float zw;
  59. int zw_class = -1;
  60. if (output2 == NULL)
  61. return -1;
  62. int numeroutput = output2->dims->data[1];
  63. for (int i = 0; i < numeroutput; ++i)
  64. {
  65. zw = output2->data.f[i];
  66. if (zw > zw_max)
  67. {
  68. zw_max = zw;
  69. zw_class = i;
  70. }
  71. }
  72. return zw_class;
  73. }
  74. */
  75. void CTfLiteClass::GetInputDimension(bool silent = false)
  76. {
  77. TfLiteTensor* input2 = this->interpreter->input(0);
  78. int numdim = input2->dims->size;
  79. if (!silent) printf("NumDimension: %d\n", numdim);
  80. int sizeofdim;
  81. for (int j = 0; j < numdim; ++j)
  82. {
  83. sizeofdim = input2->dims->data[j];
  84. if (!silent) printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  85. if (j == 1) im_height = sizeofdim;
  86. if (j == 2) im_width = sizeofdim;
  87. if (j == 3) im_channel = sizeofdim;
  88. }
  89. }
  90. int CTfLiteClass::GetAnzOutPut(bool silent)
  91. {
  92. TfLiteTensor* output2 = this->interpreter->output(0);
  93. int numdim = output2->dims->size;
  94. if (!silent) printf("NumDimension: %d\n", numdim);
  95. int sizeofdim;
  96. for (int j = 0; j < numdim; ++j)
  97. {
  98. sizeofdim = output2->dims->data[j];
  99. if (!silent) printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  100. }
  101. float fo;
  102. // Process the inference results.
  103. int numeroutput = output2->dims->data[1];
  104. for (int i = 0; i < numeroutput; ++i)
  105. {
  106. fo = output2->data.f[i];
  107. if (!silent) printf("Result %d: %f\n", i, fo);
  108. }
  109. return numeroutput;
  110. }
  111. void CTfLiteClass::Invoke()
  112. {
  113. if (interpreter != nullptr)
  114. interpreter->Invoke();
  115. }
  116. bool CTfLiteClass::LoadInputImageBasis(CImageBasis *rs)
  117. {
  118. std::string zw = "ClassFlowCNNGeneral::doNeuralNetwork nach LoadInputResizeImage: ";
  119. unsigned int w = rs->width;
  120. unsigned int h = rs->height;
  121. unsigned char red, green, blue;
  122. // printf("Image: %s size: %d x %d\n", _fn.c_str(), w, h);
  123. input_i = 0;
  124. float* input_data_ptr = (interpreter->input(0))->data.f;
  125. for (int y = 0; y < h; ++y)
  126. for (int x = 0; x < w; ++x)
  127. {
  128. red = rs->GetPixelColor(x, y, 0);
  129. green = rs->GetPixelColor(x, y, 1);
  130. blue = rs->GetPixelColor(x, y, 2);
  131. *(input_data_ptr) = (float) red;
  132. input_data_ptr++;
  133. *(input_data_ptr) = (float) green;
  134. input_data_ptr++;
  135. *(input_data_ptr) = (float) blue;
  136. input_data_ptr++;
  137. }
  138. #ifdef DEBUG_DETAIL_ON
  139. LogFile.WriteToFile("Nach dem Laden in input");
  140. #endif
  141. return true;
  142. }
  143. void CTfLiteClass::MakeAllocate()
  144. {
  145. static tflite::AllOpsResolver resolver;
  146. // printf(LogFile.getESPHeapInfo().c_str()); printf("\n");
  147. this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  148. // printf(LogFile.getESPHeapInfo().c_str()); printf("\n");
  149. TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
  150. if (allocate_status != kTfLiteOk) {
  151. TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
  152. this->GetInputDimension();
  153. return;
  154. }
  155. // printf("Allocate Done.\n");
  156. }
  157. void CTfLiteClass::GetInputTensorSize(){
  158. #ifdef DEBUG_DETAIL_ON
  159. float *zw = this->input;
  160. int test = sizeof(zw);
  161. printf("Input Tensor Dimension: %d\n", test);
  162. #endif
  163. }
  164. long CTfLiteClass::GetFileSize(std::string filename)
  165. {
  166. struct stat stat_buf;
  167. long rc = stat(filename.c_str(), &stat_buf);
  168. return rc == 0 ? stat_buf.st_size : -1;
  169. }
  170. unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
  171. {
  172. long size;
  173. size = GetFileSize(_fn);
  174. if (size == -1)
  175. {
  176. printf("\nFile existiert nicht.\n");
  177. return NULL;
  178. }
  179. unsigned char *result = (unsigned char*) malloc(size);
  180. int anz = 1;
  181. while (!result && (anz < 6)) // maximal 5x versuchen (= 5s)
  182. {
  183. #ifdef DEBUG_DETAIL_ON
  184. printf("Speicher ist voll - Versuche es erneut: %d.\n", anz);
  185. #endif
  186. result = (unsigned char*) malloc(size);
  187. anz++;
  188. }
  189. if(result != NULL) {
  190. FILE* f = OpenFileAndWait(_fn.c_str(), "rb"); // vorher nur "r"
  191. fread(result, 1, size, f);
  192. fclose(f);
  193. }else {
  194. printf("\nKein freier Speicher vorhanden.\n");
  195. }
  196. return result;
  197. }
  198. bool CTfLiteClass::LoadModel(std::string _fn){
  199. #ifdef SUPRESS_TFLITE_ERRORS
  200. this->error_reporter = new tflite::OwnMicroErrorReporter;
  201. #else
  202. this->error_reporter = new tflite::MicroErrorReporter;
  203. #endif
  204. unsigned char *rd;
  205. rd = ReadFileToCharArray(_fn.c_str());
  206. if (rd == NULL)
  207. return false;
  208. this->model = tflite::GetModel(rd);
  209. free(rd);
  210. TFLITE_MINIMAL_CHECK(model != nullptr);
  211. return true;
  212. }
  213. CTfLiteClass::CTfLiteClass()
  214. {
  215. this->model = nullptr;
  216. this->interpreter = nullptr;
  217. this->input = nullptr;
  218. this->output = nullptr;
  219. this->kTensorArenaSize = 200 * 1024; /// laut testfile: 108000 - bisher 600
  220. this->tensor_arena = new uint8_t[kTensorArenaSize];
  221. }
  222. CTfLiteClass::~CTfLiteClass()
  223. {
  224. delete this->tensor_arena;
  225. delete this->interpreter;
  226. delete this->error_reporter;
  227. }
  228. namespace tflite {
  229. int OwnMicroErrorReporter::Report(const char* format, va_list args) {
  230. return 0;
  231. }
  232. }