CTfLiteClass.cpp 6.6 KB

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