Re: [FFmpeg-devel] [PATCH V2 1/3] dnn: introduce dnn operand (in c code) to hold operand infos within network

2019-08-30 Thread Pedro Arthur
Em qui, 29 de ago de 2019 às 02:57, Guo, Yejun  escreveu:
>
> the info can be saved in dnn operand object without regenerating again and 
> again,
> and it is also needed for layer split/merge, and for memory reuse.
>
> to make things step by step, this patch just focuses on c code,
> the change within python script will be added later.
>
> Signed-off-by: Guo, Yejun 
> ---
>  libavfilter/dnn/dnn_backend_native.c   | 226 
> -
>  libavfilter/dnn/dnn_backend_native.h   |  54 +-
>  libavfilter/dnn/dnn_backend_native_layer_pad.c |  24 ++-
>  libavfilter/dnn/dnn_backend_native_layer_pad.h |   4 +-
>  tests/dnn/Makefile |   2 +-
>  tests/dnn/dnn-layer-pad-test.c |  60 +--
>  6 files changed, 236 insertions(+), 134 deletions(-)
>
> diff --git a/libavfilter/dnn/dnn_backend_native.c 
> b/libavfilter/dnn/dnn_backend_native.c
> index d52abc6..daa4f50 100644
> --- a/libavfilter/dnn/dnn_backend_native.c
> +++ b/libavfilter/dnn/dnn_backend_native.c
> @@ -30,77 +30,30 @@
>  static DNNReturnType set_input_output_native(void *model, DNNInputData 
> *input, const char *input_name, const char **output_names, uint32_t nb_output)
>  {
>  ConvolutionalNetwork *network = (ConvolutionalNetwork *)model;
> -InputParams *input_params;
> -ConvolutionalParams *conv_params;
> -DepthToSpaceParams *depth_to_space_params;
> -LayerPadParams *pad_params;
> -int cur_width, cur_height, cur_channels;
> -int32_t layer;
>
> -if (network->layers_num <= 0 || network->layers[0].type != INPUT){
> +if (network->layers_num <= 0 || network->operands_num <= 0)
>  return DNN_ERROR;
> -}
> -else{
> -input_params = (InputParams *)network->layers[0].params;
> -input_params->width = cur_width = input->width;
> -input_params->height = cur_height = input->height;
> -input_params->channels = cur_channels = input->channels;
> -if (input->data){
> -av_freep(&input->data);
> -}
> -av_assert0(input->dt == DNN_FLOAT);
> -network->layers[0].output = input->data = av_malloc(cur_height * 
> cur_width * cur_channels * sizeof(float));
> -if (!network->layers[0].output){
> -return DNN_ERROR;
> -}
> -}
> -
> -for (layer = 1; layer < network->layers_num; ++layer){
> -switch (network->layers[layer].type){
> -case CONV:
> -conv_params = (ConvolutionalParams 
> *)network->layers[layer].params;
> -if (conv_params->input_num != cur_channels){
> -return DNN_ERROR;
> -}
> -cur_channels = conv_params->output_num;
> -
> -if (conv_params->padding_method == VALID) {
> -int pad_size = (conv_params->kernel_size - 1) * 
> conv_params->dilation;
> -cur_height -= pad_size;
> -cur_width -= pad_size;
> -}
> -break;
> -case DEPTH_TO_SPACE:
> -depth_to_space_params = (DepthToSpaceParams 
> *)network->layers[layer].params;
> -if (cur_channels % (depth_to_space_params->block_size * 
> depth_to_space_params->block_size) != 0){
> -return DNN_ERROR;
> -}
> -cur_channels = cur_channels / (depth_to_space_params->block_size 
> * depth_to_space_params->block_size);
> -cur_height *= depth_to_space_params->block_size;
> -cur_width *= depth_to_space_params->block_size;
> -break;
> -case MIRROR_PAD:
> -pad_params = (LayerPadParams *)network->layers[layer].params;
> -cur_height = cur_height + pad_params->paddings[1][0] + 
> pad_params->paddings[1][1];
> -cur_width = cur_width + pad_params->paddings[2][0] + 
> pad_params->paddings[2][1];
> -cur_channels = cur_channels + pad_params->paddings[3][0] + 
> pad_params->paddings[3][1];
> -break;
> -default:
> -return DNN_ERROR;
> -}
> -if (network->layers[layer].output){
> -av_freep(&network->layers[layer].output);
> -}
> -
> -if (cur_height <= 0 || cur_width <= 0)
> -return DNN_ERROR;
>
> -network->layers[layer].output = av_malloc(cur_height * cur_width * 
> cur_channels * sizeof(float));
> -if (!network->layers[layer].output){
> -return DNN_ERROR;
> -}
> -}
> +av_assert0(input->dt == DNN_FLOAT);
> +
> +/**
> + * as the first step, suppose network->operands[0] is the input operand.
> + */
> +network->operands[0].dims[0] = 1;
> +network->operands[0].dims[1] = input->height;
> +network->operands[0].dims[2] = input->width;
> +network->operands[0].dims[3] = input->channels;
> +network->operands[0].type = DOT_INPUT;
> +network->operands[0].data_type = DNN_FLOAT;
> +network->operands[0].isNHWC = 1;
> +
> +av_freep(&network->o

[FFmpeg-devel] [PATCH V2 1/3] dnn: introduce dnn operand (in c code) to hold operand infos within network

2019-08-28 Thread Guo, Yejun
the info can be saved in dnn operand object without regenerating again and 
again,
and it is also needed for layer split/merge, and for memory reuse.

to make things step by step, this patch just focuses on c code,
the change within python script will be added later.

Signed-off-by: Guo, Yejun 
---
 libavfilter/dnn/dnn_backend_native.c   | 226 -
 libavfilter/dnn/dnn_backend_native.h   |  54 +-
 libavfilter/dnn/dnn_backend_native_layer_pad.c |  24 ++-
 libavfilter/dnn/dnn_backend_native_layer_pad.h |   4 +-
 tests/dnn/Makefile |   2 +-
 tests/dnn/dnn-layer-pad-test.c |  60 +--
 6 files changed, 236 insertions(+), 134 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_native.c 
b/libavfilter/dnn/dnn_backend_native.c
index d52abc6..daa4f50 100644
--- a/libavfilter/dnn/dnn_backend_native.c
+++ b/libavfilter/dnn/dnn_backend_native.c
@@ -30,77 +30,30 @@
 static DNNReturnType set_input_output_native(void *model, DNNInputData *input, 
const char *input_name, const char **output_names, uint32_t nb_output)
 {
 ConvolutionalNetwork *network = (ConvolutionalNetwork *)model;
-InputParams *input_params;
-ConvolutionalParams *conv_params;
-DepthToSpaceParams *depth_to_space_params;
-LayerPadParams *pad_params;
-int cur_width, cur_height, cur_channels;
-int32_t layer;
 
-if (network->layers_num <= 0 || network->layers[0].type != INPUT){
+if (network->layers_num <= 0 || network->operands_num <= 0)
 return DNN_ERROR;
-}
-else{
-input_params = (InputParams *)network->layers[0].params;
-input_params->width = cur_width = input->width;
-input_params->height = cur_height = input->height;
-input_params->channels = cur_channels = input->channels;
-if (input->data){
-av_freep(&input->data);
-}
-av_assert0(input->dt == DNN_FLOAT);
-network->layers[0].output = input->data = av_malloc(cur_height * 
cur_width * cur_channels * sizeof(float));
-if (!network->layers[0].output){
-return DNN_ERROR;
-}
-}
-
-for (layer = 1; layer < network->layers_num; ++layer){
-switch (network->layers[layer].type){
-case CONV:
-conv_params = (ConvolutionalParams *)network->layers[layer].params;
-if (conv_params->input_num != cur_channels){
-return DNN_ERROR;
-}
-cur_channels = conv_params->output_num;
-
-if (conv_params->padding_method == VALID) {
-int pad_size = (conv_params->kernel_size - 1) * 
conv_params->dilation;
-cur_height -= pad_size;
-cur_width -= pad_size;
-}
-break;
-case DEPTH_TO_SPACE:
-depth_to_space_params = (DepthToSpaceParams 
*)network->layers[layer].params;
-if (cur_channels % (depth_to_space_params->block_size * 
depth_to_space_params->block_size) != 0){
-return DNN_ERROR;
-}
-cur_channels = cur_channels / (depth_to_space_params->block_size * 
depth_to_space_params->block_size);
-cur_height *= depth_to_space_params->block_size;
-cur_width *= depth_to_space_params->block_size;
-break;
-case MIRROR_PAD:
-pad_params = (LayerPadParams *)network->layers[layer].params;
-cur_height = cur_height + pad_params->paddings[1][0] + 
pad_params->paddings[1][1];
-cur_width = cur_width + pad_params->paddings[2][0] + 
pad_params->paddings[2][1];
-cur_channels = cur_channels + pad_params->paddings[3][0] + 
pad_params->paddings[3][1];
-break;
-default:
-return DNN_ERROR;
-}
-if (network->layers[layer].output){
-av_freep(&network->layers[layer].output);
-}
-
-if (cur_height <= 0 || cur_width <= 0)
-return DNN_ERROR;
 
-network->layers[layer].output = av_malloc(cur_height * cur_width * 
cur_channels * sizeof(float));
-if (!network->layers[layer].output){
-return DNN_ERROR;
-}
-}
+av_assert0(input->dt == DNN_FLOAT);
+
+/**
+ * as the first step, suppose network->operands[0] is the input operand.
+ */
+network->operands[0].dims[0] = 1;
+network->operands[0].dims[1] = input->height;
+network->operands[0].dims[2] = input->width;
+network->operands[0].dims[3] = input->channels;
+network->operands[0].type = DOT_INPUT;
+network->operands[0].data_type = DNN_FLOAT;
+network->operands[0].isNHWC = 1;
+
+av_freep(&network->operands[0].data);
+network->operands[0].length = 
calculate_operand_data_length(&network->operands[0]);
+network->operands[0].data = av_malloc(network->operands[0].length);
+if (!network->operands[0].data)
+return DNN_ERROR;
 
+input->data = network->operands[0].data;
 ret