未验证 提交 156aa419 编写于 作者: S shitouren1994 提交者: GitHub

add tm_mobilefacenet (#384)

* add tm_mobilefacenet

* fix bug __fp16 operate error

* clang-format code
上级 7c5f09e2
......@@ -21,6 +21,7 @@ tengine_example(tm_yolov3_tiny tm_yolov3_tiny.cpp)
tengine_example(tm_yolov3_uint8 tm_yolov3_uint8.cpp)
tengine_example(tm_landmark tm_landmark.cpp)
tengine_example(tm_landmark_uint8 tm_landmark_uint8.cpp)
tengine_example(tm_mobilefacenet tm_mobilefacenet.cpp)
# add examples with opencv
......
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "common.h"
#include "tengine_c_api.h"
#include "tengine_operations.h"
#define DEFAULT_MEAN1 104.007
#define DEFAULT_MEAN2 116.669
#define DEFAULT_MEAN3 122.679
#define MOBILE_FACE_HEIGHT 110
#define MOBILE_FACE_WIDTH 110
graph_t graph;
tensor_t input_tensor;
tensor_t output_tensor;
int feature_len;
void init(const char* modelfile)
{
int dims[4] = {1, 3, MOBILE_FACE_HEIGHT, MOBILE_FACE_WIDTH};
init_tengine();
fprintf(stderr, "tengine version: %s\n", get_tengine_version());
graph = create_graph(NULL, "tengine", modelfile);
if (graph == NULL)
{
fprintf(stderr, "grph nullptr %\n", get_tengine_errno());
}
else
{
fprintf(stderr, "success init graph\n");
}
input_tensor = get_graph_input_tensor(graph, 0, 0);
set_tensor_shape(input_tensor, dims, 4);
int rc = prerun_graph(graph);
output_tensor = get_graph_output_tensor(graph, 0, 0);
get_tensor_shape(output_tensor, dims, 4);
feature_len = dims[1];
fprintf(stderr, "mobilefacenet prerun %d\n", rc);
}
int getFeature(const char* imagefile, float* feature)
{
int height = MOBILE_FACE_HEIGHT;
int width = MOBILE_FACE_WIDTH;
int img_size = height * width * 3;
int dims[] = {1, 3, height, width};
float means[3] = {DEFAULT_MEAN1, DEFAULT_MEAN2, DEFAULT_MEAN3};
float scales[3] = {1, 1, 1};
std::vector<float> input_data(img_size);
get_input_data(imagefile, input_data.data(), height, width, means, scales);
set_tensor_buffer(input_tensor, input_data.data(), img_size * sizeof(float));
if (run_graph(graph, 1) < 0)
{
fprintf(stderr, "run_graph fail");
return -1;
}
float* data = ( float* )get_tensor_buffer(output_tensor);
int outsize;
outsize = get_tensor_buffer_size(output_tensor) / sizeof(float);
for (int i = 0; i < outsize; i++)
feature[i] = data[i];
return outsize;
}
void normlize(float* feature, int size)
{
float norm = 0;
for (int i = 0; i < size; ++i)
{
norm += feature[i] * feature[i];
}
for (int i = 0; i < size; ++i)
{
feature[i] /= sqrt(norm);
}
}
void release()
{
release_graph_tensor(input_tensor);
release_graph_tensor(output_tensor);
destroy_graph(graph);
}
void show_usage()
{
fprintf(stderr, "[Usage]: [-h]\n [-m model_file] [-a person_a -b person_b]\n [-t thread_count]\n");
fprintf(stderr, "\nmobilefacenet example: \n ./mobilefacenet -m /path/to/mobilenet.tmfile -a "
"/path/to/person_a.jpg -b /path/to/person_b.jpg\n");
}
int main(int argc, char* argv[])
{
char* model_file = NULL;
char* person_a = NULL;
char* person_b = NULL;
int res;
while ((res = getopt(argc, argv, "m:a:b:h")) != -1)
{
switch (res)
{
case 'm':
model_file = optarg;
break;
case 'a':
person_a = optarg;
break;
case 'b':
person_b = optarg;
break;
case 'h':
show_usage();
return 0;
default:
break;
}
}
/* check files */
if (model_file == NULL)
{
fprintf(stderr, "Error: Tengine model file not specified!\n");
show_usage();
return -1;
}
if (!check_file_exist(model_file) || !check_file_exist(person_a) || !check_file_exist(person_b))
return -1;
init(model_file);
std::vector<float> featurea(feature_len);
std::vector<float> featureb(feature_len);
int outputsizea = getFeature(person_a, featurea.data());
int outputsizeb = getFeature(person_b, featureb.data());
if (outputsizea != feature_len || outputsizeb != feature_len)
{
fprintf(stderr, "getFeature feature out len error");
}
normlize(featurea.data(), feature_len);
normlize(featureb.data(), feature_len);
float sim = 0;
for (int i = 0; i < feature_len; ++i)
{
sim += featurea[i] * featureb[i];
}
fprintf(stderr, "the cosine sim of person_a and person_b is %f\n", sim);
release();
return 0;
}
\ No newline at end of file
......@@ -421,7 +421,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
__fp16 val = base_ptr[offset];
float val_fp32 = fp16_to_fp32(val);
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -487,7 +487,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
__fp16 val = base_ptr[offset];
float val_fp32 = fp16_to_fp32(val);
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -546,7 +546,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
__fp16 val = base_ptr[offset];
float val_fp32 = fp16_to_fp32(val);
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -587,7 +587,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
__fp16 val = base_ptr[offset];
float val_fp32 = fp16_to_fp32(val);
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -608,7 +608,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
{
__fp16 val = base_ptr[w];
float val_fp32 = fp16_to_fp32(val);
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -682,7 +682,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
unsigned char val = base_ptr[offset];
float val_fp32 = (val - zero_point) * scale;
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -748,7 +748,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
unsigned char val = base_ptr[offset];
float val_fp32 = (val - zero_point) * scale;
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -807,7 +807,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
unsigned char val = base_ptr[offset];
float val_fp32 = (val - zero_point) * scale;
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -848,7 +848,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
unsigned char val = base_ptr[offset];
float val_fp32 = (val - zero_point) * scale;
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......@@ -869,7 +869,7 @@ void extract_feature_blob_f32(const char* comment, const char* layer_name, const
{
unsigned char val = base_ptr[w];
float val_fp32 = (val - zero_point) * scale;
if (val < 0)
if (val_fp32 < 0)
fprintf(pFile, "%.4f ", val_fp32);
else
fprintf(pFile, " %.4f ", val_fp32);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册