main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  30. #define FILE_LENGTH 1028
  31. #define SHAPE_LENGHT 128
  32. void *csinn_(char *params);
  33. void csinn_run(void *data0, void *td);
  34. void *csinn_nbg(const char *nbg_file_name);
  35. int input_size[] = {1 * 3 * 300 * 300, };
  36. const char model_name[] = "network";
  37. #define RESIZE_HEIGHT 300
  38. #define RESIZE_WIDTH 300
  39. #define CROP_HEGHT 300
  40. #define CROP_WIDTH 300
  41. #define R_MEAN 0
  42. #define G_MEAN 0
  43. #define B_MEAN 0
  44. #define SCALE 1.0
  45. /*
  46. * Preprocess function
  47. */
  48. void preprocess(struct image_data *img, int is_rgb, int to_bgr)
  49. {
  50. uint32_t new_height, new_width;
  51. uint32_t min_side;
  52. if (is_rgb) {
  53. im2rgb(img);
  54. }
  55. if (RESIZE_WIDTH == 0) {
  56. min_side = MIN(img->shape[0], img->shape[1]);
  57. new_height = (uint32_t) (img->shape[0] * (((float)RESIZE_HEIGHT) / (float)min_side));
  58. new_width = (uint32_t) (img->shape[1] * (((float)RESIZE_HEIGHT) / (float)min_side));
  59. imresize(img, new_height, new_width);
  60. } else {
  61. imresize(img, RESIZE_HEIGHT, RESIZE_WIDTH);
  62. }
  63. data_crop(img, CROP_HEGHT, CROP_WIDTH);
  64. sub_mean(img, R_MEAN, G_MEAN, B_MEAN);
  65. data_scale(img, SCALE);
  66. if(to_bgr) {
  67. imrgb2bgr(img);
  68. }
  69. imhwc2chw(img);
  70. }
  71. static void print_tensor_info(struct csinn_tensor *t) {
  72. printf("\n=== tensor info ===\n");
  73. printf("shape: ");
  74. for (int j = 0; j < t->dim_count; j++) {
  75. printf("%d ", t->dim[j]);
  76. }
  77. printf("\n");
  78. if (t->dtype == CSINN_DTYPE_UINT8) {
  79. printf("scale: %f\n", t->qinfo->scale);
  80. printf("zero point: %d\n", t->qinfo->zero_point);
  81. }
  82. printf("data pointer: %p\n", t->data);
  83. }
  84. /*
  85. * Postprocess function
  86. */
  87. static void postprocess(void *sess, const char *filename_prefix) {
  88. int output_num, input_num;
  89. struct csinn_tensor *input = csinn_alloc_tensor(NULL);
  90. struct csinn_tensor *output = csinn_alloc_tensor(NULL);
  91. input_num = csinn_get_input_number(sess);
  92. for (int i = 0; i < input_num; i++) {
  93. input->data = NULL;
  94. csinn_get_input(i, input, sess);
  95. print_tensor_info(input);
  96. }
  97. output_num = csinn_get_output_number(sess);
  98. for (int i = 0; i < output_num; i++) {
  99. output->data = NULL;
  100. csinn_get_output(i, output, sess);
  101. print_tensor_info(output);
  102. struct csinn_tensor *foutput = shl_ref_tensor_transform_f32(output);
  103. shl_show_top5(foutput, sess);
  104. char filename[FILE_LENGTH] = {0};
  105. char shape[SHAPE_LENGHT] = {0};
  106. shape2string(output->dim, output->dim_count, shape, SHAPE_LENGHT);
  107. snprintf(filename, FILE_LENGTH, "%s_output%u_%s.txt", filename_prefix, i, shape);
  108. int output_size = csinn_tensor_size(foutput);
  109. save_data_to_file(filename, (float*)foutput->data, output_size);
  110. shl_ref_tensor_transform_free_f32(foutput);
  111. }
  112. }
  113. void *create_graph(char *params_path) {
  114. int binary_size;
  115. char *params = get_binary_from_file(params_path, &binary_size);
  116. if (params == NULL) {
  117. return NULL;
  118. }
  119. char *suffix = params_path + (strlen(params_path) - 7);
  120. if (strcmp(suffix, ".params") == 0) {
  121. // create general graph
  122. return csinn_(params);
  123. }
  124. suffix = params_path + (strlen(params_path) - 3);
  125. if (strcmp(suffix, ".bm") == 0) {
  126. struct shl_bm_sections *section = (struct shl_bm_sections *)(params + 4128);
  127. if (section->graph_offset) {
  128. return csinn_import_binary_model(params);
  129. } else {
  130. return csinn_(params + section->params_offset * 4096);
  131. }
  132. } else {
  133. return NULL;
  134. }
  135. }
  136. int main(int argc, char **argv) {
  137. char **data_path = NULL;
  138. int input_num = 1;
  139. int output_num = 3;
  140. int input_group_num = 1;
  141. int i;
  142. if (argc < (2 + input_num)) {
  143. printf("Please set valide args: ./model.elf model.params "
  144. "[tensor1/image1 ...] [tensor2/image2 ...]\n");
  145. return -1;
  146. } else {
  147. if (argc == 3 && get_file_type(argv[2]) == FILE_TXT) {
  148. data_path = read_string_from_file(argv[2], &input_group_num);
  149. input_group_num /= input_num;
  150. } else {
  151. data_path = argv + 2;
  152. input_group_num = (argc - 2) / input_num;
  153. }
  154. }
  155. void *sess = create_graph(argv[1]);
  156. uint8_t *input[input_num];
  157. float *inputf[input_num];
  158. char filename_prefix[FILE_LENGTH] = {0};
  159. uint64_t start_time, end_time;
  160. for (i = 0; i < input_group_num; i++) {
  161. /* set input */
  162. for (int j = 0; j < input_num; j++) {
  163. int input_len = csinn_tensor_size(((struct csinn_session *)sess)->input[j]);
  164. struct image_data *img = get_input_data(data_path[i * input_num + j], input_len);
  165. if (get_file_type(data_path[i * input_num + j]) == FILE_PNG || get_file_type(data_path[i * input_num + j]) == FILE_JPEG) {
  166. preprocess(img, 1, 1);
  167. }
  168. inputf[j] = img->data;
  169. free_image_data(img);
  170. input[j] = shl_ref_f32_to_input_dtype(j, inputf[j], sess);
  171. }
  172. start_time = shl_get_timespec();
  173. csinn_run(input[0], sess);
  174. end_time = shl_get_timespec();
  175. printf("Run graph execution time: %.5fms, FPS=%.2f\n", ((float)(end_time-start_time))/1000000,
  176. 1000000000.0/((float)(end_time-start_time)));
  177. snprintf(filename_prefix, FILE_LENGTH, "%s", basename(data_path[i * input_num]));
  178. postprocess(sess, filename_prefix);
  179. for (int j = 0; j < input_num; j++) {
  180. shl_mem_free(inputf[j]);
  181. shl_mem_free(input[j]);
  182. }
  183. }
  184. csinn_session_deinit(sess);
  185. csinn_free_session(sess);
  186. return 0;
  187. }