dnn_backend_native.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. * @file
  22. * DNN native backend implementation.
  23. */
  24. #include "dnn_backend_native.h"
  25. #include "libavutil/avassert.h"
  26. static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
  27. {
  28. ConvolutionalNetwork *network = (ConvolutionalNetwork *)model;
  29. InputParams *input_params;
  30. ConvolutionalParams *conv_params;
  31. DepthToSpaceParams *depth_to_space_params;
  32. int cur_width, cur_height, cur_channels;
  33. int32_t layer;
  34. if (network->layers_num <= 0 || network->layers[0].type != INPUT){
  35. return DNN_ERROR;
  36. }
  37. else{
  38. input_params = (InputParams *)network->layers[0].params;
  39. input_params->width = cur_width = input->width;
  40. input_params->height = cur_height = input->height;
  41. input_params->channels = cur_channels = input->channels;
  42. if (input->data){
  43. av_freep(&input->data);
  44. }
  45. av_assert0(input->dt == DNN_FLOAT);
  46. network->layers[0].output = input->data = av_malloc(cur_height * cur_width * cur_channels * sizeof(float));
  47. if (!network->layers[0].output){
  48. return DNN_ERROR;
  49. }
  50. }
  51. for (layer = 1; layer < network->layers_num; ++layer){
  52. switch (network->layers[layer].type){
  53. case CONV:
  54. conv_params = (ConvolutionalParams *)network->layers[layer].params;
  55. if (conv_params->input_num != cur_channels){
  56. return DNN_ERROR;
  57. }
  58. cur_channels = conv_params->output_num;
  59. if (conv_params->padding_method == VALID) {
  60. int pad_size = (conv_params->kernel_size - 1) * conv_params->dilation;
  61. cur_height -= pad_size;
  62. cur_width -= pad_size;
  63. }
  64. break;
  65. case DEPTH_TO_SPACE:
  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){
  68. return DNN_ERROR;
  69. }
  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;
  73. break;
  74. default:
  75. return DNN_ERROR;
  76. }
  77. if (network->layers[layer].output){
  78. av_freep(&network->layers[layer].output);
  79. }
  80. if (cur_height <= 0 || cur_width <= 0)
  81. return DNN_ERROR;
  82. network->layers[layer].output = av_malloc(cur_height * cur_width * cur_channels * sizeof(float));
  83. if (!network->layers[layer].output){
  84. return DNN_ERROR;
  85. }
  86. }
  87. return DNN_SUCCESS;
  88. }
  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)
  94. {
  95. DNNModel *model = NULL;
  96. ConvolutionalNetwork *network = NULL;
  97. AVIOContext *model_file_context;
  98. int file_size, dnn_size, kernel_size, i;
  99. int32_t layer;
  100. DNNLayerType layer_type;
  101. ConvolutionalParams *conv_params;
  102. DepthToSpaceParams *depth_to_space_params;
  103. model = av_malloc(sizeof(DNNModel));
  104. if (!model){
  105. return NULL;
  106. }
  107. if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
  108. av_freep(&model);
  109. return NULL;
  110. }
  111. file_size = avio_size(model_file_context);
  112. network = av_malloc(sizeof(ConvolutionalNetwork));
  113. if (!network){
  114. avio_closep(&model_file_context);
  115. av_freep(&model);
  116. return NULL;
  117. }
  118. model->model = (void *)network;
  119. network->layers_num = 1 + (int32_t)avio_rl32(model_file_context);
  120. dnn_size = 4;
  121. network->layers = av_malloc(network->layers_num * sizeof(Layer));
  122. if (!network->layers){
  123. av_freep(&network);
  124. avio_closep(&model_file_context);
  125. av_freep(&model);
  126. return NULL;
  127. }
  128. for (layer = 0; layer < network->layers_num; ++layer){
  129. network->layers[layer].output = NULL;
  130. network->layers[layer].params = NULL;
  131. }
  132. network->layers[0].type = INPUT;
  133. network->layers[0].params = av_malloc(sizeof(InputParams));
  134. if (!network->layers[0].params){
  135. avio_closep(&model_file_context);
  136. ff_dnn_free_model_native(&model);
  137. return NULL;
  138. }
  139. for (layer = 1; layer < network->layers_num; ++layer){
  140. layer_type = (int32_t)avio_rl32(model_file_context);
  141. dnn_size += 4;
  142. switch (layer_type){
  143. case CONV:
  144. conv_params = av_malloc(sizeof(ConvolutionalParams));
  145. if (!conv_params){
  146. avio_closep(&model_file_context);
  147. ff_dnn_free_model_native(&model);
  148. return NULL;
  149. }
  150. conv_params->dilation = (int32_t)avio_rl32(model_file_context);
  151. conv_params->padding_method = (int32_t)avio_rl32(model_file_context);
  152. conv_params->activation = (int32_t)avio_rl32(model_file_context);
  153. conv_params->input_num = (int32_t)avio_rl32(model_file_context);
  154. conv_params->output_num = (int32_t)avio_rl32(model_file_context);
  155. conv_params->kernel_size = (int32_t)avio_rl32(model_file_context);
  156. kernel_size = conv_params->input_num * conv_params->output_num *
  157. conv_params->kernel_size * conv_params->kernel_size;
  158. dnn_size += 24 + (kernel_size + conv_params->output_num << 2);
  159. if (dnn_size > file_size || conv_params->input_num <= 0 ||
  160. conv_params->output_num <= 0 || conv_params->kernel_size <= 0){
  161. avio_closep(&model_file_context);
  162. ff_dnn_free_model_native(&model);
  163. return NULL;
  164. }
  165. conv_params->kernel = av_malloc(kernel_size * sizeof(float));
  166. conv_params->biases = av_malloc(conv_params->output_num * sizeof(float));
  167. if (!conv_params->kernel || !conv_params->biases){
  168. avio_closep(&model_file_context);
  169. ff_dnn_free_model_native(&model);
  170. return NULL;
  171. }
  172. for (i = 0; i < kernel_size; ++i){
  173. conv_params->kernel[i] = av_int2float(avio_rl32(model_file_context));
  174. }
  175. for (i = 0; i < conv_params->output_num; ++i){
  176. conv_params->biases[i] = av_int2float(avio_rl32(model_file_context));
  177. }
  178. network->layers[layer].type = CONV;
  179. network->layers[layer].params = conv_params;
  180. break;
  181. case DEPTH_TO_SPACE:
  182. depth_to_space_params = av_malloc(sizeof(DepthToSpaceParams));
  183. if (!depth_to_space_params){
  184. avio_closep(&model_file_context);
  185. ff_dnn_free_model_native(&model);
  186. return NULL;
  187. }
  188. depth_to_space_params->block_size = (int32_t)avio_rl32(model_file_context);
  189. dnn_size += 4;
  190. network->layers[layer].type = DEPTH_TO_SPACE;
  191. network->layers[layer].params = depth_to_space_params;
  192. break;
  193. default:
  194. avio_closep(&model_file_context);
  195. ff_dnn_free_model_native(&model);
  196. return NULL;
  197. }
  198. }
  199. avio_closep(&model_file_context);
  200. if (dnn_size != file_size){
  201. ff_dnn_free_model_native(&model);
  202. return NULL;
  203. }
  204. model->set_input_output = &set_input_output_native;
  205. return model;
  206. }
  207. #define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
  208. static void convolve(const float *input, float *output, const ConvolutionalParams *conv_params, int width, int height)
  209. {
  210. int radius = conv_params->kernel_size >> 1;
  211. int src_linesize = width * conv_params->input_num;
  212. int filter_linesize = conv_params->kernel_size * conv_params->input_num;
  213. int filter_size = conv_params->kernel_size * filter_linesize;
  214. int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
  215. for (int y = pad_size; y < height - pad_size; ++y) {
  216. for (int x = pad_size; x < width - pad_size; ++x) {
  217. for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
  218. output[n_filter] = conv_params->biases[n_filter];
  219. for (int ch = 0; ch < conv_params->input_num; ++ch) {
  220. for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) {
  221. for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) {
  222. float input_pel;
  223. if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) {
  224. int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height);
  225. int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width);
  226. input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
  227. } else {
  228. int y_pos = y + (kernel_y - radius) * conv_params->dilation;
  229. int x_pos = x + (kernel_x - radius) * conv_params->dilation;
  230. input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 :
  231. input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
  232. }
  233. output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
  234. kernel_x * conv_params->input_num + ch];
  235. }
  236. }
  237. }
  238. switch (conv_params->activation){
  239. case RELU:
  240. output[n_filter] = FFMAX(output[n_filter], 0.0);
  241. break;
  242. case TANH:
  243. output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
  244. break;
  245. case SIGMOID:
  246. output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
  247. break;
  248. case NONE:
  249. break;
  250. case LEAKY_RELU:
  251. output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0);
  252. }
  253. }
  254. output += conv_params->output_num;
  255. }
  256. }
  257. }
  258. static void depth_to_space(const float *input, float *output, int block_size, int width, int height, int channels)
  259. {
  260. int y, x, by, bx, ch;
  261. int new_channels = channels / (block_size * block_size);
  262. int output_linesize = width * channels;
  263. int by_linesize = output_linesize / block_size;
  264. 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];
  271. }
  272. input += new_channels;
  273. }
  274. }
  275. }
  276. output += output_linesize;
  277. }
  278. }
  279. DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
  280. {
  281. ConvolutionalNetwork *network = (ConvolutionalNetwork *)model->model;
  282. int cur_width, cur_height, cur_channels;
  283. int32_t layer;
  284. InputParams *input_params;
  285. ConvolutionalParams *conv_params;
  286. DepthToSpaceParams *depth_to_space_params;
  287. if (network->layers_num <= 0 || network->layers[0].type != INPUT || !network->layers[0].output){
  288. return DNN_ERROR;
  289. }
  290. else{
  291. input_params = (InputParams *)network->layers[0].params;
  292. cur_width = input_params->width;
  293. cur_height = input_params->height;
  294. cur_channels = input_params->channels;
  295. }
  296. for (layer = 1; layer < network->layers_num; ++layer){
  297. if (!network->layers[layer].output){
  298. return DNN_ERROR;
  299. }
  300. switch (network->layers[layer].type){
  301. case CONV:
  302. conv_params = (ConvolutionalParams *)network->layers[layer].params;
  303. convolve(network->layers[layer - 1].output, network->layers[layer].output, conv_params, cur_width, cur_height);
  304. cur_channels = conv_params->output_num;
  305. if (conv_params->padding_method == VALID) {
  306. int pad_size = (conv_params->kernel_size - 1) * conv_params->dilation;
  307. cur_height -= pad_size;
  308. cur_width -= pad_size;
  309. }
  310. break;
  311. case DEPTH_TO_SPACE:
  312. depth_to_space_params = (DepthToSpaceParams *)network->layers[layer].params;
  313. depth_to_space(network->layers[layer - 1].output, network->layers[layer].output,
  314. depth_to_space_params->block_size, cur_width, cur_height, cur_channels);
  315. cur_height *= depth_to_space_params->block_size;
  316. cur_width *= depth_to_space_params->block_size;
  317. cur_channels /= depth_to_space_params->block_size * depth_to_space_params->block_size;
  318. break;
  319. case INPUT:
  320. return DNN_ERROR;
  321. }
  322. }
  323. // native mode does not support multiple outputs yet
  324. if (nb_output > 1)
  325. return DNN_ERROR;
  326. outputs[0].data = network->layers[network->layers_num - 1].output;
  327. outputs[0].height = cur_height;
  328. outputs[0].width = cur_width;
  329. outputs[0].channels = cur_channels;
  330. return DNN_SUCCESS;
  331. }
  332. void ff_dnn_free_model_native(DNNModel **model)
  333. {
  334. ConvolutionalNetwork *network;
  335. ConvolutionalParams *conv_params;
  336. int32_t layer;
  337. if (*model)
  338. {
  339. network = (ConvolutionalNetwork *)(*model)->model;
  340. for (layer = 0; layer < network->layers_num; ++layer){
  341. av_freep(&network->layers[layer].output);
  342. if (network->layers[layer].type == CONV){
  343. conv_params = (ConvolutionalParams *)network->layers[layer].params;
  344. av_freep(&conv_params->kernel);
  345. av_freep(&conv_params->biases);
  346. }
  347. av_freep(&network->layers[layer].params);
  348. }
  349. av_freep(&network->layers);
  350. av_freep(&network);
  351. av_freep(model);
  352. }
  353. }