1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2020 |
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 |
|
|
/** |
22 |
|
|
* @file |
23 |
|
|
* DNN native backend implementation. |
24 |
|
|
*/ |
25 |
|
|
|
26 |
|
|
#include "libavutil/avassert.h" |
27 |
|
|
#include "dnn_backend_native_layer_avgpool.h" |
28 |
|
|
|
29 |
|
|
int ff_dnn_load_layer_avg_pool(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num) |
30 |
|
|
{ |
31 |
|
|
AvgPoolParams *avgpool_params; |
32 |
|
|
int dnn_size = 0; |
33 |
|
|
avgpool_params = av_malloc(sizeof(*avgpool_params)); |
34 |
|
|
if(!avgpool_params) |
35 |
|
|
return 0; |
36 |
|
|
|
37 |
|
|
avgpool_params->strides = (int32_t)avio_rl32(model_file_context); |
38 |
|
|
avgpool_params->padding_method = (int32_t)avio_rl32(model_file_context); |
39 |
|
|
avgpool_params->kernel_size = (int32_t)avio_rl32(model_file_context); |
40 |
|
|
dnn_size += 12; |
41 |
|
|
|
42 |
|
|
if (dnn_size > file_size || avgpool_params->kernel_size <= 0 || avgpool_params->strides <=0){ |
43 |
|
|
av_freep(&avgpool_params); |
44 |
|
|
return 0; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
layer->params = avgpool_params; |
48 |
|
|
layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context); |
49 |
|
|
layer->output_operand_index = (int32_t)avio_rl32(model_file_context); |
50 |
|
|
dnn_size += 8; |
51 |
|
|
|
52 |
|
|
if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) { |
53 |
|
|
return 0; |
54 |
|
|
} |
55 |
|
|
return dnn_size; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
2 |
int ff_dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes, |
59 |
|
|
int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
60 |
|
|
{ |
61 |
|
|
float *output; |
62 |
|
|
int height_end, width_end, height_radius, width_radius, output_height, output_width, kernel_area; |
63 |
|
2 |
int32_t input_operand_index = input_operand_indexes[0]; |
64 |
|
2 |
int number = operands[input_operand_index].dims[0]; |
65 |
|
2 |
int height = operands[input_operand_index].dims[1]; |
66 |
|
2 |
int width = operands[input_operand_index].dims[2]; |
67 |
|
2 |
int channel = operands[input_operand_index].dims[3]; |
68 |
|
2 |
const float *input = operands[input_operand_index].data; |
69 |
|
2 |
const AvgPoolParams *avgpool_params = parameters; |
70 |
|
|
|
71 |
|
2 |
int kernel_strides = avgpool_params->strides; |
72 |
|
2 |
int src_linesize = width * channel; |
73 |
|
2 |
DnnOperand *output_operand = &operands[output_operand_index]; |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* When padding_method = SAME, the tensorflow will only padding the hald number of 0 pxiels |
77 |
|
|
* except the remainders. |
78 |
|
|
* Eg: assuming the input height = 1080, the strides = 11, so the remainders = 1080 % 11 = 2 |
79 |
|
|
* and if ksize = 5: it will fill (5 - 2) >> 1 = 1 line before the first line of input image, |
80 |
|
|
* and 5 - 2 - 1 = 2 lines after the last line of input image. |
81 |
|
|
* and if ksize = 7: it will fill (7 - 2) >> 1 = 2 lines before the first line of input image, |
82 |
|
|
* and 7 - 2 - 2 = 3 lines after the last line of input image. |
83 |
|
|
*/ |
84 |
✓✓ |
2 |
if (avgpool_params->padding_method == SAME) { |
85 |
|
1 |
height_end = height; |
86 |
|
1 |
width_end = width; |
87 |
|
1 |
height_radius = avgpool_params->kernel_size - ((height - 1) % kernel_strides + 1); |
88 |
|
1 |
width_radius = avgpool_params->kernel_size - ((width - 1) % kernel_strides + 1); |
89 |
✓✗ |
1 |
height_radius = height_radius < 0 ? 0 : height_radius >> 1; |
90 |
✓✗ |
1 |
width_radius = width_radius < 0 ? 0 : width_radius >> 1; |
91 |
|
1 |
output_height = ceil(height / (kernel_strides * 1.0)); |
92 |
|
1 |
output_width = ceil(width / (kernel_strides * 1.0)); |
93 |
|
|
} else { |
94 |
✗✓ |
1 |
av_assert0(avgpool_params->padding_method == VALID); |
95 |
|
1 |
height_end = height - avgpool_params->kernel_size + 1; |
96 |
|
1 |
width_end = width - avgpool_params->kernel_size + 1; |
97 |
|
1 |
height_radius = 0; |
98 |
|
1 |
width_radius = 0; |
99 |
|
1 |
output_height = ceil((height - avgpool_params->kernel_size + 1) / (kernel_strides * 1.0)); |
100 |
|
1 |
output_width = ceil((width - avgpool_params->kernel_size + 1) / (kernel_strides * 1.0)); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
2 |
output_operand->dims[0] = number; |
104 |
|
2 |
output_operand->dims[1] = output_height; |
105 |
|
2 |
output_operand->dims[2] = output_width; |
106 |
|
|
// not support pooling in channel dimension now |
107 |
|
2 |
output_operand->dims[3] = channel; |
108 |
|
2 |
output_operand->data_type = operands[input_operand_index].data_type; |
109 |
|
2 |
output_operand->length = ff_calculate_operand_data_length(output_operand); |
110 |
✗✓ |
2 |
if (output_operand->length <= 0) { |
111 |
|
|
av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); |
112 |
|
|
return DNN_ERROR; |
113 |
|
|
} |
114 |
|
2 |
output_operand->data = av_realloc(output_operand->data, output_operand->length); |
115 |
✗✓ |
2 |
if (!output_operand->data) { |
116 |
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); |
117 |
|
|
return DNN_ERROR; |
118 |
|
|
} |
119 |
|
2 |
output = output_operand->data; |
120 |
|
|
|
121 |
✓✓ |
11 |
for (int y = 0; y < height_end; y += kernel_strides) { |
122 |
✓✓ |
59 |
for (int x = 0; x < width_end; x += kernel_strides) { |
123 |
✓✓ |
200 |
for (int n_channel = 0; n_channel < channel; ++n_channel) { |
124 |
|
150 |
output[n_channel] = 0.0; |
125 |
|
150 |
kernel_area = 0; |
126 |
✓✓ |
450 |
for (int kernel_y = 0; kernel_y < avgpool_params->kernel_size; ++kernel_y) { |
127 |
✓✓ |
900 |
for (int kernel_x = 0; kernel_x < avgpool_params->kernel_size; ++kernel_x) { |
128 |
|
|
float input_pel; |
129 |
|
600 |
int y_pos = y + (kernel_y - height_radius); |
130 |
|
600 |
int x_pos = x + (kernel_x - width_radius); |
131 |
✓✗✓✓ ✓✗✓✓
|
600 |
if (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) { |
132 |
|
63 |
input_pel = 0.0; |
133 |
|
|
} else { |
134 |
|
537 |
kernel_area++; |
135 |
|
537 |
input_pel = input[y_pos * src_linesize + x_pos * channel + n_channel]; |
136 |
|
|
} |
137 |
|
600 |
output[n_channel] += input_pel; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
150 |
output[n_channel] /= kernel_area; |
141 |
|
|
} |
142 |
|
50 |
output += channel; |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
2 |
return 0; |
147 |
|
|
} |