diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/.keep b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e5089ae71dd4b3fd6b63f87092782312f42066e6 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/README.md @@ -0,0 +1,77 @@ +# MUNIT +MUNIT推理部分实现,模型概述详情请看MUNIT_ID0953_for_TensorFlow README.md + +## 训练环境 + +* TensorFlow 1.15.0 +* Python 3.7.0 + +## 代码及路径解释 + +``` +MUNIT_ID0953_for_ACL +├── ckpt2pb.py ckpt模型固化为pb +├── pb2om.sh act工具 pb==》om 转换命令 +├── test_om.py 推理数据后处理 +├── inference.sh msame工具:om离线推理命令 +├── image2bin.py 推理数据预处理:将image_test中的image文件转换为bin并进行其他图片预处理 +├── image_test 测试数据集bin文件存放位置 + └── valA domainA数据集 + └── valB domainB数据集 + └── style style文件 +├── inference msame推理结果bin文件存放位置 + └── .. +├── inference_images msame推理结果bin文件转图片 + └── inferA domainA的推理图片 + └── inferB domainB的推理图片 +``` + + +## 数据集 +* edgestoshoes数据集(测试) + +obs链接:obs://cann-id0953/dataset/edges2shoes/val/ + +## 模型文件 +包括初始ckpt文件,固化pb文件,以及推理om文件 + +obs链接:obs://cann-id0953/final_files/ + +## pb模型 + +模型固化 +```shell +python3 ckpt2pb.py +``` +## 生成om模型 +```shell +sh pb2om.sh +``` +具体参数使用方法请查看官方文档。 + +## 将测试集图片转为bin文件 +```shell +python3 image2bin.py +``` +## 使用msame工具推理 +```shell +sh inference.sh +``` +参考 https://gitee.com/ascend/tools/tree/master/msame, 获取msame推理工具及使用方法。 + +## 使用推理得到的bin文件进行推理 +```shell +python3 test_om.py +``` + +## 推理图片 + +* Input + +![input0](pic/199_AB_0.jpg) +![input1](pic/199_AB_1.jpg) + +* Output + +![output0](pic/199_AB_32_output_0.jpg) +![output1](pic/199_AB_32_output_1.jpg) diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/ckpt2pb.py b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/ckpt2pb.py new file mode 100644 index 0000000000000000000000000000000000000000..596a7b7fa9e40dbe2d012e529d18dc7b837acfe7 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/ckpt2pb.py @@ -0,0 +1,100 @@ +import tensorflow as tf +from tensorflow.python.tools import freeze_graph +from npu_bridge.estimator import npu_ops + +from npu_bridge.npu_init import * +from MUNIT import MUNIT +import argparse +from utils import * +from tensorflow.python.framework import graph_util + +# import precision_tool.tf_config as npu_tf_config +import os +import moxing as mox + +# 指定checkpoint路径 +ckpt_path = "obs://cann-id0953/final_files/ckpt" + +def parse_args(): + desc = "Tensorflow implementation of MUNIT" + + parser.add_argument('--batch_size', type=int, default=1, help='The batch size') + parser.add_argument('--style_dim', type=int, default=8, help='length of style code') + parser.add_argument('--img_h', type=int, default=256, help='The size of image hegiht') + parser.add_argument('--img_w', type=int, default=256, help='The size of image width') + parser.add_argument('--img_ch', type=int, default=3, help='The size of image channel') + + parser.add_argument('--result', type=str, default='results', + help='Directory name to save the results') + parser.add_argument('--checkpoint_dir', type=str, default='checkpoint', + help='Directory name to save the checkpoints') + parser.add_argument('--pb_dir', type=str, default="pb") + + #return parser.parse_args() + return check_args(parser.parse_args()) + +"""checking arguments""" +def check_args(args): + # --pb_dir + args.pb_dir = os.path.join(args.result, args.pb_dir) + check_folder(args.pb_dir) + + # --checkpoint_dir + args.checkpoint_dir = os.path.join(args.result, args.checkpoint_dir) + check_folder(args.checkpoint_dir) + + return args + +def main(): + # parse arguments + args = parse_args() + + print("===>>>Copy files from {} to modelarts dir:{}".format(ckpt_path, args.checkpoint_dir)) + mox.file.copy_parallel(src_url=ckpt_path, dst_url=args.checkpoint_dir) + print("Copy finished") + + if args is None: + exit() + + tf.reset_default_graph() + + # 定义网络的输入节点 + test_image_A = tf.placeholder(tf.float32, [args.batch_size, + args.img_h, args.img_w, args.img_ch], name='test_imageA') + test_image_B = tf.placeholder(tf.float32, [args.batch_size, + args.img_h, args.img_w, args.img_ch], name='test_imageB') + test_style = tf.placeholder(tf.float32, [args.batch_size, 1, 1, args.style_dim], name='test_style') + + with tf.Session() as sess: + gan = MUNIT(sess, args) + + test_content_a, _ = gan.Encoder_A(test_image_A) + test_content_b, _ = gan.Encoder_B(test_image_B) + + test_fake_A = gan.Decoder_A(content_B=test_content_b, style_A=test_style) + test_fake_B = gan.Decoder_B(content_A=test_content_a, style_B=test_style) + + test_fake_A = tf.identity(test_fake_A, name = 'outputA') + test_fake_B = tf.identity(test_fake_B, name = 'outputB') + + graph = tf.get_default_graph() + input_graph_def = graph.as_graph_def() + + sess.run(tf.global_variables_initializer()) + + saver = tf.train.Saver() + saver.restore(sess, os.path.join(args.checkpoint_dir, "MUNIT.model-100001")) # 恢复图并得到数据 + + output_graph_def = graph_util.convert_variables_to_constants( # 模型持久化,将变量值固定 + sess=sess, + input_graph_def=input_graph_def, + output_node_names=['outputA', 'outputB']) # 如果有多个输出节点,以逗号隔开 + # 保存模型 + with tf.gfile.GFile(os.path.join(args.pb_dir, "munit.pb"), "wb") as f: + f.write(output_graph_def.SerializeToString()) # 序列化输出 + print("%d ops in the final graph." % len(output_graph_def.node)) # 得到当前图有几个操作节点 + + print("done") + +if __name__ == '__main__': + main() diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/image2bin.py b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/image2bin.py new file mode 100644 index 0000000000000000000000000000000000000000..2908665515c9f1ec80ec1d435624c495790a0a37 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/image2bin.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +import os +import imageio +import numpy as np +from skimage.transform import resize + +precision_mode = 32 + +def preprocessing(x): + x = x/127.5 - 1 # -1 ~ 1 + return x + +def load_test_data(image_path, size_h=256, size_w=256): + img = imageio.imread(image_path, pilmode= 'RGB') + img = resize(img, output_shape=(size_h, size_w)) + img = preprocessing(img) + + return img + +def files2bin(src_path, dst_path): + files = os.listdir(src_path) + for file in files: + print(f"start to process {os.path.join(src_path, file)}") + sample_image = np.asarray(load_test_data(os.path.join(src_path, file), size_h=256, size_w=256),dtype=f"float{precision_mode}") + + sample_image.tofile(os.path.join(dst_path, f"{file.split('.')[0]}_{precision_mode}") + ".bin") # 处理后的图片保存为bin文件 + +src_path_A = "val/valA" +src_path_B = "val/valB" + +dst_path = "image_test" +dst_path_A = os.path.join(dst_path, "valA") +dst_path_B = os.path.join(dst_path, "valB") +dst_path_style = os.path.join(dst_path, "style") + +os.makedirs(dst_path, exist_ok=True) +os.makedirs(dst_path_A, exist_ok=True) +os.makedirs(dst_path_B, exist_ok=True) +os.makedirs(dst_path_style, exist_ok=True) + +# 生成图片的bin文件 +files2bin(src_path_A, dst_path_A) +files2bin(src_path_B, dst_path_B) + +# 生成style的bin文件 +files = os.listdir(src_path_A) +for file in files: + test_style = np.random.normal(loc=0.0, scale=1.0, size=[1, 1, 1, 8]).astype(f"float{precision_mode}") + test_style.tofile(os.path.join(dst_path_style, f"{file.split('.')[0]}_{precision_mode}") + ".bin") \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/inference.sh b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/inference.sh new file mode 100644 index 0000000000000000000000000000000000000000..bdabbacc2b4cea07ffb2947c1970796c221186dc --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/inference.sh @@ -0,0 +1,5 @@ +./msame --model munit.om \ + --input image_test/valA,image_test/valB,image_test/style \ + --output inference \ + --outfmt BIN \ + | tee msame.log \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pb2om.sh b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pb2om.sh new file mode 100644 index 0000000000000000000000000000000000000000..80687fcb557b3e4c8d0d16f586a02ee846c50041 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pb2om.sh @@ -0,0 +1,14 @@ +/usr/local/Ascend/ascend-toolkit/set_env.sh # source 环境变量 +export ASCEND_SLOG_PRINT_TO_STDOUT=1 # 设置输出日志打屏 + +mkdir debug_info +atc --model=munit.pb \ + --framework=3 \ + --output=munit \ + --soc_version=Ascend310 \ + --input_shape="test_imageA:1,256,256,3;test_imageB:1,256,256,3;test_style:1,1,1,8" \ + --log=info \ + --out_nodes="outputA:0;outputB:0" \ + --precision_mode="allow_fp32_to_fp16" \ + --debug_dir=debug_info \ +| tee pb2om.log \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/.keep b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_0.jpg b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9b62407f6dc332290f4ded20c4d2991447d4b656 Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_0.jpg differ diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_1.jpg b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e46dd6dcd3fc153bcf187afa0ec504eb539defb7 Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_1.jpg differ diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_32_output_0.jpg b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_32_output_0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2f1c212caab87a1b82680c51fa80d3ffeb125318 Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_32_output_0.jpg differ diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_32_output_1.jpg b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_32_output_1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9e1c6b6c410e669101aa9e40846419dc151df71d Binary files /dev/null and b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/pic/199_AB_32_output_1.jpg differ diff --git a/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/test_om.py b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/test_om.py new file mode 100644 index 0000000000000000000000000000000000000000..ef9a77c2557dba9c361795ef5b257890da8ae3fa --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MUNIT_ID0953_for_ACL/test_om.py @@ -0,0 +1,43 @@ +import numpy as np +from PIL import Image +import imageio +import os + +def save_images(images, size, image_path): + return imsave(inverse_transform(images), size, image_path) + +def inverse_transform(images): + return (images+1.) / 2 + +def imsave(images, size, path): + return imageio.imwrite(path, merge(images, size)) + +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 + +bin_path = "inference/2022728_14_55_36_141275" # msame推理得到的结果路径 +dst_path = "inference_images" +dst_path_A = os.path.join(dst_path, "inferA") +dst_path_B = os.path.join(dst_path, "inferB") + +os.makedirs(dst_path, exist_ok=True) +os.makedirs(dst_path_A, exist_ok=True) +os.makedirs(dst_path_B, exist_ok=True) + +files = os.listdir(bin_path) + +for file in files: + file_name = file.split(".")[0] + out_image = np.fromfile(os.path.join(bin_path, file),dtype="float32").reshape(1,256,256,3) + + if file_name[-1] == '0': + save_images(out_image, [1,1],f"{dst_path_A}/{file_name}.jpg") + elif file_name[-1] == '1': + save_images(out_image, [1,1],f"{dst_path_B}/{file_name}.jpg")