main_det.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* auto generate by HHB_VERSION "2.0.21" */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <libgen.h>
  25. #include <unistd.h>
  26. #include "io.h"
  27. #include "shl_ref.h"
  28. #include "process.h"
  29. #include "detect.h"
  30. #include "output_120_out0_nchw_1_2_7668_1.h"
  31. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  32. #define FILE_LENGTH 1028
  33. #define SHAPE_LENGHT 128
  34. void *csinn_(char *params);
  35. void csinn_run(void *data0, void *td);
  36. void *csinn_nbg(const char *nbg_file_name);
  37. int input_size[] = {1 * 3 * 304 * 304, };
  38. const char model_name[] = "network";
  39. const char class_name[][FILE_LENGTH] = {
  40. "background", "aeroplane", "bicycle", "bird", "boat",
  41. "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
  42. "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
  43. "sofa", "train", "tvmonitor"
  44. };
  45. #define RESIZE_HEIGHT 304
  46. #define RESIZE_WIDTH 304
  47. #define CROP_HEGHT 304
  48. #define CROP_WIDTH 304
  49. #define R_MEAN 0
  50. #define G_MEAN 0
  51. #define B_MEAN 0
  52. #define SCALE 1.0
  53. /*
  54. * Preprocess function
  55. */
  56. void preprocess(struct image_data *img, int is_rgb, int to_bgr)
  57. {
  58. uint32_t new_height, new_width;
  59. uint32_t min_side;
  60. if (is_rgb) {
  61. im2rgb(img);
  62. }
  63. if (RESIZE_WIDTH == 0) {
  64. min_side = MIN(img->shape[0], img->shape[1]);
  65. new_height = (uint32_t) (img->shape[0] * (((float)RESIZE_HEIGHT) / (float)min_side));
  66. new_width = (uint32_t) (img->shape[1] * (((float)RESIZE_HEIGHT) / (float)min_side));
  67. imresize(img, new_height, new_width);
  68. } else {
  69. imresize(img, RESIZE_HEIGHT, RESIZE_WIDTH);
  70. }
  71. data_crop(img, CROP_HEGHT, CROP_WIDTH);
  72. sub_mean(img, R_MEAN, G_MEAN, B_MEAN);
  73. data_scale(img, SCALE);
  74. if(to_bgr) {
  75. imrgb2bgr(img);
  76. }
  77. imhwc2chw(img);
  78. }
  79. static void print_tensor_info(struct csinn_tensor *t) {
  80. printf("\n=== tensor info ===\n");
  81. printf("shape: ");
  82. for (int j = 0; j < t->dim_count; j++) {
  83. printf("%d ", t->dim[j]);
  84. }
  85. printf("\n");
  86. if (t->dtype == CSINN_DTYPE_UINT8) {
  87. printf("scale: %f\n", t->qinfo->scale);
  88. printf("zero point: %d\n", t->qinfo->zero_point);
  89. }
  90. printf("data pointer: %p\n", t->data);
  91. }
  92. /*
  93. * Postprocess function
  94. */
  95. static void postprocess(void *sess, const char *filename_prefix) {
  96. int output_num, input_num;
  97. struct csinn_tensor *input = csinn_alloc_tensor(NULL);
  98. struct csinn_tensor *output = csinn_alloc_tensor(NULL);
  99. input_num = csinn_get_input_number(sess);
  100. for (int i = 0; i < input_num; i++) {
  101. input->data = NULL;
  102. csinn_get_input(i, input, sess);
  103. print_tensor_info(input);
  104. }
  105. float *location;
  106. float *confidence;
  107. output_num = csinn_get_output_number(sess);
  108. for (int i = 0; i < output_num; i++) {
  109. output->data = NULL;
  110. csinn_get_output(i, output, sess);
  111. print_tensor_info(output);
  112. struct csinn_tensor *foutput = shl_ref_tensor_transform_f32(output);
  113. shl_show_top5(foutput, sess);
  114. char filename[FILE_LENGTH] = {0};
  115. char shape[SHAPE_LENGHT] = {0};
  116. shape2string(output->dim, output->dim_count, shape, SHAPE_LENGHT);
  117. snprintf(filename, FILE_LENGTH, "%s_output%u_%s.txt", filename_prefix, i, shape);
  118. int output_size = csinn_tensor_size(foutput);
  119. save_data_to_file(filename, (float*)foutput->data, output_size);
  120. if (i == 0) location = (float *)foutput->data;
  121. if (i == 1) confidence = (float *)foutput->data;
  122. // shl_ref_tensor_transform_free_f32(foutput);
  123. }
  124. BBoxOut out[100];
  125. BBox gbboxes[num_prior];
  126. int num = ssdforward(location, confidence, priorbox, gbboxes, out);
  127. printf("%d\n", num);
  128. for (int i = 0; i < num; i++) {
  129. printf("%d, label=%s, score=%f, x1=%f, y1=%f, x2=%f, y2=%f\n", out[i].label, class_name[out[i].label],
  130. out[i].score, out[i].xmin, out[i].ymin, out[i].xmax, out[i].ymax);
  131. }
  132. }
  133. void *create_graph(char *params_path) {
  134. void *ret;
  135. int binary_size;
  136. char *params = get_binary_from_file(params_path, &binary_size);
  137. if (params == NULL) {
  138. return NULL;
  139. }
  140. char *suffix = params_path + (strlen(params_path) - 7);
  141. if (strcmp(suffix, ".params") == 0) {
  142. // create general graph
  143. ret = csinn_(params);
  144. free(params);
  145. return ret;
  146. }
  147. suffix = params_path + (strlen(params_path) - 3);
  148. if (strcmp(suffix, ".bm") == 0) {
  149. struct shl_bm_sections *section = (struct shl_bm_sections *)(params + 4128);
  150. if (section->graph_offset) {
  151. ret = csinn_import_binary_model(params);
  152. free(params);
  153. return ret;
  154. } else {
  155. ret = csinn_(params + section->params_offset * 4096);
  156. free(params);
  157. return ret;
  158. }
  159. } else {
  160. free(params);
  161. return NULL;
  162. }
  163. }
  164. int main(int argc, char **argv) {
  165. char **data_path = NULL;
  166. int input_num = 1;
  167. int output_num = 3;
  168. int input_group_num = 1;
  169. int i;
  170. if (argc < (2 + input_num)) {
  171. printf("Please set valide args: ./model.elf model.params "
  172. "[tensor1/image1 ...] [tensor2/image2 ...]\n");
  173. return -1;
  174. } else {
  175. if (argc == 3 && get_file_type(argv[2]) == FILE_TXT) {
  176. data_path = read_string_from_file(argv[2], &input_group_num);
  177. input_group_num /= input_num;
  178. } else {
  179. data_path = argv + 2;
  180. input_group_num = (argc - 2) / input_num;
  181. }
  182. }
  183. void *sess = create_graph(argv[1]);
  184. uint8_t *input[input_num];
  185. float *inputf[input_num];
  186. char filename_prefix[FILE_LENGTH] = {0};
  187. void *input_aligned[input_num];
  188. for (i = 0; i < input_num; i++) {
  189. input_aligned[i] = shl_mem_alloc_aligned(input_size[i], 0);
  190. }
  191. uint64_t start_time, end_time;
  192. char *loop_s;
  193. int32_t loop_time = 1;
  194. loop_s = getenv("HHB_LOOP_TIME");
  195. if (loop_s) {
  196. loop_time = atoi(loop_s);
  197. }
  198. for (i = 0; i < input_group_num; i++) {
  199. /* set input */
  200. for (int j = 0; j < input_num; j++) {
  201. int input_len = csinn_tensor_size(((struct csinn_session *)sess)->input[j]);
  202. if (get_file_type(data_path[i * input_num + j]) == 0) {
  203. input[j] = (uint8_t *)get_binary_from_file(data_path[i * input_num + j], &input_size[j]);
  204. }
  205. else {
  206. struct image_data *img = get_input_data(data_path[i * input_num + j], input_size[j]);
  207. if (get_file_type(data_path[i * input_num + j]) == FILE_PNG || get_file_type(data_path[i * input_num + j]) == FILE_JPEG) {
  208. preprocess(img, 1, 1);
  209. }
  210. inputf[j] = img->data;
  211. free_image_data(img);
  212. input[j] = shl_ref_f32_to_input_dtype(j, inputf[j], sess);
  213. }
  214. }
  215. memcpy(input_aligned[0], input[0], input_size[0]);
  216. for (int t = 0; t < loop_time; t++) {
  217. start_time = shl_get_timespec();
  218. csinn_run(input_aligned[0], sess);
  219. end_time = shl_get_timespec();
  220. printf("Run graph execution time: %.5fms, FPS=%.2f\n", ((float)(end_time-start_time))/1000000,
  221. 1000000000.0/((float)(end_time-start_time)));
  222. snprintf(filename_prefix, FILE_LENGTH, "%s", basename(data_path[i * input_num]));
  223. postprocess(sess, filename_prefix);
  224. }
  225. for (int j = 0; j < input_num; j++) {
  226. if (get_file_type(data_path[i * input_num + j]) != 0) {
  227. shl_mem_free(inputf[j]);
  228. }
  229. shl_mem_free(input[j]);
  230. }
  231. }
  232. csinn_session_deinit(sess);
  233. csinn_free_session(sess);
  234. return 0;
  235. }