CTfLiteClass.cpp 6.2 KB

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