fix(knowing framework/yolov2 region layer): free region layer thresholds

This commit is contained in:
yangtuo250 2021-12-16 10:39:50 +08:00
parent c782dd26c4
commit 62df72af5b
7 changed files with 42 additions and 22 deletions

View File

@ -19,7 +19,8 @@ void simple_CSV_read()
fin = open(CSV_PATH, O_RDONLY);
if (!fin) {
printf("Error open file %s", CSV_PATH);
exit(-1);
// exit(-1);
return;
}
read(fin, buffer, sizeof(buffer));
close(fin);

View File

@ -27,7 +27,7 @@
"kmodel_size": 2714044,
"obj_thresh": [
0.7,
0.9
0.99
],
"labels": [
"head",

View File

@ -7,11 +7,10 @@ static void detect_app(int argc, char *argv[])
{
if (2 != argc) {
printf("Usage: detect_app <ABSOLUTE_CONFIG_JSON_PATH>");
exit(-1);
} else {
k210_detect(argv[1]);
}
k210_detect(argv[1]);
return;
}
// clang-format off

View File

@ -37,6 +37,10 @@ void k210_detect(char *json_file_path)
char kmodel_path[127] = {};
yolov2_params_t detect_params = param_parse(json_file_path);
if (!detect_params.is_valid) {
return;
}
g_fd = open("/dev/ov2640", O_RDONLY);
if (g_fd < 0) {
printf("open ov2640 fail !!");
@ -128,17 +132,19 @@ void k210_detect(char *json_file_path)
free(model_data);
return;
}
detect_rl.anchor_number = ANCHOR_NUM;
detect_rl.anchor = detect_params.anchor;
detect_rl.threshold = malloc(detect_params.class_num * sizeof(float));
for (int idx = 0; idx < detect_params.class_num; idx++) {
detect_rl.threshold[idx] = detect_params.obj_thresh[idx];
}
detect_rl.nms_value = detect_params.nms_thresh;
detect_rl.classes = detect_params.class_num;
result =
region_layer_init(&detect_rl, detect_params.net_output_shape[0], detect_params.net_output_shape[1],
detect_params.net_output_shape[2], detect_params.net_input_size[1], detect_params.net_input_size[0]);
printf("region_layer_init result %d \n\r", result);
for (int idx = 0; idx < detect_params.class_num; idx++) {
detect_rl.threshold[idx] = detect_params.obj_thresh[idx];
}
size_t stack_size = STACK_SIZE;
pthread_attr_t attr; /* 线程属性 */
struct sched_param prio; /* 线程优先级 */
@ -200,8 +206,8 @@ static void *thread_detect_entry(void *parameter)
detect_info.obj[cnt].prob);
}
#ifdef BSP_USING_LCD
lcd_draw_picture(0, 0, (uint16_t)detect_params.sensor_output_size[1], (uint16_t)detect_params.sensor_output_size[0],
(uint32_t *)showbuffer);
lcd_draw_picture(0, 0, (uint16_t)detect_params.sensor_output_size[1] - 1,
(uint16_t)detect_params.sensor_output_size[0] - 1, (uint32_t *)showbuffer);
// lcd_show_image(0, 0, (uint16_t)detect_params.sensor_output_size[1], (uint16_t)detect_params.sensor_output_size[0],
// (unsigned int *)showbuffer);
#endif

View File

@ -58,12 +58,14 @@ int region_layer_init(region_layer_t *rl, int width, int height, int channels, i
goto malloc_error;
}
for (uint32_t i = 0; i < rl->boxes_number; i++) rl->probs[i] = &(rl->probs_buf[i * (rl->classes + 1)]);
rl->threshold = malloc(rl->classes * sizeof(float));
return 0;
malloc_error:
free(rl->output);
free(rl->boxes);
free(rl->probs_buf);
free(rl->probs);
free(rl->threshold);
return flag;
}
@ -73,6 +75,7 @@ void region_layer_deinit(region_layer_t *rl)
free(rl->boxes);
free(rl->probs_buf);
free(rl->probs);
free(rl->threshold);
}
static inline float sigmoid(float x) { return 1.f / (1.f + expf(-x)); }

View File

@ -7,14 +7,16 @@
yolov2_params_t param_parse(char *json_file_path)
{
yolov2_params_t params_return;
params_return.is_valid = 1;
int fin;
char buffer[JSON_BUFFER_SIZE] = "";
// char *buffer;
// if (NULL != (buffer = (char*)malloc(JSON_BUFFER_SIZE * sizeof(char)))) {
// if ((yolov2_params_t *)NULL != (buffer = (char*)malloc(JSON_BUFFER_SIZE * sizeof(char)))) {
// memset(buffer, 0, JSON_BUFFER_SIZE * sizeof(char));
// } else {
// printf("Json buffer malloc failed!");
// exit(-1);
// params_return.is_valid = 0;
// return params_return;
// }
int array_size;
cJSON *json_obj;
@ -24,8 +26,9 @@ yolov2_params_t param_parse(char *json_file_path)
fin = open(json_file_path, O_RDONLY);
if (!fin) {
printf("Error open file %s\n", json_file_path);
exit(-1);
} else{
params_return.is_valid = 0;
return params_return;
} else {
printf("Reading config from: %s\n", json_file_path);
}
read(fin, buffer, sizeof(buffer));
@ -42,7 +45,8 @@ yolov2_params_t param_parse(char *json_file_path)
array_size = cJSON_GetArraySize(json_item);
if (ANCHOR_NUM * 2 != array_size) {
printf("Expect anchor size: %d, got %d in json file", ANCHOR_NUM * 2, array_size);
exit(-1);
params_return.is_valid = 0;
return params_return;
} else {
printf("Got %d anchors from json file\n", ANCHOR_NUM);
}
@ -56,7 +60,8 @@ yolov2_params_t param_parse(char *json_file_path)
array_size = cJSON_GetArraySize(json_item);
if (2 != array_size) {
printf("Expect net_input_size: %d, got %d in json file", 2, array_size);
exit(-1);
params_return.is_valid = 0;
return params_return;
} else {
printf("Got %d net_input_size from json file\n", 2);
}
@ -70,7 +75,8 @@ yolov2_params_t param_parse(char *json_file_path)
array_size = cJSON_GetArraySize(json_item);
if (3 != array_size) {
printf("Expect net_output_shape: %d, got %d in json file", 3, array_size);
exit(-1);
params_return.is_valid = 0;
return params_return;
} else {
printf("Got %d net_output_shape from json file\n", 3);
}
@ -84,7 +90,8 @@ yolov2_params_t param_parse(char *json_file_path)
array_size = cJSON_GetArraySize(json_item);
if (2 != array_size) {
printf("Expect sensor_output_size: %d, got %d in json file", 2, array_size);
exit(-1);
params_return.is_valid = 0;
return params_return;
} else {
printf("Got %d sensor_output_size from json file\n", 2);
}
@ -96,7 +103,8 @@ yolov2_params_t param_parse(char *json_file_path)
// check sensor output width and net input width
if (params_return.sensor_output_size[1] != params_return.net_input_size[1]) {
printf("Net input width must match sensor output width!\n");
exit(-1);
params_return.is_valid = 0;
return params_return;
}
// // kmodel_path
// json_item = cJSON_GetObjectItem(json_obj, "kmodel_path");
@ -112,7 +120,8 @@ yolov2_params_t param_parse(char *json_file_path)
params_return.class_num = cJSON_GetArraySize(json_item);
if (0 >= params_return.class_num) {
printf("No labels!");
exit(-1);
params_return.is_valid = 0;
return params_return;
} else {
printf("Got %d labels\n", params_return.class_num);
}
@ -128,7 +137,8 @@ yolov2_params_t param_parse(char *json_file_path)
if (params_return.class_num != array_size) {
printf("label number and thresh number mismatch! label number : %d, obj thresh number %d", params_return.class_num,
array_size);
exit(-1);
params_return.is_valid = 0;
return params_return;
} else {
printf("Got %d obj_thresh\n", array_size);
}

View File

@ -15,6 +15,7 @@ typedef struct {
float nms_thresh;
char labels[20][32];
int class_num;
int is_valid;
} yolov2_params_t;
yolov2_params_t param_parse(char *json_file_path);