From c16991a2b94b2c407804be40c48224ec95b3f481 Mon Sep 17 00:00:00 2001 From: lufeng96 <1600925872@qq.com> Date: Sat, 2 Apr 2022 13:03:26 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20DualGAN=5FID1001=5Ffor?= =?UTF-8?q?=5FACL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/.keep diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/.keep b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From 0bba98b1a6a023a0285a1beeb179d3cb6b12a75f Mon Sep 17 00:00:00 2001 From: lufeng96 <1600925872@qq.com> Date: Sat, 2 Apr 2022 13:05:41 +0000 Subject: [PATCH 2/3] init --- .../contrib/cv/DualGAN_ID1001_for_ACL/LICENSE | 201 +++++++ .../cv/DualGAN_ID1001_for_ACL/README.md | 73 +++ .../cv/DualGAN_ID1001_for_ACL/bin2jpg.py | 61 +++ .../cv/DualGAN_ID1001_for_ACL/freeze_graph.py | 121 +++++ .../cv/DualGAN_ID1001_for_ACL/jpg2bin.py | 64 +++ .../cv/DualGAN_ID1001_for_ACL/model.py | 504 ++++++++++++++++++ .../DualGAN_ID1001_for_ACL/modelzoo_level.txt | 3 + .../contrib/cv/DualGAN_ID1001_for_ACL/ops.py | 103 ++++ .../DualGAN_ID1001_for_ACL/requirements.txt | 4 + .../cv/DualGAN_ID1001_for_ACL/utils.py | 107 ++++ 10 files changed, 1241 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/LICENSE create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/bin2jpg.py create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/freeze_graph.py create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/jpg2bin.py create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/model.py create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/modelzoo_level.txt create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/ops.py create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/requirements.txt create mode 100644 ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/utils.py diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/LICENSE b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/LICENSE new file mode 100644 index 000000000..8dada3eda --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + 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. diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md new file mode 100644 index 000000000..502833ae4 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md @@ -0,0 +1,73 @@ +# 模型功能 +现有的图像之间转换的方法,大部分都是需要图像对的方法,但是实际上有的场景下,很难得到这样的图像对。DualGAN使用对偶学习模式的GAN网络结构来进行image to image translation,将 domain A 到 domain B 之间的转换,构成一个闭环(loop)。通过 minimize 该图和重构图像之间的 loss 来优化学习的目标: +给定一个 domain image A,用一个产生器 P 来生成对应的 domain image B,由于没有和A匹配的图像对,这里是没有ground truth的。如果该图伪造的很好,那么反过来,用另一个产生器 Q,应该可以很好的恢复出该图,即Q(P(A, z), z') 应该和 A 是类似的。对于 domain image B 也是如此。 + +- 参考论文: + + https://arxiv.org/abs/1704.02510 + +- 参考实现: + + https://github.com/duxingren14/DualGAN + +# pb模型 + +运行freeze_graph.py会得到A2B.pb和B2A.pb两个模型文件 +``` +python freeze_graph.py --ckpt_path=./ckpt/DualNet.model-99002 + +``` + +# om模型 + +在Ascend310推理服务器下,使用ATC模型转换工具进行模型转换: + +AtoB模型转换 +``` +atc --model=/root/dualgan/pb_model/A2B.pb --framework=3 --output=/root/dualgan/A2B --soc_version=Ascend310 --input_shape="img:1,256,256,3" --log=info --out_nodes="output:0" --op_select_implmode=high_precision +``` + +BtoA模型转换 +``` +atc --model=/root/dualgan/pb_model/B2A.pb --framework=3 --output=/root/dualgan/B2A --soc_version=Ascend310 --input_shape="img:1,256,256,3" --log=info --out_nodes="output:0" --op_select_implmode=high_precision +``` + +推理使用的pb及om模型 [在此获取](https://e-share.obs-website.cn-north-1.myhuaweicloud.com?token=kgEgUsFhjaxRtkatN8fRvFV+TH374pu2rOvPEqrVTMRgxZIZPaP/OO/LcJC/breBuJlVBpbluvxL9zIZvNovzuukk+juP5i+2xTxKhOtHcT+Xt4y+MzbBgqactNNZAkAFvv7b2zcldOJ7qYzJYin/qAEOMLpR50fwk4aPXCO6HwZXvLtgrUhilWNluGpW2QxcS7gj9uiBXF8nUcl+jhrO+v2k0zNbhvI7ISO/0IORuHbyNFKtmagrqIrQ5CsWiuOXc18LwvcO3CPUFk4kHG7/fgr6UyOaN3fMY8wtva+0pvBueGzCkr8+kPBbn5h7/lFOCHaL5P5XYV9qj237hA3LXVOdgkT4+gqsovjYAASlcra9YatkRMj3wimMEpOEZ/gIxpY00UNtxDaz0uF5QCjhbvxUELTo+iUlTmJ3fNresKRkR+3IffNYPoQbjqVYpxCO8qAIeRKD55PbKv8iP4I5LGzS1Smf00LZroDN5gdwe7e3QMgAZFKZEMVghh2Hqh87pLv00FXN8IxmFDVeVNfnjlst0/I0vyDyJDUYUejogQ=) (提取码:123456) + +# 使用msame工具推理 + +参考 https://gitee.com/ascend/tools/tree/master/msame, 获取msame推理工具及使用方法。 + +获取到msame可执行文件之后,进行推理测试。 + +## 1.数据集转换bin + +data_dir=测试集路径 + +data_bin_dir=转化后的的bin数据集路径 +``` +python3 jpg2bin.py --datasets_dir=${data_dir} --output_dir=${data_bin_dir} +``` + +## 2.推理 + +使用AtoB.om模型推理(input为输入数据路径,output为输出数据路径) +``` +./msame --model /root/dualgan/A2B.om --input "/root/dualgan/data_bin" --output "/root/dualgan/outbin" --outfmt BIN +``` + +使用BtoA.om模型转换 +``` +./msame --model /root/dualgan/B2A.om --input "/root/dualgan/data_bin" --output "/root/dualgan/outbin" --outfmt BIN +``` + +## 3.推理结果后处理 + +推理结果为二进制.bin格式数据,需要将其转换为可视的.jpg格式图片。 + +input_dir=/输入.bin数据路径 + +output_dir=/输出路径 +``` +python3 bin2jpg.py --data_dir=${input_dir} --dst_dir=${output_dir} +``` \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/bin2jpg.py b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/bin2jpg.py new file mode 100644 index 000000000..3c60f63a1 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/bin2jpg.py @@ -0,0 +1,61 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# Copyright 2021 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 numpy as np +import argparse +from PIL import Image +import os +import imageio + +""" transform .bin file to .jpg picture. + +python3 bin2jpg.py --data_dir " ./datasets/" --dst_dir "./output/" + +""" + +if __name__ == '__main__': + + parser = argparse.ArgumentParser() + parser.add_argument("--data_dir", type=str, default='../20210813_211402/') + parser.add_argument("--dst_dir", type=str, default='../inference_output_testB/') + config = parser.parse_args() + + if os.path.exists(config.dst_dir): + pass + else: + os.makedirs(config.dst_dir) + num = 0 + for file in os.listdir(config.data_dir): + if file.endswith('.bin'): + data_dir = config.data_dir + "/" + file + data = np.fromfile(data_dir, dtype=np.float32) + data = data.reshape(256, 256, 3) + im = (data + 1.) * 127.5 + imageio.imwrite(config.dst_dir+"/"+"out"+str(num)+".jpg", im) + num = num + 1 diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/freeze_graph.py b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/freeze_graph.py new file mode 100644 index 000000000..a1083a27b --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/freeze_graph.py @@ -0,0 +1,121 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# Copyright 2021 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 argparse +import os +#import scipy.misc +import numpy as np + +from model import DualNet +import tensorflow as tf +from tensorflow.python.tools import freeze_graph + +parser = argparse.ArgumentParser(description='Argument parser') +""" Arguments related to network architecture""" +parser.add_argument('--image_size', dest='image_size', type=int, default=256, help='size of input images (applicable to both A images and B images)') +parser.add_argument('--gcn', dest='gcn', type=int, default=64, help='# of filters in 1st conv layer of generator') +parser.add_argument('--dcn', dest='dcn', type=int, default=64, help='# of filters in 1st conv layer of discriminators') +parser.add_argument('--A_channels', dest='A_channels', type=int, default=3, help='# of channels of image A') +parser.add_argument('--B_channels', dest='B_channels', type=int, default=3, help='# of channels of image B') +parser.add_argument('--dropout_rate', dest='dropout_rate', type=float, default=0.0, help='dropout rate') + +"""Arguments related to run mode""" +parser.add_argument('--phase', dest='phase', default='train', choices=['train', 'test'], help='train, test') + +"""Arguments related to training""" +parser.add_argument('--loss_metric', dest='loss_metric', default='L1', choices=['L1', 'L2'], help='L1, or L2') +parser.add_argument('--niter', dest='niter', type=int, default=30, help='# of iter at starting learning rate') +parser.add_argument('--lr', dest='lr', type=float, default=0.00005, help='initial learning rate for adam')#0.0002 +parser.add_argument('--beta1', dest='beta1', type=float, default=0.5, help='momentum term of adam') +parser.add_argument('--flip', dest='flip', type=bool, default=True, help='if flip the images for data argumentation') +parser.add_argument('--dataset_name', dest='dataset_name', default='facades', help='name of the dataset') +parser.add_argument('--epoch', dest='epoch', type=int, default=50, help='# of epoch') +parser.add_argument('--batch_size', dest='batch_size', type=int, default=1, help='# images in batch') +parser.add_argument('--lambda_A', dest='lambda_A', type=float, default=500.0, help='# weights of A recovery loss') +parser.add_argument('--lambda_B', dest='lambda_B', type=float, default=500.0, help='# weights of B recovery loss') +parser.add_argument('--GAN_type', dest='GAN_type', type=str, default='wgan-gp', choices=['wgan-gp', 'wgan', 'classic'], help='wgan gp | wgan | classic') +parser.add_argument('--clip_value', dest='clip_value', type=float, default=0.01, help='clip values') +parser.add_argument('--n_critic', dest='n_critic', type=int, default=1, help='train discriminators # times per generator training') +parser.add_argument('--disc_type', dest='disc_type', type=str, default='globalgan', choices=['globalgan', 'patchgan'], help='globalgan | patchgan') + +"""Arguments related to monitoring and outputs""" +parser.add_argument('--log_freq', dest='log_freq', type=int, default=10, help='save the model every save_freq sgd iterations') +parser.add_argument('--save_freq', dest='save_freq', type=int, default=990, help='save the model every save_freq sgd iterations') +parser.add_argument('--checkpoint_dir', dest='checkpoint_dir', default='./checkpoint', help='models are saved here') +parser.add_argument('--sample_dir', dest='sample_dir', default='./sample', help='sample are saved here') +parser.add_argument('--test_dir', dest='test_dir', default='./test', help='test sample are saved here') +parser.add_argument('--ckpt_path', dest='ckpt_path', default='./ckpt_n/DualNet.model-99002', help='path of input model') + +args = parser.parse_args() + +ckpt_path = args.ckpt_path + +def ckpt2pb(A2B=True): + tf.reset_default_graph() + img = tf.placeholder(tf.float32, [args.batch_size, args.image_size, args.image_size, + args.A_channels], name='img') + sess=tf.Session() + model = DualNet(sess, image_size=args.image_size, batch_size=args.batch_size,\ + dataset_name=args.dataset_name,A_channels = args.A_channels, \ + B_channels = args.B_channels, flip = (args.flip == 'True'),\ + checkpoint_dir=args.checkpoint_dir, sample_dir=args.sample_dir,\ + gcn = args.gcn, dcn=args.dcn, \ + loss_metric=args.loss_metric, lambda_B=args.lambda_B, \ + lambda_A= args.lambda_A, dropout_rate=args.dropout_rate, \ + n_critic=args.n_critic, GAN_type = args.GAN_type, clip_value=args.clip_value, \ + log_freq=args.log_freq, disc_type=args.disc_type) + + if A2B: + generated_img=model.A_g_net(img,reuse=True) + outname='A2B.pb' + else: + generated_img=model.B_g_net(img,reuse=True) + outname='B2A.pb' + + output = tf.identity(generated_img, name='output') + + with tf.Session() as sess: + 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='output', + restore_op_name='save/restore_all', + filename_tensor_name='save/Const:0', + output_graph='./pb_model/'+outname, + clear_devices=False, + initializer_nodes='') + print("done") + +if __name__ == '__main__': + print('Export AtoB model...') + ckpt2pb(A2B=True) + print('Export BtoA model...') + ckpt2pb(A2B=False) diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/jpg2bin.py b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/jpg2bin.py new file mode 100644 index 000000000..4848ae5d8 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/jpg2bin.py @@ -0,0 +1,64 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# Copyright 2021 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. + +""" transform .jpg picture to .bin format. + +python3 jpg2bin.py --datasets_dir " ./datasets/" --output_dir "./output/" + +""" +import os +import argparse +import tensorflow as tf + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--datasets_dir", type=str, default='', help="Input datasets path.") + parser.add_argument("--output_dir", type=str, default='', help="Output dataset path.") + config = parser.parse_args() + + if os.path.exists(config.output_dir): + pass + else: + os.makedirs(config.output_dir) + + for file in os.listdir(config.datasets_dir): + if file.endswith('.jpg'): + pic_path = config.datasets_dir + "/" + file + print("start to process %s" % pic_path) + + image = tf.io.read_file(filename=pic_path) + image = tf.image.decode_jpeg(image, channels=3) + image = tf.image.resize_images(image, size=(256, 256)) + image = tf.image.convert_image_dtype(image, dtype=tf.float32) + image = (image / 127.5) - 1.0 + image.set_shape([256, 256, 3]) + + with tf.Session() as sess: + image_numpy = image.eval() + # save the pic as .bin format for Ascend310 infer. + image_numpy.tofile(config.output_dir + "/" + file + ".bin") diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/model.py b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/model.py new file mode 100644 index 000000000..3257f088a --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/model.py @@ -0,0 +1,504 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# Copyright 2021 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. + +from __future__ import division +import os +import time +from glob import glob +import tensorflow as tf +import numpy as np +#from six.moves import xrange + +from ops import * +from utils import * + +class DualNet(object): + def __init__(self, sess, image_size=256, batch_size=1,gcn = 64, dcn=64, \ + A_channels = 3, B_channels = 3, dataset_name='facades', \ + checkpoint_dir=None, lambda_A = 500., lambda_B = 500., \ + sample_dir=None, dropout_rate=0.0, loss_metric = 'L1', flip = False,\ + n_critic = 5, GAN_type = 'wgan-gp', clip_value = 0.1, log_freq=50, disc_type = 'globalgan'): + self.dcn = dcn + self.flip = flip + self.lambda_A = lambda_A + self.lambda_B = lambda_B + + self.sess = sess + self.is_grayscale_A = (A_channels == 1) + self.is_grayscale_B = (B_channels == 1) + self.batch_size = batch_size + self.image_size = image_size + self.gcn = gcn + self.A_channels = A_channels + self.B_channels = B_channels + self.loss_metric = loss_metric + + self.dataset_name = dataset_name + self.checkpoint_dir = checkpoint_dir + + #directory name for output and logs saving + self.dir_name = "%s-img_sz_%s-fltr_dim_%d-%s-lambda_AB_%s_%s" % ( + self.dataset_name, + self.image_size, + self.gcn, + self.loss_metric, + self.lambda_A, + self.lambda_B + ) + self.dropout_rate = dropout_rate + self.clip_value = clip_value + self.GAN_type = GAN_type + self.n_critic = n_critic + self.log_freq = log_freq + self.gamma = 10. + self.disc_type = disc_type + self.build_model() + + def build_model(self): + ### define place holders + self.real_A = tf.placeholder(tf.float32,[self.batch_size, self.image_size, self.image_size, + self.A_channels ],name='real_A') + self.real_B = tf.placeholder(tf.float32, [self.batch_size, self.image_size, self.image_size, + self.B_channels ], name='real_B') + + ### define graphs + self.A2B = self.A_g_net(self.real_A, reuse = False) + self.B2A = self.B_g_net(self.real_B, reuse = False) + self.A2B2A = self.B_g_net(self.A2B, reuse = True) + self.B2A2B = self.A_g_net(self.B2A, reuse = True) + + if self.loss_metric == 'L1': + self.A_loss = tf.reduce_mean(tf.abs(self.A2B2A - self.real_A)) + self.B_loss = tf.reduce_mean(tf.abs(self.B2A2B - self.real_B)) + elif self.loss_metric == 'L2': + self.A_loss = tf.reduce_mean(tf.square(self.A2B2A - self.real_A)) + self.B_loss = tf.reduce_mean(tf.square(self.B2A2B - self.real_B)) + + self.Ad_logits_fake = self.A_d_net(self.A2B, reuse = False) + self.Ad_logits_real = self.A_d_net(self.real_B, reuse = True) + + if self.GAN_type == 'wgan-gp': + epsilon_B = tf.random_uniform( + shape=[self.batch_size, 1, 1, 1], minval=0., maxval=1.) + interpolated_image_B = self.real_B + epsilon_B * (self.A2B - self.real_B) + d_interpolated_B = self.A_d_net(interpolated_image_B, reuse = True) + + if self.GAN_type == 'classic': + self.Ad_loss_real = celoss(self.Ad_logits_real, tf.ones_like(self.Ad_logits_real)) + self.Ad_loss_fake = celoss(self.Ad_logits_fake, tf.zeros_like(self.Ad_logits_fake)) + else: + self.Ad_loss_real = -tf.reduce_mean(self.Ad_logits_real) + self.Ad_loss_fake = tf.reduce_mean(self.Ad_logits_fake) + self.Ad_loss = self.Ad_loss_fake + self.Ad_loss_real + if self.GAN_type == 'classic': + self.Ag_loss = celoss(self.Ad_logits_fake, labels=tf.ones_like(self.Ad_logits_fake))+self.lambda_B * self.B_loss + else: + self.Ag_loss = - tf.reduce_mean(self.Ad_logits_fake)+self.lambda_B * self.B_loss + + self.Bd_logits_fake = self.B_d_net(self.B2A, reuse = False) + self.Bd_logits_real = self.B_d_net(self.real_A, reuse = True) + if self.GAN_type == 'wgan-gp': + epsilon_A = tf.random_uniform( + shape=[self.batch_size, 1, 1, 1], minval=0., maxval=1.) + interpolated_image_A = self.real_A + epsilon_A * (self.B2A - self.real_A) + d_interpolated_A = self.B_d_net(interpolated_image_A, reuse = True) + + if self.GAN_type == 'classic': + self.Bd_loss_real = celoss(self.Bd_logits_real, tf.ones_like(self.Bd_logits_real)) + self.Bd_loss_fake = celoss(self.Bd_logits_fake, tf.zeros_like(self.Bd_logits_fake)) + else: + self.Bd_loss_real = -tf.reduce_mean(self.Bd_logits_real) + self.Bd_loss_fake = tf.reduce_mean(self.Bd_logits_fake) + self.Bd_loss = self.Bd_loss_fake + self.Bd_loss_real + if self.GAN_type == 'classic': + self.Bg_loss = celoss(self.Bd_logits_fake, tf.ones_like(self.Bd_logits_fake))+self.lambda_A * self.A_loss + else: + self.Bg_loss = -tf.reduce_mean(self.Bd_logits_fake)+self.lambda_A * self.A_loss + + + self.d_loss = self.Ad_loss + self.Bd_loss + self.g_loss = self.Ag_loss + self.Bg_loss + + if self.GAN_type == 'wgan-gp': + grad_d_interp = tf.gradients( + d_interpolated_A, [interpolated_image_A])[0] + slopes = tf.sqrt(1e-8 + tf.reduce_sum( + tf.square(grad_d_interp), axis=[1, 2, 3])) + gradient_penalty = tf.reduce_mean((slopes - 1.) ** 2) + self.d_loss += self.gamma * gradient_penalty + + grad_d_interp = tf.gradients( + d_interpolated_B, [interpolated_image_B])[0] + slopes = tf.sqrt(1e-8 + tf.reduce_sum( + tf.square(grad_d_interp), axis=[1, 2, 3])) + gradient_penalty = tf.reduce_mean((slopes - 1.) ** 2) + self.d_loss += self.gamma * gradient_penalty + + + ## define trainable variables + t_vars = tf.trainable_variables() + self.A_d_vars = [var for var in t_vars if 'A_d_' in var.name] + self.B_d_vars = [var for var in t_vars if 'B_d_' in var.name] + self.A_g_vars = [var for var in t_vars if 'A_g_' in var.name] + self.B_g_vars = [var for var in t_vars if 'B_g_' in var.name] + self.d_vars = self.A_d_vars + self.B_d_vars + self.g_vars = self.A_g_vars + self.B_g_vars + self.saver = tf.train.Saver() + + def load_random_samples(self): + #np.random.choice( + sample_files =np.random.choice(glob('./datasets/{}/val/A/*.*[g|G]'.format(self.dataset_name)),self.batch_size) + sample_A_imgs = [load_data(f, image_size =self.image_size, flip = False) for f in sample_files] + + sample_files = np.random.choice(glob('./datasets/{}/val/B/*.*[g|G]'.format(self.dataset_name)),self.batch_size) + sample_B_imgs = [load_data(f, image_size =self.image_size, flip = False) for f in sample_files] + + sample_A_imgs = np.reshape(np.array(sample_A_imgs).astype(np.float32),(self.batch_size,self.image_size, self.image_size,-1)) + sample_B_imgs = np.reshape(np.array(sample_B_imgs).astype(np.float32),(self.batch_size,self.image_size, self.image_size,-1)) + return sample_A_imgs, sample_B_imgs + + def sample_shotcut(self, sample_dir, epoch_idx, batch_idx): + sample_A_imgs,sample_B_imgs = self.load_random_samples() + + Ag, A2B2A_imgs, A2B_imgs = self.sess.run([self.A_loss, self.A2B2A, self.A2B], feed_dict={self.real_A: sample_A_imgs, self.real_B: sample_B_imgs}) + Bg, B2A2B_imgs, B2A_imgs = self.sess.run([self.B_loss, self.B2A2B, self.B2A], feed_dict={self.real_A: sample_A_imgs, self.real_B: sample_B_imgs}) + + save_images(A2B_imgs, [self.batch_size,1], './{}/{}/{:06d}_{:04d}_A2B.jpg'.format(sample_dir,self.dir_name , epoch_idx, batch_idx)) + save_images(A2B2A_imgs, [self.batch_size,1], './{}/{}/{:06d}_{:04d}_A2B2A.jpg'.format(sample_dir,self.dir_name, epoch_idx, batch_idx)) + + save_images(B2A_imgs, [self.batch_size,1], './{}/{}/{:06d}_{:04d}_B2A.jpg'.format(sample_dir,self.dir_name, epoch_idx, batch_idx)) + save_images(B2A2B_imgs, [self.batch_size,1], './{}/{}/{:06d}_{:04d}_B2A2B.jpg'.format(sample_dir,self.dir_name, epoch_idx, batch_idx)) + + print("[Sample] A_loss: {:.8f}, B_loss: {:.8f}".format(Ag, Bg)) + fp=open("mylog.txt", 'a') + fp.write("[Sample] A_loss: {:.8f}, B_loss: {:.8f}".format(Ag, Bg) + '\n') + fp.close() + + def train(self, args): + """Train Dual GAN""" + decay = 0.9 + self.d_optim = tf.train.RMSPropOptimizer(args.lr, decay=decay) \ + .minimize(self.d_loss, var_list=self.d_vars) + + self.g_optim = tf.train.RMSPropOptimizer(args.lr, decay=decay) \ + .minimize(self.g_loss, var_list=self.g_vars) + tf.global_variables_initializer().run() + if self.GAN_type == 'wgan': + self.clip_ops = [var.assign(tf.clip_by_value(var, -self.clip_value, self.clip_value)) for var in self.d_vars] + + self.writer = tf.summary.FileWriter("./logs/"+self.dir_name, self.sess.graph) + + step = 1 + start_time = time.time() + + if self.load(self.checkpoint_dir): + print(" [*] Load SUCCESS") + else: + print(" Load failed...ignored...") + print(" start training...") + + for epoch_idx in range(args.epoch): + data_A = glob('./datasets/{}/train/A/*.*[g|G]'.format(self.dataset_name)) + data_B = glob('./datasets/{}/train/B/*.*[g|G]'.format(self.dataset_name)) + np.random.shuffle(data_A) + np.random.shuffle(data_B) + epoch_size = min(len(data_A), len(data_B)) // (self.batch_size) + print('[*] training data loaded successfully') + print("#data_A: %d #data_B:%d" %(len(data_A),len(data_B))) + print('[*] run optimizor...') + + for batch_idx in range(0, epoch_size): + imgA_batch = self.load_training_imgs(data_A, batch_idx) + imgB_batch = self.load_training_imgs(data_B, batch_idx) + if step % self.log_freq == 0: + print("Epoch: [%2d] [%4d/%4d]"%(epoch_idx, batch_idx, epoch_size)) + fp = open("mylog.txt", 'a') + fp.write("Epoch: [%2d] [%4d/%4d]"%(epoch_idx, batch_idx, epoch_size) + '\n') + fp.close() + step = step + 1 + self.run_optim(imgA_batch, imgB_batch, step, start_time, step) + + if np.mod(step, 100) == 1: + self.sample_shotcut(args.sample_dir, epoch_idx, batch_idx) + + if np.mod(step, args.save_freq) == 2: + self.save(args.checkpoint_dir, step) + + def load_training_imgs(self, files, idx): + batch_files = files[idx*self.batch_size:(idx+1)*self.batch_size] + batch_imgs = [load_data(f, image_size =self.image_size, flip = self.flip) for f in batch_files] + + batch_imgs = np.reshape(np.array(batch_imgs).astype(np.float32),(self.batch_size,self.image_size, self.image_size,-1)) + + return batch_imgs + + def run_optim(self,batch_A_imgs, batch_B_imgs, counter, start_time, batch_idx): + + + _, Adfake,Adreal,Bdfake,Bdreal, Ad, Bd = self.sess.run( + [self.d_optim, self.Ad_loss_fake, self.Ad_loss_real, self.Bd_loss_fake, self.Bd_loss_real, self.Ad_loss, self.Bd_loss], + feed_dict = {self.real_A: batch_A_imgs, self.real_B: batch_B_imgs}) + + if 'wgan' == self.GAN_type: + self.sess.run(self.clip_ops) + + if 'wgan' in self.GAN_type: + if batch_idx % self.n_critic == 0: + _, Ag, Bg, Aloss, Bloss = self.sess.run( + [self.g_optim, self.Ag_loss, self.Bg_loss, self.A_loss, self.B_loss], + feed_dict={ self.real_A: batch_A_imgs, self.real_B: batch_B_imgs}) + else: + Ag, Bg, Aloss, Bloss = self.sess.run( + [self.Ag_loss, self.Bg_loss, self.A_loss, self.B_loss], + feed_dict={ self.real_A: batch_A_imgs, self.real_B: batch_B_imgs}) + else: + _, Ag, Bg, Aloss, Bloss = self.sess.run( + [self.g_optim, self.Ag_loss, self.Bg_loss, self.A_loss, self.B_loss], + feed_dict={ self.real_A: batch_A_imgs, self.real_B: batch_B_imgs}) + _, Ag, Bg, Aloss, Bloss = self.sess.run( + [self.g_optim, self.Ag_loss, self.Bg_loss, self.A_loss, self.B_loss], + feed_dict={ self.real_A: batch_A_imgs, self.real_B: batch_B_imgs}) + if batch_idx % self.log_freq == 0: + print("time: %4.4f, Ad: %.2f, Ag: %.2f, Bd: %.2f, Bg: %.2f, U_diff: %.5f, V_diff: %.5f" \ + % (time.time() - start_time, Ad,Ag,Bd,Bg, Aloss, Bloss)) + print("Ad_fake: %.2f, Ad_real: %.2f, Bd_fake: %.2f, Bd_real: %.2f" % (Adfake,Adreal,Bdfake,Bdreal)) + fp = open("mylog.txt", 'a') + fp.write("time: %4.4f, Ad: %.2f, Ag: %.2f, Bd: %.2f, Bg: %.2f, U_diff: %.5f, V_diff: %.5f" \ + % (time.time() - start_time, Ad,Ag,Bd,Bg, Aloss, Bloss) + '\n') + fp.write("Ad_fake: %.2f, Ad_real: %.2f, Bd_fake: %.2f, Bd_real: %.2f" % (Adfake,Adreal,Bdfake,Bdreal) + '\n') + fp.close() + + + def A_d_net(self, imgs, y = None, reuse = False): + return self.discriminator(imgs, prefix = 'A_d_', reuse = reuse) + + def B_d_net(self, imgs, y = None, reuse = False): + return self.discriminator(imgs, prefix = 'B_d_', reuse = reuse) + + def discriminator(self, image, y=None, prefix='A_d_', reuse=False): + # image is 256 x 256 x (input_c_dim + output_c_dim) + with tf.variable_scope(tf.get_variable_scope()) as scope: + if reuse: + scope.reuse_variables() + else: + assert scope.reuse == False + + h0 = lrelu(conv2d(image, self.dcn, k_h=5, k_w=5, name=prefix+'h0_conv')) + # h0 is (128 x 128 x self.dcn) + h1 = lrelu(batch_norm(conv2d(h0, self.dcn*2, name=prefix+'h1_conv'), name = prefix+'bn1')) + # h1 is (64 x 64 x self.dcn*2) + h2 = lrelu(batch_norm(conv2d(h1, self.dcn*4, name=prefix+'h2_conv'), name = prefix+ 'bn2')) + # h2 is (32x 32 x self.dcn*4) + h3 = lrelu(batch_norm(conv2d(h2, self.dcn*8, name=prefix+'h3_conv'), name = prefix+ 'bn3')) + # h3 is (16 x 16 x self.dcn*8) + h3 = lrelu(batch_norm(conv2d(h3, self.dcn*8, name=prefix+'h3_1_conv'), name = prefix+ 'bn3_1')) + # h3 is (8 x 8 x self.dcn*8) + + if self.disc_type == 'patchgan': + h4 = conv2d(h3, 1, name =prefix+'h4') + else: + h4 = linear(tf.reshape(h3, [self.batch_size, -1]), 1, prefix+'d_h3_lin') + + return h4 + + def A_g_net(self, imgs, reuse=False): + return self.fcn(imgs, prefix='A_g_', reuse = reuse) + + + def B_g_net(self, imgs, reuse=False): + return self.fcn(imgs, prefix = 'B_g_', reuse = reuse) + + def fcn(self, imgs, prefix=None, reuse = False): + with tf.variable_scope(tf.get_variable_scope()) as scope: + if reuse: + scope.reuse_variables() + else: + assert scope.reuse == False + + s = self.image_size + s2, s4, s8, s16, s32, s64, s128 = int(s/2), int(s/4), int(s/8), int(s/16), int(s/32), int(s/64), int(s/128) + + # imgs is (256 x 256 x input_c_dim) + e1 = conv2d(imgs, self.gcn, k_h=5, k_w=5, name=prefix+'e1_conv') + # e1 is (128 x 128 x self.gcn) + e2 = batch_norm(conv2d(lrelu(e1), self.gcn*2, name=prefix+'e2_conv'), name = prefix+'bn_e2') + # e2 is (64 x 64 x self.gcn*2) + e3 = batch_norm(conv2d(lrelu(e2), self.gcn*4, name=prefix+'e3_conv'), name = prefix+'bn_e3') + # e3 is (32 x 32 x self.gcn*4) + e4 = batch_norm(conv2d(lrelu(e3), self.gcn*8, name=prefix+'e4_conv'), name = prefix+'bn_e4') + # e4 is (16 x 16 x self.gcn*8) + e5 = batch_norm(conv2d(lrelu(e4), self.gcn*8, name=prefix+'e5_conv'), name = prefix+'bn_e5') + # e5 is (8 x 8 x self.gcn*8) + e6 = batch_norm(conv2d(lrelu(e5), self.gcn*8, name=prefix+'e6_conv'), name = prefix+'bn_e6') + # e6 is (4 x 4 x self.gcn*8) + e7 = batch_norm(conv2d(lrelu(e6), self.gcn*8, name=prefix+'e7_conv'), name = prefix+'bn_e7') + # e7 is (2 x 2 x self.gcn*8) + e8 = batch_norm(conv2d(lrelu(e7), self.gcn*8, name=prefix+'e8_conv'), name = prefix+'bn_e8') + # e8 is (1 x 1 x self.gcn*8) + + self.d1, self.d1_w, self.d1_b = deconv2d(tf.nn.relu(e8), + [self.batch_size, s128, s128, self.gcn*8], name=prefix+'d1', with_w=True) + if self.dropout_rate <= 0.: + d1 = batch_norm(self.d1, name = prefix+'bn_d1') + else: + d1 = tf.nn.dropout(batch_norm(self.d1, name = prefix+'bn_d1'), self.dropout_rate) + d1 = tf.concat([d1, e7],3) + # d1 is (2 x 2 x self.gcn*8*2) + + self.d2, self.d2_w, self.d2_b = deconv2d(tf.nn.relu(d1), + [self.batch_size, s64, s64, self.gcn*8], name=prefix+'d2', with_w=True) + if self.dropout_rate <= 0.: + d2 = batch_norm(self.d2, name = prefix+'bn_d2') + else: + d2 = tf.nn.dropout(batch_norm(self.d2, name = prefix+'bn_d2'), self.dropout_rate) + d2 = tf.concat([d2, e6],3) + # d2 is (4 x 4 x self.gcn*8*2) + + self.d3, self.d3_w, self.d3_b = deconv2d(tf.nn.relu(d2), + [self.batch_size, s32, s32, self.gcn*8], name=prefix+'d3', with_w=True) + if self.dropout_rate <= 0.: + d3 = batch_norm(self.d3, name = prefix+'bn_d3') + else: + d3 = tf.nn.dropout(batch_norm(self.d3, name = prefix+'bn_d3'), self.dropout_rate) + d3 = tf.concat([d3, e5],3) + # d3 is (8 x 8 x self.gcn*8*2) + + self.d4, self.d4_w, self.d4_b = deconv2d(tf.nn.relu(d3), + [self.batch_size, s16, s16, self.gcn*8], name=prefix+'d4', with_w=True) + d4 = batch_norm(self.d4, name = prefix+'bn_d4') + + d4 = tf.concat([d4, e4],3) + # d4 is (16 x 16 x self.gcn*8*2) + + self.d5, self.d5_w, self.d5_b = deconv2d(tf.nn.relu(d4), + [self.batch_size, s8, s8, self.gcn*4], name=prefix+'d5', with_w=True) + d5 = batch_norm(self.d5, name = prefix+'bn_d5') + d5 = tf.concat([d5, e3],3) + # d5 is (32 x 32 x self.gcn*4*2) + + self.d6, self.d6_w, self.d6_b = deconv2d(tf.nn.relu(d5), + [self.batch_size, s4, s4, self.gcn*2], name=prefix+'d6', with_w=True) + d6 = batch_norm(self.d6, name = prefix+'bn_d6') + d6 = tf.concat([d6, e2],3) + # d6 is (64 x 64 x self.gcn*2*2) + + self.d7, self.d7_w, self.d7_b = deconv2d(tf.nn.relu(d6), + [self.batch_size, s2, s2, self.gcn], name=prefix+'d7', with_w=True) + d7 = batch_norm(self.d7, name = prefix+'bn_d7') + d7 = tf.concat([d7, e1],3) + # d7 is (128 x 128 x self.gcn*1*2) + + if prefix == 'B_g_': + self.d8, self.d8_w, self.d8_b = deconv2d(tf.nn.relu(d7),[self.batch_size, s, s, self.A_channels], k_h=5, k_w=5, name=prefix+'d8', with_w=True) + elif prefix == 'A_g_': + self.d8, self.d8_w, self.d8_b = deconv2d(tf.nn.relu(d7),[self.batch_size, s, s, self.B_channels], k_h=5, k_w=5, name=prefix+'d8', with_w=True) + # d8 is (256 x 256 x output_c_dim) + return tf.nn.tanh(self.d8) + + def save(self, checkpoint_dir, step): + model_name = "DualNet.model" + model_dir = self.dir_name + checkpoint_dir = os.path.join(checkpoint_dir, model_dir) + + if not os.path.exists(checkpoint_dir): + os.makedirs(checkpoint_dir) + + self.saver.save(self.sess, + os.path.join(checkpoint_dir, model_name), + global_step=step) + tf.train.write_graph(self.sess.graph, checkpoint_dir, "graph.pbtxt", as_text = True) + + def load(self, checkpoint_dir): + print(" [*] Reading checkpoint...") + + model_dir = self.dir_name + checkpoint_dir = os.path.join(checkpoint_dir, model_dir) + + ckpt = tf.train.get_checkpoint_state(checkpoint_dir) + if ckpt and ckpt.model_checkpoint_path: + ckpt_name = os.path.basename(ckpt.model_checkpoint_path) + self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name)) + return True + else: + return False + + def test(self, args): + """Test DualNet""" + start_time = time.time() + tf.global_variables_initializer().run() + if self.load(self.checkpoint_dir): + print(" [*] Load SUCCESS") + test_dir = './{}/{}'.format(args.test_dir, self.dir_name) + if not os.path.exists(test_dir): + os.makedirs(test_dir) + test_log = open(test_dir+'evaluation.txt','a') + test_log.write(self.dir_name) + self.test_domain(args, test_log, type = 'A') + self.test_domain(args, test_log, type = 'B') + test_log.close() + + def test_domain(self, args, test_log, type = 'A'): + test_files = glob('./datasets/{}/val/{}/*.*[g|G]'.format(self.dataset_name,type)) + # load testing input + print("Loading testing images ...") + test_imgs = [load_data(f, is_test=True, image_size =self.image_size, flip = args.flip) for f in test_files] + print("#images loaded: %d"%(len(test_imgs))) + test_imgs = np.reshape(np.asarray(test_imgs).astype(np.float32),(len(test_files),self.image_size, self.image_size,-1)) + test_imgs = [test_imgs[i*self.batch_size:(i+1)*self.batch_size] + for i in range(0, len(test_imgs)//self.batch_size)] + test_imgs = np.asarray(test_imgs) + test_path = './{}/{}/'.format(args.test_dir, self.dir_name) + # test input samples + if type == 'A': + for i in range(0, len(test_files)//self.batch_size): + filename_o = test_files[i*self.batch_size].split('/')[-1].split('.')[0] + print(filename_o) + idx = i+1 + A_imgs = np.reshape(np.array(test_imgs[i]), (self.batch_size,self.image_size, self.image_size,-1)) + print("testing A image %d"%(idx)) + print(A_imgs.shape) + A2B_imgs, A2B2A_imgs = self.sess.run( + [self.A2B, self.A2B2A], + feed_dict={self.real_A: A_imgs} + ) + save_images(A_imgs, [self.batch_size, 1], test_path+filename_o+'_realA.jpg') + save_images(A2B_imgs, [self.batch_size, 1], test_path+filename_o+'_A2B.jpg') + save_images(A2B2A_imgs, [self.batch_size, 1], test_path+filename_o+'_A2B2A.jpg') + elif type=='B': + for i in range(0, len(test_files)//self.batch_size): + filename_o = test_files[i*self.batch_size].split('/')[-1].split('.')[0] + idx = i+1 + B_imgs = np.reshape(np.array(test_imgs[i]), (self.batch_size,self.image_size, self.image_size,-1)) + print("testing B image %d"%(idx)) + B2A_imgs, B2A2B_imgs = self.sess.run( + [self.B2A, self.B2A2B], + feed_dict={self.real_B:B_imgs} + ) + save_images(B_imgs, [self.batch_size, 1],test_path+filename_o+'_realB.jpg') + save_images(B2A_imgs, [self.batch_size, 1],test_path+filename_o+'_B2A.jpg') + save_images(B2A2B_imgs, [self.batch_size, 1],test_path+filename_o+'_B2A2B.jpg') \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/modelzoo_level.txt b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/modelzoo_level.txt new file mode 100644 index 000000000..31529da2e --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/modelzoo_level.txt @@ -0,0 +1,3 @@ +FuncStatus:OK +PerfStatus:OK +PrecisionStatus:OK \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/ops.py b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/ops.py new file mode 100644 index 000000000..81fbad0c3 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/ops.py @@ -0,0 +1,103 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# Copyright 2021 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 numpy as np +import tensorflow as tf + +#from tensorflow.python.framework import ops + +from utils import * + +def batch_norm(x, name="batch_norm"): + eps = 1e-6 + with tf.variable_scope(name): + nchannels = x.get_shape()[3] + scale = tf.get_variable("scale", [nchannels], initializer=tf.random_normal_initializer(1.0, 0.02, dtype=tf.float32)) + center = tf.get_variable("center", [nchannels], initializer=tf.constant_initializer(0.0, dtype = tf.float32)) + ave, dev = tf.nn.moments(x, axes=[1,2], keep_dims=True) + inv_dev = tf.rsqrt(dev + eps) + normalized = (x-ave)*inv_dev * scale + center + return normalized + +def conv2d(input_, output_dim, + k_h=3, k_w=3, d_h=2, d_w=2, stddev=0.02, + name="conv2d"): + with tf.variable_scope(name): + w = tf.get_variable('w', [k_h, k_w, input_.get_shape()[-1], output_dim], + initializer=tf.truncated_normal_initializer(stddev=stddev)) + conv = tf.nn.conv2d(input_, w, strides=[1, d_h, d_w, 1], padding='SAME') + + biases = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0)) + conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape()) + + return conv + +def deconv2d(input_, output_shape, + k_h=3, k_w=3, d_h=2, d_w=2, stddev=0.02, + name="deconv2d", with_w=False): + with tf.variable_scope(name): + # filter : [height, width, output_channels, in_channels] + w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]], + initializer=tf.random_normal_initializer(stddev=stddev)) + try: + deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape, + strides=[1, d_h, d_w, 1]) + + # Support for verisons of TensorFlow before 0.7.0 + except AttributeError: + deconv = tf.nn.deconv2d(input_, w, output_shape=output_shape, + strides=[1, d_h, d_w, 1]) + + biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0)) + deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape()) + + if with_w: + return deconv, w, biases + else: + return deconv + +def lrelu(x, leak=0.2, name="lrelu"): + return tf.maximum(x, leak*x) + +def celoss(logits, labels): + return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels)) + + +def linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False): + shape = input_.get_shape().as_list() + + with tf.variable_scope(scope or "Linear"): + matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32, + tf.random_normal_initializer(stddev=stddev)) + bias = tf.get_variable("bias", [output_size], + initializer=tf.constant_initializer(bias_start)) + if with_w: + return tf.matmul(input_, matrix) + bias, matrix, bias + else: + return tf.matmul(input_, matrix) + bias diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/requirements.txt b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/requirements.txt new file mode 100644 index 000000000..7dc6a21c3 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/requirements.txt @@ -0,0 +1,4 @@ +Python==3.6 +tensorflow-gpu==1.15.0 +numpy +scipy \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/utils.py b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/utils.py new file mode 100644 index 000000000..1eb517257 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/utils.py @@ -0,0 +1,107 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# Copyright 2021 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. + +""" +Some codes from https://github.com/Newmu/dcgan_code +""" +from __future__ import division +import random +import cv2 +import numpy as np +import os +from time import gmtime, strftime + + + + +def load_data(image_path, flip=False, is_test=False, image_size = 128): + img = load_image(image_path) + img = preprocess_img(img, img_size=image_size, flip=flip, is_test=is_test) + + img = img/127.5 - 1. + if len(img.shape)<3: + img = np.expand_dims(img, axis=2) + return img + +def load_image(image_path): + img = imread(image_path) + return img + +def preprocess_img(img, img_size=128, flip=False, is_test=False): + img = cv2.resize(img, (img_size, img_size)) + if (not is_test) and flip and np.random.random() > 0.5: + img = np.fliplr(img) + return img + +def get_image(image_path, image_size, is_crop=True, resize_w=64, is_grayscale = False): + return transform(imread(image_path, is_grayscale), image_size, is_crop, resize_w) + +def save_images(images, size, image_path): + dir = os.path.dirname(image_path) + if not os.path.exists(dir): + os.makedirs(dir) + return imsave(inverse_transform(images), size, image_path) + +def imread(path, is_grayscale = False): + if (is_grayscale): + return cv2.imread(path, flatten = True)#.astype(np.float) + else: + return cv2.imread(path)#.astype(np.float) + +def merge_images(images, size): + return inverse_transform(images) + +def merge(images, size): + h, w = images.shape[1], images.shape[2] + if len(images.shape) < 4: + img = np.zeros((h * size[0], w * size[1], 1)) + images = np.expand_dims(images, axis = 3) + else: + img = np.zeros((h * size[0], w * size[1], images.shape[3])) + for idx, image in enumerate(images): + i = idx % size[1] + j = idx // size[1] + img[j*h:j*h+h, i*w:i*w+w, :] = image + if images.shape[3] ==1: + return np.concatenate([img,img,img],axis=2) + else: + return img.astype(np.uint8) + +def imsave(images, size, path): + return cv2.imwrite(path, merge(images, size)) + +def transform(image, npx=64, is_crop=True, resize_w=64): + # npx : # of pixels width/height of image + if is_crop: + cropped_image = center_crop(image, npx, resize_w=resize_w) + else: + cropped_image = image + return np.array(cropped_image)/127.5 - 1. + +def inverse_transform(images): + return ((images+1.)*127.5) \ No newline at end of file -- Gitee From 1e074df5931b4de16e35a4b27bd0b1d32034c7a3 Mon Sep 17 00:00:00 2001 From: lufeng96 <1600925872@qq.com> Date: Sat, 2 Apr 2022 13:12:02 +0000 Subject: [PATCH 3/3] update ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md. --- .../cv/DualGAN_ID1001_for_ACL/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md index 502833ae4..ff8ccdc71 100644 --- a/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md +++ b/ACL_TensorFlow/contrib/cv/DualGAN_ID1001_for_ACL/README.md @@ -22,12 +22,12 @@ python freeze_graph.py --ckpt_path=./ckpt/DualNet.model-99002 在Ascend310推理服务器下,使用ATC模型转换工具进行模型转换: -AtoB模型转换 +A2B模型转换 ``` atc --model=/root/dualgan/pb_model/A2B.pb --framework=3 --output=/root/dualgan/A2B --soc_version=Ascend310 --input_shape="img:1,256,256,3" --log=info --out_nodes="output:0" --op_select_implmode=high_precision ``` -BtoA模型转换 +B2A模型转换 ``` atc --model=/root/dualgan/pb_model/B2A.pb --framework=3 --output=/root/dualgan/B2A --soc_version=Ascend310 --input_shape="img:1,256,256,3" --log=info --out_nodes="output:0" --op_select_implmode=high_precision ``` @@ -44,19 +44,19 @@ atc --model=/root/dualgan/pb_model/B2A.pb --framework=3 --output=/root/dualgan/B data_dir=测试集路径 -data_bin_dir=转化后的的bin数据集路径 +data_bin_dir=转化后的bin数据集路径 ``` -python3 jpg2bin.py --datasets_dir=${data_dir} --output_dir=${data_bin_dir} +python3 jpg2bin.py --datasets_dir=data_dir --output_dir=data_bin_dir ``` ## 2.推理 -使用AtoB.om模型推理(input为输入数据路径,output为输出数据路径) +使用A2B.om模型推理(input为输入数据路径,output为输出数据路径) ``` ./msame --model /root/dualgan/A2B.om --input "/root/dualgan/data_bin" --output "/root/dualgan/outbin" --outfmt BIN ``` -使用BtoA.om模型转换 +使用B2A.om模型推理 ``` ./msame --model /root/dualgan/B2A.om --input "/root/dualgan/data_bin" --output "/root/dualgan/outbin" --outfmt BIN ``` @@ -65,9 +65,9 @@ python3 jpg2bin.py --datasets_dir=${data_dir} --output_dir=${data_bin_dir} 推理结果为二进制.bin格式数据,需要将其转换为可视的.jpg格式图片。 -input_dir=/输入.bin数据路径 +input_dir=输入.bin数据路径 -output_dir=/输出路径 +output_dir=输出路径 ``` -python3 bin2jpg.py --data_dir=${input_dir} --dst_dir=${output_dir} +python3 bin2jpg.py --data_dir=input_dir --dst_dir=output_dir ``` \ No newline at end of file -- Gitee