History

dnn: add layer pad which is equivalent to tf.pad

the reason to add this layer first is that vf_sr uses it in its tensorflow model, and the next plan is to update the python script to convert tf.pad into native model.

Guo, Yejun committed 7 years ago

Commit df8db341 parent 630ea6b3 files · +252 −0Browse files

Showing the changes to libavfilter (3 of 3 files). Show all

Unified · Split

3 files
  1. libavfilter/dnn/Makefile +1 −0
  2. libavfilter/dnn/dnn_backend_native_layer_pad.c +211 −0
  3. libavfilter/dnn/dnn_backend_native_layer_pad.h +40 −0
libavfilter/dnn/Makefile+1−0
@@ -1,5 +1,6 @@
11 OBJS-$(CONFIG_DNN) += dnn/dnn_interface.o
22 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native.o
3+OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_pad.o
34
45 DNN-OBJS-$(CONFIG_LIBTENSORFLOW) += dnn/dnn_backend_tf.o
56
libavfilter/dnn/dnn_backend_native_layer_pad.c+211−0
@@ -0,0 +1,211 @@
1+/*
2+ * Copyright (c) 2019 Guo Yejun
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 <string.h>
22+#include "libavutil/avassert.h"
23+#include "dnn_backend_native_layer_pad.h"
24+
25+static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
26+{
27+ if (mode == LPMP_SYMMETRIC) {
28+ return (2 * paddings - 1 - given);
29+ } else if (mode == LPMP_REFLECT) {
30+ return (2 * paddings - given);
31+ } else {
32+ av_assert0(!"should not reach here");
33+ return 0;
34+ }
35+}
36+
37+static int after_get_buddy(int given, int border, LayerPadModeParam mode)
38+{
39+ if (mode == LPMP_SYMMETRIC) {
40+ int offset = given - border;
41+ return (border - 1 - offset);
42+ } else if (mode == LPMP_REFLECT) {
43+ int offset = given - border;
44+ return (border - 2 - offset);
45+ } else {
46+ av_assert0(!"should not reach here");
47+ return 0;
48+ }
49+}
50+
51+void dnn_execute_layer_pad(const float *input, float *output, const LayerPadParams *params, int number, int height, int width, int channel)
52+{
53+ int32_t before_paddings;
54+ int32_t after_paddings;
55+
56+ // suppose format is <N, H, W, C>
57+ int new_number = number + params->paddings[0][0] + params->paddings[0][1];
58+ int new_height = height + params->paddings[1][0] + params->paddings[1][1];
59+ int new_width = width + params->paddings[2][0] + params->paddings[2][1];
60+ int new_channel = channel + params->paddings[3][0] + params->paddings[3][1];
61+
62+ int c_stride = channel;
63+ int wc_stride = c_stride * width;
64+ int hwc_stride = wc_stride * height;
65+
66+ int new_c_stride = new_channel;
67+ int new_wc_stride = new_c_stride * new_width;
68+ int new_hwc_stride = new_wc_stride * new_height;
69+
70+ // copy the original data
71+ for (int n = 0; n < number; n++) {
72+ for (int h = 0; h < height; h++) {
73+ for (int w = 0; w < width; w++) {
74+ const float *src = input + n * hwc_stride + h * wc_stride + w * c_stride;
75+ float *dst = output + (n + params->paddings[0][0]) * new_hwc_stride
76+ + (h + params->paddings[1][0]) * new_wc_stride
77+ + (w + params->paddings[2][0]) * new_c_stride
78+ + params->paddings[3][0];
79+ memcpy(dst, src, channel * sizeof(float));
80+ }
81+ }
82+ }
83+
84+ // handle the first dimension
85+ before_paddings = params->paddings[0][0];
86+ after_paddings = params->paddings[0][1];
87+ for (int n = 0; n < before_paddings; n++) {
88+ float *dst = output + n * new_hwc_stride;
89+ if (params->mode == LPMP_CONSTANT) {
90+ for (int i = 0; i < new_hwc_stride; i++) {
91+ dst[i] = params->constant_values;
92+ }
93+ }
94+ else {
95+ int buddy = before_get_buddy(n, before_paddings, params->mode);
96+ float *src = output + buddy * new_hwc_stride;
97+ memcpy(dst, src, new_hwc_stride * sizeof(float));
98+ }
99+ }
100+ for (int n = 0; n < after_paddings; n++) {
101+ int given = number + before_paddings + n;
102+ float *dst = output + given * new_hwc_stride;
103+ if (params->mode == LPMP_CONSTANT) {
104+ for (int i = 0; i < new_hwc_stride; i++) {
105+ dst[i] = params->constant_values;
106+ }
107+ } else {
108+ int buddy = after_get_buddy(given, number + before_paddings, params->mode);
109+ float *src = output + buddy * new_hwc_stride;
110+ memcpy(dst, src, new_hwc_stride * sizeof(float));
111+ }
112+ }
113+
114+ // handle the second dimension
115+ before_paddings = params->paddings[1][0];
116+ after_paddings = params->paddings[1][1];
117+ for (int n = 0; n < new_number; n++) {
118+ float *start = output + n * new_hwc_stride;
119+ for (int h = 0; h < before_paddings; h++) {
120+ float *dst = start + h * new_wc_stride;
121+ if (params->mode == LPMP_CONSTANT) {
122+ for (int i = 0; i < new_wc_stride; i++) {
123+ dst[i] = params->constant_values;
124+ }
125+ } else {
126+ int buddy = before_get_buddy(h, before_paddings, params->mode);
127+ float *src = start + buddy * new_wc_stride;
128+ memcpy(dst, src, new_wc_stride * sizeof(float));
129+ }
130+ }
131+ for (int h = 0; h < after_paddings; h++) {
132+ int given = height + before_paddings + h;
133+ float *dst = start + given * new_wc_stride;
134+ if (params->mode == LPMP_CONSTANT) {
135+ for (int i = 0; i < new_wc_stride; i++) {
136+ dst[i] = params->constant_values;
137+ }
138+ } else {
139+ int buddy = after_get_buddy(given, height + before_paddings, params->mode);
140+ float *src = start + buddy * new_wc_stride;
141+ memcpy(dst, src, new_wc_stride * sizeof(float));
142+ }
143+ }
144+ }
145+
146+ // handle the third dimension
147+ before_paddings = params->paddings[2][0];
148+ after_paddings = params->paddings[2][1];
149+ for (int n = 0; n < new_number; n++) {
150+ for (int h = 0; h < new_height; h++) {
151+ float *start = output + n * new_hwc_stride + h * new_wc_stride;
152+ for (int w = 0; w < before_paddings; w++) {
153+ float *dst = start + w * new_c_stride;
154+ if (params->mode == LPMP_CONSTANT) {
155+ for (int i = 0; i < new_c_stride; i++) {
156+ dst[i] = params->constant_values;
157+ }
158+ } else {
159+ int buddy = before_get_buddy(w, before_paddings, params->mode);
160+ float *src = start + buddy * new_c_stride;
161+ memcpy(dst, src, new_c_stride * sizeof(float));
162+ }
163+ }
164+ for (int w = 0; w < after_paddings; w++) {
165+ int given = width + before_paddings + w;
166+ float *dst = start + given * new_c_stride;
167+ if (params->mode == LPMP_CONSTANT) {
168+ for (int i = 0; i < new_c_stride; i++) {
169+ dst[i] = params->constant_values;
170+ }
171+ } else {
172+ int buddy = after_get_buddy(given, width + before_paddings, params->mode);
173+ float *src = start + buddy * new_c_stride;
174+ memcpy(dst, src, new_c_stride * sizeof(float));
175+ }
176+ }
177+ }
178+ }
179+
180+ // handle the fourth dimension
181+ before_paddings = params->paddings[3][0];
182+ after_paddings = params->paddings[3][1];
183+ for (int n = 0; n < new_number; n++) {
184+ for (int h = 0; h < new_height; h++) {
185+ for (int w = 0; w < new_width; w++) {
186+ float *start = output + n * new_hwc_stride + h * new_wc_stride + w * new_c_stride;
187+ for (int c = 0; c < before_paddings; c++) {
188+ float *dst = start + c;
189+ if (params->mode == LPMP_CONSTANT) {
190+ *dst = params->constant_values;
191+ } else {
192+ int buddy = before_get_buddy(c, before_paddings, params->mode);
193+ float *src = start + buddy;
194+ *dst = *src;
195+ }
196+ }
197+ for (int c = 0; c < after_paddings; c++) {
198+ int given = channel + before_paddings + c;
199+ float *dst = start + given;
200+ if (params->mode == LPMP_CONSTANT) {
201+ *dst = params->constant_values;
202+ } else {
203+ int buddy = after_get_buddy(given, channel + before_paddings, params->mode);
204+ float *src = start + buddy;
205+ *dst = *src;
206+ }
207+ }
208+ }
209+ }
210+ }
211+}
libavfilter/dnn/dnn_backend_native_layer_pad.h+40−0
@@ -0,0 +1,40 @@
1+/*
2+ * Copyright (c) 2019 Guo Yejun
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+ * layer pad (equivalent to tf.pad) for native backend.
24+ */
25+#ifndef AVFILTER_DNN_DNN_BACKEND_NATIVE_LAYER_PAD_H
26+#define AVFILTER_DNN_DNN_BACKEND_NATIVE_LAYER_PAD_H
27+
28+#include <stdint.h>
29+
30+typedef enum {LPMP_CONSTANT, LPMP_REFLECT, LPMP_SYMMETRIC} LayerPadModeParam;
31+
32+typedef struct LayerPadParams{
33+ int32_t paddings[4][2];
34+ LayerPadModeParam mode;
35+ float constant_values;
36+} LayerPadParams;
37+
38+void dnn_execute_layer_pad(const float *input, float *output, const LayerPadParams *params, int number, int height, int width, int channel);
39+
40+#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t