CTfLiteClass.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 = "ctflite_class";
  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("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. this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  138. // ESP_LOGD(TAG, "%s", LogFile.getESPHeapInfo().c_str());
  139. TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
  140. if (allocate_status != kTfLiteOk) {
  141. TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
  142. LogFile.WriteToFile("AllocateTensors() failed");
  143. this->GetInputDimension();
  144. return;
  145. }
  146. // ESP_LOGD(TAG, "Allocate Done");
  147. }
  148. void CTfLiteClass::GetInputTensorSize(){
  149. #ifdef DEBUG_DETAIL_ON
  150. float *zw = this->input;
  151. int test = sizeof(zw);
  152. ESP_LOGD(TAG, "Input Tensor Dimension: %d", test);
  153. #endif
  154. }
  155. long CTfLiteClass::GetFileSize(std::string filename)
  156. {
  157. struct stat stat_buf;
  158. long rc = stat(filename.c_str(), &stat_buf);
  159. return rc == 0 ? stat_buf.st_size : -1;
  160. }
  161. unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
  162. {
  163. long size;
  164. size = GetFileSize(_fn);
  165. if (size == -1)
  166. {
  167. ESP_LOGD(TAG, "File doesn't exist");
  168. return NULL;
  169. }
  170. unsigned char *result = (unsigned char*) malloc(size);
  171. int anz = 1;
  172. while (!result && (anz < 6)) // maximal 5x versuchen (= 5s)
  173. {
  174. #ifdef DEBUG_DETAIL_ON
  175. ESP_LOGD(TAG, "Speicher ist voll - Versuche es erneut: %d", anz);
  176. #endif
  177. result = (unsigned char*) malloc(size);
  178. anz++;
  179. }
  180. if(result != NULL) {
  181. FILE* f = OpenFileAndWait(_fn.c_str(), "rb"); // vorher nur "r"
  182. fread(result, 1, size, f);
  183. fclose(f);
  184. }else {
  185. ESP_LOGD(TAG, "No free memory available");
  186. }
  187. return result;
  188. }
  189. bool CTfLiteClass::LoadModel(std::string _fn){
  190. #ifdef SUPRESS_TFLITE_ERRORS
  191. this->error_reporter = new tflite::OwnMicroErrorReporter;
  192. #else
  193. this->error_reporter = new tflite::MicroErrorReporter;
  194. #endif
  195. modelload = ReadFileToCharArray(_fn.c_str());
  196. if (modelload == NULL)
  197. return false;
  198. model = tflite::GetModel(modelload);
  199. // free(rd);
  200. TFLITE_MINIMAL_CHECK(model != nullptr);
  201. return true;
  202. }
  203. CTfLiteClass::CTfLiteClass()
  204. {
  205. this->model = nullptr;
  206. this->interpreter = nullptr;
  207. this->input = nullptr;
  208. this->output = nullptr;
  209. this->kTensorArenaSize = 800 * 1024; /// laut testfile: 108000 - bisher 600;; 2021-09-11: 200 * 1024
  210. this->tensor_arena = new uint8_t[kTensorArenaSize];
  211. }
  212. CTfLiteClass::~CTfLiteClass()
  213. {
  214. delete this->tensor_arena;
  215. delete this->interpreter;
  216. delete this->error_reporter;
  217. if (modelload)
  218. free(modelload);
  219. }
  220. namespace tflite {
  221. int OwnMicroErrorReporter::Report(const char* format, va_list args) {
  222. return 0;
  223. }
  224. }