From 6be7f350a7bdd0f42caca33bc410c6cc7d280d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:50:38 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20IEG=5FID2049=5Ffor=5FA?= =?UTF-8?q?CL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From 90d12a7ebd34bcb74f1415558f730c0721572f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:51:27 +0000 Subject: [PATCH 2/8] readme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 虽千万人吾往矣 <1291632293@qq.com> --- .../contrib/cv/IEG_ID2049_for_ACL/README.md | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md new file mode 100644 index 000000000..9029e469d --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md @@ -0,0 +1,82 @@ +

概述

+ +Attention-OCR是一个基于卷积神经网络CNN、循环神经网络RNN以及一种新颖的注意机制的自然场景文本检测识别网络。 + +本文提出了一个高效率的噪声标签训练方法。 + + +- 参考论文: +[Distilling Effective Supervision from Severe Label Noise](https://arxiv.org/pdf/1910.00701.pdf), + CVPR2020 +- 参考实现: +https://github.com/google-research/google-research/tree/master/ieg + +- 适配昇腾 AI 处理器的实现: +https://gitee.com/lwrstudy/ModelZoo-TensorFlow/tree/master/TensorFlow/contrib/cv/Slot-Attention_ID2028_for_TensorFlow + + +

原始模型

+ +obs地址:obs://lwr-npu/tl/checkp + + +步骤一:将checkpoint.ckpt-85329转化成IEG.pb +通过代码freeze_graph将ckpt转成pb + + + +

pb模型

+ +``` +IEG.pb +``` +obs地址:obs://lwr-npu/tl/IEG.pb + + +

om模型

+ +转IEG.pb到hpiegmodel.om + +使用ATC模型转换工具进行模型转换时可以参考如下指令: + +``` +atc --model=./IEG.pb --input_shape=“input:100,3,32,32” --framework=3 --output=hpiegmodel --soc_version=Ascend910A --precision_mode=force_fp32 --op_select_implmode=high_precision +``` + +成功转化成hpiegmodel.om + +hpiegmodel.om的obs地址:obs://lwr-npu/tl/hpiegmodel.om + + +

数据集转换bin

+1.使用convertimg.py将数据集转为png,代码中的src为数据集源路径,dest为png数据集输出路径 +2.参考 https://gitee.com/ascend/tools/tree/master/img2bin, 获取msame推理工具及使用方法。 +这里对该代码库中的img2bin进行了少量修改,加入了数据预处理 +使用img2bin.py将png数据集转为bin格式,img2bin.py为tools-master中的代码文件, +``` +python3 img2bin.py -i /mnt/home/test_user01/toimg -w 32 -h 32 -f RGB -a NHWC -t float32 -c [1,1,1] -o ./imgout +``` +3.使用merge.py将bin格式的数据集合并为一个batch +转换后的bin文件见obs地址:obs://lwr-npu/tl/newinputbin + + + + +

使用msame工具推理

+ +参考 https://gitee.com/ascend/tools/tree/master/msame, 获取msame推理工具及使用方法。 + +使用msame推理工具,参考如下命令,发起推理测试: + +``` +./msame --model "/mnt/home/test_user01/hpiegmodel.om" --input "/home/test_user01/slottl/tools-master/img2bin/newinput.bin" --output "./iegout/" --outfmt TXT --loop 1 +``` + + + + + + + + + -- Gitee From 9b81ba917af3071fca4fdfbd7d64170315fffa2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:52:07 +0000 Subject: [PATCH 3/8] update ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 虽千万人吾往矣 <1291632293@qq.com> --- ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md index 9029e469d..fe293069f 100644 --- a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md @@ -1,7 +1,4 @@

概述

- -Attention-OCR是一个基于卷积神经网络CNN、循环神经网络RNN以及一种新颖的注意机制的自然场景文本检测识别网络。 - 本文提出了一个高效率的噪声标签训练方法。 -- Gitee From 29d6a7eba03801145efa8a684039afc1f11e35a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:53:37 +0000 Subject: [PATCH 4/8] update ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 虽千万人吾往矣 <1291632293@qq.com> --- ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md index fe293069f..6559e5cdb 100644 --- a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md @@ -9,7 +9,7 @@ https://github.com/google-research/google-research/tree/master/ieg - 适配昇腾 AI 处理器的实现: -https://gitee.com/lwrstudy/ModelZoo-TensorFlow/tree/master/TensorFlow/contrib/cv/Slot-Attention_ID2028_for_TensorFlow +https://gitee.com/lwrstudy/ModelZoo-TensorFlow/tree/master/TensorFlow/contrib/cv/IEG_ID2049_for_TensorFlow

原始模型

-- Gitee From 0e0dc8096494be71a52de137b06b0755146dd4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:54:53 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 虽千万人吾往矣 <1291632293@qq.com> --- .../cv/IEG_ID2049_for_ACL/convertimg.py | 97 ++++++ .../contrib/cv/IEG_ID2049_for_ACL/copyimg.py | 6 + .../cv/IEG_ID2049_for_ACL/frozen_graph.py | 58 ++++ .../contrib/cv/IEG_ID2049_for_ACL/img2bin.py | 310 ++++++++++++++++++ .../contrib/cv/IEG_ID2049_for_ACL/merge.py | 16 + 5 files changed, 487 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/convertimg.py create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/copyimg.py create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/frozen_graph.py create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/img2bin.py create mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/merge.py diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/convertimg.py b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/convertimg.py new file mode 100644 index 000000000..3ab59bf81 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/convertimg.py @@ -0,0 +1,97 @@ +import numpy as np +import os +from npu_bridge.npu_init import * + +import collections +import math +import sys + +from absl import flags +from PIL import Image +from six.moves import cPickle +import sklearn.metrics as sklearn_metrics +import tensorflow.compat.v1 as tf + +def load_batch(fpath, label_key='labels'): + """Internal utility for parsing CIFAR data. + + Args: + fpath: path the file to parse. + label_key: key for label data in the retrieve dictionary. + + Returns: + A tuple `(data, labels)`. + """ + + with tf.io.gfile.GFile(fpath, 'rb') as f: + if sys.version_info < (3,): + d = cPickle.load(f) + else: + d = cPickle.load(f, encoding='bytes') + # decode utf8 + d_decoded = {} + for k, v in d.items(): + d_decoded[k.decode('utf8')] = v + d = d_decoded + data = d['data'] + labels = d[label_key] + + data = data.reshape(data.shape[0], 3, 32, 32) + return data, labels + +def cifar10_load_data(root): + """Loads CIFAR10 dataset. + + Args: + root: path that saves data file. + + Returns: + Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. + """ + if not root: + return tf.keras.datasets.cifar10.load_data() + + num_train_samples = 50000 + + x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8') + y_train = np.empty((num_train_samples,), dtype='uint8') + + for i in range(1, 6): + fpath = os.path.join(root, 'data_batch_' + str(i)) + (x_train[(i - 1) * 10000:i * 10000, :, :, :], + y_train[(i - 1) * 10000:i * 10000]) = load_batch(fpath) + + fpath = os.path.join(root, 'test_batch') + x_test, y_test = load_batch(fpath) + + y_train = np.reshape(y_train, (len(y_train), 1)) + y_test = np.reshape(y_test, (len(y_test), 1)) + + x_train = x_train.transpose(0, 2, 3, 1) + x_test = x_test.transpose(0, 2, 3, 1) + + x_test = x_test.astype(x_train.dtype) + y_test = y_test.astype(y_train.dtype) + + return (x_train, y_train), (x_test, y_test) + + + + + +def convertimg(src,dest): + (x_train, y_train), (x_test,y_test)=cifar10_load_data(src) + print("xtest的shape",str(x_test.shape)) + print("xtrain的shape", str(x_train.shape)) + print("ytest的shape",str(y_test.shape)) + print("ytrain的shape", str(y_train.shape)) + + for i in range(100): + im = Image.fromarray(x_test[i]) + im.save(dest+"/cifar10_test_"+str(i)+".png") + + +if __name__ == '__main__': + src="./data/cifar-10-python/cifar-10-batches-py" + dest="./toimg" + convertimg(src=src,dest=dest) diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/copyimg.py b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/copyimg.py new file mode 100644 index 000000000..0c61aa740 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/copyimg.py @@ -0,0 +1,6 @@ +import os +import shutil +filelist=os.listdir("/home/test_user01/slottl/tools-master/img2bin/imgout/") +for i in range(100): + filepath=os.path.join("/home/test_user01/slottl/tools-master/img2bin/imgout/", filelist[i]) + shutil.copy(filepath,"./partimg") \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/frozen_graph.py b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/frozen_graph.py new file mode 100644 index 000000000..7849b887f --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/frozen_graph.py @@ -0,0 +1,58 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +import tensorflow as tf +from tensorflow.python.tools import freeze_graph +from tensorflow.python.framework import graph_util +from nets import nets_factory +import argparse +from models.basemodel import create_network +def parse_args(): + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('--ckpt_path', default="", + help="""set checkpoint path""") + args, unknown_args = parser.parse_known_args() + if len(unknown_args) > 0: + for bad_arg in unknown_args: + print("ERROR: Unknown command line arg: %s" % bad_arg) + raise ValueError("Invalid command line arg(s)") + return args + +def main(): + args = parse_args() + tf.reset_default_graph() + # set inputs node + inputs = tf.placeholder(tf.float32, shape=[None, 224, 224, 3], name="input") + # create inference graph + # network_fn = nets_factory.get_network_fn('mobilenet_v1', num_classes=1001, weight_decay=0.0, is_training=False) + # logits, end_points = network_fn(inputs, reuse=tf.AUTO_REUSE) + net=create_network("wrn28-10", 10) + logits=net(inputs, name='model', reuse=tf.AUTO_REUSE, training=True) + with tf.Session() as sess: + tf.train.write_graph(sess.graph_def, './', 'model.pb') + freeze_graph.freeze_graph( + input_graph='model.pb', + input_saver='', + input_binary=False, + input_checkpoint=args.ckpt_path, + output_node_names='iegoutput', # graph outputs node + restore_op_name='save/restore_all', + filename_tensor_name='save/Const:0', + output_graph='IEG.pb', # graph outputs name + clear_devices=False, + initializer_nodes='') + print("done") + +if __name__ == '__main__': + main() diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/img2bin.py b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/img2bin.py new file mode 100644 index 000000000..b78efdd8f --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/img2bin.py @@ -0,0 +1,310 @@ +# -*- coding: UTF-8 -*- +# +# ======================================================================= +# +# Copyright (C) 2018, Hisilicon Technologies Co., Ltd. All Rights Reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1 Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2 Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3 Neither the names of the copyright holders nor the names of the +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# ======================================================================= +from __future__ import print_function +import argparse +import os +import sys +import json +# import getpass + +IMG_EXT = ['.jpg', '.JPG', '.png', '.PNG', '.bmp', '.BMP', '.jpeg', '.JPEG'] + +try: + import cv2 as cv +except ImportError: + if sys.version_info.major == 2: + confirm = raw_input("[info] Begin to install opencv-python, input[Y/N]:") + else: + confirm = input("[info] Begin to install opencv-python, input[Y/N]:") + + if confirm == 'Y' or confirm == 'y': + print('[info] Starting to install opencv-python...') + if sys.version_info.major == 2: + import commands + retu, output = commands.getstatusoutput("sudo yum install -y opencv-python") + if retu != 0: + retu, output = commands.getstatusoutput("sudo apt-get install -y python-opencv") + if retu != 0: + print('[ERROR] install opencv-python failed,please check env.') + exit(0) + else: + retu = os.system('sudo python3 -m pip install opencv-python') + if retu != 0: + print('[ERROR] install opencv-python failed,please check env.') + exit(0) + + import cv2 as cv + else: + print("[info] The installation has been cancled.") + exit(0) + +try: + import numpy as np +except ImportError: + if sys.version_info.major == 2: + os.system('pip2 install numpy') + else: + os.system('python3 -m pip install numpy') + import numpy as np + +def get_args(): + parser = argparse.ArgumentParser( + conflict_handler='resolve', + description='''eg1: python2 img2bin.py + -i ./images -w 416 -h 416 -f BGR -a NHWC -t uint8 -m [104,117,123] -c [1,1,1] -o ./out + eg2: python2 img2bin.py -i ./test.txt -t uint8 -o ./out''') + parser.add_argument('-i', '--input', required=True, type=str, \ + help='folder of input image or file of other input.') + parser.add_argument('-w', '--width', required=True, type=int, \ + help='resized image width before inference.') + parser.add_argument('-h', '--height', required=True, type=int, \ + help='resized image height before inference.') + parser.add_argument('-f', '--output_image_format', default='BGR', type=str, \ + help='output image format in (BGR/RGB/YUV/GRAY).') + parser.add_argument('-a', '--output_format', default='NCHW', type=str, \ + help='output format in (NCHW/NHWC).') + parser.add_argument('-t', '--output_type', required=True, type=str, \ + help='output type in (float32/uint8/int32/uint32).') + parser.add_argument('-m', '--mean', default='[0, 0, 0]', \ + help='reduce mean for each channel.') + parser.add_argument('-c', '--coefficient', default='[1, 1, 1]', \ + help='multiplying coefficients for each channel.') + parser.add_argument('-o', '--output', default='./', \ + help='output path.') + return parser.parse_args() + + +def eprint(*args, **kwargs): + """print error message to stderr + """ + print(*args, file=sys.stderr, **kwargs) + + +def check_args(args): + """ + check console parameters according to restrictions. + return: True or False + """ + check_flag = True + is_dir = True + if os.path.isdir(args.input): + if not os.listdir(args.input): + eprint('[ERROR] input image path=%r is empty.' % args.input) + check_flag = False + elif os.path.isfile(args.input): + is_dir = False + else: + eprint('[ERROR] input path=%r does not exist.' % args.input) + check_flag = False + if args.output_image_format not in ('BGR','RGB', 'YUV', 'GRAY'): + eprint("[ERROR] Convert to %d is not support." % (args.output_image_format)) + check_flag = False + if args.height <= 0 or args.width <= 0: + eprint("[ERROR] image height or image width must be greater than 0.") + check_flag = False + elif args.output_image_format == 'YUV': + if args.width % 2 == 1 or args.height % 2 == 1: + eprint("[ERROR] when the output color format is YUV, the width and height of the picture must be even.") + check_flag = False + return check_flag, is_dir + + +def convert_img_2_yuv(input_img): + input_img_height = input_img.shape[0] + input_img_width = input_img.shape[1] + bgr2y_list = np.array([29, 150, 77]) + bgr2y_data = input_img.reshape(input_img_height * input_img_width, 3) + y_data = np.dot(bgr2y_data, bgr2y_list) >> 8 + bgr2u_list = np.array([131, -87, -44]) + bgr2v_list = np.array([-21, -110, 131]) + bgr2uv_matrix = np.transpose(np.append(bgr2u_list, bgr2v_list).reshape((2, 3))) + bgr2uv_data = input_img[0::2, 0::2, :].reshape((input_img_height // 2 * input_img_width // 2, 3)) + yuv_base_data = np.dot(bgr2uv_data, bgr2uv_matrix) >> 8 + u_data = yuv_base_data[:,0] + 128 + v_data = yuv_base_data[:,1] + 128 + u_v_data = np.transpose(np.append(u_data.flatten(), v_data.flatten()).reshape((2, input_img_height //2 * input_img_width // 2))) + nv12_data = np.append(y_data.flatten(), u_v_data.flatten()).reshape((input_img_height // 2 * 3, input_img_width)).astype(np.uint8) + return nv12_data + + +def convert_img(args, input_img): + if args.output_image_format == 'BGR': + converted_input_img = input_img + elif args.output_image_format == 'RGB': + converted_input_img = cv.cvtColor(input_img, cv.COLOR_BGR2RGB) + elif args.output_image_format == 'YUV': + converted_input_img = convert_img_2_yuv(input_img) + elif args.output_image_format == 'GRAY': + converted_input_img = cv.cvtColor(input_img, cv.COLOR_BGR2GRAY) + return converted_input_img + + +def resize_img(args, input_img): + old_size = input_img.shape[0:2] + target_size = [args.height, args.width] + ratio = min(float(target_size[i]) / (old_size[i]) for i in range(len(old_size))) + new_size = tuple([int(i*ratio) for i in old_size]) + img_new = cv.resize(input_img,(new_size[1], new_size[0])) + pad_w = target_size[1] - new_size[1] + pad_h = target_size[0] - new_size[0] + top, bottom = pad_h // 2, pad_h - (pad_h // 2) + left, right = pad_w // 2, pad_w - (pad_w // 2) + resized_img = cv.copyMakeBorder(img_new, top, bottom, left, right, cv.BORDER_CONSTANT, None,(0, 0, 0)) + return resized_img + + +def change_type(args, input_img): + if args.output_type == 'float32': + change_type_img = input_img.astype(np.float32) + elif args.output_type == 'int32': + change_type_img = input_img.astype(np.int32) + elif args.output_type == 'uint32': + change_type_img = input_img.astype(np.uint32) + else: + change_type_img = input_img.astype(np.uint8) + return change_type_img + + +def mean(args, input_img): + if isinstance (args.mean, str): + args.mean = json.loads(args.mean) + input_img = input_img.astype(np.float32) + if args.output_image_format == 'GRAY': + input_img[:, :] -= args.mean[0] + elif args.output_image_format in ('BGR', 'RGB'): + input_img[:, :, 0] -= args.mean[0] + input_img[:, :, 1] -= args.mean[1] + input_img[:, :, 2] -= args.mean[2] + return input_img + + +def coefficient(args, input_img): + """ + Normalize the input image + """ + if isinstance(args.coefficient, str): + args.coefficient = json.loads(args.coefficient) + input_img = input_img.astype(np.float32) + if args.output_image_format == 'GRAY': + input_img[:, :] *= args.coefficient[0] + elif args.output_image_format in ('BGR', 'RGB'): + input_img[:, :, 0] *= args.coefficient[0] + input_img[:, :, 1] *= args.coefficient[1] + input_img[:, :, 2] *= args.coefficient[2] + print("------执行ieg的处理--------") + print(input_img) + input_img=input_img/255.0 + input_img=input_img-0.5 + input_img=input_img*2 + print("--------执行之后的结果---------") + print(input_img) + return input_img + + +def change_format(args, input_img): + if args.output_format == 'NCHW': + if args.output_image_format in ('RGB', 'BGR'): + change_format_img = input_img.transpose(2,0,1).copy() + return change_format_img + return input_img + + +def mkdir_output(args): + if not os.path.exists(args.output): + os.makedirs(args.output) + return + + +def process(args, file_path): + if file_path.endswith(".txt"): + if sys.version_info.major == 2: + import ConfigParser as configparser + else: + import configparser + + config = configparser.ConfigParser() + config.read(file_path) + if sys.version_info.major == 2: + input_node = json.loads(config.get('baseconf', 'input_node')) + shape = json.loads(config.get('baseconf', 'shape')) + else: + input_node = json.loads(config['baseconf']['input_node']) + shape = json.loads(config['baseconf']['shape']) + input_node_np = np.array(input_node) + change_type_img_info = change_type(args, input_node_np) + img_info = np.reshape(change_type_img_info, shape) + out_path = os.path.join(args.output, os.path.splitext(os.path.split(file_path)[1])[0] + ".bin") + mkdir_output(args) + img_info.tofile(out_path) + else: + input_img = cv.imread(file_path) + resized_img1 = resize_img(args, input_img) + converted_img = convert_img(args, resized_img1) + if args.output_image_format == "YUV": + change_format_img = converted_img + else: + mean_img = mean(args, converted_img) + coefficient_img = coefficient(args, mean_img) + change_type_img = change_type(args, coefficient_img) + change_format_img = change_format(args, change_type_img) + out_path = os.path.join(args.output, os.path.splitext(os.path.split(file_path)[1])[0] + ".bin") + mkdir_output(args) + change_format_img.tofile(out_path) + + +def main(): + """main function to receive params them change data to bin. + """ + args = get_args() + ret, is_dir = check_args(args) + if ret: + if is_dir: + files_name = os.listdir(args.input) + for file_name in files_name: + if os.path.splitext(file_name)[1] in IMG_EXT: + file_path = os.path.join(args.input, file_name) + process(args, file_path) + print("[info] bin file generated successfully.") + else: + if os.path.splitext(args.input)[1] in IMG_EXT or os.path.splitext(args.input)[1] == "txt": + process(args, args.input) + print("[info] bin file generated successfully.") + else: + eprint("[ERROR] input file must be image or end with '.txt'.") + +if __name__ == '__main__': + main() + + + + diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/merge.py b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/merge.py new file mode 100644 index 000000000..4c5d39f46 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/merge.py @@ -0,0 +1,16 @@ +import os +import numpy as np +import shutil + + +data_sets = [] +sample_batch_input_bin_dir = "./imgout/" +for item in os.listdir(sample_batch_input_bin_dir): + # 读取bin文件时,bin文件内的dtype类型须根据模型的输入类型确定,下面以float32为例 + original_input_data = np.fromfile(os.path.join(sample_batch_input_bin_dir, item), dtype=np.int32) + # 将数据重新组织,具体根据模型输入中的shape值确定 + current_input_data = original_input_data.reshape(32, 32, 3) + # 将当前的数据添加到列表中 + data_sets.append(current_input_data) +# 将每个batch的数据保存到一个输入bin文件中,从而得到一个包含多batch的输入bin文件 +np.array(data_sets).tofile("newinput.bin") \ No newline at end of file -- Gitee From ff57b6e351cc0a8fb244f572a2c5ac14ff038860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:55:03 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ACL?= =?UTF-8?q?=5FTensorFlow/contrib/cv/IEG=5FID2049=5Ffor=5FACL/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/.keep deleted file mode 100644 index e69de29bb..000000000 -- Gitee From 64dc62259ea8ae940ead3cebd3255b15f018ecf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:57:24 +0000 Subject: [PATCH 7/8] update ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 虽千万人吾往矣 <1291632293@qq.com> --- ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md index 6559e5cdb..64792b2ba 100644 --- a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md @@ -47,12 +47,16 @@ hpiegmodel.om的obs地址:obs://lwr-npu/tl/hpiegmodel.om

数据集转换bin

1.使用convertimg.py将数据集转为png,代码中的src为数据集源路径,dest为png数据集输出路径 + 2.参考 https://gitee.com/ascend/tools/tree/master/img2bin, 获取msame推理工具及使用方法。 + 这里对该代码库中的img2bin进行了少量修改,加入了数据预处理 -使用img2bin.py将png数据集转为bin格式,img2bin.py为tools-master中的代码文件, +使用img2bin.py将png数据集转为bin格式,img2bin.py为tools-master中的代码文件 + ``` python3 img2bin.py -i /mnt/home/test_user01/toimg -w 32 -h 32 -f RGB -a NHWC -t float32 -c [1,1,1] -o ./imgout ``` + 3.使用merge.py将bin格式的数据集合并为一个batch 转换后的bin文件见obs地址:obs://lwr-npu/tl/newinputbin -- Gitee From 26d0f5e156291e380f3c7659758ccfab58ea6e1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BD=E5=8D=83=E4=B8=87=E4=BA=BA=E5=90=BE=E5=BE=80?= =?UTF-8?q?=E7=9F=A3?= <1291632293@qq.com> Date: Fri, 28 Oct 2022 05:58:28 +0000 Subject: [PATCH 8/8] update ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 虽千万人吾往矣 <1291632293@qq.com> --- ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md index 64792b2ba..e018a9c9d 100644 --- a/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md +++ b/ACL_TensorFlow/contrib/cv/IEG_ID2049_for_ACL/README.md @@ -48,7 +48,7 @@ hpiegmodel.om的obs地址:obs://lwr-npu/tl/hpiegmodel.om

数据集转换bin

1.使用convertimg.py将数据集转为png,代码中的src为数据集源路径,dest为png数据集输出路径 -2.参考 https://gitee.com/ascend/tools/tree/master/img2bin, 获取msame推理工具及使用方法。 +2.参考 https://gitee.com/ascend/tools/tree/master/img2bin, 获取img2bin推理工具及使用方法。 这里对该代码库中的img2bin进行了少量修改,加入了数据预处理 使用img2bin.py将png数据集转为bin格式,img2bin.py为tools-master中的代码文件 -- Gitee