CTfLiteClass.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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::GetClassFromImage(std::string _fn)
  25. {
  26. // printf("Before Load image %s\n", _fn.c_str());
  27. if (!LoadInputImage(_fn))
  28. return -1000;
  29. // printf("After Load image %s\n", _fn.c_str());
  30. Invoke();
  31. printf("After Invoke %s\n", _fn.c_str());
  32. return GetOutClassification();
  33. }
  34. int CTfLiteClass::GetOutClassification()
  35. {
  36. TfLiteTensor* output2 = interpreter->output(0);
  37. float zw_max = 0;
  38. float zw;
  39. int zw_class = -1;
  40. if (output2 == NULL)
  41. return -1;
  42. int numeroutput = output2->dims->data[1];
  43. for (int i = 0; i < numeroutput; ++i)
  44. {
  45. zw = output2->data.f[i];
  46. if (zw > zw_max)
  47. {
  48. zw_max = zw;
  49. zw_class = i;
  50. }
  51. }
  52. // printf("Result Ziffer: %d\n", zw_class);
  53. return zw_class;
  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) printf("NumDimension: %d\n", numdim);
  60. int sizeofdim;
  61. for (int j = 0; j < numdim; ++j)
  62. {
  63. sizeofdim = input2->dims->data[j];
  64. if (!silent) printf("SizeOfDimension %d: %d\n", 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. void CTfLiteClass::GetOutPut()
  71. {
  72. TfLiteTensor* output2 = this->interpreter->output(0);
  73. int numdim = output2->dims->size;
  74. printf("NumDimension: %d\n", numdim);
  75. int sizeofdim;
  76. for (int j = 0; j < numdim; ++j)
  77. {
  78. sizeofdim = output2->dims->data[j];
  79. printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  80. }
  81. float fo;
  82. // Process the inference results.
  83. int numeroutput = output2->dims->data[1];
  84. for (int i = 0; i < numeroutput; ++i)
  85. {
  86. fo = output2->data.f[i];
  87. printf("Result %d: %f\n", i, fo);
  88. }
  89. }
  90. void CTfLiteClass::Invoke()
  91. {
  92. interpreter->Invoke();
  93. // printf("Invoke Done.\n");
  94. }
  95. bool CTfLiteClass::LoadInputImageBasis(CImageBasis *rs)
  96. {
  97. std::string zw = "ClassFlowAnalog::doNeuralNetwork nach LoadInputResizeImage: ";
  98. // LogFile.WriteToFile(zw);
  99. unsigned int w = rs->width;
  100. unsigned int h = rs->height;
  101. unsigned char red, green, blue;
  102. // printf("Image: %s size: %d x %d\n", _fn.c_str(), w, h);
  103. input_i = 0;
  104. float* input_data_ptr = (interpreter->input(0))->data.f;
  105. for (int y = 0; y < h; ++y)
  106. for (int x = 0; x < w; ++x)
  107. {
  108. red = rs->GetPixelColor(x, y, 0);
  109. green = rs->GetPixelColor(x, y, 1);
  110. blue = rs->GetPixelColor(x, y, 2);
  111. *(input_data_ptr) = (float) red;
  112. input_data_ptr++;
  113. *(input_data_ptr) = (float) green;
  114. input_data_ptr++;
  115. *(input_data_ptr) = (float) blue;
  116. input_data_ptr++;
  117. // printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
  118. }
  119. if (debugdetailtflite) LogFile.WriteToFile("Nach dem Laden in input");
  120. return true;
  121. }
  122. bool CTfLiteClass::LoadInputImage(std::string _fn)
  123. {
  124. std::string zw = "ClassFlowAnalog::doNeuralNetwork nach Load Image: " + _fn;
  125. // LogFile.WriteToFile(zw);
  126. bitmap_image image(_fn);
  127. if (debugdetailtflite) LogFile.WriteToFile(zw);
  128. unsigned int w = image.width();
  129. unsigned int h = image.height();
  130. unsigned char red, green, blue;
  131. // printf("Image: %s size: %d x %d\n", _fn.c_str(), w, h);
  132. input_i = 0;
  133. float* input_data_ptr = (interpreter->input(0))->data.f;
  134. for (int y = 0; y < h; ++y)
  135. for (int x = 0; x < w; ++x)
  136. {
  137. red = image.red_channel(x, y);
  138. green = image.green_channel(x, y);
  139. blue = image.blue_channel(x, y);
  140. *(input_data_ptr) = (float) red;
  141. input_data_ptr++;
  142. *(input_data_ptr) = (float) green;
  143. input_data_ptr++;
  144. *(input_data_ptr) = (float) blue;
  145. input_data_ptr++;
  146. // printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
  147. }
  148. if (debugdetailtflite) LogFile.WriteToFile("Nach dem Laden in input");
  149. return true;
  150. }
  151. void CTfLiteClass::MakeAllocate()
  152. {
  153. // static tflite::ops::micro::AllOpsResolver resolver;
  154. static tflite::AllOpsResolver resolver;
  155. this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  156. TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
  157. if (allocate_status != kTfLiteOk) {
  158. TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
  159. this->GetInputDimension();
  160. return;
  161. }
  162. // printf("Allocate Done.\n");
  163. }
  164. void CTfLiteClass::GetInputTensorSize(){
  165. float *zw = this->input;
  166. int test = sizeof(zw);
  167. printf("Input Tensor Dimension: %d\n", test);
  168. printf("Input Tensor Dimension: %d\n", test);
  169. }
  170. long CTfLiteClass::GetFileSize(std::string filename)
  171. {
  172. struct stat stat_buf;
  173. long rc = stat(filename.c_str(), &stat_buf);
  174. return rc == 0 ? stat_buf.st_size : -1;
  175. }
  176. unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
  177. {
  178. long size;
  179. size = this->GetFileSize(_fn);
  180. if (size == -1)
  181. {
  182. printf("\nFile existiert nicht.\n");
  183. return NULL;
  184. }
  185. unsigned char *result = (unsigned char*) malloc(size);
  186. if(result != NULL) {
  187. // printf("\nSpeicher ist reserviert\n");
  188. FILE* f = OpenFileAndWait(_fn.c_str(), "rb"); // vorher nur "r"
  189. fread(result, 1, size, f);
  190. fclose(f);
  191. }else {
  192. printf("\nKein freier Speicher vorhanden.\n");
  193. }
  194. return result;
  195. }
  196. void CTfLiteClass::LoadModel(std::string _fn){
  197. #ifdef SUPRESS_TFLITE_ERRORS
  198. this->error_reporter = new tflite::OwnMicroErrorReporter;
  199. #else
  200. this->error_reporter = new tflite::MicroErrorReporter;
  201. #endif
  202. unsigned char *rd;
  203. rd = this->ReadFileToCharArray(_fn.c_str());
  204. // printf("loadedfile: %d", (int) rd);
  205. this->model = tflite::GetModel(rd);
  206. free(rd);
  207. TFLITE_MINIMAL_CHECK(model != nullptr);
  208. // printf("tfile Loaded.\n");
  209. }
  210. CTfLiteClass::CTfLiteClass()
  211. {
  212. this->model = nullptr;
  213. this->interpreter = nullptr;
  214. this->input = nullptr;
  215. this->output = nullptr;
  216. this->kTensorArenaSize = 150 * 1024; /// laut testfile: 108000 - bisher 600
  217. this->tensor_arena = new uint8_t[kTensorArenaSize];
  218. }
  219. CTfLiteClass::~CTfLiteClass()
  220. {
  221. delete this->tensor_arena;
  222. delete this->interpreter;
  223. delete this->error_reporter;
  224. }
  225. namespace tflite {
  226. int OwnMicroErrorReporter::Report(const char* format, va_list args) {
  227. return 0;
  228. }
  229. } // namespace tflite