FFmpeg
dnn_backend_native_layer_conv2d.c
Go to the documentation of this file.
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"
23 
24 #define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
25 
26 int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
27 {
28  ConvolutionalParams *conv_params;
29  int kernel_size;
30  int dnn_size = 0;
31  conv_params = av_malloc(sizeof(*conv_params));
32  if (!conv_params)
33  return 0;
34 
35  conv_params->dilation = (int32_t)avio_rl32(model_file_context);
36  conv_params->padding_method = (int32_t)avio_rl32(model_file_context);
37  conv_params->activation = (int32_t)avio_rl32(model_file_context);
38  conv_params->input_num = (int32_t)avio_rl32(model_file_context);
39  conv_params->output_num = (int32_t)avio_rl32(model_file_context);
40  conv_params->kernel_size = (int32_t)avio_rl32(model_file_context);
41  conv_params->has_bias = (int32_t)avio_rl32(model_file_context);
42  dnn_size += 28;
43 
44  kernel_size = conv_params->input_num * conv_params->output_num *
45  conv_params->kernel_size * conv_params->kernel_size;
46  dnn_size += kernel_size * 4;
47  if (conv_params->has_bias)
48  dnn_size += conv_params->output_num * 4;
49 
50  if (dnn_size > file_size || conv_params->input_num <= 0 ||
51  conv_params->output_num <= 0 || conv_params->kernel_size <= 0){
52  av_freep(&conv_params);
53  return 0;
54  }
55 
56  conv_params->kernel = av_malloc(kernel_size * sizeof(float));
57  if (!conv_params->kernel) {
58  av_freep(&conv_params);
59  return 0;
60  }
61  for (int i = 0; i < kernel_size; ++i) {
62  conv_params->kernel[i] = av_int2float(avio_rl32(model_file_context));
63  }
64 
65  conv_params->biases = NULL;
66  if (conv_params->has_bias) {
67  conv_params->biases = av_malloc(conv_params->output_num * sizeof(float));
68  if (!conv_params->biases){
69  av_freep(&conv_params->kernel);
70  av_freep(&conv_params);
71  return 0;
72  }
73  for (int i = 0; i < conv_params->output_num; ++i){
74  conv_params->biases[i] = av_int2float(avio_rl32(model_file_context));
75  }
76  }
77 
78  layer->params = conv_params;
79 
80  layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
81  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
82  dnn_size += 8;
83 
84  if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
85  return 0;
86  }
87 
88  return dnn_size;
89 }
90 
91 int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
92  int32_t output_operand_index, const void *parameters)
93 {
94  float *output;
95  int32_t input_operand_index = input_operand_indexes[0];
96  int number = operands[input_operand_index].dims[0];
97  int height = operands[input_operand_index].dims[1];
98  int width = operands[input_operand_index].dims[2];
99  int channel = operands[input_operand_index].dims[3];
100  const float *input = operands[input_operand_index].data;
101  const ConvolutionalParams *conv_params = (const ConvolutionalParams *)parameters;
102 
103  int radius = conv_params->kernel_size >> 1;
104  int src_linesize = width * conv_params->input_num;
105  int filter_linesize = conv_params->kernel_size * conv_params->input_num;
106  int filter_size = conv_params->kernel_size * filter_linesize;
107  int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
108 
109  DnnOperand *output_operand = &operands[output_operand_index];
110  output_operand->dims[0] = number;
111  output_operand->dims[1] = height - pad_size * 2;
112  output_operand->dims[2] = width - pad_size * 2;
113  output_operand->dims[3] = conv_params->output_num;
114  output_operand->data_type = operands[input_operand_index].data_type;
115  output_operand->length = calculate_operand_data_length(output_operand);
116  if (output_operand->length <= 0)
117  return -1;
118  output_operand->data = av_realloc(output_operand->data, output_operand->length);
119  if (!output_operand->data)
120  return -1;
121  output = output_operand->data;
122 
123  av_assert0(channel == conv_params->input_num);
124 
125  for (int y = pad_size; y < height - pad_size; ++y) {
126  for (int x = pad_size; x < width - pad_size; ++x) {
127  for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
128  if (conv_params->has_bias)
129  output[n_filter] = conv_params->biases[n_filter];
130  else
131  output[n_filter] = 0.f;
132 
133  for (int ch = 0; ch < conv_params->input_num; ++ch) {
134  for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) {
135  for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) {
136  float input_pel;
137  if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) {
138  int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height);
139  int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width);
140  input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
141  } else {
142  int y_pos = y + (kernel_y - radius) * conv_params->dilation;
143  int x_pos = x + (kernel_x - radius) * conv_params->dilation;
144  input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 :
145  input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
146  }
147 
148 
149  output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
150  kernel_x * conv_params->input_num + ch];
151  }
152  }
153  }
154  switch (conv_params->activation){
155  case RELU:
156  output[n_filter] = FFMAX(output[n_filter], 0.0);
157  break;
158  case TANH:
159  output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
160  break;
161  case SIGMOID:
162  output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
163  break;
164  case NONE:
165  break;
166  case LEAKY_RELU:
167  output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0);
168  }
169  }
170  output += conv_params->output_num;
171  }
172  }
173  return 0;
174 }
calculate_operand_data_length
int32_t calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:297
dnn_execute_layer_conv2d
int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters)
Definition: dnn_backend_native_layer_conv2d.c:91
NONE
@ NONE
Definition: af_afade.c:54
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
ConvolutionalParams::kernel
float * kernel
Definition: dnn_backend_native_layer_conv2d.h:35
TANH
@ TANH
Definition: dnn_backend_native_layer_conv2d.h:26
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
dnn_load_layer_conv2d
int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
Definition: dnn_backend_native_layer_conv2d.c:26
LEAKY_RELU
@ LEAKY_RELU
Definition: dnn_backend_native_layer_conv2d.h:26
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
RELU
@ RELU
Definition: dnn_backend_native_layer_conv2d.h:26
avassert.h
ConvolutionalParams::input_num
int32_t input_num
Definition: dnn_backend_native_layer_conv2d.h:30
width
#define width
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:98
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
DnnOperand::data_type
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now.
Definition: dnn_backend_native.h:79
ConvolutionalParams::activation
DNNActivationFunc activation
Definition: dnn_backend_native_layer_conv2d.h:31
f
#define f(width, name)
Definition: cbs_vp9.c:255
int32_t
int32_t
Definition: audio_convert.c:194
ConvolutionalParams::has_bias
int32_t has_bias
Definition: dnn_backend_native_layer_conv2d.h:34
Layer::params
void * params
Definition: dnn_backend_native.h:60
NULL
#define NULL
Definition: coverity.c:32
SAME_CLAMP_TO_EDGE
@ SAME_CLAMP_TO_EDGE
Definition: dnn_backend_native_layer_conv2d.h:27
DnnOperand::dims
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
Definition: dnn_backend_native.h:68
exp
int8_t exp
Definition: eval.c:72
ConvolutionalParams::kernel_size
int32_t kernel_size
Definition: dnn_backend_native_layer_conv2d.h:30
DnnOperand::length
int32_t length
Definition: dnn_backend_native.h:99
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:747
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
Layer::output_operand_index
int32_t output_operand_index
Definition: dnn_backend_native.h:59
Layer
Definition: dnn_backend_native.h:51
Layer::input_operand_indexes
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
Definition: dnn_backend_native.h:58
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
dnn_backend_native_layer_conv2d.h
height
#define height
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
ConvolutionalParams::output_num
int32_t output_num
Definition: dnn_backend_native_layer_conv2d.h:30
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
CLAMP_TO_EDGE
#define CLAMP_TO_EDGE(x, w)
Definition: dnn_backend_native_layer_conv2d.c:24
VALID
@ VALID
Definition: dnn_backend_native_layer_conv2d.h:27
DnnOperand
Definition: dnn_backend_native.h:63
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
SIGMOID
@ SIGMOID
Definition: dnn_backend_native_layer_conv2d.h:26
ConvolutionalParams::padding_method
DNNConvPaddingParam padding_method
Definition: dnn_backend_native_layer_conv2d.h:32
channel
channel
Definition: ebur128.h:39
ConvolutionalParams
Definition: dnn_backend_native_layer_conv2d.h:29
ConvolutionalParams::dilation
int32_t dilation
Definition: dnn_backend_native_layer_conv2d.h:33
ConvolutionalParams::biases
float * biases
Definition: dnn_backend_native_layer_conv2d.h:36