2019-09-05 06:00:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Sergey Lavrushkin
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
2020-09-06 12:28:53 +00:00
|
|
|
#include "libavutil/thread.h"
|
|
|
|
#include "libavutil/cpu.h"
|
2019-09-05 06:00:28 +00:00
|
|
|
#include "dnn_backend_native_layer_conv2d.h"
|
|
|
|
|
|
|
|
#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
|
|
|
|
|
2020-09-06 12:28:53 +00:00
|
|
|
//struct to pass parameters
|
2021-01-18 08:15:34 +00:00
|
|
|
typedef struct ThreadCommonParam{
|
2020-09-06 12:28:53 +00:00
|
|
|
DnnOperand *operands;
|
|
|
|
const int32_t *input_operand_indexes;
|
|
|
|
int32_t output_operand_index;
|
|
|
|
const void *parameters;
|
|
|
|
NativeContext *ctx;
|
2020-09-16 10:07:17 +00:00
|
|
|
float *output_data;
|
2021-01-18 08:15:34 +00:00
|
|
|
} ThreadCommonParam;
|
2020-09-06 12:28:53 +00:00
|
|
|
|
2021-01-18 08:15:34 +00:00
|
|
|
typedef struct ThreadParam{
|
|
|
|
ThreadCommonParam *thread_common_param;
|
2020-09-16 10:07:19 +00:00
|
|
|
int thread_start, thread_end;
|
2021-03-10 15:21:13 +00:00
|
|
|
#if HAVE_PTHREAD_CANCEL
|
|
|
|
pthread_t thread;
|
|
|
|
#endif
|
2021-01-18 08:15:34 +00:00
|
|
|
} ThreadParam;
|
2020-09-06 12:28:53 +00:00
|
|
|
|
2021-01-21 21:39:55 +00:00
|
|
|
int ff_dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
|
2019-10-09 14:08:18 +00:00
|
|
|
{
|
|
|
|
ConvolutionalParams *conv_params;
|
|
|
|
int kernel_size;
|
|
|
|
int dnn_size = 0;
|
|
|
|
conv_params = av_malloc(sizeof(*conv_params));
|
|
|
|
if (!conv_params)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
conv_params->dilation = (int32_t)avio_rl32(model_file_context);
|
|
|
|
conv_params->padding_method = (int32_t)avio_rl32(model_file_context);
|
|
|
|
conv_params->activation = (int32_t)avio_rl32(model_file_context);
|
|
|
|
conv_params->input_num = (int32_t)avio_rl32(model_file_context);
|
|
|
|
conv_params->output_num = (int32_t)avio_rl32(model_file_context);
|
|
|
|
conv_params->kernel_size = (int32_t)avio_rl32(model_file_context);
|
2019-10-21 12:38:03 +00:00
|
|
|
conv_params->has_bias = (int32_t)avio_rl32(model_file_context);
|
|
|
|
dnn_size += 28;
|
|
|
|
|
2019-10-09 14:08:18 +00:00
|
|
|
kernel_size = conv_params->input_num * conv_params->output_num *
|
2019-10-21 12:38:03 +00:00
|
|
|
conv_params->kernel_size * conv_params->kernel_size;
|
|
|
|
dnn_size += kernel_size * 4;
|
|
|
|
if (conv_params->has_bias)
|
|
|
|
dnn_size += conv_params->output_num * 4;
|
|
|
|
|
2019-10-09 14:08:18 +00:00
|
|
|
if (dnn_size > file_size || conv_params->input_num <= 0 ||
|
|
|
|
conv_params->output_num <= 0 || conv_params->kernel_size <= 0){
|
|
|
|
av_freep(&conv_params);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-21 12:38:03 +00:00
|
|
|
|
2021-01-18 08:28:11 +00:00
|
|
|
conv_params->kernel = av_malloc_array(kernel_size, sizeof(*conv_params->kernel));
|
2019-10-21 12:38:03 +00:00
|
|
|
if (!conv_params->kernel) {
|
2019-10-09 14:08:18 +00:00
|
|
|
av_freep(&conv_params);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-21 12:38:03 +00:00
|
|
|
for (int i = 0; i < kernel_size; ++i) {
|
2019-10-09 14:08:18 +00:00
|
|
|
conv_params->kernel[i] = av_int2float(avio_rl32(model_file_context));
|
|
|
|
}
|
2019-10-21 12:38:03 +00:00
|
|
|
|
|
|
|
conv_params->biases = NULL;
|
|
|
|
if (conv_params->has_bias) {
|
2021-01-18 08:28:11 +00:00
|
|
|
conv_params->biases = av_malloc_array(conv_params->output_num, sizeof(*conv_params->biases));
|
2019-10-21 12:38:03 +00:00
|
|
|
if (!conv_params->biases){
|
|
|
|
av_freep(&conv_params->kernel);
|
|
|
|
av_freep(&conv_params);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < conv_params->output_num; ++i){
|
|
|
|
conv_params->biases[i] = av_int2float(avio_rl32(model_file_context));
|
|
|
|
}
|
2019-10-09 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
layer->params = conv_params;
|
|
|
|
|
|
|
|
layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
|
|
|
|
layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
|
|
|
|
dnn_size += 8;
|
2020-06-10 05:36:11 +00:00
|
|
|
|
|
|
|
if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:08:18 +00:00
|
|
|
return dnn_size;
|
|
|
|
}
|
|
|
|
|
2020-09-06 12:28:53 +00:00
|
|
|
static void * dnn_execute_layer_conv2d_thread(void *threadarg)
|
2019-09-05 06:00:28 +00:00
|
|
|
{
|
2020-09-06 12:28:53 +00:00
|
|
|
//pass parameters
|
2021-01-18 08:15:34 +00:00
|
|
|
ThreadParam *thread_param = threadarg;
|
|
|
|
ThreadCommonParam *thread_common_param = thread_param->thread_common_param;
|
2020-09-06 12:28:53 +00:00
|
|
|
DnnOperand *operands = thread_common_param->operands;
|
|
|
|
int32_t input_operand_index = thread_common_param->input_operand_indexes[0];
|
2019-09-05 06:00:28 +00:00
|
|
|
int height = operands[input_operand_index].dims[1];
|
|
|
|
int width = operands[input_operand_index].dims[2];
|
|
|
|
int channel = operands[input_operand_index].dims[3];
|
|
|
|
const float *input = operands[input_operand_index].data;
|
2021-01-22 14:20:15 +00:00
|
|
|
const ConvolutionalParams *conv_params = thread_common_param->parameters;
|
2019-09-05 06:00:28 +00:00
|
|
|
|
|
|
|
int radius = conv_params->kernel_size >> 1;
|
|
|
|
int src_linesize = width * conv_params->input_num;
|
|
|
|
int filter_linesize = conv_params->kernel_size * conv_params->input_num;
|
|
|
|
int filter_size = conv_params->kernel_size * filter_linesize;
|
|
|
|
int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
|
|
|
|
|
2020-09-16 10:07:17 +00:00
|
|
|
float *output = thread_common_param->output_data;
|
2020-09-16 10:07:19 +00:00
|
|
|
output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_param->thread_start - pad_size);
|
2019-09-05 06:00:28 +00:00
|
|
|
|
|
|
|
av_assert0(channel == conv_params->input_num);
|
|
|
|
|
2020-09-16 10:07:19 +00:00
|
|
|
for (int y = thread_param->thread_start; y < thread_param->thread_end; ++y) {
|
2019-09-05 06:00:28 +00:00
|
|
|
for (int x = pad_size; x < width - pad_size; ++x) {
|
|
|
|
for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
|
2019-10-21 12:38:03 +00:00
|
|
|
if (conv_params->has_bias)
|
|
|
|
output[n_filter] = conv_params->biases[n_filter];
|
|
|
|
else
|
|
|
|
output[n_filter] = 0.f;
|
2019-09-05 06:00:28 +00:00
|
|
|
|
|
|
|
for (int ch = 0; ch < conv_params->input_num; ++ch) {
|
|
|
|
for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) {
|
|
|
|
for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) {
|
|
|
|
float input_pel;
|
|
|
|
if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) {
|
|
|
|
int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height);
|
|
|
|
int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width);
|
|
|
|
input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
|
|
|
|
} else {
|
|
|
|
int y_pos = y + (kernel_y - radius) * conv_params->dilation;
|
|
|
|
int x_pos = x + (kernel_x - radius) * conv_params->dilation;
|
|
|
|
input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 :
|
|
|
|
input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
|
|
|
|
kernel_x * conv_params->input_num + ch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (conv_params->activation){
|
|
|
|
case RELU:
|
|
|
|
output[n_filter] = FFMAX(output[n_filter], 0.0);
|
|
|
|
break;
|
|
|
|
case TANH:
|
|
|
|
output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
|
|
|
|
break;
|
|
|
|
case SIGMOID:
|
|
|
|
output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
|
|
|
|
break;
|
|
|
|
case NONE:
|
|
|
|
break;
|
|
|
|
case LEAKY_RELU:
|
|
|
|
output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output += conv_params->output_num;
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 20:58:15 +00:00
|
|
|
return NULL;
|
2020-09-06 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-21 21:39:55 +00:00
|
|
|
int ff_dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
|
|
|
|
int32_t output_operand_index, const void *parameters, NativeContext *ctx)
|
2020-09-06 12:28:53 +00:00
|
|
|
{
|
2021-03-10 16:03:01 +00:00
|
|
|
#if HAVE_PTHREAD_CANCEL
|
2020-09-06 12:28:53 +00:00
|
|
|
int thread_num = (ctx->options.conv2d_threads <= 0 || ctx->options.conv2d_threads > av_cpu_count())
|
|
|
|
? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
|
2021-03-10 17:08:10 +00:00
|
|
|
int ret = DNN_SUCCESS, thread_stride;
|
2021-03-10 16:38:36 +00:00
|
|
|
ThreadParam *thread_param;
|
2021-03-10 16:03:01 +00:00
|
|
|
#else
|
|
|
|
ThreadParam thread_param = { 0 };
|
|
|
|
#endif
|
2021-01-18 08:15:34 +00:00
|
|
|
ThreadCommonParam thread_common_param;
|
2021-01-22 14:20:15 +00:00
|
|
|
const ConvolutionalParams *conv_params = parameters;
|
2020-09-16 10:07:19 +00:00
|
|
|
int height = operands[input_operand_indexes[0]].dims[1];
|
|
|
|
int width = operands[input_operand_indexes[0]].dims[2];
|
2020-09-16 10:07:17 +00:00
|
|
|
int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
|
|
|
|
DnnOperand *output_operand = &operands[output_operand_index];
|
2021-03-10 14:40:36 +00:00
|
|
|
void *tmp;
|
2020-09-16 10:07:17 +00:00
|
|
|
|
|
|
|
output_operand->dims[0] = operands[input_operand_indexes[0]].dims[0];
|
2020-09-16 10:07:19 +00:00
|
|
|
output_operand->dims[1] = height - pad_size * 2;
|
|
|
|
output_operand->dims[2] = width - pad_size * 2;
|
2020-09-16 10:07:17 +00:00
|
|
|
output_operand->dims[3] = conv_params->output_num;
|
|
|
|
output_operand->data_type = operands[input_operand_indexes[0]].data_type;
|
2021-01-21 21:39:55 +00:00
|
|
|
output_operand->length = ff_calculate_operand_data_length(output_operand);
|
2020-09-16 10:07:17 +00:00
|
|
|
if (output_operand->length <= 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
|
|
|
|
return DNN_ERROR;
|
|
|
|
}
|
2021-03-10 14:40:36 +00:00
|
|
|
tmp = av_realloc(output_operand->data, output_operand->length);
|
|
|
|
if (!tmp) {
|
2020-09-16 10:07:17 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
|
|
|
|
return DNN_ERROR;
|
|
|
|
}
|
2021-03-10 14:40:36 +00:00
|
|
|
output_operand->data = tmp;
|
2020-09-16 10:07:17 +00:00
|
|
|
thread_common_param.output_data = output_operand->data;
|
2020-09-06 12:28:53 +00:00
|
|
|
thread_common_param.operands = operands;
|
|
|
|
thread_common_param.input_operand_indexes = input_operand_indexes;
|
|
|
|
thread_common_param.output_operand_index = output_operand_index;
|
|
|
|
thread_common_param.parameters = parameters;
|
|
|
|
thread_common_param.ctx = ctx;
|
2020-09-16 10:07:17 +00:00
|
|
|
|
2020-09-06 12:28:53 +00:00
|
|
|
#if HAVE_PTHREAD_CANCEL
|
2021-03-10 16:22:29 +00:00
|
|
|
thread_param = av_malloc_array(thread_num, sizeof(*thread_param));
|
2021-03-10 16:42:43 +00:00
|
|
|
if (!thread_param)
|
|
|
|
return DNN_ERROR;
|
2020-09-16 10:07:19 +00:00
|
|
|
thread_stride = (height - pad_size * 2) / thread_num;
|
2020-09-06 12:28:53 +00:00
|
|
|
//create threads
|
|
|
|
for (int i = 0; i < thread_num; i++){
|
2021-03-10 16:38:36 +00:00
|
|
|
thread_param[i].thread_common_param = &thread_common_param;
|
|
|
|
thread_param[i].thread_start = thread_stride * i + pad_size;
|
|
|
|
thread_param[i].thread_end = (i == thread_num - 1) ? (height - pad_size) : (thread_param[i].thread_start + thread_stride);
|
2021-03-10 17:08:10 +00:00
|
|
|
if (pthread_create(&thread_param[i].thread, NULL,
|
|
|
|
dnn_execute_layer_conv2d_thread, &thread_param[i])) {
|
|
|
|
thread_num = i;
|
|
|
|
ret = DNN_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-06 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < thread_num; i++){
|
2021-03-10 16:38:36 +00:00
|
|
|
pthread_join(thread_param[i].thread, NULL);
|
2020-09-06 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//release memory
|
2021-03-10 16:03:01 +00:00
|
|
|
av_freep(&thread_param);
|
2021-03-10 17:08:10 +00:00
|
|
|
|
|
|
|
return ret;
|
2020-09-06 12:28:53 +00:00
|
|
|
#else
|
2021-03-10 16:03:01 +00:00
|
|
|
thread_param.thread_common_param = &thread_common_param;
|
|
|
|
thread_param.thread_start = pad_size;
|
|
|
|
thread_param.thread_end = height - pad_size;
|
|
|
|
dnn_execute_layer_conv2d_thread(&thread_param);
|
2020-09-06 12:28:53 +00:00
|
|
|
|
2020-09-16 10:07:17 +00:00
|
|
|
return DNN_SUCCESS;
|
2021-03-10 17:08:10 +00:00
|
|
|
#endif
|
2019-09-05 06:00:28 +00:00
|
|
|
}
|