From 515bd49f35176a9ac864efb5472579fe5647afe0 Mon Sep 17 00:00:00 2001 From: Shunqi Fu <2775613813@qq.com> Date: Wed, 14 Jul 2021 12:39:45 +0800 Subject: [PATCH 1/2] update test_model_unet --- tests/models/test_model_unet.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/models/test_model_unet.cpp b/tests/models/test_model_unet.cpp index fbd0d699a..d48728585 100644 --- a/tests/models/test_model_unet.cpp +++ b/tests/models/test_model_unet.cpp @@ -174,13 +174,6 @@ int tengine_segment(const char* model_file, const char* image_file, int img_h, i std::string reference_file1 = "./data/" + model_name + "_out.bin"; std::vector reference_data1(output_size); FILE *fp1; - fp1 = fopen(reference_file1.c_str(), "wb"); - if (!fp || fwrite(output_data, sizeof(float), output_size, fp1) == 0) - { - fprintf(stderr, "write reference data file failed!\n"); - return -1; - } - fclose(fp1); fp1 = fopen(reference_file1.c_str(), "rb"); if (!fp || fread(reference_data1.data(), sizeof(float), output_size, fp1) == 0) { From 021afc30ae894164ccd952126d0f1fe1c55666da Mon Sep 17 00:00:00 2001 From: Shunqi Fu <2775613813@qq.com> Date: Wed, 14 Jul 2021 18:56:50 +0800 Subject: [PATCH 2/2] add 1 test model --- tests/CMakeLists.txt | 1 + tests/models/test_model_yolofastest.cpp | 269 ++++++++++++++++++++++++ tests/test.sh | 1 + 3 files changed, 271 insertions(+) create mode 100644 tests/models/test_model_yolofastest.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3f4cdf681..e3247d744 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -241,6 +241,7 @@ TENGINE_MODEL_TEST (test_model_retinaface models/test_model_retinaface TENGINE_MODEL_TEST (test_model_ultraface models/test_model_ultraface.cpp) TENGINE_MODEL_TEST (test_model_unet models/test_model_unet.cpp) TENGINE_MODEL_TEST (test_model_yolact models/test_model_yolact.cpp) +TENGINE_MODEL_TEST (test_model_yolofastest models/test_model_yolofastest.cpp) TENGINE_MODEL_TEST (test_model_yolov3 models/test_model_yolov3.cpp) TENGINE_MODEL_TEST (test_model_yolov3_tiny models/test_model_yolov3_tiny.cpp) TENGINE_MODEL_TEST (test_model_yolov4 models/test_model_yolov4.cpp) diff --git a/tests/models/test_model_yolofastest.cpp b/tests/models/test_model_yolofastest.cpp new file mode 100644 index 000000000..77265c31a --- /dev/null +++ b/tests/models/test_model_yolofastest.cpp @@ -0,0 +1,269 @@ +/* + * 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. + */ + +/* + * Copyright (c) 2021, OPEN AI LAB + * Author: sqfu@openailab.com + * + * original model: https://github.com/dog-qiuqiu/Yolo-Fastest/tree/master/ModelZoo/yolo-fastest-1.1_coco + */ + +#include +#include +#include + +#ifdef _MSC_VER +#define NOMINMAX +#endif + +#include +#include +#include + +#include "common.h" +#include "tengine/c_api.h" +#include "tengine_operations.h" + +#define DEFAULT_REPEAT_COUNT 1 +#define DEFAULT_THREAD_COUNT 1 +int float_mismatch(float* current, float* reference, int size) +{ + for(int i=0;i 0.0001) + { + fprintf(stderr, "test failed, index:%d, a:%f, b:%f\n", i, current[i], reference[i]); + return -1; + } + } + fprintf(stderr, "test pass\n"); + return 0; +} +enum +{ + YOLOV3 = 0, + YOLO_FASTEST = 1, + YOLO_FASTEST_XL = 2 +}; + +using namespace std; + +static void show_usage() +{ + fprintf(stderr, "[Usage]: [-h]\n [-m model_file] [-r repeat_count] [-t thread_count]\n"); +} +struct TMat +{ + operator const float*() const + { + return (const float*)data; + } + + float *row(int row) const + { + return (float *)data + w * row; + } + + TMat channel_range(int start, int chn_num) const + { + TMat mat = { 0 }; + + mat.batch = 1; + mat.c = chn_num; + mat.h = h; + mat.w = w; + mat.data = (float *)data + start * h * w; + + return mat; + } + + TMat channel(int channel) const + { + return channel_range(channel, 1); + } + + int batch, c, h, w; + void *data; +}; + +int main(int argc, char* argv[]) +{ + int repeat_count = DEFAULT_REPEAT_COUNT; + int num_thread = DEFAULT_THREAD_COUNT; + char model_string[] = "./models/yolo-fastest-1.1.tmfile"; + char* model_file = model_string; + + int net_w = 320; + int net_h = 320; + + int res; + while ((res = getopt(argc, argv, "m:i:r:t:h:")) != -1) + { + switch (res) + { + case 'm': + model_file = optarg; + break; + case 'r': + repeat_count = std::strtoul(optarg, nullptr, 10); + break; + case 't': + num_thread = std::strtoul(optarg, nullptr, 10); + break; + case 'h': + show_usage(); + return 0; + default: + break; + } + } + + /* check files */ + if (nullptr == model_file) + { + fprintf(stderr, "Error: Tengine model file not specified!\n"); + show_usage(); + return -1; + } + + if (!check_file_exist(model_file)) + return -1; + + /* set runtime options */ + struct options opt; + opt.num_thread = num_thread; + opt.cluster = TENGINE_CLUSTER_ALL; + opt.precision = TENGINE_MODE_FP32; + opt.affinity = 0; + + /* inital tengine */ + if (init_tengine() != 0) + { + fprintf(stderr, "Initial tengine failed.\n"); + return -1; + } + fprintf(stderr, "tengine-lite library version: %s\n", get_tengine_version()); + + /* create graph, load tengine model xxx.tmfile */ + graph_t graph = create_graph(nullptr, "tengine", model_file); + if (graph == nullptr) + { + fprintf(stderr, "Create graph failed.\n"); + return -1; + } + + /* set the input shape to initial the graph, and prerun graph to infer shape */ + int img_size = net_h * net_w * 3; + int dims[] = { 1, 3, net_h, net_w }; // nchw + + std::vector input_data(img_size); + + tensor_t input_tensor = get_graph_input_tensor(graph, 0, 0); + if (input_tensor == nullptr) + { + fprintf(stderr, "Get input tensor failed\n"); + return -1; + } + + if (set_tensor_shape(input_tensor, dims, 4) < 0) + { + fprintf(stderr, "Set input tensor shape failed\n"); + return -1; + } + + if (set_tensor_buffer(input_tensor, input_data.data(), img_size * 4) < 0) + { + fprintf(stderr, "Set input tensor buffer failed\n"); + return -1; + } + + /* prerun graph, set work options(num_thread, cluster, precision) */ + if (prerun_graph_multithread(graph, opt) < 0) + { + fprintf(stderr, "Prerun multithread graph failed.\n"); + return -1; + } + + /* prepare process input data, set the data mem to input tensor */ + std::string model_name="yolo-fastest-1.1"; + std::string input_file = "./data/" + model_name + "_in.bin"; + FILE *fp; + fp = fopen(input_file.c_str(), "rb"); + if (!fp ||fread(input_data.data(), sizeof(float), img_size, fp) == 0) + { + fprintf(stderr, "read input data file failed!\n"); + return -1; + } + fclose(fp); + + /* run graph */ + double min_time = DBL_MAX; + double max_time = DBL_MIN; + double total_time = 0.; + for (int i = 0; i < repeat_count; i++) + { + double start = get_current_time(); + if (run_graph(graph, 1) < 0) + { + fprintf(stderr, "Run graph failed\n"); + return -1; + } + double end = get_current_time(); + double cur = end - start; + total_time += cur; + min_time = std::min(min_time, cur); + max_time = std::max(max_time, cur); + } + fprintf(stderr, "Repeat %d times, thread %d, avg time %.2f ms, max_time %.2f ms, min_time %.2f ms\n", repeat_count, + num_thread, total_time / repeat_count, max_time, min_time); + fprintf(stderr, "--------------------------------------\n"); + + /* process the detection result */ + + int output_node_num = get_graph_output_node_number(graph); + int ret1 = 0; + tensor_t out_tensor; + for (int i = 0; i < output_node_num; ++i) + { + out_tensor = get_graph_output_tensor(graph, i, 0); //"detection_out" + // save output_data + std::string model_name = "yolo-fastest-1.1"; + int output_size1 = get_tensor_buffer_size(out_tensor) / sizeof(float);; + float* yolo_outputs = (float*)get_tensor_buffer(out_tensor); + std::string reference_file1 = "./data/" + model_name + "_out" + std::to_string(i+1)+".bin"; + std::vector reference_data1(output_size1); + FILE *fp1; + //read + fp1 = fopen(reference_file1.c_str(), "rb"); + if (fread(reference_data1.data(), sizeof(float), output_size1, fp1) == 0) + { + fprintf(stderr, "read reference data file1 failed!\n"); + return -1; + } + fclose(fp1); + ret1 |= float_mismatch(yolo_outputs, reference_data1.data(), output_size1); + } + /* release tengine */ + postrun_graph(graph); + destroy_graph(graph); + release_tengine(); + + return ret1; +} diff --git a/tests/test.sh b/tests/test.sh index a1b6631d0..d3081eb7d 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -26,6 +26,7 @@ test_models=( "./tests/test_model_ultraface" "./tests/test_model_unet" "./tests/test_model_yolact" +"./tests/test_model_yolofastest" "./tests/test_model_yolov3" "./tests/test_model_yolov3_tiny" "./tests/test_model_yolov4"