From patchwork Tue Aug 20 08:50:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 14606 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id AE0754475F4 for ; Tue, 20 Aug 2019 11:54:28 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 92D9C68AAAA; Tue, 20 Aug 2019 11:54:28 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C04EF68AA99 for ; Tue, 20 Aug 2019 11:54:21 +0300 (EEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Aug 2019 01:54:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,408,1559545200"; d="scan'208";a="180631609" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by orsmga003.jf.intel.com with ESMTP; 20 Aug 2019 01:54:18 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Aug 2019 16:50:27 +0800 Message-Id: <1566291027-12811-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 2/3] dnn: change .model file format to put layer number at the end of file X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" currently, the layer number is at the beginning of the .model file, so we have to scan twice in python script, the first scan to get the layer number. Only one scan needed after put the layer number at the end of .model file. Signed-off-by: Guo, Yejun --- libavfilter/dnn/dnn_backend_native.c | 2 ++ tools/python/convert_from_tensorflow.py | 12 +----------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_native.c b/libavfilter/dnn/dnn_backend_native.c index 78227a5..0ba4e44 100644 --- a/libavfilter/dnn/dnn_backend_native.c +++ b/libavfilter/dnn/dnn_backend_native.c @@ -93,8 +93,10 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename) } model->model = (void *)network; + avio_seek(model_file_context, file_size - 4, SEEK_SET); network->layers_num = (int32_t)avio_rl32(model_file_context); dnn_size = 4; + avio_seek(model_file_context, 0, SEEK_SET); network->layers = av_mallocz(network->layers_num * sizeof(Layer)); if (!network->layers){ diff --git a/tools/python/convert_from_tensorflow.py b/tools/python/convert_from_tensorflow.py index 34454b8..cbc76a9 100644 --- a/tools/python/convert_from_tensorflow.py +++ b/tools/python/convert_from_tensorflow.py @@ -129,15 +129,6 @@ class TFConverter: self.converted_nodes.add(node.name) - def generate_layer_number(self): - # in current hard code implementation, the layer number is the first data written to the native model file - # it is not easy to know it at the beginning time in the general converter, so first do a dry run for compatibility - # will be refined later. - with open('/tmp/tmp.model', 'wb') as f: - self.dump_layers_to_file(f) - self.converted_nodes.clear() - - def dump_layers_to_file(self, f): for node in self.nodes: if node.name in self.converted_nodes: @@ -157,10 +148,9 @@ class TFConverter: def dump_to_file(self): - self.generate_layer_number() with open(self.outfile, 'wb') as f: - np.array([self.layer_number], dtype=np.uint32).tofile(f) self.dump_layers_to_file(f) + np.array([self.layer_number], dtype=np.uint32).tofile(f) def generate_name_node_dict(self):