CTfLiteClass.cpp 5.7 KB

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