CTfLiteClass.cpp 5.8 KB

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