/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* auto generate by HHB_VERSION "2.0.21" */ #include #include #include #include #include #include #include "io.h" #include "shl_ref.h" #include "process.h" #include "detect.h" #include "output_120_out0_nchw_1_2_7668_1.h" #define MIN(x, y) ((x) < (y) ? (x) : (y)) #define FILE_LENGTH 1028 #define SHAPE_LENGHT 128 void *csinn_(char *params); void csinn_run(void *data0, void *td); void *csinn_nbg(const char *nbg_file_name); int input_size[] = {1 * 3 * 304 * 304, }; const char model_name[] = "network"; const char class_name[][FILE_LENGTH] = { "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" }; #define RESIZE_HEIGHT 304 #define RESIZE_WIDTH 304 #define CROP_HEGHT 304 #define CROP_WIDTH 304 #define R_MEAN 0 #define G_MEAN 0 #define B_MEAN 0 #define SCALE 1.0 /* * Preprocess function */ void preprocess(struct image_data *img, int is_rgb, int to_bgr) { uint32_t new_height, new_width; uint32_t min_side; if (is_rgb) { im2rgb(img); } if (RESIZE_WIDTH == 0) { min_side = MIN(img->shape[0], img->shape[1]); new_height = (uint32_t) (img->shape[0] * (((float)RESIZE_HEIGHT) / (float)min_side)); new_width = (uint32_t) (img->shape[1] * (((float)RESIZE_HEIGHT) / (float)min_side)); imresize(img, new_height, new_width); } else { imresize(img, RESIZE_HEIGHT, RESIZE_WIDTH); } data_crop(img, CROP_HEGHT, CROP_WIDTH); sub_mean(img, R_MEAN, G_MEAN, B_MEAN); data_scale(img, SCALE); if(to_bgr) { imrgb2bgr(img); } imhwc2chw(img); } static void print_tensor_info(struct csinn_tensor *t) { printf("\n=== tensor info ===\n"); printf("shape: "); for (int j = 0; j < t->dim_count; j++) { printf("%d ", t->dim[j]); } printf("\n"); if (t->dtype == CSINN_DTYPE_UINT8) { printf("scale: %f\n", t->qinfo->scale); printf("zero point: %d\n", t->qinfo->zero_point); } printf("data pointer: %p\n", t->data); } /* * Postprocess function */ static void postprocess(void *sess, const char *filename_prefix) { int output_num, input_num; struct csinn_tensor *input = csinn_alloc_tensor(NULL); struct csinn_tensor *output = csinn_alloc_tensor(NULL); input_num = csinn_get_input_number(sess); for (int i = 0; i < input_num; i++) { input->data = NULL; csinn_get_input(i, input, sess); print_tensor_info(input); } float *location; float *confidence; output_num = csinn_get_output_number(sess); for (int i = 0; i < output_num; i++) { output->data = NULL; csinn_get_output(i, output, sess); print_tensor_info(output); struct csinn_tensor *foutput = shl_ref_tensor_transform_f32(output); shl_show_top5(foutput, sess); char filename[FILE_LENGTH] = {0}; char shape[SHAPE_LENGHT] = {0}; shape2string(output->dim, output->dim_count, shape, SHAPE_LENGHT); snprintf(filename, FILE_LENGTH, "%s_output%u_%s.txt", filename_prefix, i, shape); int output_size = csinn_tensor_size(foutput); save_data_to_file(filename, (float*)foutput->data, output_size); if (i == 0) location = (float *)foutput->data; if (i == 1) confidence = (float *)foutput->data; // shl_ref_tensor_transform_free_f32(foutput); } BBoxOut out[100]; BBox gbboxes[num_prior]; int num = ssdforward(location, confidence, priorbox, gbboxes, out); printf("%d\n", num); for (int i = 0; i < num; i++) { printf("%d, label=%s, score=%f, x1=%f, y1=%f, x2=%f, y2=%f\n", out[i].label, class_name[out[i].label], out[i].score, out[i].xmin, out[i].ymin, out[i].xmax, out[i].ymax); } } void *create_graph(char *params_path) { void *ret; int binary_size; char *params = get_binary_from_file(params_path, &binary_size); if (params == NULL) { return NULL; } char *suffix = params_path + (strlen(params_path) - 7); if (strcmp(suffix, ".params") == 0) { // create general graph ret = csinn_(params); free(params); return ret; } suffix = params_path + (strlen(params_path) - 3); if (strcmp(suffix, ".bm") == 0) { struct shl_bm_sections *section = (struct shl_bm_sections *)(params + 4128); if (section->graph_offset) { ret = csinn_import_binary_model(params); free(params); return ret; } else { ret = csinn_(params + section->params_offset * 4096); free(params); return ret; } } else { free(params); return NULL; } } int main(int argc, char **argv) { char **data_path = NULL; int input_num = 1; int output_num = 3; int input_group_num = 1; int i; if (argc < (2 + input_num)) { printf("Please set valide args: ./model.elf model.params " "[tensor1/image1 ...] [tensor2/image2 ...]\n"); return -1; } else { if (argc == 3 && get_file_type(argv[2]) == FILE_TXT) { data_path = read_string_from_file(argv[2], &input_group_num); input_group_num /= input_num; } else { data_path = argv + 2; input_group_num = (argc - 2) / input_num; } } void *sess = create_graph(argv[1]); uint8_t *input[input_num]; float *inputf[input_num]; char filename_prefix[FILE_LENGTH] = {0}; void *input_aligned[input_num]; for (i = 0; i < input_num; i++) { input_aligned[i] = shl_mem_alloc_aligned(input_size[i], 0); } uint64_t start_time, end_time; char *loop_s; int32_t loop_time = 1; loop_s = getenv("HHB_LOOP_TIME"); if (loop_s) { loop_time = atoi(loop_s); } for (i = 0; i < input_group_num; i++) { /* set input */ for (int j = 0; j < input_num; j++) { int input_len = csinn_tensor_size(((struct csinn_session *)sess)->input[j]); if (get_file_type(data_path[i * input_num + j]) == 0) { input[j] = (uint8_t *)get_binary_from_file(data_path[i * input_num + j], &input_size[j]); } else { struct image_data *img = get_input_data(data_path[i * input_num + j], input_size[j]); if (get_file_type(data_path[i * input_num + j]) == FILE_PNG || get_file_type(data_path[i * input_num + j]) == FILE_JPEG) { preprocess(img, 1, 1); } inputf[j] = img->data; free_image_data(img); input[j] = shl_ref_f32_to_input_dtype(j, inputf[j], sess); } } memcpy(input_aligned[0], input[0], input_size[0]); for (int t = 0; t < loop_time; t++) { start_time = shl_get_timespec(); csinn_run(input_aligned[0], sess); end_time = shl_get_timespec(); printf("Run graph execution time: %.5fms, FPS=%.2f\n", ((float)(end_time-start_time))/1000000, 1000000000.0/((float)(end_time-start_time))); snprintf(filename_prefix, FILE_LENGTH, "%s", basename(data_path[i * input_num])); postprocess(sess, filename_prefix); } for (int j = 0; j < input_num; j++) { if (get_file_type(data_path[i * input_num + j]) != 0) { shl_mem_free(inputf[j]); } shl_mem_free(input[j]); } } csinn_session_deinit(sess); csinn_free_session(sess); return 0; }