CTfLiteClass.cpp 5.6 KB

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