CTfLiteClass.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "CTfLiteClass.h"
  2. #include "bitmap_image.hpp"
  3. #include <sys/stat.h>
  4. float CTfLiteClass::GetOutputValue(int nr)
  5. {
  6. TfLiteTensor* output2 = this->interpreter->output(0);
  7. int numeroutput = output2->dims->data[1];
  8. if ((nr+1) > numeroutput)
  9. return -1000;
  10. return output2->data.f[nr];
  11. }
  12. int CTfLiteClass::GetClassFromImage(std::string _fn)
  13. {
  14. if (!LoadInputImage(_fn))
  15. return -1000;
  16. Invoke();
  17. return GetOutClassification();
  18. // return 0;
  19. }
  20. int CTfLiteClass::GetOutClassification()
  21. {
  22. TfLiteTensor* output2 = interpreter->output(0);
  23. float zw_max = 0;
  24. float zw;
  25. int zw_class = -1;
  26. if (output2 == NULL)
  27. return -1;
  28. int numeroutput = output2->dims->data[1];
  29. for (int i = 0; i < numeroutput; ++i)
  30. {
  31. zw = output2->data.f[i];
  32. if (zw > zw_max)
  33. {
  34. zw_max = zw;
  35. zw_class = i;
  36. }
  37. }
  38. // printf("Result Ziffer: %d\n", zw_class);
  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. // printf("Invoke Done.\n");
  80. }
  81. /* mit CImageBasis --> funktioniert nicht, keine Ahnung warum !!!
  82. bool CTfLiteClass::LoadInputImage(std::string _fn)
  83. {
  84. CImageBasis *cib = new CImageBasis(_fn);
  85. GetInputDimension(true);
  86. printf("TFlite load image size: %d x %d x %d\n", cib->getWidth(), cib->getHeight(), cib->getChannels());
  87. // printf("TFlite expected image size: %d x %d x %d\n", im_width, im_height, im_channel);
  88. if ((cib->getWidth() != im_width) || (cib->getHeight() != im_height) || (cib->getChannels() != im_channel))
  89. {
  90. printf("Input Imagesize Wrong.\n");
  91. return false;
  92. }
  93. input_i = 0;
  94. float* input_data_ptr = (interpreter->input(0))->data.f;
  95. uint8_t ui8;
  96. unsigned char zw;
  97. for (int y = 0; y < cib->getHeight(); ++y)
  98. for (int x = 0; x < cib->getWidth(); ++x)
  99. {
  100. printf("CIB: ");
  101. for (int ch = 0; ch < cib->getChannels(); ++ch)
  102. {
  103. ui8 = cib->GetPixelColor(x, y, ch);
  104. printf("%f ", (float) ui8);
  105. *(input_data_ptr) = (float) ui8;
  106. input_data_ptr++;
  107. }
  108. printf("\n");
  109. }
  110. delete cib;
  111. return true;
  112. }
  113. */
  114. bool CTfLiteClass::LoadInputImage(std::string _fn)
  115. {
  116. bitmap_image image(_fn);
  117. unsigned int w = image.width();
  118. unsigned int h = image.height();
  119. unsigned char red, green, blue;
  120. input_i = 0;
  121. float* input_data_ptr = (interpreter->input(0))->data.f;
  122. for (int y = 0; y < h; ++y)
  123. for (int x = 0; x < w; ++x)
  124. {
  125. red = image.red_channel(x, y);
  126. green = image.green_channel(x, y);
  127. blue = image.blue_channel(x, y);
  128. *(input_data_ptr) = (float) red;
  129. input_data_ptr++;
  130. *(input_data_ptr) = (float) green;
  131. input_data_ptr++;
  132. *(input_data_ptr) = (float) blue;
  133. input_data_ptr++;
  134. // printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
  135. }
  136. return true;
  137. }
  138. void CTfLiteClass::MakeAllocate()
  139. {
  140. /*
  141. this->micro_op_resolver.AddBuiltin(
  142. tflite::BuiltinOperator_RESHAPE,
  143. tflite::ops::micro::Register_RESHAPE());
  144. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
  145. tflite::ops::micro::Register_CONV_2D());
  146. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_FULLY_CONNECTED,
  147. tflite::ops::micro::Register_FULLY_CONNECTED());
  148. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_SOFTMAX,
  149. tflite::ops::micro::Register_SOFTMAX());
  150. this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
  151. tflite::ops::micro::Register_DEPTHWISE_CONV_2D());
  152. this->interpreter = new tflite::MicroInterpreter(this->model, this->micro_op_resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  153. */
  154. static tflite::ops::micro::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 = fopen(_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. this->error_reporter = new tflite::MicroErrorReporter;
  198. unsigned char *rd;
  199. rd = this->ReadFileToCharArray(_fn.c_str());
  200. // printf("loadedfile: %d", (int) rd);
  201. this->model = tflite::GetModel(rd);
  202. free(rd);
  203. TFLITE_MINIMAL_CHECK(model != nullptr);
  204. printf("tfile Loaded.\n");
  205. }
  206. CTfLiteClass::CTfLiteClass()
  207. {
  208. // this->accessSD = _accessSD;
  209. this->model = nullptr;
  210. this->interpreter = nullptr;
  211. this->input = nullptr;
  212. this->output = nullptr;
  213. this->kTensorArenaSize = 600 * 1024;
  214. this->tensor_arena = new uint8_t[kTensorArenaSize];
  215. // micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
  216. // tflite::ops::micro::Register_CONV_2D());
  217. }
  218. CTfLiteClass::~CTfLiteClass()
  219. {
  220. delete this->tensor_arena;
  221. }