History

avfilter/dnn/dnn_backend_tf: simplify the code with ff_hex_to_data

please use tools/python/tf_sess_config.py to get the sess_config after that. note the byte order of session config is in normal order. bump the MICRO version for the config change.

Limin Wang committed 5 years ago

Commit f183d651 parent 4e8d2243 files · +54 −35Browse files

Unified · Split

3 files
  1. libavfilter/dnn/dnn_backend_tf.c +8 −34
  2. libavfilter/version.h +1 −1
  3. tools/python/tf_sess_config.py +45 −0
libavfilter/dnn/dnn_backend_tf.c+8−34
@@ -28,6 +28,7 @@ · 27 unmodified lines
28#include "dnn_backend_native_layer_conv2d.h"28#include "dnn_backend_native_layer_conv2d.h"
29#include "dnn_backend_native_layer_depth2space.h"29#include "dnn_backend_native_layer_depth2space.h"
30#include "libavformat/avio.h"30#include "libavformat/avio.h"
31#include "libavformat/internal.h"
31#include "libavutil/avassert.h"32#include "libavutil/avassert.h"
32#include "../internal.h"33#include "../internal.h"
33#include "dnn_backend_native_layer_pad.h"34#include "dnn_backend_native_layer_pad.h"
@@ -206,53 +207,26 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 172 unmodified lines
206 207
207 // prepare the sess config data208 // prepare the sess config data
208 if (tf_model->ctx.options.sess_config != NULL) {209 if (tf_model->ctx.options.sess_config != NULL) {
210 const char *config;
209 /*211 /*
210 tf_model->ctx.options.sess_config is hex to present the serialized proto212 tf_model->ctx.options.sess_config is hex to present the serialized proto
211 required by TF_SetConfig below, so we need to first generate the serialized213 required by TF_SetConfig below, so we need to first generate the serialized
212 proto in a python script, the following is a script example to generate214 proto in a python script, tools/python/tf_sess_config.py is a script example
213 serialized proto which specifies one GPU, we can change the script to add215 to generate the configs of sess_config.
214 more options.
215
216 import tensorflow as tf
217 gpu_options = tf.GPUOptions(visible_device_list='0')
218 config = tf.ConfigProto(gpu_options=gpu_options)
219 s = config.SerializeToString()
220 b = ''.join("%02x" % int(ord(b)) for b in s[::-1])
221 print('0x%s' % b)
222
223 the script output looks like: 0xab...cd, and then pass 0xab...cd to sess_config.
224 */216 */
225 char tmp[3];
226 tmp[2] = '\0';
227
228 if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) {217 if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) {
229 av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n");218 av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n");
230 return DNN_ERROR;219 return DNN_ERROR;
231 }220 }
221 config = tf_model->ctx.options.sess_config + 2;
222 sess_config_length = ff_hex_to_data(NULL, config);
232 223
233 sess_config_length = strlen(tf_model->ctx.options.sess_config);224 sess_config = av_mallocz(sess_config_length + AV_INPUT_BUFFER_PADDING_SIZE);
234 if (sess_config_length % 2 != 0) {
235 av_log(ctx, AV_LOG_ERROR, "the length of sess_config is not even (%s), "
236 "please re-generate the config.\n",
237 tf_model->ctx.options.sess_config);
238 return DNN_ERROR;
239 }
240
241 sess_config_length -= 2; //ignore the first '0x'
242 sess_config_length /= 2; //get the data length in byte
243
244 sess_config = av_malloc(sess_config_length);
245 if (!sess_config) {225 if (!sess_config) {
246 av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");226 av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");
247 return DNN_ERROR;227 return DNN_ERROR;
248 }228 }
249 229 ff_hex_to_data(sess_config, config);
250 for (int i = 0; i < sess_config_length; i++) {
251 int index = 2 + (sess_config_length - 1 - i) * 2;
252 tmp[0] = tf_model->ctx.options.sess_config[index];
253 tmp[1] = tf_model->ctx.options.sess_config[index + 1];
254 sess_config[i] = strtol(tmp, NULL, 16);
255 }
256 }230 }
257 231
258 graph_def = read_graph(model_filename);232 graph_def = read_graph(model_filename);
libavfilter/version.h+1−1
@@ -31,7 +31,7 @@ · 30 unmodified lines
31 31
32#define LIBAVFILTER_VERSION_MAJOR 832#define LIBAVFILTER_VERSION_MAJOR 8
33#define LIBAVFILTER_VERSION_MINOR 033#define LIBAVFILTER_VERSION_MINOR 0
34#define LIBAVFILTER_VERSION_MICRO 10034#define LIBAVFILTER_VERSION_MICRO 101
35 35
36 36
37#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \37#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
tools/python/tf_sess_config.py+45−0
@@ -0,0 +1,45 @@
1# Copyright (c) 2021
2#
3# This file is part of FFmpeg.
4#
5# FFmpeg is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# FFmpeg is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with FFmpeg; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18# ==============================================================================
19
20# verified with Python 3.6.8 on CentOS 7.2
21import tensorflow as tf
22
23visible_device_list = '0' # use , separator for more GPUs like '0, 1'
24per_process_gpu_memory_fraction = 0.9 # avoid out of memory
25intra_op_parallelism_threads = 2 # default in tensorflow
26inter_op_parallelism_threads = 5 # default in tensorflow
27
28gpu_options = tf.compat.v1.GPUOptions(
29 per_process_gpu_memory_fraction = per_process_gpu_memory_fraction,
30 visible_device_list = visible_device_list,
31 allow_growth = True)
32
33config = tf.compat.v1.ConfigProto(
34 allow_soft_placement = True,
35 log_device_placement = False,
36 intra_op_parallelism_threads = intra_op_parallelism_threads,
37 inter_op_parallelism_threads = inter_op_parallelism_threads,
38 gpu_options = gpu_options)
39
40s = config.SerializeToString()
41# print(list(map(hex, s))) # print by json if need
42
43print('a serialized protobuf string for TF_SetConfig, note the byte order is in normal order.')
44b = ''.join(format(b,'02x') for b in s)
45print('0x%s' % b) # print by hex format

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