History

libavfilter/dnn: separate conv2d layer from dnn_backend_native.c to a new file

the logic is that one layer in one separated source file to make the source files simple for maintaining.

Guo, Yejun committed 7 years ago

Commit 5f058dd1 parent c2ab9986 files · +143 −92Browse files

Unified · Split

6 files
  1. libavfilter/dnn/Makefile +1 −0
  2. libavfilter/dnn/dnn_backend_native.c +1 −79
  3. libavfilter/dnn/dnn_backend_native.h +0 −13
  4. libavfilter/dnn/dnn_backend_native_layer_conv2d.c +101 −0
  5. libavfilter/dnn/dnn_backend_native_layer_conv2d.h +39 −0
  6. libavfilter/dnn/dnn_backend_tf.c +1 −0
libavfilter/dnn/Makefile+1−0
@@ -1,6 +1,7 @@
1OBJS-$(CONFIG_DNN) += dnn/dnn_interface.o1OBJS-$(CONFIG_DNN) += dnn/dnn_interface.o
2OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native.o2OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native.o
3OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_pad.o3OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_pad.o
4OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_conv2d.o
4 5
5DNN-OBJS-$(CONFIG_LIBTENSORFLOW) += dnn/dnn_backend_tf.o6DNN-OBJS-$(CONFIG_LIBTENSORFLOW) += dnn/dnn_backend_tf.o
6 7
libavfilter/dnn/dnn_backend_native.c+1−79
@@ -26,6 +26,7 @@ · 25 unmodified lines
26#include "dnn_backend_native.h"26#include "dnn_backend_native.h"
27#include "libavutil/avassert.h"27#include "libavutil/avassert.h"
28#include "dnn_backend_native_layer_pad.h"28#include "dnn_backend_native_layer_pad.h"
29#include "dnn_backend_native_layer_conv2d.h"
29 30
30static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)31static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
31{32{
@@ -281,85 +282,6 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename) · 249 unmodified lines
281 return model;282 return model;
282}283}
283 284
284#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
285
286static int convolve(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const ConvolutionalParams *conv_params)
287{
288 float *output;
289 int32_t input_operand_index = input_operand_indexes[0];
290 int number = operands[input_operand_index].dims[0];
291 int height = operands[input_operand_index].dims[1];
292 int width = operands[input_operand_index].dims[2];
293 int channel = operands[input_operand_index].dims[3];
294 const float *input = operands[input_operand_index].data;
295
296 int radius = conv_params->kernel_size >> 1;
297 int src_linesize = width * conv_params->input_num;
298 int filter_linesize = conv_params->kernel_size * conv_params->input_num;
299 int filter_size = conv_params->kernel_size * filter_linesize;
300 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
301
302 DnnOperand *output_operand = &operands[output_operand_index];
303 output_operand->dims[0] = number;
304 output_operand->dims[1] = height - pad_size * 2;
305 output_operand->dims[2] = width - pad_size * 2;
306 output_operand->dims[3] = conv_params->output_num;
307 output_operand->length = calculate_operand_data_length(output_operand);
308 output_operand->data = av_realloc(output_operand->data, output_operand->length);
309 if (!output_operand->data)
310 return -1;
311 output = output_operand->data;
312
313 av_assert0(channel == conv_params->input_num);
314
315 for (int y = pad_size; y < height - pad_size; ++y) {
316 for (int x = pad_size; x < width - pad_size; ++x) {
317 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
318 output[n_filter] = conv_params->biases[n_filter];
319
320 for (int ch = 0; ch < conv_params->input_num; ++ch) {
321 for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) {
322 for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) {
323 float input_pel;
324 if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) {
325 int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height);
326 int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width);
327 input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
328 } else {
329 int y_pos = y + (kernel_y - radius) * conv_params->dilation;
330 int x_pos = x + (kernel_x - radius) * conv_params->dilation;
331 input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 :
332 input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
333 }
334
335
336 output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
337 kernel_x * conv_params->input_num + ch];
338 }
339 }
340 }
341 switch (conv_params->activation){
342 case RELU:
343 output[n_filter] = FFMAX(output[n_filter], 0.0);
344 break;
345 case TANH:
346 output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
347 break;
348 case SIGMOID:
349 output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
350 break;
351 case NONE:
352 break;
353 case LEAKY_RELU:
354 output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0);
355 }
356 }
357 output += conv_params->output_num;
358 }
359 }
360 return 0;
361}
362
363static int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size)285static int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size)
364{286{
365 float *output;287 float *output;
libavfilter/dnn/dnn_backend_native.h+0−13
@@ -32,10 +32,6 @@ · 31 unmodified lines
32 32
33typedef enum {INPUT, CONV, DEPTH_TO_SPACE, MIRROR_PAD} DNNLayerType;33typedef enum {INPUT, CONV, DEPTH_TO_SPACE, MIRROR_PAD} DNNLayerType;
34 34
35typedef enum {RELU, TANH, SIGMOID, NONE, LEAKY_RELU} DNNActivationFunc;
36
37typedef enum {VALID, SAME, SAME_CLAMP_TO_EDGE} DNNConvPaddingParam;
38
39typedef enum {DOT_INPUT = 1, DOT_OUTPUT = 2, DOT_INTERMEDIATE = DOT_INPUT | DOT_INPUT} DNNOperandType;35typedef enum {DOT_INPUT = 1, DOT_OUTPUT = 2, DOT_INTERMEDIATE = DOT_INPUT | DOT_INPUT} DNNOperandType;
40 36
41typedef struct Layer{37typedef struct Layer{
@@ -90,15 +86,6 @@ typedef struct DnnOperand{ · 48 unmodified lines
90 int32_t usedNumbersLeft;86 int32_t usedNumbersLeft;
91}DnnOperand;87}DnnOperand;
92 88
93typedef struct ConvolutionalParams{
94 int32_t input_num, output_num, kernel_size;
95 DNNActivationFunc activation;
96 DNNConvPaddingParam padding_method;
97 int32_t dilation;
98 float *kernel;
99 float *biases;
100} ConvolutionalParams;
101
102typedef struct InputParams{89typedef struct InputParams{
103 int height, width, channels;90 int height, width, channels;
104} InputParams;91} InputParams;
libavfilter/dnn/dnn_backend_native_layer_conv2d.c+101−0
@@ -0,0 +1,101 @@
1/*
2 * Copyright (c) 2018 Sergey Lavrushkin
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/avassert.h"
22#include "dnn_backend_native_layer_conv2d.h"
23
24#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
25
26int convolve(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const ConvolutionalParams *conv_params)
27{
28 float *output;
29 int32_t input_operand_index = input_operand_indexes[0];
30 int number = operands[input_operand_index].dims[0];
31 int height = operands[input_operand_index].dims[1];
32 int width = operands[input_operand_index].dims[2];
33 int channel = operands[input_operand_index].dims[3];
34 const float *input = operands[input_operand_index].data;
35
36 int radius = conv_params->kernel_size >> 1;
37 int src_linesize = width * conv_params->input_num;
38 int filter_linesize = conv_params->kernel_size * conv_params->input_num;
39 int filter_size = conv_params->kernel_size * filter_linesize;
40 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
41
42 DnnOperand *output_operand = &operands[output_operand_index];
43 output_operand->dims[0] = number;
44 output_operand->dims[1] = height - pad_size * 2;
45 output_operand->dims[2] = width - pad_size * 2;
46 output_operand->dims[3] = conv_params->output_num;
47 output_operand->length = calculate_operand_data_length(output_operand);
48 output_operand->data = av_realloc(output_operand->data, output_operand->length);
49 if (!output_operand->data)
50 return -1;
51 output = output_operand->data;
52
53 av_assert0(channel == conv_params->input_num);
54
55 for (int y = pad_size; y < height - pad_size; ++y) {
56 for (int x = pad_size; x < width - pad_size; ++x) {
57 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
58 output[n_filter] = conv_params->biases[n_filter];
59
60 for (int ch = 0; ch < conv_params->input_num; ++ch) {
61 for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) {
62 for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) {
63 float input_pel;
64 if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) {
65 int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height);
66 int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width);
67 input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
68 } else {
69 int y_pos = y + (kernel_y - radius) * conv_params->dilation;
70 int x_pos = x + (kernel_x - radius) * conv_params->dilation;
71 input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 :
72 input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
73 }
74
75
76 output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
77 kernel_x * conv_params->input_num + ch];
78 }
79 }
80 }
81 switch (conv_params->activation){
82 case RELU:
83 output[n_filter] = FFMAX(output[n_filter], 0.0);
84 break;
85 case TANH:
86 output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
87 break;
88 case SIGMOID:
89 output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
90 break;
91 case NONE:
92 break;
93 case LEAKY_RELU:
94 output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0);
95 }
96 }
97 output += conv_params->output_num;
98 }
99 }
100 return 0;
101}
libavfilter/dnn/dnn_backend_native_layer_conv2d.h+39−0
@@ -0,0 +1,39 @@
1/*
2 * Copyright (c) 2018 Sergey Lavrushkin
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef AVFILTER_DNN_DNN_BACKEND_NATIVE_LAYER_CONV2D_H
22#define AVFILTER_DNN_DNN_BACKEND_NATIVE_LAYER_CONV2D_H
23
24#include "dnn_backend_native.h"
25
26typedef enum {RELU, TANH, SIGMOID, NONE, LEAKY_RELU} DNNActivationFunc;
27typedef enum {VALID, SAME, SAME_CLAMP_TO_EDGE} DNNConvPaddingParam;
28
29typedef struct ConvolutionalParams{
30 int32_t input_num, output_num, kernel_size;
31 DNNActivationFunc activation;
32 DNNConvPaddingParam padding_method;
33 int32_t dilation;
34 float *kernel;
35 float *biases;
36} ConvolutionalParams;
37
38int convolve(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const ConvolutionalParams *conv_params);
39#endif
libavfilter/dnn/dnn_backend_tf.c+1−0
@@ -25,6 +25,7 @@ · 24 unmodified lines
25 25
26#include "dnn_backend_tf.h"26#include "dnn_backend_tf.h"
27#include "dnn_backend_native.h"27#include "dnn_backend_native.h"
28#include "dnn_backend_native_layer_conv2d.h"
28#include "libavformat/avio.h"29#include "libavformat/avio.h"
29#include "libavutil/avassert.h"30#include "libavutil/avassert.h"
30#include "dnn_backend_native_layer_pad.h"31#include "dnn_backend_native_layer_pad.h"

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t