2 * Copyright (c) 2018 Sergey Lavrushkin
4 * This file is part of FFmpeg.
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.
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.
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
23 * DNN native backend implementation.
26 #include "dnn_backend_native.h"
27 #include "libavutil/avassert.h"
29 static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
31 ConvolutionalNetwork *network = (ConvolutionalNetwork *)model;
32 InputParams *input_params;
33 ConvolutionalParams *conv_params;
34 DepthToSpaceParams *depth_to_space_params;
35 int cur_width, cur_height, cur_channels;
38 if (network->layers_num <= 0 || network->layers[0].type != INPUT){
42 input_params = (InputParams *)network->layers[0].params;
43 input_params->width = cur_width = input->width;
44 input_params->height = cur_height = input->height;
45 input_params->channels = cur_channels = input->channels;
47 av_freep(&input->data);
49 av_assert0(input->dt == DNN_FLOAT);
50 network->layers[0].output = input->data = av_malloc(cur_height * cur_width * cur_channels * sizeof(float));
51 if (!network->layers[0].output){
56 for (layer = 1; layer < network->layers_num; ++layer){
57 switch (network->layers[layer].type){
59 conv_params = (ConvolutionalParams *)network->layers[layer].params;
60 if (conv_params->input_num != cur_channels){
63 cur_channels = conv_params->output_num;
66 depth_to_space_params = (DepthToSpaceParams *)network->layers[layer].params;
67 if (cur_channels % (depth_to_space_params->block_size * depth_to_space_params->block_size) != 0){
70 cur_channels = cur_channels / (depth_to_space_params->block_size * depth_to_space_params->block_size);
71 cur_height *= depth_to_space_params->block_size;
72 cur_width *= depth_to_space_params->block_size;
77 if (network->layers[layer].output){
78 av_freep(&network->layers[layer].output);
80 network->layers[layer].output = av_malloc(cur_height * cur_width * cur_channels * sizeof(float));
81 if (!network->layers[layer].output){
89 // Loads model and its parameters that are stored in a binary file with following structure:
90 // layers_num,layer_type,layer_parameterss,layer_type,layer_parameters...
91 // For CONV layer: activation_function, input_num, output_num, kernel_size, kernel, biases
92 // For DEPTH_TO_SPACE layer: block_size
93 DNNModel *ff_dnn_load_model_native(const char *model_filename)
95 DNNModel *model = NULL;
96 ConvolutionalNetwork *network = NULL;
97 AVIOContext *model_file_context;
98 int file_size, dnn_size, kernel_size, i;
100 DNNLayerType layer_type;
101 ConvolutionalParams *conv_params;
102 DepthToSpaceParams *depth_to_space_params;
104 model = av_malloc(sizeof(DNNModel));
109 if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
113 file_size = avio_size(model_file_context);
115 network = av_malloc(sizeof(ConvolutionalNetwork));
117 avio_closep(&model_file_context);
121 model->model = (void *)network;
123 network->layers_num = 1 + (int32_t)avio_rl32(model_file_context);
126 network->layers = av_malloc(network->layers_num * sizeof(Layer));
127 if (!network->layers){
129 avio_closep(&model_file_context);
134 for (layer = 0; layer < network->layers_num; ++layer){
135 network->layers[layer].output = NULL;
136 network->layers[layer].params = NULL;
138 network->layers[0].type = INPUT;
139 network->layers[0].params = av_malloc(sizeof(InputParams));
140 if (!network->layers[0].params){
141 avio_closep(&model_file_context);
142 ff_dnn_free_model_native(&model);
146 for (layer = 1; layer < network->layers_num; ++layer){
147 layer_type = (int32_t)avio_rl32(model_file_context);
151 conv_params = av_malloc(sizeof(ConvolutionalParams));
153 avio_closep(&model_file_context);
154 ff_dnn_free_model_native(&model);
157 conv_params->activation = (int32_t)avio_rl32(model_file_context);
158 conv_params->input_num = (int32_t)avio_rl32(model_file_context);
159 conv_params->output_num = (int32_t)avio_rl32(model_file_context);
160 conv_params->kernel_size = (int32_t)avio_rl32(model_file_context);
161 kernel_size = conv_params->input_num * conv_params->output_num *
162 conv_params->kernel_size * conv_params->kernel_size;
163 dnn_size += 16 + (kernel_size + conv_params->output_num << 2);
164 if (dnn_size > file_size || conv_params->input_num <= 0 ||
165 conv_params->output_num <= 0 || conv_params->kernel_size <= 0){
166 avio_closep(&model_file_context);
167 ff_dnn_free_model_native(&model);
170 conv_params->kernel = av_malloc(kernel_size * sizeof(float));
171 conv_params->biases = av_malloc(conv_params->output_num * sizeof(float));
172 if (!conv_params->kernel || !conv_params->biases){
173 avio_closep(&model_file_context);
174 ff_dnn_free_model_native(&model);
177 for (i = 0; i < kernel_size; ++i){
178 conv_params->kernel[i] = av_int2float(avio_rl32(model_file_context));
180 for (i = 0; i < conv_params->output_num; ++i){
181 conv_params->biases[i] = av_int2float(avio_rl32(model_file_context));
183 network->layers[layer].type = CONV;
184 network->layers[layer].params = conv_params;
187 depth_to_space_params = av_malloc(sizeof(DepthToSpaceParams));
188 if (!depth_to_space_params){
189 avio_closep(&model_file_context);
190 ff_dnn_free_model_native(&model);
193 depth_to_space_params->block_size = (int32_t)avio_rl32(model_file_context);
195 network->layers[layer].type = DEPTH_TO_SPACE;
196 network->layers[layer].params = depth_to_space_params;
199 avio_closep(&model_file_context);
200 ff_dnn_free_model_native(&model);
205 avio_closep(&model_file_context);
207 if (dnn_size != file_size){
208 ff_dnn_free_model_native(&model);
212 model->set_input_output = &set_input_output_native;
217 #define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
219 static void convolve(const float *input, float *output, const ConvolutionalParams *conv_params, int width, int height)
221 int y, x, n_filter, ch, kernel_y, kernel_x;
222 int radius = conv_params->kernel_size >> 1;
223 int src_linesize = width * conv_params->input_num;
224 int filter_linesize = conv_params->kernel_size * conv_params->input_num;
225 int filter_size = conv_params->kernel_size * filter_linesize;
227 for (y = 0; y < height; ++y){
228 for (x = 0; x < width; ++x){
229 for (n_filter = 0; n_filter < conv_params->output_num; ++n_filter){
230 output[n_filter] = conv_params->biases[n_filter];
231 for (ch = 0; ch < conv_params->input_num; ++ch){
232 for (kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y){
233 for (kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x){
234 output[n_filter] += input[CLAMP_TO_EDGE(y + kernel_y - radius, height) * src_linesize +
235 CLAMP_TO_EDGE(x + kernel_x - radius, width) * conv_params->input_num + ch] *
236 conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
237 kernel_x * conv_params->input_num + ch];
241 switch (conv_params->activation){
243 output[n_filter] = FFMAX(output[n_filter], 0.0);
246 output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
249 output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
252 output += conv_params->output_num;
257 static void depth_to_space(const float *input, float *output, int block_size, int width, int height, int channels)
259 int y, x, by, bx, ch;
260 int new_channels = channels / (block_size * block_size);
261 int output_linesize = width * channels;
262 int by_linesize = output_linesize / block_size;
263 int x_linesize = new_channels * block_size;
265 for (y = 0; y < height; ++y){
266 for (x = 0; x < width; ++x){
267 for (by = 0; by < block_size; ++by){
268 for (bx = 0; bx < block_size; ++bx){
269 for (ch = 0; ch < new_channels; ++ch){
270 output[by * by_linesize + x * x_linesize + bx * new_channels + ch] = input[ch];
272 input += new_channels;
276 output += output_linesize;
280 DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
282 ConvolutionalNetwork *network = (ConvolutionalNetwork *)model->model;
283 int cur_width, cur_height, cur_channels;
285 InputParams *input_params;
286 ConvolutionalParams *conv_params;
287 DepthToSpaceParams *depth_to_space_params;
289 if (network->layers_num <= 0 || network->layers[0].type != INPUT || !network->layers[0].output){
293 input_params = (InputParams *)network->layers[0].params;
294 cur_width = input_params->width;
295 cur_height = input_params->height;
296 cur_channels = input_params->channels;
299 for (layer = 1; layer < network->layers_num; ++layer){
300 if (!network->layers[layer].output){
303 switch (network->layers[layer].type){
305 conv_params = (ConvolutionalParams *)network->layers[layer].params;
306 convolve(network->layers[layer - 1].output, network->layers[layer].output, conv_params, cur_width, cur_height);
307 cur_channels = conv_params->output_num;
310 depth_to_space_params = (DepthToSpaceParams *)network->layers[layer].params;
311 depth_to_space(network->layers[layer - 1].output, network->layers[layer].output,
312 depth_to_space_params->block_size, cur_width, cur_height, cur_channels);
313 cur_height *= depth_to_space_params->block_size;
314 cur_width *= depth_to_space_params->block_size;
315 cur_channels /= depth_to_space_params->block_size * depth_to_space_params->block_size;
322 // native mode does not support multiple outputs yet
325 outputs[0].data = network->layers[network->layers_num - 1].output;
326 outputs[0].height = cur_height;
327 outputs[0].width = cur_width;
328 outputs[0].channels = cur_channels;
333 void ff_dnn_free_model_native(DNNModel **model)
335 ConvolutionalNetwork *network;
336 ConvolutionalParams *conv_params;
341 network = (ConvolutionalNetwork *)(*model)->model;
342 for (layer = 0; layer < network->layers_num; ++layer){
343 av_freep(&network->layers[layer].output);
344 if (network->layers[layer].type == CONV){
345 conv_params = (ConvolutionalParams *)network->layers[layer].params;
346 av_freep(&conv_params->kernel);
347 av_freep(&conv_params->biases);
349 av_freep(&network->layers[layer].params);
351 av_freep(&network->layers);