From 16f048d5b1d82314d8bbd0bb68cfd49d74846f1f Mon Sep 17 00:00:00 2001 From: yfggdyx Date: Sun, 26 Feb 2023 17:30:14 +0800 Subject: [PATCH] Add basicVSRplusplus --- .../BasicVSR_plusplus/LICENSE | 201 ++++++++++++++ .../BasicVSR_plusplus/README.md | 256 ++++++++++++++++++ .../BasicVSR_plusplus/basicvsr_plusplus.patch | 161 +++++++++++ .../basicvsr_plusplus_postprocess.py | 67 +++++ .../basicvsr_plusplus_preprocess.py | 72 +++++ .../BasicVSR_plusplus/modelzoo_level.txt | 4 + .../BasicVSR_plusplus/requirements.txt | 3 + 7 files changed, 764 insertions(+) create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/LICENSE create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/README.md create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus.patch create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_postprocess.py create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_preprocess.py create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/modelzoo_level.txt create mode 100644 ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/requirements.txt diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/LICENSE b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/LICENSE new file mode 100644 index 0000000000..3d33284651 --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/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. \ No newline at end of file diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/README.md b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/README.md new file mode 100644 index 0000000000..dfdee14469 --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/README.md @@ -0,0 +1,256 @@ +# BasicVSR_plusplus模型-推理指导 + + +- [概述](#ZH-CN_TOPIC_0000001172161501) + + - [输入输出数据](#section540883920406) + + + +- [推理环境准备](#ZH-CN_TOPIC_0000001126281702) + +- [快速上手](#ZH-CN_TOPIC_0000001126281700) + + - [获取源码](#section4622531142816) + - [准备数据集](#section183221994411) + - [模型推理](#section741711594517) + +- [模型推理性能&精度](#ZH-CN_TOPIC_0000001172201573) + + ****** + +# 概述 + +BasicVSR++通过设计二阶网格传播和流引导的可变形对齐来重新设计BasicVSR,使信息能够更有效地传播和聚合,[论文链接](https://arxiv.org/abs/2104.13371)。 + +- 参考实现: + + ``` + url=https://github.com/open-mmlab/mmediting.git + commit_id=6ac402ec4545398662f043186688a40fb2e97d5f + code_path=https://github.com/open-mmlab/mmediting/tree/master/configs/restorers/basicvsr_plusplus + model_name=BasicVSR_plusplus + ``` + +## 输入输出数据 + +- 输入数据 + + | 输入数据 | 数据类型 | 大小 | 数据排布格式 | + | -------- | -------- | ------------------------- | ------------ | + | input | RGB_FP32 | 1 x 14 x 3 x 64 x 112 | NCDHW | + + +- 输出数据 + + | 输出数据 | 数据类型 | 大小 | 数据排布格式 | + | -------- | -------- | -------- | ------------ | + | output1 | FLOAT32 | 1 x 14 x 3 x 256 x 448| NCDHW | + + +# 推理环境准备 + +- 该模型需要以下插件与驱动 + + **表 1** 版本配套表 + + | 配套 | 版本 | 环境准备指导 | + | ------------------------------------------------------------ | ------- | ------------------------------------------------------------ | + | 固件与驱动 | 22.0.3 | [Pytorch框架推理环境准备](https://www.hiascend.com/document/detail/zh/ModelZoo/pytorchframework/pies) | + | CANN | 6.0.RC1 | - | + | Python | 3.7.5 | - | + | PyTorch | 1.8.1 | - | + | 说明:Atlas 300I Duo 推理卡请以CANN版本选择实际固件与驱动版本。 | \ | \ | + + + +# 快速上手 + +## 获取源码 + +1. 获取源码。 + + ``` + git clone https://github.com/open-mmlab/mmediting.git + ``` + +2. 安装依赖。 + + ``` + pip3 install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html + pip3 install -U openmim + mim install mmcv-full + + cd mmediting + git reset --hard 6ac402ec4545398662f043186688a40fb2e97d5f + git apply ../basicvsr_plusplus.patch + pip3 install -r requirements.txt + pip3 install -v -e . + cd .. + pip3 install -r requirements.txt + ``` + +## 准备数据集 + +1. 获取原始数据集。(解压命令参考tar –xvf \*.tar与 unzip \*.zip) + + 模型推理使用Vimeo90K数据集,数据集介绍参考[官网链接](http://toflow.csail.mit.edu/),测试数据集[下载链接](http://data.csail.mit.edu/tofu/testset/vimeo_super_resolution_test.zip) + + 下载后将vimeo_super_resolution_test.zip放在当前工作目录。 + +2. 数据预处理,将原始数据集转换为模型输入的数据。 + + 数据预处理命令如下。 + + ``` + unzip -d ./data/ vimeo_super_resolution_test.zip + mv ./data/vimeo_super_resolution_test ./data/vimeo90k + mv ./data/vimeo90k/low_resolution ./data/vimeo90k/BIx4 + mv ./data/vimeo90k/target ./data/vimeo90k/GT + + python3 ./mmediting/tools/data/super-resolution/vimeo90k/preprocess_vimeo90k_dataset.py ./data/vimeo90k/sep_testlist.txt + mv ./data/vimeo90k/meta_info_Vimeo90K_GT.txt ./data/vimeo90k/meta_info_Vimeo90K_test_GT.txt + + python3 basicvsr_plusplus_preprocess.py \ + ./mmediting/configs/restorers/basicvsr_plusplus/basicvsr_plusplus_c64n7_4x2_300k_vimeo90k_bi.py \ + ./pre_data + ``` + + 生成并保存为bin文件,保存目录为`./pre_data`。 + + +## 模型推理 + +1. 模型转换。 + + 使用PyTorch将模型权重文件.pkl转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。 + + 1. 获取权重文件。 + + 权重文件为:basicvsr_plusplus_c64n7_8x1_300k_vimeo90k_bi_20210305-4ef437e2.pth,[获取链接](https://download.openmmlab.com/mmediting/restorers/basicvsr_plusplus/basicvsr_plusplus_c64n7_8x1_300k_vimeo90k_bi_20210305-4ef437e2.pth) + 将获取的权重文件放在当前工作路径下,参考命令: + + ``` + wget https://download.openmmlab.com/mmediting/restorers/basicvsr_plusplus/basicvsr_plusplus_c64n7_8x1_300k_vimeo90k_bi_20210305-4ef437e2.pth + ``` + + 2. 导出onnx文件。 + + 1. 使用pytorch2onnx.py导出onnx文件。 + + 运行pytorch2onnx.py脚本。 + + ``` + python3 mmediting/tools/pytorch2onnx.py \ + mmediting/configs/restorers/basicvsr_plusplus/basicvsr_plusplus_c64n7_4x2_300k_vimeo90k_bi.py \ + ./basicvsr_plusplus_c64n7_8x1_300k_vimeo90k_bi_20210305-4ef437e2.pth \ + restorer \ + data/vimeo90k/BIx4/00001/0266/ \ + --output-file ./basicvsr_plusplus.onnx + ``` + + 获得basicvsr_plusplus.onnx文件。 + + 3. 使用ATC工具将ONNX模型转OM模型。 + + 1. 配置环境变量。 + + ``` + source /usr/local/Ascend/ascend-toolkit/set_env.sh + ``` + + 2. 执行命令查看芯片名称($\{chip\_name\})。 + + ``` + npu-smi info + #该设备芯片名为Ascend310P3 (自行替换) + 回显如下: + +-------------------+-----------------+------------------------------------------------------+ + | NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page) | + | Chip Device | Bus-Id | AICore(%) Memory-Usage(MB) | + +===================+=================+======================================================+ + | 0 310P3 | OK | 15.8 42 0 / 0 | + | 0 0 | 0000:82:00.0 | 0 1074 / 21534 | + +===================+=================+======================================================+ + | 1 310P3 | OK | 15.4 43 0 / 0 | + | 0 1 | 0000:89:00.0 | 0 1070 / 21534 | + +===================+=================+======================================================+ + ``` + + 3. 执行ATC命令。 + + ``` + atc --framework=5 \ + --model=./basicvsr_plusplus.onnx \ + --input_shape="input:1,14,3,64,112" \ + --output=./basicvsr_plusplus_bs1 \ + --input_format=NCHW \ + --log=error \ + --soc_version=Ascend${chip_name} + ``` + + - 参数说明: + + - --model:为ONNX模型文件。 + - --framework:5代表ONNX模型。 + - --output:输出的OM模型。 + - --input\_format:输入数据的格式。 + - --input\_shape:输入数据的shape。 + - --log:日志级别。 + - --soc\_version:处理器型号。 + + 运行成功后生成`basicvsr_plusplus_bs1.om`模型文件。 + +2. 开始推理验证。 + + 1. 安装ais_bench推理工具。 + + 请访问[ais_bench推理工具](https://gitee.com/ascend/tools/tree/master/ais-bench_workload/tool/ais_infer)代码仓,根据readme文档进行工具安装。 + + 2. 执行推理。 + + ``` + python3 -m ais_bench --model=basicvsr_plusplus_bs1.om --input=./pre_data/vimeo90k/BIx4 --output=./ --outfmt=NPY --batchsize=1 + ``` + + - 参数说明: + + - --model:om模型。 + - --input:输入数据路径。 + - --output:推理结果路径。 + - --batchsize:om模型的batchsize。 + + 推理后的输出默认在当前目录下。 + + + 3. 精度验证。 + + 调用`basicvsr_plusplus_postprocess.py`脚本将om模型的推理结果。 + + ``` + python3 basicvsr_plusplus_postprocess.py ./pre_data ${output_path} + ``` + + 第一个参数为预处理数据路径,第二个参数${output_path}为推理工具生成的推理结果路径。 + + 4. 性能验证。 + + 可使用ais_bench推理工具的纯推理模式验证不同batch_size的om模型的性能,参考命令如下: + + ``` + python3 -m ais_bench --model=basicvsr_plusplus_bs1.om --loop=20 --batchsize=1 + ``` + + - 参数说明: + - --model:om模型。 + - --loop:模型推理次数。 + - --batchsize:om模型的batchsize。 + + +# 模型推理性能&精度 + +调用ACL接口推理计算,性能参考下列数据。 + +| 芯片型号 | Batch Size | 数据集 | 精度 | 性能 | +| --------- | ---------- | ---------- | ---------- | --------------- | +| Ascend310P | 1 | Vimeo90K | PSNR=35.04, SSIM=0.9198 | 2.27 fps | diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus.patch b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus.patch new file mode 100644 index 0000000000..24c347f2ff --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus.patch @@ -0,0 +1,161 @@ +diff --git a/mmedit/models/backbones/sr_backbones/basicvsr_pp.py b/mmedit/models/backbones/sr_backbones/basicvsr_pp.py +index 082cf618..790a1f34 100644 +--- a/mmedit/models/backbones/sr_backbones/basicvsr_pp.py ++++ b/mmedit/models/backbones/sr_backbones/basicvsr_pp.py +@@ -368,6 +368,52 @@ class BasicVSRPlusPlus(nn.Module): + f'But received {type(pretrained)}.') + + ++class DeformConv2dFunction(torch.autograd.Function): ++ @staticmethod ++ def forward(ctx, ++ input, ++ weight, ++ offset, ++ bias, ++ stride=1, ++ padding=0, ++ dilation=1, ++ groups=1, ++ deform_groups=1): ++ output_size = [input.shape[0], weight.shape[0]] ++ for d in range(input.dim() - 2): ++ in_size = input.size(d + 2) ++ kernel = dilation * (weight.size(d + 2) - 1) + 1 ++ output_size.append((in_size + (2 * padding) - kernel) // stride + 1) ++ out = torch.randn(output_size).to(input.dtype) ++ return out ++ ++ @staticmethod ++ def symbolic(g, ++ input, ++ weight, ++ offset, ++ bias, ++ stride, ++ padding, ++ dilation, ++ groups, ++ deform_groups): ++ return g.op( ++ "DeformableConv2D", ++ input, ++ weight, ++ offset, ++ bias, ++ strides_i=[stride,stride], ++ pads_i=[padding,padding], ++ dilations_i=dilation, ++ groups_i=groups, ++ deformable_groups_i=deform_groups) ++ ++deform_conv2d = DeformConv2dFunction.apply ++ ++ + class SecondOrderDeformableAlignment(ModulatedDeformConv2d): + """Second-order deformable alignment module. + +@@ -425,8 +471,15 @@ class SecondOrderDeformableAlignment(ModulatedDeformConv2d): + + # mask + mask = torch.sigmoid(mask) +- +- return modulated_deform_conv2d(x, offset, mask, self.weight, self.bias, +- self.stride, self.padding, +- self.dilation, self.groups, +- self.deform_groups) ++ offset_x = offset[:, 0::2, :, :] ++ offset_y = offset[:, 1::2, :, :] ++ offset = torch.cat([offset_y, offset_x, mask], dim=1) ++ return deform_conv2d(x, ++ self.weight, ++ offset, ++ self.bias, ++ self.stride[0], ++ self.padding[0], ++ self.dilation[0], ++ self.groups, ++ self.deform_groups) +diff --git a/mmedit/models/common/flow_warp.py b/mmedit/models/common/flow_warp.py +index 7083230d..d34c9cd8 100644 +--- a/mmedit/models/common/flow_warp.py ++++ b/mmedit/models/common/flow_warp.py +@@ -3,6 +3,36 @@ import torch + import torch.nn.functional as F + + ++class GridSampler(torch.autograd.Function): ++ @staticmethod ++ def forward(ctx, ++ x, ++ grid, ++ mode='bilinear', ++ padding_mode='zeros', ++ align_corners=False): ++ output_size = x.shape ++ out = torch.randn(output_size).to(x.dtype) ++ return out ++ ++ @staticmethod ++ def symbolic(g, ++ x, ++ grid, ++ mode, ++ padding_mode, ++ align_corners): ++ return g.op( ++ "GridSample", ++ x, ++ grid, ++ mode_s=mode, ++ padding_mode_s=padding_mode, ++ align_corners_i=align_corners) ++ ++grid_sample = GridSampler.apply ++ ++ + def flow_warp(x, + flow, + interpolation='bilinear', +@@ -41,10 +71,5 @@ def flow_warp(x, + grid_flow_x = 2.0 * grid_flow[:, :, :, 0] / max(w - 1, 1) - 1.0 + grid_flow_y = 2.0 * grid_flow[:, :, :, 1] / max(h - 1, 1) - 1.0 + grid_flow = torch.stack((grid_flow_x, grid_flow_y), dim=3) +- output = F.grid_sample( +- x, +- grid_flow, +- mode=interpolation, +- padding_mode=padding_mode, +- align_corners=align_corners) ++ output = grid_sample(x, grid_flow, interpolation, padding_mode, align_corners) + return output +diff --git a/tools/pytorch2onnx.py b/tools/pytorch2onnx.py +index 4bf524c7..46eafa33 100644 +--- a/tools/pytorch2onnx.py ++++ b/tools/pytorch2onnx.py +@@ -73,7 +73,8 @@ def pytorch2onnx(model, + keep_initializers_as_inputs=False, + verbose=show, + opset_version=opset_version, +- dynamic_axes=dynamic_axes) ++ dynamic_axes=dynamic_axes, ++ enable_onnx_checker=False) + print(f'Successfully exported ONNX model: {output_file}') + if verify: + # check by onnx +@@ -192,6 +193,15 @@ if __name__ == '__main__': + data = dict(merged_path=args.img_path, trimap_path=args.trimap_path) + elif model_type == 'restorer': + data = dict(lq_path=args.img_path) ++ data['lq_path'] = [ ++ 'data/vimeo90k/BIx4/00001/0266/im1.png', ++ 'data/vimeo90k/BIx4/00001/0266/im2.png', ++ 'data/vimeo90k/BIx4/00001/0266/im3.png', ++ 'data/vimeo90k/BIx4/00001/0266/im4.png', ++ 'data/vimeo90k/BIx4/00001/0266/im5.png', ++ 'data/vimeo90k/BIx4/00001/0266/im6.png', ++ 'data/vimeo90k/BIx4/00001/0266/im7.png'] ++ data['key'] = '00001/0266' + data = test_pipeline(data) + + # convert model to onnx file diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_postprocess.py b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_postprocess.py new file mode 100644 index 0000000000..81f3d41df9 --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_postprocess.py @@ -0,0 +1,67 @@ +# Copyright 2023 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 os +import torch +import argparse +import numpy as np + +from tqdm import tqdm +from mmedit.core import tensor2img +from mmedit.core.evaluation import psnr, ssim + + +def parse_args(): + parser = argparse.ArgumentParser(description='basicvsr_plusplus data preprocess.') + parser.add_argument('pre_path', help='save bin path') + parser.add_argument('res_path', help='results path') + args = parser.parse_args() + return args + + +def main(): + args = parse_args() + + gt_path = "{}/vimeo90k/GT".format(args.pre_path) + res_path = args.res_path + + eval_result = dict() + eval_result['PSNR'] = 0 + eval_result['SSIM'] = 0 + + for root, dirs, files in os.walk(res_path): + for idx, f in enumerate(tqdm(files)): + res = torch.from_numpy(np.load(f"{res_path}/{f}")) + gt = torch.from_numpy(np.load(f"{gt_path}/{f.replace('_0.', '.')}")) + + t = res.size(1) + res = 0.5 * (res[:, t // 4] + res[:, -1 - t // 4]) + + res_img = tensor2img(res) + gt_img = tensor2img(gt) + + psnr_val = psnr(res_img, gt_img, 0, convert_to='y') + ssim_val = ssim(res_img, gt_img, 0, convert_to='y') + + eval_result['PSNR'] += psnr_val + eval_result['SSIM'] += ssim_val + + psnr_res = eval_result['PSNR'] / len(files) + ssim_res = eval_result['SSIM'] / len(files) + + print(f"The results of postprocess: PSNR={psnr_res}, SSIM={ssim_res}") + + +if __name__ == '__main__': + main() diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_preprocess.py b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_preprocess.py new file mode 100644 index 0000000000..c733cee853 --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/basicvsr_plusplus_preprocess.py @@ -0,0 +1,72 @@ +# Copyright 2023 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 os +import mmcv +import torch +import shutil +import argparse +import numpy as np + +from tqdm import tqdm +from mmcv import Config +from mmedit.datasets import build_dataloader, build_dataset + + +def parse_args(): + parser = argparse.ArgumentParser(description='basicvsr_plusplus data preprocess.') + parser.add_argument('config', help='config file path') + parser.add_argument('pre_path', help='save bin path') + args = parser.parse_args() + return args + + +def main(): + args = parse_args() + + data_path = "{}/vimeo90k".format(args.pre_path) + lq_path = "{}/vimeo90k/BIx4".format(args.pre_path) + gt_path = "{}/vimeo90k/GT".format(args.pre_path) + + if not os.path.exists(data_path): + os.makedirs(lq_path) + os.makedirs(gt_path) + else: + shutil.rmtree(data_path) + os.makedirs(lq_path) + os.makedirs(gt_path) + + cfg = Config.fromfile(args.config) + dataset = build_dataset(cfg.data.test) + + loader_cfg = { + **dict((k, cfg.data[k]) for k in ['workers_per_gpu'] if k in cfg.data), + **dict( + samples_per_gpu=1, + drop_last=False, + shuffle=False, + dist=False), + **cfg.data.get('test_dataloader', {}) + } + + data_loader = build_dataloader(dataset, **loader_cfg) + + for idx, data in enumerate(tqdm(data_loader)): + data_name = data['meta'].data[0][0]['key'].replace('/', '_') + np.save(f"{lq_path}/{data_name}.npy", data['lq'].numpy()) + np.save(f"{gt_path}/{data_name}.npy", data['gt'].numpy()) + + +if __name__ == '__main__': + main() diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/modelzoo_level.txt b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/modelzoo_level.txt new file mode 100644 index 0000000000..55d7316449 --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/modelzoo_level.txt @@ -0,0 +1,4 @@ +FuncStatus:OK +PerfStatus:PERFECT +PrecisionStatus:OK +ModelConvert:OK \ No newline at end of file diff --git a/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/requirements.txt b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/requirements.txt new file mode 100644 index 0000000000..334c8b67f2 --- /dev/null +++ b/ACL_PyTorch/contrib/cv/super_resolution/BasicVSR_plusplus/requirements.txt @@ -0,0 +1,3 @@ +-r mmediting/requirements/runtime.txt +-r mmediting/requirements/tests.txt +onnx==1.13.0 \ No newline at end of file -- Gitee