diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/.keep b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4314623a8f239a59af02edc5c674f7ae1f60ebfd --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/README.md @@ -0,0 +1,102 @@ +## 概述 + +特征量化(FQ),将真数据样本和假数据样本嵌入到共享离散空间中。FQ的量化值被构造为一个进化词典,与最近分布历史的特征统计一致。因此,FQ隐式地在紧凑空间中实现了鲁棒的特征匹配。我们的方法可以很容易地插入现有的GAN模型中,在训练中几乎没有计算开销。 + +- 参考论文: + + [Feature Quantization Improves GAN Training](https://arxiv.org/abs/2004.02088) + +- 参考实现:[YangNaruto](https://github.com/YangNaruto)/**[FQ-GAN](https://github.com/YangNaruto/FQ-GAN)** + +- 适配昇腾 AI 处理器的实现: + + https://gitee.com/ascend/ModelZoo-TensorFlow/tree/master/TensorFlow/contrib/cv/FQ-GAN_ID1117_for_TensorFlow + + + +## 原始模型 + +百度网盘分享地址:链接:https://pan.baidu.com/s/1EhnLjVI-abNbpPGV_ptwBA +提取码:so7q + + +步骤一:通过代码ckpt2pb.py将ckpt转成pb + +## pb模型 + +``` +UGATIT_new.pb +``` + +百度网盘地址:链接:https://pan.baidu.com/s/1k7AfDg0aYB5RMHJ9IaGjYw +提取码:2ndx + +## om模型 + +使用atc命令将pb文件转为om + +使用ATC模型转换工具进行模型转换时可以参考如下指令: + +```shell +atc --model=/home/test_user07/FQ-GAN/FQ-U-GAT-IT/pb_model/UGATIT_new.pb --framework=3 --output=/home/test_user07/FQ-GAN/FQ-U-GAT-IT/pb_model/UGATIT_acc_new --soc_version=Ascend910 --input_shape="input1:1,256,256,3" --log=info --out_nodes="p_cam:0;c_out:0" --precision_mode=force_fp32 --op_select_implmode=high_precision +``` + +成功转化成UGATIT_acc_new.om + +UGATIT_acc_new.om的百度网盘地址:链接:https://pan.baidu.com/s/12TWKBjuem6q4RqUmrru6Jw +提取码:dxxw + + +## 数据集转换bin + +使用自己修改过的img2bin.py将jpg格式的测试图片转为bin格式。 + +命令为: + +```shell +python2 /home/test_user07/tools/img2bin/img2bin.py -i/home/test_user07/FQ-GAN/FQ-U-GAT-IT/dataset/selfie2anime/testB -w 256 -h 256 -f RGB -a NHWC -t float32 -c [1,1,1] -o /home/test_user07/FQ-GAN/FQ-U-GAT-IT/dataset/selfie2anime/testB_out_new +``` + +转换后的bin文件见百度网盘地址:链接:https://pan.baidu.com/s/15vpEjtbuse9XZ9bqK72kiA +提取码:wte9 + +## 使用msame工具推理 + +参考 https://gitee.com/ascend/tools/tree/master/msame, 获取msame推理工具及使用方法。 + +使用msame推理工具,参考如下命令,发起推理测试: + +```shell +./msame --model "/home/test_user07/FQ-GAN/FQ-U-GAT-IT/pb_model/UGATIT_acc_new.om" --input "/home/test_user07/FQ-GAN/FQ-U-GAT-IT/dataset/selfie2anime/testB_out_new" --output "/home/test_user07/FQ-GAN/" --outfmt TXT +``` + +推理后会在目的地址下面生成一个以日期命名的文件夹,每个测试图片对应生成的推理结果以名称对应,如图:![image-20221016213203471](https://gitee.com/hkx888/ModelZoo-TensorFlow/raw/master/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016213203471.png) + +## 推理精度 + +生成图片的格式为txt格式,通过代码post_pro.py代码处理结果,转化为jpg格式的图片。 + +```shell +python3 post_pro.py --input="/home/test_user07/FQ-GAN/20221016_18_13_56_414701" +``` + +使用ckpt测试,生成图片。 + +```shell +python3.7 main.py --dataset='selfie2anime' --phase='test' --test_train=False --quant=True --epoch=100 --iteration=10000 +``` + +对比结果,数值结果和图片结果均相同: + +![image-20221016223754779](https://gitee.com/hkx888/ModelZoo-TensorFlow/raw/master/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016223754779.png) + +ckpt生成: + +![711665931223_.pic](https://gitee.com/hkx888/ModelZoo-TensorFlow/raw/master/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt_out.jpg) + +推理结果还原: + +![721665931223_.pic](https://gitee.com/hkx888/ModelZoo-TensorFlow/raw/master/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/msame_out.jpg) + + + diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt2pb.py b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt2pb.py new file mode 100644 index 0000000000000000000000000000000000000000..0c8b27345794a8f79ec5e93f5bf652a8d24bd6a4 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt2pb.py @@ -0,0 +1,159 @@ +import tensorflow as tf +from tensorflow.python.tools import freeze_graph +from npu_bridge.estimator import npu_ops +from tensorflow.python.framework import graph_util +from ops import * +from UGATIT import UGATIT +import argparse +from utils import * +from logger import Logger +import npu_bridge +from npu_bridge.npu_init import * +from npu_bridge.estimator.npu import util +from tensorflow.core.protobuf.rewriter_config_pb2 import RewriterConfig + + +import sys +"""parsing and configuration""" + +def parse_args(): + desc = "Tensorflow implementation of U-GAT-IT" + parser = argparse.ArgumentParser(description=desc) + parser.add_argument('--phase', type=str, default='test', help='[train / test]') + parser.add_argument('--light', type=str2bool, default=False, help='[U-GAT-IT full version / ' + 'U-GAT-IT light version]') + parser.add_argument('--dataset', type=str, default='selfie2anime', help='dataset_name') + + parser.add_argument('--epoch', type=int, default=101, help='The number of epochs to run') + parser.add_argument('--iteration', type=int, default=10000, help='The number of training ' + 'iterations') + parser.add_argument('--batch_size', type=int, default=1, help='The size of batch size') + parser.add_argument('--print_freq', type=int, default=1000, help='The number of ' + 'image_print_freq') + parser.add_argument('--save_freq', type=int, default=10, help='The number of ckpt_save_freq') + parser.add_argument('--decay_flag', type=str2bool, default=True, help='The decay_flag') + parser.add_argument('--decay_epoch', type=int, default=50, help='decay epoch') + + parser.add_argument('--lr', type=float, default=0.0001, help='The learning rate') + parser.add_argument('--GP_ld', type=int, default=10, help='The gradient penalty lambda') + parser.add_argument('--adv_weight', type=int, default=1, help='Weight about GAN') + parser.add_argument('--cycle_weight', type=int, default=10, help='Weight about Cycle') + parser.add_argument('--identity_weight', type=int, default=10, help='Weight about Identity') + parser.add_argument('--cam_weight', type=int, default=1000, help='Weight about CAM') + parser.add_argument('--gan_type', type=str, default='lsgan', help='[gan / lsgan / wgan-gp / wgan-lp / dragan / hinge]') + + parser.add_argument('--smoothing', type=str2bool, default=True, help='AdaLIN smoothing effect') + + parser.add_argument('--ch', type=int, default=64, help='base channel number per layer') + parser.add_argument('--n_res', type=int, default=4, help='The number of resblock') + parser.add_argument('--n_dis', type=int, default=6, help='The number of discriminator layer') + parser.add_argument('--n_critic', type=int, default=1, help='The number of critic') + parser.add_argument('--sn', type=str2bool, default=True, help='using spectral norm') + + parser.add_argument('--img_size', type=int, default=256, help='The size of image') + parser.add_argument('--img_ch', type=int, default=3, help='The size of image channel') + parser.add_argument('--augment_flag', type=str2bool, default=True, help='Image augmentation use or not') + + parser.add_argument('--checkpoint_dir', type=str, default='checkpoint', + help='Directory name to save the checkpoints') + parser.add_argument('--result_dir', type=str, default='results', + help='Directory name to save the generated images') + parser.add_argument('--log_dir', type=str, default='logs', + help='Directory name to save training logs') + parser.add_argument('--sample_dir', type=str, default='samples', + help='Directory name to save the samples on training') + + # Quantization argument + parser.add_argument('--quant', type=str2bool, default=True, + help='quantization or not?') + parser.add_argument('--commitment_cost', type=float, default=2.0, help='commitment cost') + parser.add_argument('--quantization_layer', type=str, default='123', help='which layer?') + parser.add_argument('--decay', type=float, default=0.85, help='dictionary learning decay') + parser.add_argument('--test_train', type=str2bool, default=False, help='if test while training') + + return check_args(parser.parse_args()) + +"""checking arguments""" +def check_args(args): + # --checkpoint_dir + + if args.quant: + args.checkpoint_dir += '_quant' + args.result_dir += '_quant' + args.log_dir += '_quant' + args.sample_dir += '_quant' + + check_folder(args.checkpoint_dir) + + # --result_dir + check_folder(args.result_dir) + + # --result_dir + check_folder(args.log_dir) + + # --sample_dir + check_folder(args.sample_dir) + # --epoch + try: + assert args.epoch >= 1 + except: + print('number of epochs must be larger than or equal to one') + + # --batch_size + try: + assert args.batch_size >= 1 + except: + print('batch size must be larger than or equal to one') + return args + + + + +config = tf.ConfigProto(allow_soft_placement=True) +custom_op = config.graph_options.rewrite_options.custom_optimizers.add() +custom_op.name = "NpuOptimizer" +custom_op.parameter_map["use_off_line"].b = True +config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF +config.graph_options.rewrite_options.remapping = RewriterConfig.OFF +batch_size = 1 + + +ckpt_path = "/home/test_user07/FQ-GAN/FQ-U-GAT-IT/checkpoint_quant/UGATIT_q_selfie2anime_lsgan_4resblock_6dis_1_1_10_10_1000_sn_smoothing_123_2.0_0.85/UGATIT.model-2" + +def main(): + tf.compat.v1.disable_eager_execution() + tf.reset_default_graph() + inputs1 = tf.placeholder(tf.float32, shape=[1, 256, 256, 3], name="input1") + + args = parse_args() + if args is None: + exit() + + with tf.Session(config=config) as sess: + gan = UGATIT(sess, args) + out, cam= gan.generate_b2a(inputs1) + + c_out = tf.identity(out, name='c_out') + p_cam = tf.identity(cam, name='p_cam') + + graph_def = tf.get_default_graph().as_graph_def(add_shapes=True) + node_list = [n.name for n in graph_def.node] + for node in node_list: + print("node_name", node) + tf.train.write_graph(sess.graph_def, './pb_model', 'model.pb') + freeze_graph.freeze_graph( + input_graph='./pb_model/model.pb', + input_saver='', + input_binary=False, + input_checkpoint=ckpt_path, + output_node_names='p_cam,c_out', + restore_op_name='save/restore_all', + filename_tensor_name='save/Const:0', + output_graph='./pb_model/UGATIT_new.pb', + clear_devices=False, + initializer_nodes='') + print("done") + +if __name__ == '__main__': + main() + diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt_out.jpg b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt_out.jpg new file mode 100644 index 0000000000000000000000000000000000000000..28d117d83a87acd9b675aacdd746e48b87a90863 Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/ckpt_out.jpg differ diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016213203471.png b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016213203471.png new file mode 100644 index 0000000000000000000000000000000000000000..72f2329f4b174f0aa1d5c80e159f74b8cf167b5e Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016213203471.png differ diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016223754779.png b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016223754779.png new file mode 100644 index 0000000000000000000000000000000000000000..96238695881b5a7580292d963c5af7e2788f2c7b Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/image-20221016223754779.png differ diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/img2bin.py b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/img2bin.py new file mode 100644 index 0000000000000000000000000000000000000000..bfd5dc5d52f7778c671df4a80cc47f7bf50d9486 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/img2bin.py @@ -0,0 +1,316 @@ +# -*- 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 +from operator import delitem +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] + 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") + out_path1 = os.path.join(args.output, os.path.splitext(os.path.split(file_path)[1])[0] + ".txt") + mkdir_output(args) + img_info.tofile(out_path) + # np.savetxt(out_path1,change_format_img) + + 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") + out_path1 = os.path.join(args.output, os.path.splitext(os.path.split(file_path)[1])[0] + ".txt") + + mkdir_output(args) + change_format_img = change_format_img/127.5 - 1 + + print("out_path={}, img={}, num={}, type={}".format(out_path, change_format_img, + change_format_img[-1][-1][-1], type(change_format_img[-1][-1][-1]))) + change_format_img.tofile(out_path) + # np.savetxt(out_path1,change_format_img,fmt='%f') + + +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/FQ-GAN_ID1117_for_ACL/msame_out.jpg b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/msame_out.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b26db9cfbe623c432a2b75e09fc5b8b0b7b6749f Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/msame_out.jpg differ diff --git a/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/post_pro.py b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/post_pro.py new file mode 100644 index 0000000000000000000000000000000000000000..5fbc69a86dc99540b68da19f63625223d556f926 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/FQ-GAN_ID1117_for_ACL/post_pro.py @@ -0,0 +1,112 @@ +from email.mime import image +import tensorflow as tf +from tensorflow.contrib import slim +import cv2 +import os, random +import numpy as np +import argparse +from glob import glob + + + +class ImageData: + + def __init__(self, load_size, channels, augment_flag): + self.load_size = load_size + self.channels = channels + self.augment_flag = augment_flag + + def image_processing(self, filename): + x = tf.read_file(filename) + x_decode = tf.image.decode_jpeg(x, channels=self.channels) + img = tf.image.resize_images(x_decode, [self.load_size, self.load_size]) + img = tf.cast(img, tf.float32) / 127.5 - 1 + + if self.augment_flag: + augment_size = self.load_size + (30 if self.load_size == 256 else 15) + p = random.random() + if p > 0.5: + img = augmentation(img, augment_size) + + return img + +def load_test_data(image_path, size=256): + img = cv2.imread(image_path, flags=cv2.IMREAD_COLOR) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + + img = cv2.resize(img, dsize=(size, size)) + + img = np.expand_dims(img, axis=0) + img = img/127.5 - 1 + + return img + +def augmentation(image, augment_size): + seed = random.randint(0, 2 ** 31 - 1) + ori_image_shape = tf.shape(image) + image = tf.image.random_flip_left_right(image, seed=seed) + image = tf.image.resize_images(image, [augment_size, augment_size]) + image = tf.random_crop(image, ori_image_shape, seed=seed) + return image + +def save_images(images, size, image_path): + return imsave(inverse_transform(images), size, image_path) + +def inverse_transform(images): + return ((images+1.) / 2) * 255.0 + + +def imsave(images, size, path): + images = merge(images, size) + images = cv2.cvtColor(images.astype('uint8'), cv2.COLOR_RGB2BGR) + + return cv2.imwrite(path, images) + +def merge(images, size): + h, w = images.shape[1], images.shape[2] + img = np.zeros((h * size[0], w * size[1], 3)) + for idx, image in enumerate(images): + i = idx % size[1] + j = idx // size[1] + img[h*j:h*(j+1), w*i:w*(i+1), :] = image + + return img + +def show_all_variables(): + model_vars = tf.trainable_variables() + slim.model_analyzer.analyze_vars(model_vars, print_info=True) + +def check_folder(log_dir): + if not os.path.exists(log_dir): + os.makedirs(log_dir) + return log_dir + +def str2bool(x): + return x.lower() in ('true') +def parse_args(): + desc = "post prosess of fake_img" + parser = argparse.ArgumentParser(description=desc) + parser.add_argument('--input', type=str, default='/home/test_user07/FQ-GAN/20221016_18_13_56_414701', + help='path of msame output') + return parser.parse_args() +def process(args): + print(args.input) + files = glob('{}/*1.txt'.format(args.input)) + output_dir=os.path.join(args.input, "img_output") + if not os.path.exists(output_dir): + os.makedirs(output_dir) + print(len(files)) + for sample_file in files: + a=np.loadtxt(sample_file) + a=np.reshape(a, (1, 256, 256, 3)) + file_name=os.path.basename(sample_file) + file_name=file_name.split('.')[0][: -2] + image_path=os.path.join(args.input+"/img_output",file_name+".jpg") + print(image_path) + save_images(a, [1, 1], image_path) + print("done") +def main(): + args = parse_args() + process(args) +if __name__ == '__main__': + main() \ No newline at end of file