From patchwork Mon Apr 26 10:48:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 27426 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6109:0:0:0:0:0 with SMTP id v9csp1891iob; Mon, 26 Apr 2021 04:14:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMq4rBjS+vn0aiH5fWUJu4oa5j7AZe2OttAdKLlQ2szGp/OokbcRNOa8+Hxz5wvu2R8XjX X-Received: by 2002:a17:906:7c82:: with SMTP id w2mr18112655ejo.448.1619435650818; Mon, 26 Apr 2021 04:14:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619435650; cv=none; d=google.com; s=arc-20160816; b=X8jd+g9fMgvwUSA12qjO33eEYMDJHXUyxOQTQEVTc0mxQj4QHL1Q3sFbcvI/ojxTE4 pu5XsCygovD6U7BxCK7ng6/EK3I95tlhPhO6IicS+jIqMXvPp2MY4csYH409Hjs9vXOq TCWwai6NrejJZofWxedDU/dNBXFroetufDobc+/kWa06Cgve+LGh4OPzrGfrKfSFSA5T U+lUQMECMmmtvCXel/Xa9NFHdLXz0WX0VkCbPOW1h99uPb+BR92AmU9h5rLRjfMxQ+aT WQ+C7ONtb4GGRv9DhIuDOFT2Ka/yNbVa1YOcXobqxbPE9BcisCRjX0sniD4agJlmmuk0 HJxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=RMAQKBM9m6UnmKdQ3Qtg1mMVgveXl5J8Ep3jqjMiWRE=; b=eMrZzgxGE9s0mhuRlySPCgYmPIJtgtduuFku9D1LWsaC8vlHySJ7+Oi4+6H0K1oXeK JUgJTcNSsWCf3yJArV9SW0h6LdlA6k06w2/yaFYPzUSwY9h6E2xl8EC9ZPb47aUFeBFi bxPilnTykyuYxM1FehAy+YenR6FPAufm2VtalFWmkC2fakN2AunXzZNzH7cuF+A35asC MgBA7pkDGOwFHCe89mkblVE6/JXiLCj8eskI0nJXNXPdGjUMHDTTDw7gW0jJy77k4AfD +hfUZMGjr8W5Uk4eajdEfpy8y2Zkn7rOHg2sQpLKO5WudFDzZLJPa7iFqG7Z0elQ4BDG 4ceg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b="UB21Y/1y"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gt40si13089493ejc.577.2021.04.26.04.14.10; Mon, 26 Apr 2021 04:14:10 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b="UB21Y/1y"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 13A6F68982F; Mon, 26 Apr 2021 14:14:07 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4E5A7680396 for ; Mon, 26 Apr 2021 14:14:00 +0300 (EEST) Received: by mail-oi1-f175.google.com with SMTP id u16so38778444oiu.7 for ; Mon, 26 Apr 2021 04:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hGCyIPOAQg68+dcu36vw21TmD0oiX/IahsYcKKAPsS0=; b=UB21Y/1ynEwaRNtxBLeqgUB4p1a3zf0tCD7AheOq32T/5h6WC+Q7CZ31EgNLw2NHRg 70IQe8cszWInMtRfNSastadn/+zEiLcEy4PLimMv+kEEH+vwW6cOd7P31jwAcqpnFzvS Z+7V/ToEFyK0eNtpZkYD7ZaGbqplYZAM2DjwRfLoC/C1/Vi6cEcRwYrrCo8CnR7MSSK6 H/Pwon+yFUkwNYk0Hb/zqAP9EaEU50j66sDAKRW0p6Nidt0fNBh0rlQDaaRiKbS0rHsW d0waW49nx9a1RHeXD1k+xQN16meMtogdCKUaq2+BsVjHVkDCKgdhmpYTVGA7T+MUMyty LWGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hGCyIPOAQg68+dcu36vw21TmD0oiX/IahsYcKKAPsS0=; b=jY6S/XYwvg6b9yYDw9Di0hcS1d1YXVBxr7PkUKOtzcy8P5AySL687gEch+KllwiGJd mAdGCt6l2UCWaDhsMoFhh5bFEpNn07JdQYV48eA6ZRC81RtKKXbhgpEygSX7+u4/QjDi 1hciZmxHNjBBBarR7QNtZv7K+H/wLclWWFaiZdbVMd2MarO4QkZkagk2EYnll60DeoGI A2Y9T4TbwjK+4HQYSRRWoILXHWfaUkzIEIJmRE+S0x9JMrJbJZxNHBO1edF2wVqCwZke rGlrdLJ66IqQkIJr9ht5AzzMvMrjhMcQDMxcTA/I5fMChFhJldfxLD+gGm6XCKuZUgqi TyXw== X-Gm-Message-State: AOAM533m9zz5zUJq9gZGiJp95l1KS4qejq6iia+RKJdGE4F9Q8iaWVX8 axp8jS76vXim1E3nZXohcZTz3f9fw8c= X-Received: by 2002:a17:90a:4410:: with SMTP id s16mr20488275pjg.203.1619434152942; Mon, 26 Apr 2021 03:49:12 -0700 (PDT) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id u14sm1275592pjy.6.2021.04.26.03.49.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Apr 2021 03:49:12 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 26 Apr 2021 18:48:55 +0800 Message-Id: <1619434136-17760-5-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1619434136-17760-1-git-send-email-lance.lmwang@gmail.com> References: <1619434136-17760-1-git-send-email-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH 5/6] avfilter/dnn/dnn_backend_tf: simplify the code with ff_hex_to_data X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 37nQ1oW8+Dpf From: Limin Wang please use tools/python/tf_sess_config.py to get the sess_config after that. note the byte order of session config is the normal order. Signed-off-by: Limin Wang --- libavfilter/dnn/dnn_backend_tf.c | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index fb799d2..0084157 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -28,6 +28,7 @@ #include "dnn_backend_native_layer_conv2d.h" #include "dnn_backend_native_layer_depth2space.h" #include "libavformat/avio.h" +#include "libavformat/internal.h" #include "libavutil/avassert.h" #include "../internal.h" #include "dnn_backend_native_layer_pad.h" @@ -202,35 +203,21 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename TF_SessionOptions *sess_opts; const TF_Operation *init_op; uint8_t *sess_config = NULL; - int sess_config_length = 0; + int sess_config_length = ff_hex_to_data(NULL, tf_model->ctx.options.sess_config + 2); // prepare the sess config data if (tf_model->ctx.options.sess_config != NULL) { /* tf_model->ctx.options.sess_config is hex to present the serialized proto required by TF_SetConfig below, so we need to first generate the serialized - proto in a python script, the following is a script example to generate - serialized proto which specifies one GPU, we can change the script to add - more options. - - import tensorflow as tf - gpu_options = tf.GPUOptions(visible_device_list='0') - config = tf.ConfigProto(gpu_options=gpu_options) - s = config.SerializeToString() - b = ''.join("%02x" % int(ord(b)) for b in s[::-1]) - print('0x%s' % b) - - the script output looks like: 0xab...cd, and then pass 0xab...cd to sess_config. + proto in a python script, tools/python/tf_sess_config.py is a script example + to generate the configs of sess_config. */ - char tmp[3]; - tmp[2] = '\0'; - if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) { av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n"); return DNN_ERROR; } - sess_config_length = strlen(tf_model->ctx.options.sess_config); if (sess_config_length % 2 != 0) { av_log(ctx, AV_LOG_ERROR, "the length of sess_config is not even (%s), " "please re-generate the config.\n", @@ -238,21 +225,12 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename return DNN_ERROR; } - sess_config_length -= 2; //ignore the first '0x' - sess_config_length /= 2; //get the data length in byte - - sess_config = av_malloc(sess_config_length); + sess_config = av_mallocz(sess_config_length + AV_INPUT_BUFFER_PADDING_SIZE); if (!sess_config) { av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n"); return DNN_ERROR; } - - for (int i = 0; i < sess_config_length; i++) { - int index = 2 + (sess_config_length - 1 - i) * 2; - tmp[0] = tf_model->ctx.options.sess_config[index]; - tmp[1] = tf_model->ctx.options.sess_config[index + 1]; - sess_config[i] = strtol(tmp, NULL, 16); - } + ff_hex_to_data(sess_config, tf_model->ctx.options.sess_config + 2); } graph_def = read_graph(model_filename);