├── .DS_Store ├── .gitignore ├── LICENSE ├── README-CN.md ├── README.md ├── audio_denoising ├── .DS_Store └── dtln │ ├── .DS_Store │ ├── README.md │ ├── dtln.cpp │ └── models │ ├── .DS_Store │ ├── convert1.py │ ├── convert2.py │ ├── dtln_1.param │ └── dtln_2.param ├── contribute.md ├── diffusion └── stablediffuson │ └── README.md ├── docs ├── .DS_Store ├── Cmakelists.txt └── images │ └── logo.png ├── face_dection ├── .DS_Store ├── Anime_Face │ ├── .DS_Store │ ├── README.md │ ├── anime_face.cpp │ └── models │ │ ├── anime-face_hrnetv2.param │ │ └── convert.py ├── pfld │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── pfld-sim.param │ └── pfld.cpp └── ultraface │ ├── .DS_Store │ ├── README.md │ ├── UltraFace.cpp │ ├── UltraFace.h │ ├── main.cpp │ └── models │ ├── .DS_Store │ ├── convert.py │ ├── version-RFB-320.param │ └── version-RFB-640.param ├── face_restoration ├── .DS_Store ├── codeformer │ └── README.md └── gfpgan │ ├── .DS_Store │ ├── README.md │ ├── gfpgan.cpp │ ├── gfpgan.h │ ├── main.cpp │ └── models │ └── downlaod.txt ├── face_swap └── roop │ ├── README.md │ └── inswapper_128.param ├── image_classification ├── .DS_Store ├── cait │ ├── .DS_Store │ ├── Cait.cpp │ ├── README.md │ └── models │ │ ├── cait_xxs36_384.param │ │ ├── convert.py │ │ └── download.txt ├── denseNet │ ├── .DS_Store │ ├── README.md │ ├── densenet.cpp │ └── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── densenet121.param ├── efficientnet │ ├── .DS_Store │ ├── README.md │ ├── efficientnet.cpp │ └── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── efficientnet_b0.param ├── mobilenet_v2 │ ├── .DS_Store │ ├── README.md │ ├── mobilenet_v2.cpp │ └── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── mobilenet_v2.param ├── mobilenet_v3 │ ├── .DS_Store │ ├── README.md │ ├── mobilenet_v3.cpp │ └── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── mobilenet_v3.param ├── res2net │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── convert.py │ │ └── res2net101_26w_4s.param │ └── res2net.cpp ├── res2next50 │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── convert.py │ │ └── res2next50.param │ └── res2next50.cpp ├── resnet18 │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ ├── download.txt │ │ └── resnet18.param │ └── resnet18.cpp ├── shufflenetv2 │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── shufflenet_v2.param │ └── shufflenetv2.cpp └── vgg19 │ ├── .DS_Store │ ├── README.md │ ├── models │ ├── convert.py │ ├── download.txt │ └── vgg16.param │ └── vgg16.cpp ├── image_inpainting ├── .DS_Store ├── deoldify │ ├── .DS_Store │ ├── README.md │ ├── deoldify.cpp │ ├── input.png │ ├── models │ │ ├── convert.py │ │ ├── deoldify.256.param │ │ └── download.txt │ └── output.jpg └── mat │ ├── README.md │ └── mat.cpp ├── image_matting ├── .DS_Store ├── RVM │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── convert.py │ │ └── download.txt │ └── rvm.cpp ├── deeplabv3 │ ├── .DS_Store │ ├── README.md │ ├── deeplabv3.cpp │ └── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ ├── deeplabv3_mobilenet_v3_large.param │ │ ├── deeplabv3_resnet101.param │ │ ├── deeplabv3_resnet50.param │ │ └── download.txt ├── vitae │ ├── .DS_Store │ ├── README.md │ └── models │ │ ├── P3M-Net_ViTAE-S_trained_on_P3M-10k.param │ │ └── convert.py └── yolov7_mask │ ├── README.md │ └── yolov7_mask.cpp ├── nlp ├── .DS_Store └── gpt2-chinese │ ├── README.md │ └── gpt2.cpp ├── object_dection ├── .DS_Store ├── fastestdet │ ├── README.md │ ├── fastestdet.cpp │ ├── fastestdet.png │ └── pt2ncnn.sh ├── hybridnets │ ├── .DS_Store │ ├── README.md │ ├── hybridnets.cpp │ └── models │ │ └── convert.py ├── nanodet │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── nanodet416.param │ └── nanodet.cpp ├── yolo-fastestv2 │ ├── .DS_Store │ ├── README.md │ ├── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ └── yolo-fastestv2.param │ └── yolo-fastestv2.cpp ├── yolop │ ├── .DS_Store │ ├── README.md │ ├── models │ │ └── convert.py │ └── yolop.cpp ├── yolov5 │ ├── .DS_Store │ ├── README.md │ ├── color.cpp │ ├── models │ │ ├── .DS_Store │ │ ├── convert.sh │ │ ├── yolov5n-7.ncnn.param │ │ └── yolov5s.ncnn.param │ └── yolov5.cpp ├── yolov6 │ ├── .DS_Store │ ├── README.md │ ├── models │ │ └── convert.py │ └── yolov6.cpp ├── yolov7 │ ├── README.md │ ├── pt2ncnn.sh │ └── yolov7.cpp └── yolox │ ├── .DS_Store │ ├── README.md │ ├── models │ ├── .DS_Store │ ├── convert.py │ └── yolox_nano.param │ └── yolox.cpp ├── style_transfer ├── .DS_Store ├── anime2real │ ├── .DS_Store │ ├── README.md │ ├── anime2real.cpp │ └── models │ │ ├── convert.py │ │ ├── netG_A2B.param │ │ ├── netG_B2A.param │ │ └── out.jpg ├── animeganv2 │ ├── .DS_Store │ ├── README.md │ ├── animeganv2.cpp │ └── models │ │ ├── .DS_Store │ │ ├── convert.py │ │ ├── face_paint_512_v2.param │ │ └── paprika.param ├── animeganv3 │ ├── .DS_Store │ ├── README.md │ ├── animeganv3.cpp │ ├── input.jpg │ └── models │ │ └── .DS_Store └── styletransfer │ ├── .DS_Store │ ├── README.md │ ├── models │ ├── .DS_Store │ ├── candy9.param │ ├── convert.py │ ├── mosaic-9.param │ ├── pointilism-9.param │ ├── rain-princess-9.param │ └── udnie-9.param │ └── styletransfer.cpp ├── tts ├── sherpa │ └── README.md └── vits │ └── README.md └── video ├── .DS_Store ├── ifrnet ├── 0.png ├── 1.png ├── README.md └── main.cpp ├── nerf └── README.md └── rife ├── .DS_Store ├── README.md ├── models ├── .DS_Store └── flownet.param ├── rife.cpp ├── rife_ops.h ├── warp.comp.hex.h ├── warp.cpp ├── warp_pack4.comp.hex.h └── warp_pack8.comp.hex.h /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore vscode 2 | .vscode 3 | # ignore large file 4 | deoldify.256.bin 5 | deeplabv3_resnet50.bin 6 | res2net101_26w_4s.bin 7 | res2next50.bin 8 | vgg16.bin 9 | cait_xxs36_384.bin 10 | anime-face_hrnetv2.bin 11 | netG_A2B.bin 12 | netG_B2A.bin 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 佰阅 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-CN.md: -------------------------------------------------------------------------------- 1 | ##

ncnn models

2 | 3 | The collection of pre-trained AI models, and how they were converted, deployed. 4 | 5 | ![](docs/images/logo.png) 6 | 7 | ### 关于 8 | 9 | ncnn框架借助vulkan实现了全平台部署,我们通过pytorch、tensorflow、飞桨等预训练模型,然后转换成ncnn通用模型,实现Windows、mac、linux、安卓、ios、WebAssembly 以及uni-app的最终部署。然而模型转换并不是一键的,需要手动处理。为了拓展ncnn的边界应用,我们建立此仓库,欢迎提交接收任何转换成功或失败的案例。 10 | 11 | ### 如何参与贡献? 12 | 13 | fork代码,然后按照如下格式提交,最好是c++20 的最小demo,不要内嵌并发。 14 | 15 | yolov5 # 项目名称 16 | - models/xx.bin or xx.param # model 17 | - input.png # 输入 18 | - out.png # 输出 19 | - README.md # 模型推理介绍、转换步骤、相关案例 20 | - convert.py # 从pytorch等模型转换具体复现代码 21 | 22 | 灵感来自[ailia](https://github.com/axinc-ai/ailia-models),鉴于接收失败案例为主,因此失败案例优先排序,成功案例分类排序。 23 | 24 | ### 一些代表 25 | 26 | - [nihui](https://github.com/nihui) ncnn作者 27 | - [飞哥](https://github.com/feigechuanshu) ncnn安卓系列 28 | - [EdVince](https://github.com/EdVince) ncnn自然语言系列 29 | - [baiyue](https://github.com/Baiyuetribe/paper2gui) ncnn在PC桌面GUI系列 30 | - [670***@qq.com](https://ext.dcloud.net.cn/plugin?id=5243) ncnn在uni-app中的应用 31 | - [nihui](https://github.com/nihui/ncnn_on_esp32) ncnn在嵌入式设备上的应用 32 | - [nihui](https://github.com/nihui/ncnn-webassembly-yolov5) ncnn在wasm实现案例 33 | 34 | ### 跨设备的意义 35 | 36 | > 维护一次代码,多设备可用。 37 | 38 | - 借助vulkan,无需繁重的cuda驱动 39 | - 借助wasm实现任意设备的模型部署 40 | - 借助uni-app可绕过java实现多端部署 -------------------------------------------------------------------------------- /audio_denoising/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/audio_denoising/.DS_Store -------------------------------------------------------------------------------- /audio_denoising/dtln/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/audio_denoising/dtln/.DS_Store -------------------------------------------------------------------------------- /audio_denoising/dtln/README.md: -------------------------------------------------------------------------------- 1 | # DTLN 2 | 3 | ## Compare input audio with output audio 4 | 5 | ![](https://github.com/lhwcv/DTLN_pytorch/raw/main/samples/in.png) 6 | ![](https://github.com/lhwcv/DTLN_pytorch/raw/main/samples/out.png) 7 | 8 | ## Convert DTLN from ONNX to NCNN 9 | 10 | - Part 1 11 | 12 | ```python 13 | import os 14 | import torch 15 | from DTLN_model_ncnn_compat import Pytorch_DTLN_P1_stateful, Pytorch_DTLN_P2_stateful 16 | 17 | if __name__ == '__main__': 18 | import argparse 19 | 20 | parser = argparse.ArgumentParser() 21 | parser.add_argument("--model_path", 22 | type=str, 23 | help="model dir", 24 | default=os.path.dirname(__file__) + "/pretrained/model.pth") 25 | parser.add_argument("--model_1", 26 | type=str, 27 | help="model part 1 save path", 28 | default=os.path.dirname(__file__) + "/pretrained/model_p1.pt") 29 | 30 | parser.add_argument("--model_2", 31 | type=str, 32 | help="model part 2 save path", 33 | default=os.path.dirname(__file__) + "/pretrained/model_p2.pt") 34 | 35 | args = parser.parse_args() 36 | 37 | model1 = Pytorch_DTLN_P1_stateful() 38 | print('==> load model from: ', args.model_path) 39 | model1.load_state_dict(torch.load(args.model_path), strict=False) 40 | model1.eval() 41 | model2 = Pytorch_DTLN_P2_stateful() 42 | model2.load_state_dict(torch.load(args.model_path), strict=False) 43 | model2.eval() 44 | 45 | block_len = 512 46 | hidden_size = 128 47 | # in_state1 = torch.zeros(2, 1, hidden_size, 2) 48 | # in_state2 = torch.zeros(2, 1, hidden_size, 2) 49 | in_state1 = torch.zeros(4, 1, 1, 128) 50 | in_state2 = torch.zeros(4, 1, 1, 128) 51 | 52 | mag = torch.zeros(1, 1, (block_len // 2 + 1)) 53 | phase = torch.zeros(1, 1, (block_len // 2 + 1)) 54 | y1 = torch.zeros(1, block_len, 1) 55 | 56 | # NCNN not support Gather 57 | input_names = ["mag", "h1_in", "c1_in", "h2_in", "c2_in"] 58 | output_names = ["y1", "out_state1"] 59 | 60 | print("==> export to: ", args.model_1) 61 | # torch.onnx.export(model1, 62 | # (mag, in_state1[0], in_state1[1], in_state1[2], in_state1[3]), 63 | # args.model_1, 64 | # input_names=input_names, output_names=output_names) 65 | # torch.save(model1.state_dict(), args.model_1) 66 | # 1. pt --> torchscript 67 | traced_script_module = torch.jit.trace(model1, (mag, in_state1[0], in_state1[1], in_state1[2], in_state1[3])) 68 | traced_script_module.save("ts1.pt") 69 | # 2. ts --> pnnx --> ncnn 70 | os.system("pnnx ts1.pt inputshape=[1,1,257],[1,1,128],[1,1,128],[1,1,128][1,1,128]") 71 | ``` 72 | 73 | - Part 2 74 | 75 | ```python 76 | import os 77 | 78 | # 2. onnx --> onnxsim 79 | os.system("python -m onnxsim model_p2.onnx sim.onnx") 80 | 81 | # 3. onnx --> ncnn 82 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 83 | 84 | # 4. ncnn --> optmize ---> ncnn 85 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 86 | 87 | # 从原始项目中的onnx模型进行转换,原始项目pnnx模式报错,意思pnnx算子不支持aten:square 88 | 89 | ``` 90 | 91 | ## Example project 92 | 93 | ## Reference 94 | 95 | - [lhwcv/DTLN_pytorch](https://github.com/lhwcv/DTLN_pytorch) 96 | - [breizhn/DTLN](https://github.com/breizhn/DTLN) 97 | -------------------------------------------------------------------------------- /audio_denoising/dtln/dtln.cpp: -------------------------------------------------------------------------------- 1 | // 本项目难点在音频前处理上,暂无最小demo,请参考原作者的deploment方法 2 | // 欢迎pr 3 | 4 | // https://github.com/lhwcv/DTLN_pytorch/tree/main/deploy 5 | -------------------------------------------------------------------------------- /audio_denoising/dtln/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/audio_denoising/dtln/models/.DS_Store -------------------------------------------------------------------------------- /audio_denoising/dtln/models/convert1.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | from DTLN_model_ncnn_compat import Pytorch_DTLN_P1_stateful, Pytorch_DTLN_P2_stateful 4 | 5 | if __name__ == '__main__': 6 | import argparse 7 | 8 | parser = argparse.ArgumentParser() 9 | parser.add_argument("--model_path", 10 | type=str, 11 | help="model dir", 12 | default=os.path.dirname(__file__) + "/pretrained/model.pth") 13 | parser.add_argument("--model_1", 14 | type=str, 15 | help="model part 1 save path", 16 | default=os.path.dirname(__file__) + "/pretrained/model_p1.pt") 17 | 18 | parser.add_argument("--model_2", 19 | type=str, 20 | help="model part 2 save path", 21 | default=os.path.dirname(__file__) + "/pretrained/model_p2.pt") 22 | 23 | args = parser.parse_args() 24 | 25 | model1 = Pytorch_DTLN_P1_stateful() 26 | print('==> load model from: ', args.model_path) 27 | model1.load_state_dict(torch.load(args.model_path), strict=False) 28 | model1.eval() 29 | model2 = Pytorch_DTLN_P2_stateful() 30 | model2.load_state_dict(torch.load(args.model_path), strict=False) 31 | model2.eval() 32 | 33 | block_len = 512 34 | hidden_size = 128 35 | # in_state1 = torch.zeros(2, 1, hidden_size, 2) 36 | # in_state2 = torch.zeros(2, 1, hidden_size, 2) 37 | in_state1 = torch.zeros(4, 1, 1, 128) 38 | in_state2 = torch.zeros(4, 1, 1, 128) 39 | 40 | mag = torch.zeros(1, 1, (block_len // 2 + 1)) 41 | phase = torch.zeros(1, 1, (block_len // 2 + 1)) 42 | y1 = torch.zeros(1, block_len, 1) 43 | 44 | # NCNN not support Gather 45 | input_names = ["mag", "h1_in", "c1_in", "h2_in", "c2_in"] 46 | output_names = ["y1", "out_state1"] 47 | 48 | print("==> export to: ", args.model_1) 49 | # torch.onnx.export(model1, 50 | # (mag, in_state1[0], in_state1[1], in_state1[2], in_state1[3]), 51 | # args.model_1, 52 | # input_names=input_names, output_names=output_names) 53 | # torch.save(model1.state_dict(), args.model_1) 54 | # 1. pt --> torchscript 55 | traced_script_module = torch.jit.trace(model1, (mag, in_state1[0], in_state1[1], in_state1[2], in_state1[3])) 56 | traced_script_module.save("ts1.pt") 57 | # 2. ts --> pnnx --> ncnn 58 | os.system("pnnx ts1.pt inputshape=[1,1,257],[1,1,128],[1,1,128],[1,1,128][1,1,128]") 59 | 60 | # input_names = ["y1", "h1_in", "c1_in", "h2_in", "c2_in"] 61 | # output_names = ["y", "out_state2"] 62 | 63 | # print("==> export to: ", args.model_2) 64 | # torch.onnx.export(model2, 65 | # (y1, in_state2[0], in_state2[1], in_state2[2], in_state2[3]), 66 | # args.model_2, 67 | # input_names=input_names, output_names=output_names) 68 | 69 | # traced_script_module = torch.jit.trace(model2, (y1, in_state2[0], in_state2[1], in_state2[2], in_state2[3])) 70 | # traced_script_module.save("ts2.pt") 71 | # # 2. ts --> pnnx --> ncnn 72 | # os.system("pnnx ts2.pt inputshape=[1,512,1],[1,1,128],[1,1,128],[1,1,128][1,1,128]") 73 | 74 | # 把文件丢到原始项目目录下,进行转换 75 | -------------------------------------------------------------------------------- /audio_denoising/dtln/models/convert2.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # 2. onnx --> onnxsim 4 | os.system("python -m onnxsim model_p2.onnx sim.onnx") 5 | 6 | # 3. onnx --> ncnn 7 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 8 | 9 | # 4. ncnn --> optmize ---> ncnn 10 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 11 | 12 | # 从原始项目中的onnx模型进行转换,原始项目pnnx模式报错,意思pnnx算子不支持aten:square 13 | -------------------------------------------------------------------------------- /audio_denoising/dtln/models/dtln_1.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 13 18 3 | Input in0 0 1 in0 4 | Split splitncnn_0 1 2 in0 1 2 5 | Input in1 0 1 in1 6 | Input in2 0 1 in2 7 | Input in3 0 1 in3 8 | Input in4 0 1 in4 9 | LSTM lstm_1 3 3 1 in1 in2 7 8 9 0=128 1=131584 2=0 10 | LSTM lstm_2 3 3 7 in3 in4 10 11 12 0=128 1=65536 2=0 11 | Concat cat_0 4 1 8 9 11 12 out1 0=0 12 | InnerProduct fcsigmoid_0 1 1 10 14 0=257 1=1 2=32896 9=4 13 | Reshape reshape_5 1 1 14 15 0=-1 14 | Reshape reshape_4 1 1 2 16 0=-1 15 | BinaryOp mul_0 2 1 15 16 out0 0=2 16 | -------------------------------------------------------------------------------- /audio_denoising/dtln/models/dtln_2.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 33 41 3 | Input y1 0 1 y1 4 | Input h1_in 0 1 h1_in 5 | Input c1_in 0 1 c1_in 6 | Input h2_in 0 1 h2_in 7 | Input c2_in 0 1 c2_in 8 | MemoryData encoder_norm1.beta 0 1 encoder_norm1.beta 0=256 1=1 9 | MemoryData encoder_norm1.gamma 0 1 encoder_norm1.gamma 0=256 1=1 10 | MemoryData sep2.dense.bias 0 1 sep2.dense.bias 0=256 11 | Convolution1D Conv_0 1 1 y1 19 0=256 1=1 6=131072 12 | Permute Transpose_1 1 1 19 20 0=1 13 | Split splitncnn_0 1 3 20 20_splitncnn_0 20_splitncnn_1 20_splitncnn_2 14 | Reduction ReduceMean_2 1 1 20_splitncnn_2 21 0=3 15 | BinaryOp Sub_3 2 1 20_splitncnn_1 21 22 0=1 16 | Split splitncnn_1 1 3 22 22_splitncnn_0 22_splitncnn_1 22_splitncnn_2 17 | BinaryOp Mul_4 2 1 22_splitncnn_2 22_splitncnn_1 23 0=2 18 | Reduction ReduceMean_5 1 1 23 24 0=3 19 | BinaryOp Add_7 1 1 24 26 1=1 2=1.000000e-07 20 | UnaryOp Sqrt_8 1 1 26 27 0=5 21 | BinaryOp Div_9 2 1 22_splitncnn_0 27 28 0=3 22 | BinaryOp Mul_10 2 1 28 encoder_norm1.gamma 29 0=2 23 | BinaryOp Add_11 2 1 29 encoder_norm1.beta 30 24 | LSTM LSTM_13 3 3 30 h1_in c1_in 56 54 55 0=128 1=131072 25 | LSTM LSTM_15 3 3 56 h2_in c2_in 82 79 80 0=128 1=65536 26 | InnerProduct MatMul_18 1 1 82 84 0=256 2=32768 27 | BinaryOp Add_19 2 1 sep2.dense.bias 84 85 28 | Sigmoid Sigmoid_20 1 1 85 86 29 | Concat Concat_21 4 1 54 55 79 80 out_state2 30 | Reshape Reshape_23 1 1 86 89 0=-1 31 | Reshape Reshape_25 1 1 20_splitncnn_0 91 0=-1 32 | BinaryOp Mul_26 2 1 89 91 92 0=2 33 | Reshape Reshape_28 1 1 92 94 0=256 1=1 34 | Permute Transpose_29 1 1 94 95 0=1 35 | Convolution1D Conv_30 1 1 95 y 0=512 1=1 6=131072 36 | -------------------------------------------------------------------------------- /contribute.md: -------------------------------------------------------------------------------- 1 | ### How can I get involved in contributing? 2 | 3 | fork the code, then submit it in the following format, preferably a minimal demo of c++20, without inline concurrency. 4 | 5 | ``` 6 | git clone https://github.com/Baiyuetribe/ncnn-models.git --depth=1 7 | cd ncnn-models 8 | code . 9 | ``` 10 | 11 | ### Examples 12 | 13 | yolov5 # model name 14 | - models/xx.bin or xx.param # model 15 | - input.png 16 | - out.png 17 | - README.md 18 | - convert.py 19 | -------------------------------------------------------------------------------- /diffusion/stablediffuson/README.md: -------------------------------------------------------------------------------- 1 | # StableDiffuson 2 | 3 | ## Input --> Output 4 | 5 | ![](https://user-images.githubusercontent.com/43716063/213969098-433f39eb-7171-43c8-aae4-dd2de91a0b4c.png) 6 | 7 | ## 出入信息 8 | 9 | ### 输入尺寸 10 | 11 | [1,4,-1,-1],[1],[1,77,768] 12 | 13 | ## 模型转换 14 | 15 | ```python 16 | import os 17 | import torch 18 | from diffusers import StableDiffusionPipeline 19 | 20 | # config 21 | device = "cuda" 22 | from_model = "../diffusers-model" 23 | to_model = "model" 24 | height, width = 512, 512 25 | 26 | # check 27 | assert height % 8 == 0 and width % 8 == 0 28 | height, width = height // 8, width // 8 29 | os.makedirs(to_model, exist_ok=True) 30 | 31 | # load model 32 | pipe = StableDiffusionPipeline.from_pretrained(from_model, torch_dtype=torch.float32) 33 | pipe = pipe.to(device) 34 | 35 | # jit unet 36 | unet = torch.jit.trace(pipe.unet, (torch.rand(1,4,height,width).to(device),torch.rand(1).to(device),torch.rand(1,77,768).to(device))) 37 | unet.save(os.path.join(to_model,"unet.pt")) 38 | 39 | ## 4. fp16量化 40 | pnnx unet.pt inputshape=[1,4,64,64],[1],[1,77,768] 41 | 42 | ``` 43 | 44 | ## c++实现 45 | 46 | 参见 47 | 48 | ## Example project 49 | 50 | ## Reference 51 | 52 | - [EdVince/Stable-Diffusion-NCNN](https://github.com/EdVince/Stable-Diffusion-NCNN) 53 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 54 | -------------------------------------------------------------------------------- /docs/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/docs/.DS_Store -------------------------------------------------------------------------------- /docs/Cmakelists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION "3.5.1" ) # 定义cmake最小版本号 2 | add_compile_options("$<$:/source-charset:utf-8>") # 解决源码含有中文时报错 3 | 4 | set(CMAKE_BUILD_TYPE Release) 5 | 6 | # Create project 7 | set(ProjectName "demo") # 定义变量 8 | project(${ProjectName}) # 定义项目名称 9 | message("ProjectName: ${ProjectName}") # 打印项目名称 10 | add_executable(${ProjectName} yolov5.cpp) # 需要编译的如何文件 11 | 12 | 13 | # Opencv库引入 14 | set(OpenCV_DIR "C:\\Temp\\cinclude\\opencv\\build\\x64\\vc15\\lib") # 来源 https://github.com/opencv/opencv/releases/tag/4.5.5 15 | find_package(OpenCV REQUIRED) 16 | target_link_libraries(${ProjectName} ${OpenCV_LIBS}) 17 | 18 | # ncnn库引入 19 | set(ncnn_DIR "C:\\Temp\\cinclude\\ncnn\\x64\\lib\\cmake\\ncnn") # 来源 https://github.com/Tencent/ncnn/releases/tag/20220420 20 | find_package(ncnn REQUIRED) 21 | target_link_libraries(${ProjectName} ncnn) 22 | -------------------------------------------------------------------------------- /docs/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/docs/images/logo.png -------------------------------------------------------------------------------- /face_dection/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_dection/.DS_Store -------------------------------------------------------------------------------- /face_dection/Anime_Face/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_dection/Anime_Face/.DS_Store -------------------------------------------------------------------------------- /face_dection/Anime_Face/README.md: -------------------------------------------------------------------------------- 1 | # Anime Face Detector 2 | 3 | ## Input --> Output 4 | 5 | ![](https://raw.githubusercontent.com/hysts/anime-face-detector/main/assets/output.jpg) 6 | 7 | ## Convert 8 | 9 | pt --> onnx --> onnx-sim --> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | 14 | # 1. download -- anime-face_hrnetv2.onnx \ anime-face_yolov3.onnx \anime-face_faster-rcnn.onnx 15 | # download_url = https://storage.googleapis.com/ailia-models/anime-face-detector/anime-face_hrnetv2.onnx 16 | os.system("") 17 | 18 | # 2. onnx --> onnxsim 19 | os.system("python -m onnxsim anime-face_hrnetv2.onnx sim.onnx --input-shape 1,3,256,256") 20 | 21 | # 3. onnx --> ncnn 22 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 23 | 24 | # 4. ncnn --> optmize ---> ncnn 25 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 26 | ``` 27 | 28 | ## Example project 29 | 30 | 31 | 32 | ## Reference 33 | 34 | - [face_detection/anime-face-detector](https://github.com/axinc-ai/ailia-models/blob/master/face_detection/anime-face-detector/README.md) 35 | - [hysts/anime-face-detector](https://github.com/hysts/anime-face-detector) 36 | 37 | 38 | -------------------------------------------------------------------------------- /face_dection/Anime_Face/anime_face.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // opencv2 3 | #include 4 | #include 5 | #include 6 | // ncnn 7 | #include "net.h" 8 | 9 | int main(int argc, char **argv) 10 | { 11 | cv::Mat image = cv::imread("demo.jpg"); // 读取图片 12 | if (image.empty()) 13 | { 14 | std::cout << "read image failed" << std::endl; 15 | return -1; 16 | } 17 | // 推理模型定义 18 | ncnn::Net net; 19 | net.load_param("models/anime-face_hrnetv2.param"); 20 | net.load_model("models/anime-face_hrnetv2.bin"); 21 | // 前处理 22 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR2RGB, image.cols, image.rows, 256, 256); 23 | const float mean_vals[3] = {127.5f, 127.5f, 127.5f}; 24 | const float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f}; 25 | // const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f}; 26 | in.substract_mean_normalize(mean_vals, norm_vals); // 可能有误 27 | 28 | ncnn::Extractor ex = net.create_extractor(); // 创建提取器 29 | ex.input("img", in); // 输入 30 | ncnn::Mat out; 31 | ex.extract("heatmap", out); // 输出 32 | std::cout << out.w << " " << out.h << " " << out.dims << std::endl; // 28 * 64 * 64 33 | // 后处理 -- 欢迎修正 34 | 35 | // std::vector keypoints; 36 | // keypoints.resize(out.w); 37 | // for (int j = 0; j < out.w; j++) 38 | // { 39 | // // std::cout << out[j] << std::endl; 40 | // keypoints[j] = out[j] * 256; // 一次存储两个节点位置 41 | // } 42 | // size_t max_len = keypoints.size(); // 画点 43 | // cv::Mat res; 44 | // cv::resize(image, res, cv::Size(256, 256)); 45 | // // for (size_t i = 0; i < max_len; i += 2) 46 | // for (size_t i = 0; i < max_len; i += 2) 47 | // { 48 | // cv::circle(res, cv::Point(keypoints[i] * image.cols / 256, keypoints[i + 1] * image.rows / 256), 3, cv::Scalar(255, 255, 255), -1); 49 | // } 50 | // cv::imshow("demo", res); 51 | // cv::waitKey(); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /face_dection/Anime_Face/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # 1. download -- anime-face_hrnetv2.onnx \ anime-face_yolov3.onnx \anime-face_faster-rcnn.onnx 4 | # download_url = https://storage.googleapis.com/ailia-models/anime-face-detector/anime-face_hrnetv2.onnx 5 | os.system("") 6 | 7 | # 2. onnx --> onnxsim 8 | os.system("python -m onnxsim anime-face_hrnetv2.onnx sim.onnx --input-shape 1,3,256,256") 9 | 10 | # 3. onnx --> ncnn 11 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 12 | 13 | # 4. ncnn --> optmize ---> ncnn 14 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 15 | -------------------------------------------------------------------------------- /face_dection/pfld/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_dection/pfld/.DS_Store -------------------------------------------------------------------------------- /face_dection/pfld/README.md: -------------------------------------------------------------------------------- 1 | # pfld 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/guoqiangqi/PFLD/raw/master/data/sample_imgs/ucgif_20190809185908.gif) 6 | 7 | ## Convert 8 | 9 | pt --> onnx --> onnx-sim --> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | # import os 13 | # import torch 14 | # # 0. pt模型下载及初始化 15 | # # 1. download onnx model # Windows下手动执行 16 | # os.system("wget xxxx.onnx -O xxx.onnx") 17 | 18 | # # 2. onnx --> onnxsim 19 | # os.system("python3 -m onnxsim xxx.onnx sim.onnx") 20 | 21 | # # 3. onnx --> ncnn 22 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 23 | 24 | # # 4. ncnn --> optmize ---> ncnn 25 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 26 | ``` 27 | 28 | ## Example project 29 | 30 | - [abyssss52/PFLD_ncnn_test](https://github.com/abyssss52/PFLD_ncnn_test) 31 | 32 | 33 | ## Reference 34 | 35 | - [polarisZhao/PFLD-pytorch](https://github.com/polarisZhao/PFLD-pytorch) 36 | 37 | 38 | -------------------------------------------------------------------------------- /face_dection/pfld/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_dection/pfld/models/.DS_Store -------------------------------------------------------------------------------- /face_dection/pfld/models/convert.py: -------------------------------------------------------------------------------- 1 | # 欢迎pr 2 | -------------------------------------------------------------------------------- /face_dection/pfld/pfld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // opencv2 3 | #include 4 | #include 5 | #include 6 | // ncnn 7 | #include "net.h" 8 | 9 | int main(int argc, char **argv) 10 | { 11 | cv::Mat image = cv::imread("demo.jpg"); // 读取图片 12 | if (image.empty()) 13 | { 14 | std::cout << "read image failed" << std::endl; 15 | return -1; 16 | } 17 | // 推理模型定义 18 | ncnn::Net net; 19 | net.load_param("models/pfld-sim.param"); 20 | net.load_model("models/pfld-sim.bin"); 21 | // 前处理 22 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR, image.cols, image.rows, 112, 112); 23 | const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f}; 24 | in.substract_mean_normalize(0, norm_vals); 25 | 26 | ncnn::Extractor ex = net.create_extractor(); // 创建提取器 27 | ex.input("input_1", in); // 输入 28 | ncnn::Mat out; 29 | ex.extract("415", out); // 输出 30 | // 后处理 31 | std::vector keypoints; 32 | keypoints.resize(out.w); 33 | for (int j = 0; j < out.w; j++) 34 | { 35 | keypoints[j] = out[j] * 112; 36 | } 37 | size_t max_len = keypoints.size(); // 画点 38 | for (size_t i = 0; i < max_len; i += 2) 39 | { 40 | cv::circle(image, cv::Point(keypoints[i] * image.cols / 112, keypoints[i + 1] * image.rows / 112), 3, cv::Scalar(255, 255, 255), -1); 41 | } 42 | cv::imshow("demo", image); 43 | cv::waitKey(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /face_dection/ultraface/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_dection/ultraface/.DS_Store -------------------------------------------------------------------------------- /face_dection/ultraface/README.md: -------------------------------------------------------------------------------- 1 | # UltraFace 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/raw/master/readme_imgs/4.jpg) 6 | 7 | ## Convert 8 | 9 | pt --> onnx --> onnx-sim --> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | # 1. download onnx model # Windows下手动执行 16 | os.system("wget https://raw.githubusercontent.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/master/models/onnx/version-RFB-320.onnx -O version-RFB-320.onnx") 17 | 18 | # 2. onnx --> onnxsim 19 | os.system("python3 -m onnxsim version-RFB-320.onnx sim.onnx") 20 | 21 | # 3. onnx --> ncnn 22 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 23 | 24 | # 4. ncnn --> optmize ---> ncnn 25 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 26 | ``` 27 | 28 | ## Example project 29 | 30 | - [Android: oaup/ncnn-android-ultraface](https://github.com/oaup/ncnn-android-ultraface) 31 | 32 | 33 | ## Reference 34 | 35 | - [Tencent/ncnn](https://github.com/Tencent/ncnn/blob/master/examples/rvm.cpp) 36 | - [Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB) 37 | 38 | 39 | -------------------------------------------------------------------------------- /face_dection/ultraface/UltraFace.h: -------------------------------------------------------------------------------- 1 | // 2 | // UltraFace.hpp 3 | // UltraFaceTest 4 | // 5 | // Created by vealocia on 2019/10/17. 6 | // Copyright © 2019 vealocia. All rights reserved. 7 | // 8 | 9 | #ifndef UltraFace_hpp 10 | #define UltraFace_hpp 11 | 12 | #pragma once 13 | 14 | #include "gpu.h" 15 | #include "net.h" 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #define num_featuremap 4 22 | #define hard_nms 1 23 | #define blending_nms 2 /* mix nms was been proposaled in paper blaze face, aims to minimize the temporal jitter*/ 24 | 25 | typedef struct FaceInfo 26 | { 27 | float x1; 28 | float y1; 29 | float x2; 30 | float y2; 31 | float score; 32 | 33 | float *landmarks; 34 | } FaceInfo; 35 | 36 | class UltraFace 37 | { 38 | public: 39 | UltraFace(const std::string &bin_path, const std::string ¶m_path, 40 | int input_width, int input_length, int num_thread_ = 4, float score_threshold_ = 0.7, float iou_threshold_ = 0.3, int topk_ = -1); 41 | 42 | ~UltraFace(); 43 | 44 | int detect(ncnn::Mat &img, std::vector &face_list); 45 | 46 | private: 47 | void generateBBox(std::vector &bbox_collection, ncnn::Mat scores, ncnn::Mat boxes, float score_threshold, int num_anchors); 48 | 49 | void nms(std::vector &input, std::vector &output, int type = blending_nms); 50 | 51 | private: 52 | ncnn::Net ultraface; 53 | 54 | int num_thread; 55 | int image_w; 56 | int image_h; 57 | 58 | int in_w; 59 | int in_h; 60 | int num_anchors; 61 | 62 | int topk; 63 | float score_threshold; 64 | float iou_threshold; 65 | 66 | const float mean_vals[3] = {127, 127, 127}; 67 | const float norm_vals[3] = {1.0 / 128, 1.0 / 128, 1.0 / 128}; 68 | 69 | const float center_variance = 0.1; 70 | const float size_variance = 0.2; 71 | const std::vector> min_boxes = { 72 | {10.0f, 16.0f, 24.0f}, 73 | {32.0f, 48.0f}, 74 | {64.0f, 96.0f}, 75 | {128.0f, 192.0f, 256.0f}}; 76 | const std::vector strides = {8.0, 16.0, 32.0, 64.0}; 77 | std::vector> featuremap_size; 78 | std::vector> shrinkage_size; 79 | std::vector w_h_list; 80 | 81 | std::vector> priors = {}; 82 | }; 83 | 84 | #endif /* UltraFace_hpp */ 85 | -------------------------------------------------------------------------------- /face_dection/ultraface/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // UltraFaceTest 4 | // 5 | // Created by vealocia on 2019/10/17. 6 | // Copyright © 2019 vealocia. All rights reserved. 7 | // 8 | 9 | #include "UltraFace.hpp" 10 | #include 11 | #include 12 | 13 | int main(int argc, char **argv) 14 | { 15 | UltraFace ultraface("models/version-RFB-320.bin", "models/version-RFB-320.param", 320, 240, 1, 0.7); // config model input 16 | std::string image_file = "intput.jpg"; // input image 17 | cv::Mat frame = cv::imread(image_file); 18 | if (frame.empty()) 19 | { 20 | std::cout << "read image failed" << std::endl; 21 | return -1; 22 | } 23 | ncnn::Mat inmat = ncnn::Mat::from_pixels(frame.data, ncnn::Mat::PIXEL_BGR2RGB, frame.cols, frame.rows); 24 | 25 | std::vector face_info; 26 | ultraface.detect(inmat, face_info); 27 | for (int i = 0; i < face_info.size(); i++) 28 | { 29 | auto face = face_info[i]; 30 | cv::Point pt1(face.x1, face.y1); 31 | cv::Point pt2(face.x2, face.y2); 32 | cv::rectangle(frame, pt1, pt2, cv::Scalar(0, 255, 0), 2); 33 | } 34 | 35 | cv::imshow("UltraFace", frame); 36 | cv::waitKey(); 37 | cv::imwrite("result.jpg", frame); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /face_dection/ultraface/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_dection/ultraface/models/.DS_Store -------------------------------------------------------------------------------- /face_dection/ultraface/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | # 1. download onnx model # Windows下手动执行 5 | os.system("wget https://raw.githubusercontent.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/master/models/onnx/version-RFB-320.onnx -O version-RFB-320.onnx") 6 | 7 | # 2. onnx --> onnxsim 8 | os.system("python3 -m onnxsim version-RFB-320.onnx sim.onnx") 9 | 10 | # 3. onnx --> ncnn 11 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 12 | 13 | # 4. ncnn --> optmize ---> ncnn 14 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 15 | -------------------------------------------------------------------------------- /face_restoration/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_restoration/.DS_Store -------------------------------------------------------------------------------- /face_restoration/codeformer/README.md: -------------------------------------------------------------------------------- 1 | # Codeformer 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/FeiGeChuanShu/CodeFormer-ncnn/blob/main/data/03.jpg) 6 | ![](https://github.com/FeiGeChuanShu/CodeFormer-ncnn/raw/main/data/03_pro.png) 7 | 8 | ## 出入信息 9 | 10 | ## 模型转换 11 | 12 | ```python 13 | 欢迎pr 14 | ``` 15 | 16 | ## c++实现 17 | 18 | 参见 https://github.com/FeiGeChuanShu/CodeFormer-ncnn 19 | 20 | ## Example project 21 | 22 | ## Reference 23 | 24 | - [FeiGeChuanShu/CodeFormer-ncnn](https://github.com/FeiGeChuanShu/CodeFormer-ncnn) 25 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 26 | -------------------------------------------------------------------------------- /face_restoration/gfpgan/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/face_restoration/gfpgan/.DS_Store -------------------------------------------------------------------------------- /face_restoration/gfpgan/README.md: -------------------------------------------------------------------------------- 1 | # GFPGAN 2 | 3 | ## Input --> Output 4 | 5 | ![](https://camo.githubusercontent.com/37b7b9eeea773388ddf21aa313800d83dfcc99dc8358fad14f9772ccd43106e8/68747470733a2f2f78696e6e74616f2e6769746875622e696f2f70726f6a656374732f47465047414e5f7372632f67667067616e5f7465617365722e6a7067) 6 | ![](https://github.com/Baiyuetribe/paper2gui/raw/main/docs/images/gfpgan_gui.png) 7 | 8 | ## Convert 9 | 10 | pt --> onnx --> onnx-sim --> ncnnOptimize --> ncnn 11 | 12 | ```python 13 | # need pr 14 | ``` 15 | 受限于模型转换,目前仅能固定输出512*512图片 16 | 17 | ## Example project 18 | 19 | - [GFPGAN-GUI](https://github.com/Baiyuetribe/paper2gui/blob/main/FaceRestoration/readme.md) 20 | 21 | ## Reference 22 | 23 | - [TencentARC/GFPGAN](https://github.com/TencentARC/GFPGAN) 24 | - [FeiGeChuanShu/GFPGAN-ncnn](https://github.com/FeiGeChuanShu/GFPGAN-ncnn) 25 | 26 | 27 | -------------------------------------------------------------------------------- /face_restoration/gfpgan/gfpgan.h: -------------------------------------------------------------------------------- 1 | // gfpgan implemented with ncnn library 2 | 3 | #ifndef GFPGAN_H 4 | #define GFPGAN_H 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // ncnn 15 | #include "net.h" 16 | #include "cpu.h" 17 | #include "layer.h" 18 | 19 | typedef struct StyleConvWeights 20 | { 21 | int data_size; 22 | int inc; 23 | int hid_dim; 24 | int num_output; 25 | std::vector style_convs_modulated_conv_weight; 26 | std::vector style_convs_modulated_conv_modulation_weight; 27 | std::vector style_convs_modulated_conv_modulation_bias; 28 | std::vector style_convs_weight; 29 | std::vector style_convs_bias; 30 | } StyleConvWeights; 31 | typedef struct ToRgbConvWeights 32 | { 33 | int data_size; 34 | int inc; 35 | int hid_dim; 36 | int num_output; 37 | std::vector to_rgbs_modulated_conv_weight; 38 | std::vector to_rgbs_modulated_conv_modulation_weight; 39 | std::vector to_rgbs_modulated_conv_modulation_bias; 40 | std::vector to_rgbs_bias; 41 | } ToRgbConvWeights; 42 | const int style_conv_sizes[][5] = {{512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 0 43 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 1 44 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 2 45 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 3 46 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 4 47 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 5 48 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 6 49 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}, // 7 50 | {256 * 512 * 3 * 3, 512 * 512, 512, 1, 256}, // 8 51 | {256 * 256 * 3 * 3, 256 * 512, 256, 1, 256}, // 9 52 | {128 * 256 * 3 * 3, 256 * 512, 256, 1, 128}, // 10 53 | {128 * 128 * 3 * 3, 128 * 512, 128, 1, 128}, // 11 54 | {64 * 128 * 3 * 3, 128 * 512, 128, 1, 64}, // 12 55 | {64 * 64 * 3 * 3, 64 * 512, 64, 1, 64}, // 13 56 | {512 * 512 * 3 * 3, 512 * 512, 512, 1, 512}}; // 14 57 | const int to_rgb_sizes[][4] = {{3 * 512 * 1 * 1, 512 * 512, 512, 3}, // 0 58 | {3 * 512 * 1 * 1, 512 * 512, 512, 3}, // 1 59 | {3 * 512 * 1 * 1, 512 * 512, 512, 3}, // 2 60 | {3 * 512 * 1 * 1, 512 * 512, 512, 3}, // 3 61 | {3 * 256 * 1 * 1, 256 * 512, 256, 3}, // 4 62 | {3 * 128 * 1 * 1, 128 * 512, 128, 3}, // 5 63 | {3 * 64 * 1 * 1, 64 * 512, 64, 3}, // 6 64 | {3 * 512 * 1 * 1, 512 * 512, 512, 3}}; // 7 65 | const int style_conv_channels[][3] = {{512, 512, 512}, // 0 66 | {512, 512, 512}, // 1 67 | {512, 512, 512}, // 2 68 | {512, 512, 512}, // 3 69 | {512, 512, 512}, // 4 70 | {512, 512, 512}, // 5 71 | {512, 512, 512}, // 6 72 | {512, 512, 512}, // 7 73 | {512, 512, 256}, // 8 74 | {512, 256, 256}, // 9 75 | {512, 256, 128}, // 10 76 | {512, 128, 128}, // 11 77 | {512, 128, 64}, // 12 78 | {512, 64, 64}, // 13 79 | {512, 512, 512}}; // 14 80 | 81 | const int to_rgb_channels[][3] = {{512, 512, 3}, // 0 82 | {512, 512, 3}, // 1 83 | {512, 512, 3}, // 2 84 | {512, 512, 3}, // 3 85 | {512, 256, 3}, // 4 86 | {512, 128, 3}, // 5 87 | {512, 64, 3}, // 6 88 | {512, 512, 3}}; // 7 89 | 90 | class GFPGAN 91 | { 92 | public: 93 | GFPGAN(); 94 | ~GFPGAN(); 95 | 96 | int load(const std::string ¶m_path, const std::string &model_path, const std::string &style_path); 97 | 98 | int process(const cv::Mat &img, ncnn::Mat &outimage); 99 | 100 | private: 101 | int modulated_conv(ncnn::Mat &x, ncnn::Mat &style, 102 | const float *self_weight, const float *weights, 103 | const float *bias, int sample_mode, int demodulate, 104 | int inc, int num_output, int kernel_size, int hid_dim, ncnn::Mat &out); 105 | int to_rgbs(ncnn::Mat &out, ncnn::Mat &latent, ncnn::Mat &skip, 106 | int inc, int hid_dim, int num_output, 107 | const float *to_rgbs_modulated_conv_weight, 108 | const float *to_rgbs_modulated_conv_modulation_weight, 109 | const float *to_rgbs_modulated_conv_modulation_bias, 110 | const float *to_rgbs_bias); 111 | int style_convs_modulated_conv(ncnn::Mat &x, ncnn::Mat style, int sample_mode, 112 | int demodulate, ncnn::Mat &out, int inc, int hid_dim, int num_output, 113 | const float *style_convs_modulated_conv_weight, 114 | const float *style_convs_modulated_conv_modulation_weight, 115 | const float *style_convs_modulated_conv_modulation_bias, 116 | const float *style_convs_weight, 117 | const float *style_convs_bias); 118 | int load_weights(const char *model_path, std::vector &style_conv_weights, 119 | std::vector &to_rgbs_conv_weights, ncnn::Mat &const_input); 120 | 121 | private: 122 | const float mean_vals[3] = {127.5f, 127.5f, 127.5f}; 123 | const float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f}; 124 | std::vector style_conv_weights; 125 | std::vector to_rgbs_conv_weights; 126 | ncnn::Mat const_input; 127 | ncnn::Net net; 128 | }; 129 | 130 | #endif // GFPGAN_H 131 | -------------------------------------------------------------------------------- /face_restoration/gfpgan/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include "gfpgan.h" // 自定义 5 | 6 | static void to_ocv(const ncnn::Mat &result, cv::Mat &out) 7 | { 8 | cv::Mat cv_result_32F = cv::Mat::zeros(cv::Size(512, 512), CV_32FC3); 9 | for (int i = 0; i < result.h; i++) 10 | { 11 | for (int j = 0; j < result.w; j++) 12 | { 13 | cv_result_32F.at(i, j)[2] = (result.channel(0)[i * result.w + j] + 1) / 2; 14 | cv_result_32F.at(i, j)[1] = (result.channel(1)[i * result.w + j] + 1) / 2; 15 | cv_result_32F.at(i, j)[0] = (result.channel(2)[i * result.w + j] + 1) / 2; 16 | } 17 | } 18 | 19 | cv::Mat cv_result_8U; 20 | cv_result_32F.convertTo(cv_result_8U, CV_8UC3, 255.0, 0); 21 | cv_result_8U.copyTo(out); 22 | } 23 | 24 | // 先测试gfpan 25 | int main(int argc, char **argv) 26 | { 27 | std::string in_path = "in.png"; 28 | std::string out_path = "out.png"; 29 | cv::Mat img = cv::imread(in_path); 30 | if (img.empty()) 31 | { 32 | std::cout << "image not found" << std::endl; 33 | return -1; 34 | } 35 | // std::cout << "入口0 size: " << img.size() << std::endl; 36 | // 测试gfpgan 37 | GFPGAN gfpgan; 38 | // gfpgan.load("models/encoder.param", "models/encoder.bin", "models/style.bin"); // 单张处理4s,但是GPU消耗很小 39 | static std::string exe_path = getExeDir(); 40 | gfpgan.load("models/encoder.param", "models/encoder.bin", "models/style.bin"); // 单张处理4s,但是GPU消耗很小 41 | ncnn::Mat res3; 42 | auto start = std::chrono::system_clock::now(); 43 | gfpgan.process(img, res3); // 入口任意,出口都是512*512 44 | auto end = std::chrono::system_clock::now(); 45 | // 汇总时间 46 | std::chrono::duration elapsed_seconds = (end - start); 47 | std::cout << "Total elapsed time: " << elapsed_seconds.count() << "s" << std::endl; 48 | // std::cout << "res3 size" << res3.w << "h:" << res3.h << std::endl; 49 | // 转换为opencv格式 50 | cv::Mat res3_cv; 51 | to_ocv(res3, res3_cv); 52 | // cv::imshow("res3", res3_cv); 53 | // cv::waitKey(0); 54 | cv::imwrite(out_path, res3_cv); // 保存结果 55 | return 0; 56 | } -------------------------------------------------------------------------------- /face_restoration/gfpgan/models/downlaod.txt: -------------------------------------------------------------------------------- 1 | see releases -------------------------------------------------------------------------------- /face_swap/roop/README.md: -------------------------------------------------------------------------------- 1 | # ROOP 换脸 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/s0md3v/roop/blob/main/demo.gif) 6 | 7 | ## 出入信息 8 | ### 输入尺寸 9 | target [1,3,128,128] 10 | source [1,512] 11 | ### 输出尺寸 12 | output [1,3,128,128] 13 | 14 | ## 模型转换 15 | 16 | 17 | ```python 18 | 19 | ## 1. 下载onnx模型 20 | wget https://huggingface.co/optobsafetens/inswapper_128/resolve/main/roopVideoFace_v10.onnx 21 | 22 | ## 2. onnxsim简化 23 | onnxsim in.onnx sim.onnx 24 | 25 | ## 3. onnx2ncnn 26 | onnx2ncnn sim.onnx sim.param sim.bin 27 | 28 | ## 4. fp16量化 29 | ncnnoptimize sim.param sim.bin opt.param opt.bin 1 30 | 31 | ## 5. 模型信息 32 | Op. Total arm loongarch mips riscv vulkan x86 33 | BinaryOp 92 Y Y Y Y Y Y 34 | Split 43 35 | Reduction 24 36 | Crop 24 Y Y Y Y Y Y 37 | Convolution 20 Y Y Y Y Y Y 38 | Padding 14 Y Y Y Y Y Y 39 | UnaryOp 13 Y Y Y Y Y Y 40 | InnerProduct 12 Y Y Y Y Y Y 41 | ExpandDims 12 42 | ReLU 6 Y Y Y Y Y Y 43 | Interp 2 Y Y Y Y Y Y 44 | 45 | ``` 46 | 47 | ## python实现 48 | https://github.com/s0md3v/sd-webui-roop/blob/main/scripts/faceswap.py 49 | 50 | ## c++实现 51 | 52 | 欢迎pr 53 | 54 | ## Example project 55 | 56 | 57 | 58 | ## Reference 59 | 60 | - [s0md3v/roop](https://github.com/s0md3v/roop) 61 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 62 | 63 | 64 | -------------------------------------------------------------------------------- /image_classification/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/.DS_Store -------------------------------------------------------------------------------- /image_classification/cait/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/cait/.DS_Store -------------------------------------------------------------------------------- /image_classification/cait/Cait.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/efficientnet_b0.param"); // 模型加载 44 | net.load_model("models/efficientnet_b0.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("in0", in); // 输入 54 | ex.extract("out0", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/cait/README.md: -------------------------------------------------------------------------------- 1 | # efficientnet 2 | 3 | ## Input --> Output 4 | 5 | ## Convert 6 | 7 | pt --> torchscript--> pnnx --> ncnn 8 | 9 | ```python 10 | import torch 11 | import timm 12 | import os 13 | 14 | model = timm.create_model('cait_xxs36_384', pretrained=True) 15 | model.eval() 16 | 17 | x = torch.rand(1, 3, 384, 384) 18 | traced_script_module = torch.jit.trace(model, x, strict=False) 19 | traced_script_module.save("ts.pt") 20 | 21 | # 2. ts --> pnnx --> ncnn 22 | os.system("pnnx ts.pt inputshape=[1,3,384,384]") 23 | ``` 24 | 25 | ## Example project 26 | 27 | 28 | ## Reference 29 | 30 | - [lucidrains/vit-pytorch](https://github.com/lucidrains/vit-pytorch) 31 | - [rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) 32 | 33 | 34 | -------------------------------------------------------------------------------- /image_classification/cait/models/convert.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import timm 3 | import os 4 | 5 | model = timm.create_model('cait_xxs36_384', pretrained=True) 6 | model.eval() 7 | 8 | x = torch.rand(1, 3, 384, 384) 9 | traced_script_module = torch.jit.trace(model, x, strict=False) 10 | traced_script_module.save("ts.pt") 11 | 12 | # 2. ts --> pnnx --> ncnn 13 | os.system("pnnx ts.pt inputshape=[1,3,384,384]") 14 | 15 | # 模型文件已支持如下: 16 | # "cait_m36_384", 17 | # "cait_m48_448", 18 | # "cait_s24_224", 19 | # "cait_s24_384", 20 | # "cait_s36_384", 21 | # "cait_xs24_384", 22 | # "cait_xxs24_224", 23 | # "cait_xxs24_384", 24 | # "cait_xxs36_224", 25 | # "cait_xxs36_384", 26 | -------------------------------------------------------------------------------- /image_classification/cait/models/download.txt: -------------------------------------------------------------------------------- 1 | 模型转换成功,但是该系列体积巨大,60Mb~1GB,精度位列当前分类第一名 -------------------------------------------------------------------------------- /image_classification/denseNet/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/denseNet/.DS_Store -------------------------------------------------------------------------------- /image_classification/denseNet/README.md: -------------------------------------------------------------------------------- 1 | # densenet121 2 | 3 | ## Input --> Output 4 | 5 | ![](https://pytorch.org/assets/images/densenet1.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript --> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet121', pretrained=True) 16 | model.eval() 17 | x = torch.rand(1, 3, 224, 224) 18 | # 方法1: pnnx 19 | # 1. pt --> torchscript 20 | traced_script_module = torch.jit.trace(model, x, strict=False) 21 | traced_script_module.save("ts.pt") 22 | 23 | # 2. ts --> pnnx --> ncnn 24 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 25 | 26 | # # 方法2: onnx 27 | # # 1. pt ---> onnx 28 | # torch_out = torch.onnx._export(model, x, "densenet121.onnx", export_params=True) 29 | 30 | # # 2. onnx --> onnxsim 31 | # os.system("python3 -m onnxsim densenet121.onnx sim.onnx") 32 | 33 | # # 3. onnx --> ncnn 34 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 35 | 36 | # # 4. ncnn --> optmize ---> ncnn 37 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 38 | 39 | # 两种转换都成功 40 | ``` 41 | 42 | ## Example project 43 | 44 | 45 | ## Reference 46 | 47 | - [pytorch_vision_densenet](https://pytorch.org/hub/pytorch_vision_densenet/) 48 | 49 | 50 | -------------------------------------------------------------------------------- /image_classification/denseNet/densenet.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/densenet121.param"); // 模型加载 44 | net.load_model("models/densenet121.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("input.1", in); // 输入 54 | ex.extract("626", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/denseNet/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/denseNet/models/.DS_Store -------------------------------------------------------------------------------- /image_classification/denseNet/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet121', pretrained=True) 5 | model.eval() 6 | x = torch.rand(1, 3, 224, 224) 7 | # 方法1: pnnx 8 | # 1. pt --> torchscript 9 | traced_script_module = torch.jit.trace(model, x, strict=False) 10 | traced_script_module.save("ts.pt") 11 | 12 | # 2. ts --> pnnx --> ncnn 13 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 14 | 15 | # # 方法2: onnx 16 | # # 1. pt ---> onnx 17 | # torch_out = torch.onnx._export(model, x, "densenet121.onnx", export_params=True) 18 | 19 | # # 2. onnx --> onnxsim 20 | # os.system("python3 -m onnxsim densenet121.onnx sim.onnx") 21 | 22 | # # 3. onnx --> ncnn 23 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 24 | 25 | # # 4. ncnn --> optmize ---> ncnn 26 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 27 | 28 | # 两种转换都成功 29 | -------------------------------------------------------------------------------- /image_classification/efficientnet/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/efficientnet/.DS_Store -------------------------------------------------------------------------------- /image_classification/efficientnet/README.md: -------------------------------------------------------------------------------- 1 | # efficientnet 2 | 3 | ## Input --> Output 4 | 5 | ![](https://raw.githubusercontent.com/tensorflow/tpu/master/models/official/efficientnet/g3doc/params.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript--> pnnx --> ncnn 10 | 11 | ```python 12 | import torch 13 | import timm # 优秀的预训练模型库 14 | import os 15 | 16 | model = timm.create_model('efficientnet_b0', pretrained=True) 17 | model.eval() 18 | 19 | x = torch.rand(1, 3, 224, 224) 20 | traced_script_module = torch.jit.trace(model, x, strict=False) 21 | traced_script_module.save("ts.pt") 22 | 23 | # 2. ts --> pnnx --> ncnn 24 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 25 | ``` 26 | 27 | ## Example project 28 | 29 | 30 | ## Reference 31 | 32 | - [qubvel/efficientnet](https://github.com/qubvel/efficientnet) 33 | - [rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) 34 | 35 | 36 | -------------------------------------------------------------------------------- /image_classification/efficientnet/efficientnet.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/efficientnet_b0.param"); // 模型加载 44 | net.load_model("models/efficientnet_b0.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("in0", in); // 输入 54 | ex.extract("out0", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/efficientnet/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/efficientnet/models/.DS_Store -------------------------------------------------------------------------------- /image_classification/efficientnet/models/convert.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import timm # 优秀的预训练模型库 3 | import os 4 | 5 | model = timm.create_model('efficientnet_b0', pretrained=True) # efficientnet_b0~... 6 | model.eval() 7 | 8 | x = torch.rand(1, 3, 224, 224) 9 | traced_script_module = torch.jit.trace(model, x, strict=False) 10 | traced_script_module.save("ts.pt") 11 | 12 | # 2. ts --> pnnx --> ncnn 13 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 14 | 15 | # 支持下列所有模型 16 | # "efficientnet_b0", 17 | # "efficientnet_b1", 18 | # "efficientnet_b1_pruned", 19 | # "efficientnet_b2", 20 | # "efficientnet_b2_pruned", 21 | # "efficientnet_b3", 22 | # "efficientnet_b3_pruned", 23 | # "efficientnet_b4", 24 | # "efficientnet_el", 25 | # "efficientnet_el_pruned", 26 | # "efficientnet_em", 27 | # "efficientnet_es", 28 | # "efficientnet_es_pruned", 29 | # "efficientnet_lite0", 30 | -------------------------------------------------------------------------------- /image_classification/mobilenet_v2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/mobilenet_v2/.DS_Store -------------------------------------------------------------------------------- /image_classification/mobilenet_v2/README.md: -------------------------------------------------------------------------------- 1 | # mobilenet_v2 2 | 3 | ## Input --> Output 4 | 5 | ![](https://pytorch.org/assets/images/mobilenet_v2_2.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript--> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | model = torch.hub.load('pytorch/vision:v0.10.0', 'mobilenet_v2', pretrained=True) 16 | model.eval() # 下面两种方式都可以成功,推理结果也一样 17 | # # inference 18 | x = torch.randn(1, 3, 224, 224) 19 | # 方法1: pnnx 20 | # 1. pt --> torchscript 21 | traced_script_module = torch.jit.trace(model, x, strict=False) 22 | traced_script_module.save("ts.pt") 23 | 24 | # 2. ts --> pnnx --> ncnn 25 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 26 | 27 | # 方法2: onnx 28 | # # 1. pt ---> onnx 29 | # torch_out = torch.onnx._export(model, x, "mobilenet_v2.onnx", export_params=True) 30 | 31 | # # 2. onnx --> onnxsim 32 | # os.system("python3 -m onnxsim mobilenet_v2.onnx sim.onnx") 33 | 34 | # # 3. onnx --> ncnn 35 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 36 | 37 | # # 4. ncnn --> optmize ---> ncnn 38 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 39 | 40 | # 两种转换都成功 41 | ``` 42 | 两种转换都可以成功,且推理结果一样 43 | 44 | ## Example project 45 | 46 | 47 | ## Reference 48 | 49 | - [pytorch_vision_mobilenet_v2](https://pytorch.org/hub/pytorch_vision_mobilenet_v2/) 50 | 51 | 52 | -------------------------------------------------------------------------------- /image_classification/mobilenet_v2/mobilenet_v2.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/mobilenet_v2.param"); // 模型加载 44 | net.load_model("models/mobilenet_v2.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("in0", in); // 输入 54 | ex.extract("out0", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/mobilenet_v2/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/mobilenet_v2/models/.DS_Store -------------------------------------------------------------------------------- /image_classification/mobilenet_v2/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load('pytorch/vision:v0.10.0', 'mobilenet_v2', pretrained=True) 5 | model.eval() # 下面两种方式都可以成功,推理结果也一样 6 | # # inference 7 | x = torch.randn(1, 3, 224, 224) 8 | # 方法1: pnnx 9 | # 1. pt --> torchscript 10 | traced_script_module = torch.jit.trace(model, x, strict=False) 11 | traced_script_module.save("ts.pt") 12 | 13 | # 2. ts --> pnnx --> ncnn 14 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 15 | 16 | # 方法2: onnx 17 | # # 1. pt ---> onnx 18 | # torch_out = torch.onnx._export(model, x, "mobilenet_v2.onnx", export_params=True) 19 | 20 | # # 2. onnx --> onnxsim 21 | # os.system("python3 -m onnxsim mobilenet_v2.onnx sim.onnx") 22 | 23 | # # 3. onnx --> ncnn 24 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 25 | 26 | # # 4. ncnn --> optmize ---> ncnn 27 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 28 | 29 | # 两种转换都成功 30 | -------------------------------------------------------------------------------- /image_classification/mobilenet_v2/models/mobilenet_v2.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 75 85 3 | Input in0 0 1 in0 4 | Convolution convclip_0 1 1 in0 1 0=32 1=3 3=2 4=1 5=1 6=864 9=3 -23310=2,0.000000e+00,6.000000e+00 5 | ConvolutionDepthWise convdwclip_0 1 1 1 2 0=32 1=3 4=1 5=1 6=288 7=32 9=3 -23310=2,0.000000e+00,6.000000e+00 6 | Convolution conv_2 1 1 2 3 0=16 1=1 5=1 6=512 7 | Convolution convclip_1 1 1 3 4 0=96 1=1 5=1 6=1536 9=3 -23310=2,0.000000e+00,6.000000e+00 8 | ConvolutionDepthWise convdwclip_1 1 1 4 5 0=96 1=3 3=2 4=1 5=1 6=864 7=96 9=3 -23310=2,0.000000e+00,6.000000e+00 9 | Convolution conv_4 1 1 5 6 0=24 1=1 5=1 6=2304 10 | Split splitncnn_0 1 2 6 7 8 11 | Convolution convclip_2 1 1 8 9 0=144 1=1 5=1 6=3456 9=3 -23310=2,0.000000e+00,6.000000e+00 12 | ConvolutionDepthWise convdwclip_2 1 1 9 10 0=144 1=3 4=1 5=1 6=1296 7=144 9=3 -23310=2,0.000000e+00,6.000000e+00 13 | Convolution conv_6 1 1 10 11 0=24 1=1 5=1 6=3456 14 | BinaryOp add_0 2 1 7 11 12 15 | Convolution convclip_3 1 1 12 13 0=144 1=1 5=1 6=3456 9=3 -23310=2,0.000000e+00,6.000000e+00 16 | ConvolutionDepthWise convdwclip_3 1 1 13 14 0=144 1=3 3=2 4=1 5=1 6=1296 7=144 9=3 -23310=2,0.000000e+00,6.000000e+00 17 | Convolution conv_8 1 1 14 15 0=32 1=1 5=1 6=4608 18 | Split splitncnn_1 1 2 15 16 17 19 | Convolution convclip_4 1 1 17 18 0=192 1=1 5=1 6=6144 9=3 -23310=2,0.000000e+00,6.000000e+00 20 | ConvolutionDepthWise convdwclip_4 1 1 18 19 0=192 1=3 4=1 5=1 6=1728 7=192 9=3 -23310=2,0.000000e+00,6.000000e+00 21 | Convolution conv_10 1 1 19 20 0=32 1=1 5=1 6=6144 22 | BinaryOp add_1 2 1 16 20 21 23 | Split splitncnn_2 1 2 21 22 23 24 | Convolution convclip_5 1 1 23 24 0=192 1=1 5=1 6=6144 9=3 -23310=2,0.000000e+00,6.000000e+00 25 | ConvolutionDepthWise convdwclip_5 1 1 24 25 0=192 1=3 4=1 5=1 6=1728 7=192 9=3 -23310=2,0.000000e+00,6.000000e+00 26 | Convolution conv_12 1 1 25 26 0=32 1=1 5=1 6=6144 27 | BinaryOp add_2 2 1 22 26 27 28 | Convolution convclip_6 1 1 27 28 0=192 1=1 5=1 6=6144 9=3 -23310=2,0.000000e+00,6.000000e+00 29 | ConvolutionDepthWise convdwclip_6 1 1 28 29 0=192 1=3 3=2 4=1 5=1 6=1728 7=192 9=3 -23310=2,0.000000e+00,6.000000e+00 30 | Convolution conv_14 1 1 29 30 0=64 1=1 5=1 6=12288 31 | Split splitncnn_3 1 2 30 31 32 32 | Convolution convclip_7 1 1 32 33 0=384 1=1 5=1 6=24576 9=3 -23310=2,0.000000e+00,6.000000e+00 33 | ConvolutionDepthWise convdwclip_7 1 1 33 34 0=384 1=3 4=1 5=1 6=3456 7=384 9=3 -23310=2,0.000000e+00,6.000000e+00 34 | Convolution conv_16 1 1 34 35 0=64 1=1 5=1 6=24576 35 | BinaryOp add_3 2 1 31 35 36 36 | Split splitncnn_4 1 2 36 37 38 37 | Convolution convclip_8 1 1 38 39 0=384 1=1 5=1 6=24576 9=3 -23310=2,0.000000e+00,6.000000e+00 38 | ConvolutionDepthWise convdwclip_8 1 1 39 40 0=384 1=3 4=1 5=1 6=3456 7=384 9=3 -23310=2,0.000000e+00,6.000000e+00 39 | Convolution conv_18 1 1 40 41 0=64 1=1 5=1 6=24576 40 | BinaryOp add_4 2 1 37 41 42 41 | Split splitncnn_5 1 2 42 43 44 42 | Convolution convclip_9 1 1 44 45 0=384 1=1 5=1 6=24576 9=3 -23310=2,0.000000e+00,6.000000e+00 43 | ConvolutionDepthWise convdwclip_9 1 1 45 46 0=384 1=3 4=1 5=1 6=3456 7=384 9=3 -23310=2,0.000000e+00,6.000000e+00 44 | Convolution conv_20 1 1 46 47 0=64 1=1 5=1 6=24576 45 | BinaryOp add_5 2 1 43 47 48 46 | Convolution convclip_10 1 1 48 49 0=384 1=1 5=1 6=24576 9=3 -23310=2,0.000000e+00,6.000000e+00 47 | ConvolutionDepthWise convdwclip_10 1 1 49 50 0=384 1=3 4=1 5=1 6=3456 7=384 9=3 -23310=2,0.000000e+00,6.000000e+00 48 | Convolution conv_22 1 1 50 51 0=96 1=1 5=1 6=36864 49 | Split splitncnn_6 1 2 51 52 53 50 | Convolution convclip_11 1 1 53 54 0=576 1=1 5=1 6=55296 9=3 -23310=2,0.000000e+00,6.000000e+00 51 | ConvolutionDepthWise convdwclip_11 1 1 54 55 0=576 1=3 4=1 5=1 6=5184 7=576 9=3 -23310=2,0.000000e+00,6.000000e+00 52 | Convolution conv_24 1 1 55 56 0=96 1=1 5=1 6=55296 53 | BinaryOp add_6 2 1 52 56 57 54 | Split splitncnn_7 1 2 57 58 59 55 | Convolution convclip_12 1 1 59 60 0=576 1=1 5=1 6=55296 9=3 -23310=2,0.000000e+00,6.000000e+00 56 | ConvolutionDepthWise convdwclip_12 1 1 60 61 0=576 1=3 4=1 5=1 6=5184 7=576 9=3 -23310=2,0.000000e+00,6.000000e+00 57 | Convolution conv_26 1 1 61 62 0=96 1=1 5=1 6=55296 58 | BinaryOp add_7 2 1 58 62 63 59 | Convolution convclip_13 1 1 63 64 0=576 1=1 5=1 6=55296 9=3 -23310=2,0.000000e+00,6.000000e+00 60 | ConvolutionDepthWise convdwclip_13 1 1 64 65 0=576 1=3 3=2 4=1 5=1 6=5184 7=576 9=3 -23310=2,0.000000e+00,6.000000e+00 61 | Convolution conv_28 1 1 65 66 0=160 1=1 5=1 6=92160 62 | Split splitncnn_8 1 2 66 67 68 63 | Convolution convclip_14 1 1 68 69 0=960 1=1 5=1 6=153600 9=3 -23310=2,0.000000e+00,6.000000e+00 64 | ConvolutionDepthWise convdwclip_14 1 1 69 70 0=960 1=3 4=1 5=1 6=8640 7=960 9=3 -23310=2,0.000000e+00,6.000000e+00 65 | Convolution conv_30 1 1 70 71 0=160 1=1 5=1 6=153600 66 | BinaryOp add_8 2 1 67 71 72 67 | Split splitncnn_9 1 2 72 73 74 68 | Convolution convclip_15 1 1 74 75 0=960 1=1 5=1 6=153600 9=3 -23310=2,0.000000e+00,6.000000e+00 69 | ConvolutionDepthWise convdwclip_15 1 1 75 76 0=960 1=3 4=1 5=1 6=8640 7=960 9=3 -23310=2,0.000000e+00,6.000000e+00 70 | Convolution conv_32 1 1 76 77 0=160 1=1 5=1 6=153600 71 | BinaryOp add_9 2 1 73 77 78 72 | Convolution convclip_16 1 1 78 79 0=960 1=1 5=1 6=153600 9=3 -23310=2,0.000000e+00,6.000000e+00 73 | ConvolutionDepthWise convdwclip_16 1 1 79 80 0=960 1=3 4=1 5=1 6=8640 7=960 9=3 -23310=2,0.000000e+00,6.000000e+00 74 | Convolution conv_34 1 1 80 81 0=320 1=1 5=1 6=307200 75 | Convolution convclip_17 1 1 81 82 0=1280 1=1 5=1 6=409600 9=3 -23310=2,0.000000e+00,6.000000e+00 76 | Pooling gap_0 1 1 82 84 0=1 4=1 77 | InnerProduct linear_36 1 1 84 out0 0=1000 1=1 2=1280000 78 | -------------------------------------------------------------------------------- /image_classification/mobilenet_v3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/mobilenet_v3/.DS_Store -------------------------------------------------------------------------------- /image_classification/mobilenet_v3/README.md: -------------------------------------------------------------------------------- 1 | # mobilenet_v3 2 | 3 | ## Input --> Output 4 | 5 | ![](https://pytorch.org/assets/images/mobilenet_v2_2.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript--> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | import timm 15 | # 0. pt模型下载及初始化 16 | model = timm.create_model('mobilenetv3_large_100', pretrained=True) 17 | model.eval() 18 | 19 | x = torch.randn(1, 3, 224, 224) # 224起步 20 | # # 1. pt --> torchscript 21 | traced_script_module = torch.jit.trace(model, x, strict=False) 22 | traced_script_module.save("ts.pt") 23 | 24 | # # 2. ts --> pnnx --> ncnn 25 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 26 | 27 | ``` 28 | 29 | ## Example project 30 | 31 | 32 | ## Reference 33 | 34 | - [pytorch-image-models/models/mobilenet-v3](https://rwightman.github.io/pytorch-image-models/models/mobilenet-v3/) 35 | 36 | 37 | -------------------------------------------------------------------------------- /image_classification/mobilenet_v3/mobilenet_v3.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/mobilenet_v3.param"); // 模型加载 44 | net.load_model("models/mobilenet_v3.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("in0", in); // 输入 54 | ex.extract("out0", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/mobilenet_v3/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/mobilenet_v3/models/.DS_Store -------------------------------------------------------------------------------- /image_classification/mobilenet_v3/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import timm 4 | # 0. pt模型下载及初始化 5 | model = timm.create_model('mobilenetv3_large_100', pretrained=True) 6 | model.eval() 7 | 8 | x = torch.randn(1, 3, 224, 224) # 224起步 9 | # # 1. pt --> torchscript 10 | traced_script_module = torch.jit.trace(model, x, strict=False) 11 | traced_script_module.save("ts.pt") 12 | 13 | # # 2. ts --> pnnx --> ncnn 14 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 15 | -------------------------------------------------------------------------------- /image_classification/res2net/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/res2net/.DS_Store -------------------------------------------------------------------------------- /image_classification/res2net/README.md: -------------------------------------------------------------------------------- 1 | # res2net 2 | 3 | ## Input --> Output 4 | 5 | ![](https://production-media.paperswithcode.com/methods/Screen_Shot_2020-06-12_at_12.23.33_PM.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript --> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | import timm 15 | # 0. pt模型下载及初始化 16 | model = timm.create_model('res2net101_26w_4s', pretrained=True) 17 | model.eval() 18 | 19 | x = torch.randn(1, 3, 224, 224) # 224起步 20 | # # 1. pt --> torchscript 21 | traced_script_module = torch.jit.trace(model, x, strict=False) 22 | traced_script_module.save("ts.pt") 23 | 24 | # # 2. ts --> pnnx --> ncnn 25 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 26 | ``` 27 | 28 | ## Example project 29 | 30 | 31 | ## Reference 32 | 33 | - [pytorch-image-models/models/res2net/](https://rwightman.github.io/pytorch-image-models/models/res2net) 34 | 35 | 36 | -------------------------------------------------------------------------------- /image_classification/res2net/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import timm 4 | # 0. pt模型下载及初始化 5 | model = timm.create_model('res2net101_26w_4s', pretrained=True) 6 | model.eval() 7 | 8 | x = torch.randn(1, 3, 224, 224) # 224起步 9 | # # 1. pt --> torchscript 10 | traced_script_module = torch.jit.trace(model, x, strict=False) 11 | traced_script_module.save("ts.pt") 12 | 13 | # # 2. ts --> pnnx --> ncnn 14 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 15 | -------------------------------------------------------------------------------- /image_classification/res2net/res2net.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/res2net101_26w_4s.param"); // 模型加载 44 | net.load_model("models/res2net101_26w_4s.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("in0", in); // 输入 54 | ex.extract("out0", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/res2next50/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/res2next50/.DS_Store -------------------------------------------------------------------------------- /image_classification/res2next50/README.md: -------------------------------------------------------------------------------- 1 | # res2net 2 | 3 | ## Input --> Output 4 | 5 | ![](https://production-media.paperswithcode.com/methods/Screen_Shot_2020-06-06_at_4.32.52_PM.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript --> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | import timm 15 | # 0. pt模型下载及初始化 16 | model = timm.create_model('res2next50', pretrained=True) 17 | model.eval() 18 | 19 | x = torch.randn(1, 3, 224, 224) # 224起步 20 | # # 1. pt --> torchscript 21 | traced_script_module = torch.jit.trace(model, x, strict=False) 22 | traced_script_module.save("ts.pt") 23 | 24 | # # 2. ts --> pnnx --> ncnn 25 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 26 | ``` 27 | 28 | ## Example project 29 | 30 | 31 | ## Reference 32 | 33 | - [pytorch-image-models/models/res2next/](https://rwightman.github.io/pytorch-image-models/models/res2next/) 34 | 35 | 36 | -------------------------------------------------------------------------------- /image_classification/res2next50/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import timm 4 | # 0. pt模型下载及初始化 5 | model = timm.create_model('res2next50', pretrained=True) 6 | model.eval() 7 | 8 | x = torch.randn(1, 3, 224, 224) # 224起步 9 | # # 1. pt --> torchscript 10 | traced_script_module = torch.jit.trace(model, x, strict=False) 11 | traced_script_module.save("ts.pt") 12 | 13 | # # 2. ts --> pnnx --> ncnn 14 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 15 | -------------------------------------------------------------------------------- /image_classification/res2next50/res2next50.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/res2next50/res2next50.cpp -------------------------------------------------------------------------------- /image_classification/resnet18/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/resnet18/.DS_Store -------------------------------------------------------------------------------- /image_classification/resnet18/README.md: -------------------------------------------------------------------------------- 1 | # resnet18 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/PeterL1n/RobustVideoMatting/raw/master/documentation/image/showreel.gif) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript --> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | import torchvision 15 | import torch.onnx 16 | # 0. pt模型下载及初始化 17 | model = torchvision.models.resnet18(pretrained=True) 18 | x = torch.rand(1, 3, 224, 224) # 入口参数 19 | # 方法1: pnnx 20 | # 1. pt --> torchscript 21 | traced_script_module = torch.jit.trace(model, x, strict=False) 22 | traced_script_module.save("ts.pt") 23 | 24 | # 2. ts --> pnnx --> ncnn 25 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 26 | 27 | # # 方法2:onnx 28 | # # 1. pt ---> onnx 29 | # torch_out = torch.onnx._export(model, x, "resnet18.onnx", export_params=True) 30 | 31 | # # 2. onnx --> onnxsim 32 | # os.system("python3 -m onnxsim resnet18.onnx sim.onnx") 33 | 34 | # # 3. onnx --> ncnn 35 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 36 | 37 | # # 4. ncnn --> optmize ---> ncnn 38 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 39 | 40 | # 两种方法都可以转换成ncnn 41 | ``` 42 | 43 | ## Example project 44 | 45 | 46 | ## Reference 47 | 48 | - [Tencent/ncnn](https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx) 49 | 50 | 51 | -------------------------------------------------------------------------------- /image_classification/resnet18/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/resnet18/models/.DS_Store -------------------------------------------------------------------------------- /image_classification/resnet18/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import torchvision 4 | import torch.onnx 5 | # 0. pt模型下载及初始化 6 | model = torchvision.models.resnet18(pretrained=True) 7 | x = torch.rand(1, 3, 224, 224) # 入口参数 8 | # 方法1: pnnx 9 | # 1. pt --> torchscript 10 | traced_script_module = torch.jit.trace(model, x, strict=False) 11 | traced_script_module.save("ts.pt") 12 | 13 | # 2. ts --> pnnx --> ncnn 14 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 15 | 16 | # # 方法2:onnx 17 | # # 1. pt ---> onnx 18 | # torch_out = torch.onnx._export(model, x, "resnet18.onnx", export_params=True) 19 | 20 | # # 2. onnx --> onnxsim 21 | # os.system("python3 -m onnxsim resnet18.onnx sim.onnx") 22 | 23 | # # 3. onnx --> ncnn 24 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 25 | 26 | # # 4. ncnn --> optmize ---> ncnn 27 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 28 | 29 | # 两种方法都可以转换成ncnn 30 | -------------------------------------------------------------------------------- /image_classification/resnet18/models/download.txt: -------------------------------------------------------------------------------- 1 | bin文件体积巨大,请自行转换 -------------------------------------------------------------------------------- /image_classification/resnet18/models/resnet18.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 48 56 3 | Input input.1 0 1 input.1 4 | Convolution Conv_0 1 1 input.1 onnx::MaxPool_125 0=64 1=7 3=2 4=3 5=1 6=9408 9=1 5 | Pooling MaxPool_2 1 1 onnx::MaxPool_125 input.8 1=3 2=2 3=1 5=1 6 | Split splitncnn_0 1 2 input.8 input.8_splitncnn_0 input.8_splitncnn_1 7 | Convolution Conv_3 1 1 input.8_splitncnn_1 onnx::Conv_129 0=64 1=3 4=1 5=1 6=36864 9=1 8 | Convolution Conv_5 1 1 onnx::Conv_129 onnx::Add_198 0=64 1=3 4=1 5=1 6=36864 9 | BinaryOp Add_6 2 1 onnx::Add_198 input.8_splitncnn_0 onnx::Relu_132 10 | ReLU Relu_7 1 1 onnx::Relu_132 input.24 11 | Split splitncnn_1 1 2 input.24 input.24_splitncnn_0 input.24_splitncnn_1 12 | Convolution Conv_8 1 1 input.24_splitncnn_1 onnx::Conv_136 0=64 1=3 4=1 5=1 6=36864 9=1 13 | Convolution Conv_10 1 1 onnx::Conv_136 onnx::Add_204 0=64 1=3 4=1 5=1 6=36864 14 | BinaryOp Add_11 2 1 onnx::Add_204 input.24_splitncnn_0 onnx::Relu_139 15 | ReLU Relu_12 1 1 onnx::Relu_139 input.40 16 | Split splitncnn_2 1 2 input.40 input.40_splitncnn_0 input.40_splitncnn_1 17 | Convolution Conv_13 1 1 input.40_splitncnn_1 onnx::Conv_143 0=128 1=3 3=2 4=1 5=1 6=73728 9=1 18 | Convolution Conv_15 1 1 onnx::Conv_143 onnx::Add_210 0=128 1=3 4=1 5=1 6=147456 19 | Convolution Conv_16 1 1 input.40_splitncnn_0 onnx::Add_213 0=128 1=1 3=2 5=1 6=8192 20 | BinaryOp Add_17 2 1 onnx::Add_210 onnx::Add_213 onnx::Relu_148 21 | ReLU Relu_18 1 1 onnx::Relu_148 input.60 22 | Split splitncnn_3 1 2 input.60 input.60_splitncnn_0 input.60_splitncnn_1 23 | Convolution Conv_19 1 1 input.60_splitncnn_1 onnx::Conv_152 0=128 1=3 4=1 5=1 6=147456 9=1 24 | Convolution Conv_21 1 1 onnx::Conv_152 onnx::Add_219 0=128 1=3 4=1 5=1 6=147456 25 | BinaryOp Add_22 2 1 onnx::Add_219 input.60_splitncnn_0 onnx::Relu_155 26 | ReLU Relu_23 1 1 onnx::Relu_155 input.76 27 | Split splitncnn_4 1 2 input.76 input.76_splitncnn_0 input.76_splitncnn_1 28 | Convolution Conv_24 1 1 input.76_splitncnn_1 onnx::Conv_159 0=256 1=3 3=2 4=1 5=1 6=294912 9=1 29 | Convolution Conv_26 1 1 onnx::Conv_159 onnx::Add_225 0=256 1=3 4=1 5=1 6=589824 30 | Convolution Conv_27 1 1 input.76_splitncnn_0 onnx::Add_228 0=256 1=1 3=2 5=1 6=32768 31 | BinaryOp Add_28 2 1 onnx::Add_225 onnx::Add_228 onnx::Relu_164 32 | ReLU Relu_29 1 1 onnx::Relu_164 input.96 33 | Split splitncnn_5 1 2 input.96 input.96_splitncnn_0 input.96_splitncnn_1 34 | Convolution Conv_30 1 1 input.96_splitncnn_1 onnx::Conv_168 0=256 1=3 4=1 5=1 6=589824 9=1 35 | Convolution Conv_32 1 1 onnx::Conv_168 onnx::Add_234 0=256 1=3 4=1 5=1 6=589824 36 | BinaryOp Add_33 2 1 onnx::Add_234 input.96_splitncnn_0 onnx::Relu_171 37 | ReLU Relu_34 1 1 onnx::Relu_171 input.112 38 | Split splitncnn_6 1 2 input.112 input.112_splitncnn_0 input.112_splitncnn_1 39 | Convolution Conv_35 1 1 input.112_splitncnn_1 onnx::Conv_175 0=512 1=3 3=2 4=1 5=1 6=1179648 9=1 40 | Convolution Conv_37 1 1 onnx::Conv_175 onnx::Add_240 0=512 1=3 4=1 5=1 6=2359296 41 | Convolution Conv_38 1 1 input.112_splitncnn_0 onnx::Add_243 0=512 1=1 3=2 5=1 6=131072 42 | BinaryOp Add_39 2 1 onnx::Add_240 onnx::Add_243 onnx::Relu_180 43 | ReLU Relu_40 1 1 onnx::Relu_180 input.132 44 | Split splitncnn_7 1 2 input.132 input.132_splitncnn_0 input.132_splitncnn_1 45 | Convolution Conv_41 1 1 input.132_splitncnn_1 onnx::Conv_184 0=512 1=3 4=1 5=1 6=2359296 9=1 46 | Convolution Conv_43 1 1 onnx::Conv_184 onnx::Add_249 0=512 1=3 4=1 5=1 6=2359296 47 | BinaryOp Add_44 2 1 onnx::Add_249 input.132_splitncnn_0 onnx::Relu_187 48 | ReLU Relu_45 1 1 onnx::Relu_187 input.148 49 | Pooling GlobalAveragePool_46 1 1 input.148 onnx::Gemm_190 0=1 4=1 50 | InnerProduct Gemm_48 1 1 onnx::Gemm_190 191 0=1000 1=1 2=512000 51 | -------------------------------------------------------------------------------- /image_classification/resnet18/resnet18.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/resnet18.param"); // 模型加载 44 | net.load_model("models/resnet18.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 入口RGB格式 shape (3 x H x W), where H and W are expected to be at least 224. 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("input.1", in); // 输入 54 | ex.extract("191", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/shufflenetv2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/shufflenetv2/.DS_Store -------------------------------------------------------------------------------- /image_classification/shufflenetv2/README.md: -------------------------------------------------------------------------------- 1 | # shufflenetv2 2 | 3 | ## Input --> Output 4 | 5 | ![](https://pytorch.org/assets/images/shufflenet_v2_1.png) 6 | 7 | ## Convert 8 | 9 | onnx --> onnx-sim --> onnx2ncnn --> ncnnoptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | 14 | # 1. download 15 | # download_url = https://github.com/onnx/models/tree/main/vision/classification/shufflenet 16 | os.system("") 17 | 18 | # 2. onnx --> onnxsim 19 | os.system("python -m onnxsim shufflenet-v2-12.onnx sim.onnx") 20 | 21 | # 3. onnx --> ncnn 22 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 23 | 24 | # 4. ncnn --> optmize ---> ncnn 25 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 26 | ``` 27 | 28 | ## Example project 29 | 30 | 31 | ## Reference 32 | 33 | - [hub/pytorch_vision_shufflenet_v2/](https://pytorch.org/hub/pytorch_vision_shufflenet_v2/) 34 | - [shufflenet](https://github.com/onnx/models/tree/main/vision/classification/shufflenet) 35 | 36 | 37 | -------------------------------------------------------------------------------- /image_classification/shufflenetv2/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/shufflenetv2/models/.DS_Store -------------------------------------------------------------------------------- /image_classification/shufflenetv2/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # 1. download 4 | # download_url = https://github.com/onnx/models/tree/main/vision/classification/shufflenet 5 | os.system("") 6 | 7 | # 2. onnx --> onnxsim 8 | os.system("python -m onnxsim shufflenet-v2-12.onnx sim.onnx") 9 | 10 | # 3. onnx --> ncnn 11 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 12 | 13 | # 4. ncnn --> optmize ---> ncnn 14 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 15 | os.remove("sim.onnx") 16 | -------------------------------------------------------------------------------- /image_classification/shufflenetv2/shufflenetv2.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/resnet18.param"); // 模型加载 44 | net.load_model("models/resnet18.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 入口RGB格式 shape (3 x H x W), where H and W are expected to be at least 224. 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("input.1", in); // 输入 54 | ex.extract("191", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_classification/vgg19/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_classification/vgg19/.DS_Store -------------------------------------------------------------------------------- /image_classification/vgg19/README.md: -------------------------------------------------------------------------------- 1 | # vgg 2 | 3 | ## Input --> Output 4 | 5 | ![](https://pytorch.org/assets/images/vgg.png) 6 | 7 | ## Convert 8 | 9 | pt --> torchscript --> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | model = torch.hub.load('pytorch/vision:v0.10.0', 'vgg16', pretrained=True) # vgg11_bn vgg13 vgg13_bn vgg16_bn vgg19_bn vgg19 16 | model.eval() 17 | x = torch.rand(1, 3, 224, 224) # 最低224*224起步 18 | # 1. pt --> torchscript 19 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 224, 224), strict=False) 20 | traced_script_module.save("ts.pt") 21 | 22 | # 2. ts --> pnnx --> ncnn 23 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 24 | ``` 25 | 26 | ## Example project 27 | 28 | 29 | ## Reference 30 | 31 | - [hub/pytorch_vision_vgg](https://pytorch.org/hub/pytorch_vision_vgg/) 32 | 33 | 34 | -------------------------------------------------------------------------------- /image_classification/vgg19/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load('pytorch/vision:v0.10.0', 'vgg16', pretrained=True) # vgg11_bn vgg13 vgg13_bn vgg16_bn vgg19_bn vgg19 5 | model.eval() 6 | x = torch.rand(1, 3, 224, 224) # 最低224*224起步 7 | # 1. pt --> torchscript 8 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 224, 224), strict=False) 9 | traced_script_module.save("ts.pt") 10 | 11 | # 2. ts --> pnnx --> ncnn 12 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") 13 | -------------------------------------------------------------------------------- /image_classification/vgg19/models/download.txt: -------------------------------------------------------------------------------- 1 | vgg16.bin文件体积巨大,请自行转换 -------------------------------------------------------------------------------- /image_classification/vgg19/models/vgg16.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 24 24 3 | Input in0 0 1 in0 4 | Convolution convrelu_0 1 1 in0 1 0=64 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=1728 9=1 5 | Convolution convrelu_1 1 1 1 2 0=64 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=1 6 | Pooling maxpool2d_16 1 1 2 3 0=0 1=2 11=2 12=2 13=0 2=2 3=0 5=1 7 | Convolution convrelu_2 1 1 3 4 0=128 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=73728 9=1 8 | Convolution convrelu_3 1 1 4 5 0=128 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=1 9 | Pooling maxpool2d_17 1 1 5 6 0=0 1=2 11=2 12=2 13=0 2=2 3=0 5=1 10 | Convolution convrelu_4 1 1 6 7 0=256 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=294912 9=1 11 | Convolution convrelu_5 1 1 7 8 0=256 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=589824 9=1 12 | Convolution convrelu_6 1 1 8 9 0=256 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=589824 9=1 13 | Pooling maxpool2d_18 1 1 9 10 0=0 1=2 11=2 12=2 13=0 2=2 3=0 5=1 14 | Convolution convrelu_7 1 1 10 11 0=512 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=1179648 9=1 15 | Convolution convrelu_8 1 1 11 12 0=512 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2359296 9=1 16 | Convolution convrelu_9 1 1 12 13 0=512 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2359296 9=1 17 | Pooling maxpool2d_19 1 1 13 14 0=0 1=2 11=2 12=2 13=0 2=2 3=0 5=1 18 | Convolution convrelu_10 1 1 14 15 0=512 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2359296 9=1 19 | Convolution convrelu_11 1 1 15 16 0=512 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2359296 9=1 20 | Convolution convrelu_12 1 1 16 17 0=512 1=3 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2359296 9=1 21 | Pooling maxpool2d_20 1 1 17 18 0=0 1=2 11=2 12=2 13=0 2=2 3=0 5=1 22 | Pooling aap_37 1 1 18 19 0=1 18=7 7=1 8=7 23 | Flatten flatten_36 1 1 19 20 24 | InnerProduct fcrelu_0 1 1 20 21 0=4096 1=1 2=102760448 9=1 25 | InnerProduct fcrelu_1 1 1 21 22 0=4096 1=1 2=16777216 9=1 26 | InnerProduct linear_15 1 1 22 out0 0=1000 1=1 2=4096000 27 | -------------------------------------------------------------------------------- /image_classification/vgg19/vgg16.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | static int print_topk(const std::vector &cls_scores, int topk) 8 | { 9 | // partial sort topk with index 10 | int size = cls_scores.size(); 11 | std::vector> vec; 12 | vec.resize(size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | vec[i] = std::make_pair(cls_scores[i], i); 16 | } 17 | 18 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), 19 | std::greater>()); 20 | 21 | // print topk and score 22 | for (int i = 0; i < topk; i++) 23 | { 24 | float score = vec[i].first; 25 | int index = vec[i].second; 26 | fprintf(stderr, "%d = %f\n", index, score); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 35 | if (m.empty()) 36 | { 37 | std::cout << "read image failed" << std::endl; 38 | return -1; 39 | } 40 | 41 | ncnn::Net net; 42 | net.opt.use_vulkan_compute = true; // GPU环境 43 | net.load_param("models/vgg16.param"); // 模型加载 44 | net.load_model("models/vgg16.bin"); 45 | 46 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 47 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 48 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 49 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 50 | 51 | ncnn::Mat out; 52 | ncnn::Extractor ex = net.create_extractor(); 53 | ex.input("in0", in); // 输入 54 | ex.extract("out0", out); // 输出 55 | std::vector cls_scores; // 所有分类的得分 56 | cls_scores.resize(out.w); // 分类个数 57 | for (int j = 0; j < out.w; j++) 58 | { 59 | cls_scores[j] = out[j]; 60 | } 61 | print_topk(cls_scores, 5); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /image_inpainting/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_inpainting/.DS_Store -------------------------------------------------------------------------------- /image_inpainting/deoldify/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_inpainting/deoldify/.DS_Store -------------------------------------------------------------------------------- /image_inpainting/deoldify/README.md: -------------------------------------------------------------------------------- 1 | # AnimeGanV3 2 | 3 | ## Input --> Output 4 | 5 | ![](input.png) 6 | ![](output.jpg) 7 | 8 | ## Convert 9 | 10 | ### 方法1: 11 | pt --> TorchScript --> pnnx --> ncnnOptimize --> ncnn 12 | 13 | ```python 14 | # 欢迎pr 15 | ``` 16 | ### 方法2: 17 | pt ---> onnx ---> onnxsim ---> onnx2ncnn ---> 手动修复reshape等 ---> ncnnOptimize --> ncnn 18 | 19 | 实现案例:https://zhuanlan.zhihu.com/p/350332071 20 | 21 | ## fix 22 | 23 | 已修复模型-23303=1,0 5=1;移除C代码;简化demo运行 24 | 25 | ## Reference 26 | 27 | - [jantic/DeOldify](https://github.com/jantic/DeOldify) 28 | - [KeepGoing2019HaHa/AI-application](https://github.com/KeepGoing2019HaHa/AI-application) 29 | 30 | 31 | -------------------------------------------------------------------------------- /image_inpainting/deoldify/deoldify.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include // min/max 6 | #include "iostream" 7 | 8 | float get_max(ncnn::Mat &out) 9 | { 10 | int out_c = out.c; 11 | int out_w = out.w; 12 | int out_h = out.h; 13 | for (int i = 0; i < out_c; i++) 14 | { 15 | float max = -100, min = 9999999999999; 16 | for (int j = 0; j < out_h; j++) 17 | { 18 | for (int k = 0; k < out_w; k++) 19 | { 20 | if (out.channel(i).row(j)[k] > max) 21 | { 22 | max = out.channel(i).row(j)[k]; 23 | } 24 | if (out.channel(i).row(j)[k] < min) 25 | { 26 | min = out.channel(i).row(j)[k]; 27 | } 28 | } 29 | } 30 | // std::cout << max << " " << min << std::endl; 31 | } 32 | return 0; 33 | } 34 | 35 | int main(int argc, char **argv) 36 | { 37 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 38 | if (m.empty()) 39 | { 40 | std::cout << "read image failed" << std::endl; 41 | return -1; 42 | } 43 | 44 | ncnn::Net net; 45 | net.opt.use_vulkan_compute = true; // GPU环境 46 | net.load_param("models/deoldify.256.param"); // 模型加载 47 | net.load_model("models/deoldify.256.bin"); 48 | 49 | // ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_RGB, m.cols, m.rows, 512, 512); 50 | // ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_RGB, m.cols, m.rows, 128, 128); 51 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 256, 256); // 图片缩放 52 | // const float pre_mean_vals[3] = {0.f, 0.f, 0.f}; 53 | // const float pre_norm_vals[3] = {1.0/255.0f,1.0/255.0f,1.0/255.0f}; 54 | // in.substract_mean_normalize(pre_mean_vals, pre_norm_vals); 55 | // const float mean_vals[3] = {0.485f, 0.456f, 0.406f}; 56 | // const float norm_vals[3] = {0.229f, 0.224f, 0.225f}; 57 | // in.substract_mean_normalize(mean_vals, norm_vals); 58 | ncnn::Mat out; 59 | ncnn::Extractor ex = net.create_extractor(); 60 | ex.input("input", in); // 输入 61 | ex.extract("out", out); // 输出 62 | get_max(out); 63 | 64 | // cv::Mat cv_out = cv::Mat::zeros(512, 512, CV_8UC3); 65 | // cv::Mat cv_out = cv::Mat::zeros(128, 128, CV_8UC3); 66 | cv::Mat cv_out = cv::Mat::zeros(256, 256, CV_8UC3); 67 | 68 | // out.to_pixels(cv_out.data, ncnn::Mat::PIXEL_RGB); 69 | // out.to_pixels(cv_out.data, ncnn::Mat::PIXEL_BGR); 70 | for (int c = 0; c < 3; c++) 71 | { 72 | for (int i = 0; i < out.h; i++) 73 | { 74 | for (int j = 0; j < out.w; j++) 75 | { 76 | float t = ((float *)out.data)[j + i * out.w + c * out.h * out.w]; 77 | cv_out.data[(2 - c) + j * 3 + i * out.w * 3] = t; 78 | } 79 | } 80 | } 81 | 82 | cv::imwrite("ncnn.jpg", cv_out); 83 | cv::imshow("output", cv_out); 84 | cv::waitKey(0); 85 | 86 | return 0; 87 | } -------------------------------------------------------------------------------- /image_inpainting/deoldify/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_inpainting/deoldify/input.png -------------------------------------------------------------------------------- /image_inpainting/deoldify/models/convert.py: -------------------------------------------------------------------------------- 1 | # 欢迎pr 2 | -------------------------------------------------------------------------------- /image_inpainting/deoldify/models/download.txt: -------------------------------------------------------------------------------- 1 | 该模型文件体积巨大,无法使用GitHub存储,bin文件下载地址:https://drive.google.com/drive/folders/1tywoozrH76DjSFNFhQGjnRzZGD1EPKm7 2 | param文件可使用现成的 -------------------------------------------------------------------------------- /image_inpainting/deoldify/output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_inpainting/deoldify/output.jpg -------------------------------------------------------------------------------- /image_inpainting/mat/README.md: -------------------------------------------------------------------------------- 1 | # MAT: Mask-Aware Transformer for Large Hole Image Inpainting 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/fenglinglwb/MAT/raw/main/figures/teasing.png) 6 | ![](https://github.com/fenglinglwb/MAT/raw/main/figures/sota.png) 7 | 8 | ## Convert 9 | 10 | pt --> TorchScript --> pnnx --> ncnn 11 | 12 | ```bash 13 | # 欢迎pr 14 | 个人运行依赖环境报错: 15 | ImportError: DLL load failed while importing upfirdn2d_plugin: 找不到指定的模块。 16 | warnings.warn('Failed to build CUDApython generate_image.py --network pretrained/CelebA-HQ_512.pkl --dpath test_sets/CelebA-HQ/images --mpath test_sets/CelebA-HQ/masks --outdir 17 | ``` 18 | 19 | ## fix 20 | 21 | 22 | 23 | ## Reference 24 | 25 | - [fenglinglwb/MAT](https://github.com/fenglinglwb/MAT) 26 | 27 | 28 | -------------------------------------------------------------------------------- /image_inpainting/mat/mat.cpp: -------------------------------------------------------------------------------- 1 | // 欢迎pr -------------------------------------------------------------------------------- /image_matting/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_matting/.DS_Store -------------------------------------------------------------------------------- /image_matting/RVM/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_matting/RVM/.DS_Store -------------------------------------------------------------------------------- /image_matting/RVM/README.md: -------------------------------------------------------------------------------- 1 | # RVM 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/PeterL1n/RobustVideoMatting/raw/master/documentation/image/showreel.gif) 6 | 7 | ## Convert [❌] 8 | 9 | pt --> TorchScript --> pnnx --> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | model = torch.hub.load("PeterL1n/RobustVideoMatting", "mobilenetv3") # or "resnet50" 16 | model.cpu() 17 | model.eval() 18 | # model(src, *rec, downsample_ratio=0.25) # src can be [B, C, H, W] or [B, T, C, H, W] RGB input is normalized to 0~1 range. 19 | # 1. pt --> torchscript 20 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 512, 512), strict=False) 21 | traced_script_module.save("ts.pt") 22 | 23 | # 2. ts --> pnnx --> ncnn 24 | os.system("pnnx ts.pt inputshape=[1,3,512,512],[1,3,320,320] device=cpu") # 可能错误 25 | 26 | # 3. ncnn ---> optmize ----> ncnn 27 | os.system("ncnnoptimize ts.ncnn.param ts.ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 28 | ``` 29 | 报错内容如下: 30 | ```log 31 | # 运行后c++报错:原因暂时不清楚 32 | # pass_level5 33 | # pass_ncnn 34 | # ignore pnnx.Expression pnnx_expr_256 param expr=1 35 | # create_custom_layer pnnx.Expression 36 | # fuse_convolution_activation conv_12 hswish_83 37 | ``` 38 | c++运行后报错: 39 | ``` 40 | layer pnnx_expr_256 not exists or registered 41 | ``` 42 | 43 | ## Example project 44 | 45 | - [Desktop: RVM-GUI](https://github.com/Baiyuetribe/paper2gui/blob/main/ImageMatting/rvm_gui.md) 46 | - [Android: FeiGeChuanShu/ncnn_Android_RobustVideoMatting](https://github.com/FeiGeChuanShu/ncnn_Android_RobustVideoMatting) 47 | 48 | ## Reference 49 | 50 | - [Tencent/ncnn](https://github.com/Tencent/ncnn/blob/master/examples/rvm.cpp) 51 | - [PeterL1n/RobustVideoMatting](https://github.com/PeterL1n/RobustVideoMatting) 52 | 53 | 54 | -------------------------------------------------------------------------------- /image_matting/RVM/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load("PeterL1n/RobustVideoMatting", "mobilenetv3") # or "resnet50" 5 | model.cpu() 6 | model.eval() 7 | # model(src, *rec, downsample_ratio=0.25) # src can be [B, C, H, W] or [B, T, C, H, W] RGB input is normalized to 0~1 range. 8 | # 1. pt --> torchscript 9 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 512, 512), strict=False) 10 | traced_script_module.save("ts.pt") 11 | 12 | # 2. ts --> pnnx --> ncnn 13 | os.system("pnnx ts.pt inputshape=[1,3,512,512] device=cpu") # 可能错误 14 | 15 | 16 | # 运行后c++报错:原因暂时不清楚 17 | # ignore pnnx.Expression pnnx_expr_256 param expr=1 18 | # create_custom_layer pnnx.Expression 19 | # fuse_convolution_activation conv_12 hswish_83 20 | -------------------------------------------------------------------------------- /image_matting/RVM/models/download.txt: -------------------------------------------------------------------------------- 1 | 因为原始项目转ncnn复现失败,急用的可以前往云盘下载: ncnn model https://pan.baidu.com/s/11iEY2RGfzWFtce8ue7T3JQ password: d9t6 -------------------------------------------------------------------------------- /image_matting/RVM/rvm.cpp: -------------------------------------------------------------------------------- 1 | // opencv2 2 | #include 3 | #include 4 | #include 5 | // ncnn 6 | #include "net.h" 7 | #include 8 | #include 9 | 10 | static void draw_objects(const cv::Mat &bgr, const cv::Mat &fgr, const cv::Mat &pha) 11 | { 12 | cv::Mat fgr8U; 13 | fgr.convertTo(fgr8U, CV_8UC3, 255.0, 0); 14 | cv::Mat pha8U; 15 | pha.convertTo(pha8U, CV_8UC1, 255.0, 0); 16 | 17 | cv::Mat comp; 18 | cv::resize(bgr, comp, pha.size(), 0, 0, 1); 19 | for (int i = 0; i < pha8U.rows; i++) 20 | { 21 | for (int j = 0; j < pha8U.cols; j++) 22 | { 23 | uchar data = pha8U.at(i, j); 24 | float alpha = (float)data / 255; 25 | comp.at(i, j)[0] = fgr8U.at(i, j)[0] * alpha + (1 - alpha) * 155; 26 | comp.at(i, j)[1] = fgr8U.at(i, j)[1] * alpha + (1 - alpha) * 255; 27 | comp.at(i, j)[2] = fgr8U.at(i, j)[2] * alpha + (1 - alpha) * 120; 28 | } 29 | } 30 | 31 | cv::imshow("pha", pha8U); // alpha图 32 | cv::imshow("fgr", fgr8U); // 前景图 33 | cv::imshow("comp", comp); // 合成图 34 | cv::waitKey(0); 35 | } 36 | static int detect_rvm(const cv::Mat &bgr, cv::Mat &pha, cv::Mat &fgr) 37 | { 38 | const float downsample_ratio = 0.5f; 39 | const int target_width = 512; 40 | const int target_height = 512; 41 | 42 | ncnn::Net net; 43 | net.opt.use_vulkan_compute = true; 44 | // original pretrained model from https://github.com/PeterL1n/RobustVideoMatting 45 | // ncnn model https://pan.baidu.com/s/11iEY2RGfzWFtce8ue7T3JQ password: d9t6 46 | net.load_param("rvm_512.param"); 47 | net.load_model("rvm_512.bin"); 48 | 49 | // if you use another input size,pleaze change input shape 50 | ncnn::Mat r1i = ncnn::Mat(128, 128, 16); 51 | ncnn::Mat r2i = ncnn::Mat(64, 64, 20); 52 | ncnn::Mat r3i = ncnn::Mat(32, 32, 40); 53 | ncnn::Mat r4i = ncnn::Mat(16, 16, 64); 54 | r1i.fill(0.0f); 55 | r2i.fill(0.0f); 56 | r3i.fill(0.0f); 57 | r4i.fill(0.0f); 58 | 59 | ncnn::Extractor ex = net.create_extractor(); 60 | const float mean_vals1[3] = {123.675f, 116.28f, 103.53f}; 61 | const float norm_vals1[3] = {0.01712475f, 0.0175f, 0.01742919f}; 62 | const float mean_vals2[3] = {0, 0, 0}; 63 | const float norm_vals2[3] = {1 / 255.0, 1 / 255.0, 1 / 255.0}; 64 | ncnn::Mat ncnn_in2 = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_width, target_height); 65 | ncnn::Mat ncnn_in1 = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_width * downsample_ratio, target_height * downsample_ratio); 66 | 67 | ncnn_in1.substract_mean_normalize(mean_vals1, norm_vals1); 68 | ncnn_in2.substract_mean_normalize(mean_vals2, norm_vals2); 69 | 70 | ex.input("src1", ncnn_in1); 71 | ex.input("src2", ncnn_in2); 72 | ex.input("r1i", r1i); 73 | ex.input("r2i", r2i); 74 | ex.input("r3i", r3i); 75 | ex.input("r4i", r4i); 76 | 77 | // if use video matting,these output will be input of next infer 78 | ex.extract("r4o", r4i); 79 | ex.extract("r3o", r3i); 80 | ex.extract("r2o", r2i); 81 | ex.extract("r1o", r1i); 82 | 83 | ncnn::Mat pha_; 84 | ex.extract("pha", pha_); 85 | ncnn::Mat fgr_; 86 | ex.extract("fgr", fgr_); 87 | 88 | cv::Mat cv_pha = cv::Mat(pha_.h, pha_.w, CV_32FC1, (float *)pha_.data); 89 | cv::Mat cv_fgr = cv::Mat(fgr_.h, fgr_.w, CV_32FC3); 90 | float *fgr_data = (float *)fgr_.data; 91 | for (int i = 0; i < fgr_.h; i++) 92 | { 93 | for (int j = 0; j < fgr_.w; j++) 94 | { 95 | cv_fgr.at(i, j)[2] = fgr_data[0 * fgr_.h * fgr_.w + i * fgr_.w + j]; 96 | cv_fgr.at(i, j)[1] = fgr_data[1 * fgr_.h * fgr_.w + i * fgr_.w + j]; 97 | cv_fgr.at(i, j)[0] = fgr_data[2 * fgr_.h * fgr_.w + i * fgr_.w + j]; 98 | } 99 | } 100 | 101 | cv_pha.copyTo(pha); 102 | cv_fgr.copyTo(fgr); 103 | 104 | return 0; 105 | } 106 | int main(int argc, char **argv) 107 | { 108 | if (argc != 2) 109 | { 110 | std::cout << "Usage: " << argv[0] << " video_file" << std::endl; 111 | return -1; 112 | } 113 | std::string imagepath = argv[1]; 114 | 115 | cv::Mat m = cv::imread(imagepath, 1); 116 | if (m.empty()) 117 | { 118 | std::cout << "read image failed" << std::endl; 119 | return -1; 120 | } 121 | cv::Mat fgr, pha; 122 | detect_rvm(m, pha, fgr); 123 | draw_objects(m, fgr, pha); // 后处理 124 | 125 | return 0; 126 | } -------------------------------------------------------------------------------- /image_matting/deeplabv3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_matting/deeplabv3/.DS_Store -------------------------------------------------------------------------------- /image_matting/deeplabv3/README.md: -------------------------------------------------------------------------------- 1 | # deeplabv3_ 2 | 3 | ## Input --> Output 4 | 5 | ![](https://pytorch.org/assets/images/deeplab1.png) 6 | ![](https://pytorch.org/assets/images/deeplab2.png) 7 | 8 | ## Convert 9 | 10 | pt --> torchscript --> pnnx --> ncnn 11 | 12 | ```python 13 | import os 14 | import torch 15 | # 0. pt模型下载及初始化 16 | model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_resnet50', pretrained=True) 17 | # model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_resnet101', pretrained=True) 18 | # model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_mobilenet_v3_large', pretrained=True) 19 | model.eval() 20 | x = torch.rand(1, 3, 224, 224) 21 | # 方法1 :pnnx 22 | # 1. pt --> torchscript 23 | traced_script_module = torch.jit.trace(model, x, strict=False) 24 | traced_script_module.save("ts.pt") 25 | 26 | # 2. ts --> pnnx --> ncnn 27 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 28 | 29 | # ## 方法2: onnx 30 | # # 1. pt ---> onnx 31 | # torch_out = torch.onnx._export(model, x, "deeplabv3_resnet50.onnx", export_params=True) 32 | 33 | # # 2. onnx --> onnxsim 34 | # os.system("python3 -m onnxsim deeplabv3_resnet50.onnx sim.onnx") 35 | 36 | # # 3. onnx --> ncnn 37 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 38 | 39 | # # 4. ncnn --> optmize ---> ncnn 40 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 41 | 42 | # 两种转换都成功 43 | ``` 44 | 45 | 46 | 47 | 48 | ## Example project 49 | 50 | 51 | ## Reference 52 | 53 | - [pytorch_vision_deeplabv3_resnet101](https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/) 54 | 55 | 56 | -------------------------------------------------------------------------------- /image_matting/deeplabv3/deeplabv3.cpp: -------------------------------------------------------------------------------- 1 | #include "net.h" // ncnn 2 | #include 3 | #include 4 | #include 5 | #include "iostream" 6 | 7 | int main(int argc, char **argv) 8 | { 9 | cv::Mat m = cv::imread("input.png"); // 输入一张图片,BGR格式 10 | if (m.empty()) 11 | { 12 | std::cout << "read image failed" << std::endl; 13 | return -1; 14 | } 15 | 16 | ncnn::Net net; 17 | net.opt.use_vulkan_compute = true; // GPU环境 18 | net.load_param("models/deeplabv3_resnet50.param"); // 模型加载 or resnet101 or deeplabv3_mobilenet_v3_large 19 | net.load_model("models/deeplabv3_resnet50.bin"); 20 | 21 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(m.data, ncnn::Mat::PIXEL_BGR2RGB, m.cols, m.rows, 224, 224); // 图片缩放 22 | const float mean_vals[3] = {0.485f / 255.f, 0.456f / 255.f, 0.406f / 255.f}; // Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 23 | const float norm_vals[3] = {1.0 / 0.229f / 255.f, 1.0 / 0.224f / 255.f, 1.0 / 0.225f / 255.f}; 24 | in.substract_mean_normalize(mean_vals, norm_vals); // 像素范围0~255 25 | 26 | ncnn::Mat out; 27 | ncnn::Extractor ex = net.create_extractor(); 28 | ex.input("input.1", in); // 输入 29 | ex.extract("607", out); // 输出 30 | // print shape(out); 31 | std::cout << out.w << out.h << out.dims << out.c << std::endl; // 输出维度 24*224*224 shape (21, H, W) 32 | // 后处理待写 33 | return 0; 34 | } -------------------------------------------------------------------------------- /image_matting/deeplabv3/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_matting/deeplabv3/models/.DS_Store -------------------------------------------------------------------------------- /image_matting/deeplabv3/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_resnet50', pretrained=True) 5 | # model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_resnet101', pretrained=True) 6 | # model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_mobilenet_v3_large', pretrained=True) 7 | model.eval() 8 | x = torch.rand(1, 3, 224, 224) 9 | # 方法1 :pnnx 10 | # 1. pt --> torchscript 11 | traced_script_module = torch.jit.trace(model, x, strict=False) 12 | traced_script_module.save("ts.pt") 13 | 14 | # 2. ts --> pnnx --> ncnn 15 | os.system("pnnx ts.pt inputshape=[1,3,224,224]") # # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 16 | 17 | # ## 方法2: onnx 18 | # # 1. pt ---> onnx 19 | # torch_out = torch.onnx._export(model, x, "deeplabv3_resnet50.onnx", export_params=True) 20 | 21 | # # 2. onnx --> onnxsim 22 | # os.system("python3 -m onnxsim deeplabv3_resnet50.onnx sim.onnx") 23 | 24 | # # 3. onnx --> ncnn 25 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 26 | 27 | # # 4. ncnn --> optmize ---> ncnn 28 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 29 | 30 | # 两种转换都成功 31 | -------------------------------------------------------------------------------- /image_matting/deeplabv3/models/download.txt: -------------------------------------------------------------------------------- 1 | bin文件体积过大,受github限制。运行convert即可获得bin和param文件 2 | -------------------------------------------------------------------------------- /image_matting/vitae/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/image_matting/vitae/.DS_Store -------------------------------------------------------------------------------- /image_matting/yolov7_mask/README.md: -------------------------------------------------------------------------------- 1 | # YOLOv7-Mask 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/FeiGeChuanShu/yolov7-mask-ncnn/raw/main/screenshot.jpg) 6 | 7 | ## Convert [❌] 8 | 9 | 欢迎pr 10 | 11 | ## Example project 12 | 13 | ## NCNN-MODELS 14 | 15 | - [Download](https://github.com/Baiyuetribe/ncnn-models/releases/tag/models) 16 | 17 | 18 | ## Reference 19 | 20 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 21 | - [FeiGeChuanShu/yolov7-mask-ncnn](https://github.com/FeiGeChuanShu/yolov7-mask-ncnn) 22 | 23 | 24 | -------------------------------------------------------------------------------- /nlp/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/nlp/.DS_Store -------------------------------------------------------------------------------- /nlp/gpt2-chinese/README.md: -------------------------------------------------------------------------------- 1 | # GPT2-Chinese 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/Morizeyao/GPT2-Chinese/raw/old_gpt_2_chinese_before_2021_4_22/sample/%E6%95%A3%E6%96%871.png) 6 | ![](https://github.com/EdVince/GPT2-ChineseChat-NCNN/raw/main/resources/android.jpg) 7 | 8 | ## Convert [❌] 9 | 10 | pt --> TorchScript --> pnnx --> ncnnOptimize --> ncnn 11 | 12 | ```python 13 | # 暂无方法,欢迎pr 14 | ``` 15 | 16 | ## 复现结果 17 | 18 | 个人尝试复现,输出结果如下: 19 | ```log 20 | cpp.vcxproj -> C:\Users\baiyue\Desktop\Dev\桌面APP\sdk开发\gpt2-sdk\Release\cpp.exe 21 | 割 22 | [0 NVIDIA GeForce RTX 2070 SUPER] queueC=2[8] queueG=0[16] queueT=1[2] 23 | [0 NVIDIA GeForce RTX 2070 SUPER] bugsbn1=0 bugbilz=0 bugcopc=0 bugihfa=0 24 | [0 NVIDIA GeForce RTX 2070 SUPER] fp16-p/s/a=1/1/1 int8-p/s/a=1/1/1 25 | [0 NVIDIA GeForce RTX 2070 SUPER] subgroup=32 basic=1 vote=1 ballot=1 shuffle=1 26 | 尽量用中文,目前英文有点小问题,输入quit退出,输入refresh清空记忆 27 | user:你好 28 | chatbot:[PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD][PAD] 29 | user: 30 | ``` 31 | [PAD]为字典首字符,暂未清楚什么原因导致失败的,欢迎pr或issue讨论 32 | 33 | ## NCNN-MODELS 34 | 35 | - [Download](https://github.com/Baiyuetribe/ncnn-models/releases/tag/models) 36 | 37 | ## Example project 38 | 39 | - [Android: EdVince/GPT2-ChineseChat-NCNN](https://github.com/EdVince/GPT2-ChineseChat-NCNN) 40 | 41 | ## Reference 42 | 43 | - [Morizeyao/GPT2-Chinese](https://github.com/Morizeyao/GPT2-Chinese) 44 | - [EdVince/GPT2-ChineseChat-NCNN](https://github.com/EdVince/GPT2-ChineseChat-NCNN) 45 | - [Tencent/ncnn](https://github.com/Tencent/ncnn/blob/master/examples/rvm.cpp) 46 | 47 | 48 | -------------------------------------------------------------------------------- /object_dection/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/.DS_Store -------------------------------------------------------------------------------- /object_dection/fastestdet/README.md: -------------------------------------------------------------------------------- 1 | # FastestDet 2 | 3 | 4 | ## Input --> Output 5 | 6 | ![](https://github.com/dog-qiuqiu/FastestDet/raw/main/data/data.png) 7 | ![](fastestdet.png) 8 | 9 | ## Convert 10 | 11 | pt --> TorchScript --> pnnx --> ncnn 12 | ``` 13 | git clone https://github.com/dog-qiuqiu/FastestDet.git # clone 14 | cd FastestDet 15 | pip install -r requirements.txt # install 16 | # export models: pt 00> torchscript 17 | python test.py --yaml configs/coco.yaml --weight weights/coco_ap05_0.250_280epoch.pth --img data/3.jpg --torchscript 18 | # torchscript --> ncnn 19 | pnnx yolov7.torchscript.pt inputshape=[1,3,352,352] 20 | 21 | ## 注意,crop算子计算有问题,最新的ncnn可能已修复,如果未修复,请使用已有的param文件替换你自己的即可使用。 22 | # https://github.com/Tencent/ncnn/pull/3999 23 | ``` 24 | 25 | ## Example project 26 | 27 | ## NCNN-MODELS 28 | 29 | - [Download](https://github.com/Baiyuetribe/ncnn-models/releases/tag/models) 30 | 31 | 32 | ## Reference 33 | 34 | - [dog-qiuqiu/FastestDet](https://github.com/dog-qiuqiu/FastestDet) 35 | 36 | 37 | -------------------------------------------------------------------------------- /object_dection/fastestdet/fastestdet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/fastestdet/fastestdet.png -------------------------------------------------------------------------------- /object_dection/fastestdet/pt2ncnn.sh: -------------------------------------------------------------------------------- 1 | git clone https://github.com/dog-qiuqiu/FastestDet.git # clone 2 | cd FastestDet 3 | pip install -r requirements.txt # install 4 | # export models: pt 00> torchscript 5 | python test.py --yaml configs/coco.yaml --weight weights/coco_ap05_0.250_280epoch.pth --img data/3.jpg --torchscript 6 | # torchscript --> ncnn 7 | pnnx yolov7.torchscript.pt inputshape=[1,3,352,352] 8 | 9 | ## 注意,crop算子计算有问题,最新的ncnn可能已修复,如果未修复,请使用已有的param文件替换你自己的即可使用。 10 | # https://github.com/Tencent/ncnn/pull/3999 11 | -------------------------------------------------------------------------------- /object_dection/hybridnets/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/hybridnets/.DS_Store -------------------------------------------------------------------------------- /object_dection/hybridnets/README.md: -------------------------------------------------------------------------------- 1 | # hybridnets 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/datvuthanh/HybridNets/raw/1aa5dd3783e3760d88e372079ec5f07907e9406e/images/hybridnets.jpg) 6 | ![](https://github.com/datvuthanh/HybridNets/raw/1aa5dd3783e3760d88e372079ec5f07907e9406e/images/full_video.gif) 7 | 8 | ## Convert 9 | 10 | pt --> TorchScript --> pnnx --> ncnnOptimize --> ncnn 11 | 12 | ```python 13 | import os 14 | import torch 15 | # 0. pt模型下载及初始化 16 | model = torch.hub.load('datvuthanh/hybridnets', 'hybridnets', pretrained=True) 17 | model.eval() 18 | # # inference 19 | # x = torch.randn(1, 3, 640, 384) 20 | # # 1. pt ---> onnx 21 | # torch_out = torch.onnx._export(model, x, "hybridnets.onnx", export_params=True, opset_version=11) 22 | 23 | # # 2. onnx --> onnxsim 24 | # os.system("python3 -m onnxsim hybridnets.onnx sim.onnx") 25 | 26 | # # 3. onnx --> ncnn 27 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 28 | 29 | # # 4. ncnn --> optmize ---> ncnn 30 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 31 | 32 | 33 | # 1. pt --> torchscript 34 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 640, 384), strict=False) 35 | traced_script_module.save("ts.pt") 36 | 37 | # 2. ts --> pnnx --> ncnn 38 | os.system("pnnx ts.pt inputshape=[1,3,640,384] device=cpu") # 可能错误 39 | 40 | # 3. ncnn ---> optmize ----> ncnn 41 | os.system("ncnnoptimize ts.ncnn.param ts.ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 42 | ``` 43 | 44 | 两种转换均已失败告终,报错内容如下: 45 | ``` 46 | onnx模式下报错: 47 | Warning: Constant folding - Only steps=1 can be constant folded for opset >= 10 onnx::Slice op. Constant folding not applied. 48 | pnnx模式下: 49 | [ CPUFloatType{1,46035,4} ]) of traced region did not have observable data dependence with trace inputs; this probably indicates your program cannot be understood by the tracer. 50 | ``` 51 | 52 | ## Example project 53 | 54 | 55 | ## Reference 56 | 57 | - [datvuthanh/HybridNets](https://github.com/datvuthanh/HybridNets) 58 | 59 | 60 | -------------------------------------------------------------------------------- /object_dection/hybridnets/hybridnets.cpp: -------------------------------------------------------------------------------- 1 | // 欢迎pr -------------------------------------------------------------------------------- /object_dection/hybridnets/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load('datvuthanh/hybridnets', 'hybridnets', pretrained=True) 5 | model.eval() 6 | # # inference 7 | # x = torch.randn(1, 3, 640, 384) 8 | # # 1. pt ---> onnx 9 | # torch_out = torch.onnx._export(model, x, "hybridnets.onnx", export_params=True, opset_version=11) 10 | 11 | # # 2. onnx --> onnxsim 12 | # os.system("python -m onnxsim hybridnets.onnx sim.onnx") 13 | 14 | # # 3. onnx --> ncnn 15 | # os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 16 | 17 | # # 4. ncnn --> optmize ---> ncnn 18 | # os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 19 | 20 | 21 | # 1. pt --> torchscript 22 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 640, 384), strict=False) 23 | traced_script_module.save("ts.pt") 24 | 25 | # 2. ts --> pnnx --> ncnn 26 | os.system("pnnx ts.pt inputshape=[1,3,640,384] device=cpu") # 可能错误 27 | 28 | # 3. ncnn ---> optmize ----> ncnn 29 | os.system("ncnnoptimize ts.ncnn.param ts.ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 30 | 31 | # 两种方式都报错 32 | -------------------------------------------------------------------------------- /object_dection/nanodet/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/nanodet/.DS_Store -------------------------------------------------------------------------------- /object_dection/nanodet/README.md: -------------------------------------------------------------------------------- 1 | # nanodet 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/RangiLyu/nanodet/raw/main/docs/imgs/Android_demo.jpg) 6 | 7 | ## Convert 8 | 9 | pt --> onnx --> onnx-sim --> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | # 1. download onnx model 16 | os.system("wget https://github.com/RangiLyu/nanodet/releases/download/v1.0.0-alpha-1/nanodet-plus-m_416.onnx -O nanodet-plus-m_416.onnx") 17 | 18 | # 2. onnx --> onnxsim 19 | os.system("python3 -m onnxsim nanodet-plus-m_416.onnx sim.onnx") 20 | 21 | # 3. onnx --> ncnn 22 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 23 | 24 | # 4. ncnn --> optmize ---> ncnn 25 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 26 | ``` 27 | 28 | ## Example project 29 | 30 | - [Android: nihui/ncnn-android-nanodet](https://github.com/nihui/ncnn-android-nanodet) 31 | - [WASM: nihui/ncnn-webassembly-nanodet](https://github.com/nihui/ncnn-webassembly-nanodet) 32 | 33 | ## Reference 34 | 35 | - [RangiLyu/nanodet](https://github.com/RangiLyu/nanodet) 36 | 37 | 38 | -------------------------------------------------------------------------------- /object_dection/nanodet/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/nanodet/models/.DS_Store -------------------------------------------------------------------------------- /object_dection/nanodet/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | # 1. download onnx model 5 | os.system("wget https://github.com/RangiLyu/nanodet/releases/download/v1.0.0-alpha-1/nanodet-plus-m_416.onnx -O nanodet-plus-m_416.onnx") 6 | 7 | # 2. onnx --> onnxsim 8 | os.system("python3 -m onnxsim nanodet-plus-m_416.onnx sim.onnx") 9 | 10 | # 3. onnx --> ncnn 11 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 12 | 13 | # 4. ncnn --> optmize ---> ncnn 14 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 15 | -------------------------------------------------------------------------------- /object_dection/yolo-fastestv2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolo-fastestv2/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolo-fastestv2/README.md: -------------------------------------------------------------------------------- 1 | # yolo-fastestv2 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/dog-qiuqiu/Yolo-FastestV2/raw/main/img/demo.png) 6 | 7 | ## Convert 8 | 9 | pt --> TorchScript --> pnnx --> ncnn 10 | 11 | ```python 12 | import torch 13 | import model.detector # 导入模型 14 | import os 15 | 16 | # model = model.detector.Detector(cfg["classes"], cfg["anchor_num"], True, True) 17 | model = model.detector.Detector(80, 3, True, True) 18 | model.load_state_dict(torch.load(r"modelzoo\coco2017-0.241078ap-model.pth")) 19 | # sets the module in eval node 20 | model.eval() 21 | 22 | 23 | # # 1. pt --> torchscript 24 | # traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 352, 352)) 25 | # traced_script_module.save("ts.pt") 26 | # # 2. ts --> pnnx --> ncnn 27 | # os.system("pnnx ts.pt inputshape=[1,3,352,352]") 28 | 29 | torch.onnx.export(model, # model being run 30 | torch.randn(1, 3, 352, 352), # model input (or a tuple for multiple inputs) 31 | "out.onnx", # where to save the model (can be a file or file-like object) 32 | export_params=True, # store the trained parameter weights inside the model file 33 | opset_version=11, # the ONNX version to export the model to 34 | do_constant_folding=True) # whether to execute constant folding for optimization 35 | 36 | # 2. onnx --> onnxsim 37 | os.system("python -m onnxsim out.onnx sim.onnx") 38 | 39 | # 3. onnx --> ncnn 40 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 41 | 42 | # 4. ncnn --> optmize ---> ncnn 43 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 44 | ``` 45 | 转换方法,将上述文件放到项目目录下,然后运行命令就可以成功。 46 | torchscript转换成功,但是运行失败,onnx方法转换成功 47 | 48 | ## Example project 49 | 50 | 51 | 52 | ## Reference 53 | 54 | - [dog-qiuqiu/Yolo-FastestV2](https://github.com/dog-qiuqiu/Yolo-FastestV2) 55 | 56 | 57 | -------------------------------------------------------------------------------- /object_dection/yolo-fastestv2/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolo-fastestv2/models/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolo-fastestv2/models/convert.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import model.detector # 导入模型 3 | import os 4 | 5 | # model = model.detector.Detector(cfg["classes"], cfg["anchor_num"], True, True) 6 | model = model.detector.Detector(80, 3, True, True) 7 | model.load_state_dict(torch.load(r"modelzoo\coco2017-0.241078ap-model.pth")) 8 | # sets the module in eval node 9 | model.eval() 10 | 11 | 12 | # # 1. pt --> torchscript 13 | # traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 352, 352)) 14 | # traced_script_module.save("ts.pt") 15 | # # 2. ts --> pnnx --> ncnn 16 | # os.system("pnnx ts.pt inputshape=[1,3,352,352]") 17 | 18 | torch.onnx.export(model, # model being run 19 | torch.randn(1, 3, 352, 352), # model input (or a tuple for multiple inputs) 20 | "out.onnx", # where to save the model (can be a file or file-like object) 21 | export_params=True, # store the trained parameter weights inside the model file 22 | opset_version=11, # the ONNX version to export the model to 23 | do_constant_folding=True) # whether to execute constant folding for optimization 24 | 25 | # 2. onnx --> onnxsim 26 | os.system("python -m onnxsim out.onnx sim.onnx") 27 | 28 | # 3. onnx --> ncnn 29 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 30 | 31 | # 4. ncnn --> optmize ---> ncnn 32 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 33 | 34 | # 转换方法,将上述文件放到项目目录下,然后运行命令就可以成功。 35 | # torchscript转换成功,但是运行失败,onnx方法转换成功 36 | -------------------------------------------------------------------------------- /object_dection/yolop/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolop/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolop/README.md: -------------------------------------------------------------------------------- 1 | # yolop 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/hustvl/YOLOP/raw/main/pictures/detect.png) 6 | ![](https://github.com/hustvl/YOLOP/raw/main/pictures/da.png) 7 | 8 | ## Convert 9 | 10 | pt --> TorchScript --> pnnx --> ncnnOptimize --> ncnn 11 | 12 | ```python 13 | import os 14 | import torch 15 | # 0. pt模型下载及初始化 16 | model = torch.hub.load('hustvl/yolop', 'yolop', pretrained=True) 17 | model.eval() 18 | # inference 19 | x = torch.randn(1, 3, 640, 640) 20 | # 1. pt ---> onnx 21 | torch_out = torch.onnx._export(model, x, "yolop.onnx", export_params=True) 22 | 23 | # 2. onnx --> onnxsim 24 | os.system("python3 -m onnxsim yolop.onnx sim.onnx") 25 | 26 | # 3. onnx --> ncnn 27 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 28 | 29 | # 4. ncnn --> optmize ---> ncnn 30 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 31 | 32 | # ==== pnnx方法 33 | # # 1. pt --> torchscript 34 | # traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 640, 640), strict=False) 35 | # traced_script_module.save("ts.pt") 36 | 37 | # # 2. ts --> pnnx --> ncnn 38 | # os.system("pnnx ts.pt inputshape=[1,3,640,640] device=cpu") # 可能错误 39 | 40 | # # 3. ncnn ---> optmize ----> ncnn 41 | # os.system("ncnnoptimize ts.ncnn.param ts.ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 42 | ``` 43 | 44 | 两种转换均已失败告终,报错内容如下: 45 | ``` 46 | onnx模式下报错: 47 | RuntimeError: step!=1 is currently not supported 48 | pnnx模式下: 49 | :List inputs to traced functions must have consistent element type. Found Tuple[Tensor, List[Tensor]] and Tensor 50 | ``` 51 | 52 | ## Example project 53 | 54 | - [Android: EdVince/YOLOP-NCNN](https://github.com/EdVince/YOLOP-NCNN) 55 | - [Desktop: EdVince/YOLOP-NCNN](https://github.com/EdVince/YOLOP-NCNN) 56 | 57 | ## Reference 58 | 59 | - [详细记录u版YOLOv5目标检测ncnn实现(第二版)](https://zhuanlan.zhihu.com/p/471357671) 60 | - [ultralytics/yolov5](https://github.com/ultralytics/yolov5) 61 | 62 | 63 | -------------------------------------------------------------------------------- /object_dection/yolop/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load('hustvl/yolop', 'yolop', pretrained=True) 5 | model.eval() 6 | # inference 7 | x = torch.randn(1, 3, 640, 640) 8 | # 1. pt ---> onnx 9 | torch_out = torch.onnx._export(model, x, "yolop.onnx", export_params=True) 10 | 11 | # 2. onnx --> onnxsim 12 | os.system("python3 -m onnxsim yolop.onnx sim.onnx") 13 | 14 | # 3. onnx --> ncnn 15 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 16 | 17 | # 4. ncnn --> optmize ---> ncnn 18 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 19 | 20 | # ==== pnnx方法 21 | # # 1. pt --> torchscript 22 | # traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 640, 640), strict=False) 23 | # traced_script_module.save("ts.pt") 24 | 25 | # # 2. ts --> pnnx --> ncnn 26 | # os.system("pnnx ts.pt inputshape=[1,3,640,640] device=cpu") # 可能错误 27 | 28 | # # 3. ncnn ---> optmize ----> ncnn 29 | # os.system("ncnnoptimize ts.ncnn.param ts.ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 30 | 31 | 32 | # 两种均报错 33 | 34 | # 模型复现失败,急用的可前往: https://github.com/EdVince/YOLOP-NCNN 35 | -------------------------------------------------------------------------------- /object_dection/yolop/yolop.cpp: -------------------------------------------------------------------------------- 1 | // 欢迎pr -------------------------------------------------------------------------------- /object_dection/yolov5/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolov5/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolov5/README.md: -------------------------------------------------------------------------------- 1 | # yolov5 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/ultralytics/yolov5/releases/download/v1.0/splash.jpg) 6 | 7 | ## Convert 8 | 9 | pt --> TorchScript --> pnnx --> ncnn 10 | ``` 11 | git clone https://github.com/ultralytics/yolov5 # clone 12 | cd yolov5 13 | pip install -r requirements.txt # install 14 | python export.py --weights yolov5s.pt --include torchscript --train 15 | ./pnnx yolov5s.torchscript inputshape=[1,3,640,640] inputshape2=[1,3,320,320] 16 | ``` 17 | 18 | ## Example project 19 | 20 | - [Android: FeiGeChuanShu/ncnn-android-yolox](https://github.com/FeiGeChuanShu/ncnn-android-yolox) 21 | - [WASM: nihui/ncnn-webassembly-yolov5](https://github.com/nihui/ncnn-webassembly-yolov5) 22 | - [Uni-app: 670***@qq.com](https://ext.dcloud.net.cn/plugin?id=5243) 23 | 24 | ## Reference 25 | 26 | - [详细记录u版YOLOv5目标检测ncnn实现(第二版)](https://zhuanlan.zhihu.com/p/471357671) 27 | - [ultralytics/yolov5](https://github.com/ultralytics/yolov5) 28 | 29 | 30 | -------------------------------------------------------------------------------- /object_dection/yolov5/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolov5/models/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolov5/models/convert.sh: -------------------------------------------------------------------------------- 1 | git clone https://github.com/ultralytics/yolov5 # clone 2 | cd yolov5 3 | pip install -r requirements.txt # install 4 | python export.py --weights yolov5s.pt --include torchscript --train 5 | ./pnnx yolov5s.torchscript inputshape=[1,3,640,640] inputshape2=[1,3,320,320] 6 | # --train必须添加,否则会报错;inputshape2代表动态化尺寸,可以根据实际情况设置 7 | -------------------------------------------------------------------------------- /object_dection/yolov6/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolov6/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolov6/README.md: -------------------------------------------------------------------------------- 1 | # YOLOv6 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/meituan/YOLOv6/raw/main/assets/picture.png) 6 | 7 | ## Convert 8 | 9 | pt --> TorchScript --> pnnx --> ncnn 10 | 11 | ```python 12 | # import os 13 | 14 | # # 0. 项目下载 15 | # os.system("git clone https://github.com/meituan/YOLOv6.git") 16 | 17 | # # 1. 模型下载yolox_tiny,yolox_s,yolox_m,yolox_l,yolox_xl,yolox_Darknet53 18 | 19 | # # 2. pt ---> torchscript 20 | # os.system("python3 tools/export_torchscript.py --output-name ts.pt -n yolox-nano -c yolox_nano.pth") 21 | 22 | # # 3. ts ---> pnnx ---> ncnn 23 | # os.system("pnnx ts.pt inputshape=[1,3,416,416] inputshape2=[1,3,640,640]") # nano 和tiny输入尺寸为416*416.其余为640*640. 24 | ``` 25 | 通过onnx和PNNX转换均存在以下胶水op,暂无详细手动调整操作 26 | 27 | ## NCNN-MODELS 28 | 29 | - [Download](https://github.com/Baiyuetribe/ncnn-models/releases/tag/models) 30 | 31 | ## Example project 32 | 33 | - [Android: FeiGeChuanShu/ncnn-android-yolov6](https://github.com/FeiGeChuanShu/ncnn-android-yolov6) 34 | 35 | ## Reference 36 | 37 | - [meituan/YOLOv6](https://github.com/meituan/YOLOv6) 38 | 39 | 40 | -------------------------------------------------------------------------------- /object_dection/yolov6/models/convert.py: -------------------------------------------------------------------------------- 1 | # import os 2 | 3 | # # 0. 项目下载 4 | # os.system("git clone https://github.com/Megvii-BaseDetection/YOLOX.git") 5 | 6 | # # 1. 模型下载yolox_s,yolox_m,yolox_l,yolox_xl,yolox_Darknet53 7 | # os.system("wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.pth -O yolox_nano.pth") 8 | 9 | # # 2. pt ---> torchscript 10 | # os.system("python3 tools/export_torchscript.py --output-name ts.pt -n yolox-nano -c yolox_nano.pth") 11 | 12 | # # 3. ts ---> pnnx ---> ncnn 13 | # os.system("pnnx ts.pt inputshape=[1,3,416,416]") # nano 和tiny输入尺寸为416*416.其余为640*640. 14 | 15 | # # 注意还有后处理,参见readme.md 16 | 17 | # pnnx或onnx转换均存在胶水op 18 | -------------------------------------------------------------------------------- /object_dection/yolov7/README.md: -------------------------------------------------------------------------------- 1 | # YOLOv7 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/WongKinYiu/yolov7/raw/main/figure/performance.png) 6 | 7 | ## Convert 8 | 9 | pt --> TorchScript --> pnnx --> ncnn 10 | ``` 11 | git clone https://github.com/WongKinYiu/yolov7.git # clone 12 | cd yolov7 13 | pip install -r requirements.txt # install 14 | # download models 15 | wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt 16 | python models/export.py --weights yolov7 # export 17 | pnnx yolov7.torchscript.pt inputshape=[1,3,640,640] inputshape=[1,3,320,320] 18 | ``` 19 | 20 | ## Example project 21 | 22 | ## NCNN-MODELS 23 | 24 | - [Download](https://github.com/Baiyuetribe/ncnn-models/releases/tag/models) 25 | 26 | 27 | 28 | ## Reference 29 | 30 | - [WongKinYiu/yolov7](https://github.com/WongKinYiu/yolov7) 31 | 32 | 33 | -------------------------------------------------------------------------------- /object_dection/yolov7/pt2ncnn.sh: -------------------------------------------------------------------------------- 1 | git clone https://github.com/WongKinYiu/yolov7.git # clone 2 | cd yolov7 3 | pip install -r requirements.txt # install 4 | # download models 5 | wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt 6 | python models/export.py --weights yolov7 # export 7 | pnnx yolov7.torchscript.pt inputshape=[1,3,640,640] inputshape=[1,3,320,320] 8 | -------------------------------------------------------------------------------- /object_dection/yolox/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolox/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolox/README.md: -------------------------------------------------------------------------------- 1 | # yolox 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/Megvii-BaseDetection/YOLOX/raw/main/assets/git_fig.png) 6 | 7 | ## Convert 8 | 9 | pt --> TorchScript --> pnnx --> ncnn 10 | 11 | ```python 12 | import os 13 | 14 | # 0. 项目下载 15 | os.system("git clone https://github.com/Megvii-BaseDetection/YOLOX.git") 16 | 17 | # 1. 模型下载yolox_tiny,yolox_s,yolox_m,yolox_l,yolox_xl,yolox_Darknet53 18 | os.system("wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.pth -O yolox_nano.pth") 19 | 20 | # 2. pt ---> torchscript 21 | os.system("python3 tools/export_torchscript.py --output-name ts.pt -n yolox-nano -c yolox_nano.pth") 22 | 23 | # 3. ts ---> pnnx ---> ncnn 24 | os.system("pnnx ts.pt inputshape=[1,3,416,416] inputshape2=[1,3,640,640]") # nano 和tiny输入尺寸为416*416.其余为640*640. 25 | ``` 26 | 生成的param文件里,手动移除Input后面到Contact6层,然后新加一层,最后变为如下效果: 27 | ```ruby 28 | 7767517 29 | 279 317 30 | Input in0 0 1 in0 31 | YoloV5Focus focus 1 1 in0 9 32 | ``` 33 | 结尾也是一个Contact层,补加一层Permute. 34 | 改前: 35 | ```ruby 36 | Concat cat_17 3 1 313 314 315 out0 0=1 37 | ``` 38 | 改后: 39 | ```ruby 40 | Concat cat_17 3 1 313 314 315 316 0=1 41 | Permute Transpose_333 1 1 316 out0 0=1 42 | ``` 43 | 为什么加这一层?暂不清楚。不加之前输出out0.shape: 8400 85 1 2 原始加上Permute后结果是:85 8400 1 2。 44 | 也许不需要这一层处理,但要变更下后处理,个人尝试简单替换w、h是无效的。 45 | 46 | ## Example project 47 | 48 | - [Android: FeiGeChuanShu/ncnn-android-yolox](https://github.com/FeiGeChuanShu/ncnn-android-yolox) 49 | 50 | ## Reference 51 | 52 | - [Megvii-BaseDetection/YOLOX](https://github.com/Megvii-BaseDetection/YOLOX) 53 | 54 | 55 | -------------------------------------------------------------------------------- /object_dection/yolox/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/object_dection/yolox/models/.DS_Store -------------------------------------------------------------------------------- /object_dection/yolox/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # 0. 项目下载 4 | os.system("git clone https://github.com/Megvii-BaseDetection/YOLOX.git") 5 | 6 | # 1. 模型下载yolox_s,yolox_m,yolox_l,yolox_xl,yolox_Darknet53 7 | os.system("wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.pth -O yolox_nano.pth") 8 | 9 | # 2. pt ---> torchscript 10 | os.system("python3 tools/export_torchscript.py --output-name ts.pt -n yolox-nano -c yolox_nano.pth") 11 | 12 | # 3. ts ---> pnnx ---> ncnn 13 | os.system("pnnx ts.pt inputshape=[1,3,416,416]") # nano 和tiny输入尺寸为416*416.其余为640*640. 14 | 15 | # 注意还有后处理,参见readme.md 16 | -------------------------------------------------------------------------------- /style_transfer/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/.DS_Store -------------------------------------------------------------------------------- /style_transfer/anime2real/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/anime2real/.DS_Store -------------------------------------------------------------------------------- /style_transfer/anime2real/README.md: -------------------------------------------------------------------------------- 1 | # Anime to Real 2 | 3 | ## Input --> Output 4 | 5 | ![](models/out.jpg) 6 | 7 | ## Convert 8 | 9 | pt --> onnx --> onnx-sim --> onnx2ncnn ---> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | from models import Generator, Discriminator 15 | 16 | # Networks 17 | netG_A2B = Generator(3, 3) # 输入和输出的channnel数目 18 | netG_B2A = Generator(3, 3) 19 | 20 | # Load state dicts 21 | netG_A2B.load_state_dict(torch.load("output/netG_A2B.pth")) # 加载模型文件 22 | netG_B2A.load_state_dict(torch.load("output/netG_B2A.pth")) # 加载模型文件 23 | # Set model's test mode 24 | netG_A2B.eval() 25 | netG_B2A.eval() 26 | 27 | x = torch.randn(1, 3, 256, 256) 28 | # ==== pnnx方法 提示 layer nn.InstanceNorm2d not exists or registered 29 | # # # 1. pt --> torchscript 30 | # traced_script_module = torch.jit.trace(netG_A2B, x, strict=False) 31 | # traced_script_module.save("ts.pt") 32 | 33 | # # # 2. ts --> pnnx --> ncnn 34 | # os.system("pnnx ts.pt inputshape=[1,3,256,256]") # 可能错误 35 | # ==== onnx方法 36 | torch.onnx._export(netG_B2A, # 或netG_A2B 37 | x, 38 | "out.onnx", 39 | export_params=True, 40 | operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK, 41 | opset_version=13, 42 | input_names=['in0'], 43 | output_names=['out0']) 44 | # 2. onnx --> onnxsim 45 | os.system("python -m onnxsim out.onnx sim.onnx") 46 | 47 | # 3. onnx --> ncnn 48 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 49 | 50 | # 4. ncnn --> optmize ---> ncnn 51 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 52 | os.remove("sim.onnx") 53 | 54 | ######################################################################## 55 | # 将该文件放在项目目录下,就可以完成转换 56 | ``` 57 | 58 | ## Example project 59 | 60 | 61 | ## Reference 62 | 63 | - [Averyyy/PyTorch-Anime2Real](https://github.com/Averyyy/PyTorch-Anime2Real) 64 | 65 | 66 | -------------------------------------------------------------------------------- /style_transfer/anime2real/anime2real.cpp: -------------------------------------------------------------------------------- 1 | // opencv 2 | #include 3 | #include 4 | #include 5 | 6 | // ncnn 7 | #include "gpu.h" 8 | #include "net.h" 9 | #include 10 | 11 | int main(int argc, char **argv) 12 | { 13 | cv::Mat image = cv::imread("input.png"); 14 | if (image.empty()) 15 | { 16 | std::cout << "cv::imread failed" << std::endl; 17 | return -1; 18 | } 19 | ncnn::Net net; 20 | net.opt.use_vulkan_compute = true; // 开启GPU加速 21 | net.load_param("models/netG_A2B.param"); // netG_A2B OR netG_B2A 22 | net.load_model("models/netG_A2B.bin"); 23 | // 前处理 24 | const int w = image.cols; 25 | const int h = image.rows; 26 | int target_w = w; 27 | int target_h = h; 28 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR2RGB, w, h, 512, 512); 29 | // const float mean_vals[3] = {0.5f * 255.f, 0.5f * 255.f, 0.5f * 255.f}; // transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)) ] 30 | // const float norm_vals[3] = {1 / 0.5f / 255.f, 1 / 0.5f / 255.f, 1 / 0.5f / 255.f}; 31 | const float mean_vals[3] = {127.5f, 127.5f, 127.5f}; 32 | const float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f}; 33 | in.substract_mean_normalize(mean_vals, norm_vals); 34 | ncnn::Mat out; 35 | ncnn::Extractor ex = net.create_extractor(); 36 | ex.input("in0", in); 37 | std::cout << "input shape:" << in.w << " " << in.h << " " << in.c << std::endl; 38 | ex.extract("out0", out); 39 | std::cout << "output shape:" << out.w << " " << out.h << " " << out.c << std::endl; 40 | // 后处理 41 | cv::Mat result(out.h, out.w, CV_32FC3); 42 | for (int i = 0; i < out.c; i++) 43 | { 44 | float *out_data = out.channel(i); 45 | for (int h = 0; h < out.h; h++) 46 | { 47 | for (int w = 0; w < out.w; w++) 48 | { 49 | result.at(h, w)[2 - i] = out_data[h * out.h + w]; 50 | } 51 | } 52 | } 53 | cv::Mat result8U(out.h, out.w, CV_8UC3); 54 | result.convertTo(result8U, CV_8UC3, 127.5, 127.5); 55 | 56 | // cv::imwrite("out.png", outbgr); // 保存图片 57 | cv::imshow("in", image); 58 | cv::imshow("out", result8U); 59 | cv::waitKey(0); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /style_transfer/anime2real/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | from models import Generator, Discriminator 4 | 5 | # Networks 6 | netG_A2B = Generator(3, 3) # 输入和输出的channnel数目 7 | netG_B2A = Generator(3, 3) 8 | 9 | # Load state dicts 10 | netG_A2B.load_state_dict(torch.load("output/netG_A2B.pth")) # 加载模型文件 11 | netG_B2A.load_state_dict(torch.load("output/netG_B2A.pth")) # 加载模型文件 12 | # Set model's test mode 13 | netG_A2B.eval() 14 | netG_B2A.eval() 15 | 16 | x = torch.randn(1, 3, 256, 256) 17 | # ==== pnnx方法 提示 layer nn.InstanceNorm2d not exists or registered 18 | # # # 1. pt --> torchscript 19 | # traced_script_module = torch.jit.trace(netG_A2B, x, strict=False) 20 | # traced_script_module.save("ts.pt") 21 | 22 | # # # 2. ts --> pnnx --> ncnn 23 | # os.system("pnnx ts.pt inputshape=[1,3,256,256]") # 可能错误 24 | # ==== onnx方法 25 | torch.onnx._export(netG_B2A, # 或netG_A2B 26 | x, 27 | "out.onnx", 28 | export_params=True, 29 | operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK, 30 | opset_version=13, 31 | input_names=['in0'], 32 | output_names=['out0']) 33 | # 2. onnx --> onnxsim 34 | os.system("python -m onnxsim out.onnx sim.onnx") 35 | 36 | # 3. onnx --> ncnn 37 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 38 | 39 | # 4. ncnn --> optmize ---> ncnn 40 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 41 | os.remove("sim.onnx") 42 | 43 | ######################################################################## 44 | # 将该文件放在项目目录下,就可以完成转换 45 | -------------------------------------------------------------------------------- /style_transfer/anime2real/models/out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/anime2real/models/out.jpg -------------------------------------------------------------------------------- /style_transfer/animeganv2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/animeganv2/.DS_Store -------------------------------------------------------------------------------- /style_transfer/animeganv2/README.md: -------------------------------------------------------------------------------- 1 | # AnimeGanV3 2 | 3 | ## Input --> Output 4 | 5 | ![](https://user-images.githubusercontent.com/26464535/142294796-54394a4a-a566-47a1-b9ab-4e715b901442.gif) 6 | 7 | ## Convert 8 | 9 | pt --> TorchScript --> pnnx --> ncnnOptimize --> ncnn 10 | 11 | ```python 12 | import os 13 | import torch 14 | # 0. pt模型下载及初始化 15 | model = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", pretrained="face_paint_512_v2") 16 | # model = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", pretrained="paprika") 17 | model.eval() 18 | 19 | # 1. pt-->torchscript 20 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 512, 512)) 21 | traced_script_module.save("ts.pt") 22 | 23 | # 2. ts --> pnnx --> ncnn 24 | os.system("pnnx ts.pt inputshape=[1,3,512,512]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 25 | ``` 26 | 27 | ## Example project 28 | 29 | - [Android: nihui/ncnn-android-styletransfer](https://github.com/nihui/ncnn-android-styletransfer) 30 | - [Desktop: baiyue/animegan_gui](https://github.com/Baiyuetribe/paper2gui/blob/main/Style%20Transfer/animegan_gui.md) 31 | 32 | ## Reference 33 | 34 | - [bryandlee/animegan2-pytorch](https://github.com/bryandlee/animegan2-pytorch) 35 | 36 | 37 | -------------------------------------------------------------------------------- /style_transfer/animeganv2/animeganv2.cpp: -------------------------------------------------------------------------------- 1 | // opencv 2 | #include 3 | #include 4 | #include 5 | // ncnn 6 | #include "gpu.h" 7 | #include "net.h" 8 | #include 9 | 10 | static int styletransfer(const ncnn::Net &net, const cv::Mat &bgr, cv::Mat &outbgr) 11 | { 12 | const int w = bgr.cols; 13 | const int h = bgr.rows; 14 | 15 | int target_w = 512; 16 | int target_h = 512; 17 | 18 | const float mean_vals[3] = {127.5f, 127.5f, 127.5f}; 19 | const float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f}; 20 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, w, h, target_w, target_h); 21 | in.substract_mean_normalize(mean_vals, norm_vals); 22 | ncnn::Mat out; 23 | { 24 | ncnn::Extractor ex = net.create_extractor(); 25 | ex.input("input", in); 26 | ex.extract("out", out); 27 | } 28 | 29 | cv::Mat result(out.h, out.w, CV_32FC3); 30 | for (int i = 0; i < out.c; i++) 31 | { 32 | float *out_data = out.channel(i); 33 | for (int h = 0; h < out.h; h++) 34 | { 35 | for (int w = 0; w < out.w; w++) 36 | { 37 | result.at(h, w)[2 - i] = out_data[h * out.h + w]; 38 | } 39 | } 40 | } 41 | cv::Mat result8U(out.h, out.w, CV_8UC3); 42 | result.convertTo(result8U, CV_8UC3, 127.5, 127.5); 43 | result8U.copyTo(outbgr); 44 | return 0; 45 | } 46 | 47 | int main(int argc, char **argv) 48 | { 49 | if (argc != 3) // 输入和输出 50 | { 51 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 52 | return -1; 53 | } 54 | 55 | std::string imagepath = argv[1]; 56 | std::string outpath = argv[2]; 57 | 58 | cv::Mat bgr = cv::imread(imagepath, 1); 59 | if (bgr.empty()) 60 | { 61 | std::cerr << "cv::imread failed" << std::endl; 62 | return -1; 63 | } 64 | 65 | ncnn::Net net; // 定义神经网络 66 | net.opt.use_vulkan_compute = true; // 开启GPU加速 67 | net.load_param("models/face_paint_512_v2.param"); // 加载模型参数 68 | net.load_model("models/face_paint_512_v2.bin"); 69 | // 开始推理 70 | cv::Mat outbgr; 71 | styletransfer(net, bgr, outbgr); // 具体推理 72 | // 展示输出 73 | cv::imwrite(outpath, outbgr); // 保存结果 74 | cv::imshow("out", outbgr); // 保存结果 75 | cv::waitKey(0); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /style_transfer/animeganv2/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/animeganv2/models/.DS_Store -------------------------------------------------------------------------------- /style_transfer/animeganv2/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | # 0. pt模型下载及初始化 4 | model = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", pretrained="face_paint_512_v2") 5 | # model = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", pretrained="paprika") 6 | model.eval() 7 | 8 | # 1. pt-->torchscript 9 | traced_script_module = torch.jit.trace(model, torch.randn(1, 3, 512, 512)) 10 | traced_script_module.save("ts.pt") 11 | 12 | # 2. ts --> pnnx --> ncnn 13 | os.system("pnnx ts.pt inputshape=[1,3,512,512]") # 2022年5月25日起,pnnx默认自动量化,不需要再次optmize 14 | -------------------------------------------------------------------------------- /style_transfer/animeganv3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/animeganv3/.DS_Store -------------------------------------------------------------------------------- /style_transfer/animeganv3/README.md: -------------------------------------------------------------------------------- 1 | # AnimeGanV3 2 | 3 | ## Input --> Output 4 | 5 | ![Input](input.jpg) 6 | 7 | ## Convert 8 | 9 | > 此模型当前仅有onnx,无法使用pnnx进行转换。 10 | 11 | onnx --> simpleonnx --> onnx2ncnn[x] --> ncnnOptimize --> ncnn 12 | 13 | 在onnx2ncnn步骤失败,报错如下: 14 | ``` 15 | # step1: 16 | python -m onnxsim animeganv3_H64_model0.onnx sim.onnx --dynamic-input-shape 17 | # step2: 18 | onnx2ncnn.exe sim.onnx ncnn.param ncnn.bin 19 | ``` 20 | 报错内容: 21 | ```log 22 | Shape not supported yet! 23 | Gather not supported yet! 24 | Cast not supported yet! 25 | # to=6 26 | Cast not supported yet! 27 | # to=7 28 | Unknown data type 0 29 | Shape not supported yet! 30 | Gather not supported yet! 31 | Cast not supported yet! 32 | # to=6 33 | Unsupported squeeze axes ! 34 | Unsupported unsqueeze axes ! 35 | Unsupported squeeze axes ! 36 | ``` 37 | 解决办法: 手工优化 38 | https://zhuanlan.zhihu.com/p/93017149 39 | 40 | 待测试 41 | 42 | 43 | ## Reference 44 | 45 | - [TachibanaYoshino/AnimeGANv3](https://github.com/TachibanaYoshino/AnimeGANv3) 46 | 47 | 48 | -------------------------------------------------------------------------------- /style_transfer/animeganv3/animeganv3.cpp: -------------------------------------------------------------------------------- 1 | // opencv 2 | #include 3 | #include 4 | #include 5 | // ncnn 6 | #include "gpu.h" 7 | #include "net.h" 8 | #include 9 | 10 | static int styletransfer(const ncnn::Net &net, const cv::Mat &bgr, cv::Mat &outbgr) 11 | { 12 | const int w = bgr.cols; 13 | const int h = bgr.rows; 14 | 15 | int target_w = 512; 16 | int target_h = 512; 17 | 18 | const float mean_vals[3] = {127.5f, 127.5f, 127.5f}; 19 | const float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f}; 20 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, w, h, target_w, target_h); 21 | in.substract_mean_normalize(mean_vals, norm_vals); 22 | ncnn::Mat out; 23 | { 24 | ncnn::Extractor ex = net.create_extractor(); 25 | ex.input("input", in); 26 | ex.extract("out", out); 27 | } 28 | 29 | cv::Mat result(out.h, out.w, CV_32FC3); 30 | for (int i = 0; i < out.c; i++) 31 | { 32 | float *out_data = out.channel(i); 33 | for (int h = 0; h < out.h; h++) 34 | { 35 | for (int w = 0; w < out.w; w++) 36 | { 37 | result.at(h, w)[2 - i] = out_data[h * out.h + w]; 38 | } 39 | } 40 | } 41 | cv::Mat result8U(out.h, out.w, CV_8UC3); 42 | result.convertTo(result8U, CV_8UC3, 127.5, 127.5); 43 | result8U.copyTo(outbgr); 44 | return 0; 45 | } 46 | 47 | int main(int argc, char **argv) 48 | { 49 | if (argc != 3) // 第一个为本身,第二个为图片路径,第三个为导出路径 50 | { 51 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 52 | return -1; 53 | } 54 | 55 | std::string imagepath = argv[1]; 56 | std::string outpath = argv[2]; 57 | 58 | cv::Mat bgr = cv::imread(imagepath, 1); 59 | if (bgr.empty()) 60 | { 61 | std::cerr << "cv::imread failed" << std::endl; 62 | return -1; 63 | } 64 | 65 | ncnn::Net net; // 定义神经网络 66 | stylenet.opt.use_vulkan_compute = true; // 开启GPU加速 67 | stylenet.load_param("models/v3.param"); // 加载模型参数 68 | stylenet.load_model("models/v3.param"); 69 | // 开始推理 70 | cv::Mat outbgr; 71 | styletransfer(stylenet, bgr, outbgr); // 具体推理 72 | // 展示输出 73 | cv::imwrite(outpath, outbgr); // 保存结果 74 | cv::imshow("out", outbgr); // 保存结果 75 | cv::waitKey(0); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /style_transfer/animeganv3/input.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/animeganv3/input.jpg -------------------------------------------------------------------------------- /style_transfer/animeganv3/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/animeganv3/models/.DS_Store -------------------------------------------------------------------------------- /style_transfer/styletransfer/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/styletransfer/.DS_Store -------------------------------------------------------------------------------- /style_transfer/styletransfer/README.md: -------------------------------------------------------------------------------- 1 | # fast_neural_style 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/pytorch/examples/raw/main/fast_neural_style/images/content-images/amber.jpg) 6 | ![](https://github.com/pytorch/examples/raw/main/fast_neural_style/images/style-images/mosaic.jpg) 7 | ![](https://github.com/pytorch/examples/blob/main/fast_neural_style/images/style-images/candy.jpg) 8 | ![](https://github.com/pytorch/examples/raw/main/fast_neural_style/images/style-images/rain-princess-cropped.jpg) 9 | 10 | ## Convert 11 | 12 | onnx --> simpleonnx --> onnx2ncnn --> ncnnOptimize --> ncnn 13 | 14 | ``` 15 | import os 16 | # 1. download models 17 | # download_url = https://github.com/onnx/models/tree/main/vision/style_transfer/fast_neural_style/model 18 | os.system("") 19 | 20 | # 2. onnx --> onnxsim 21 | os.system("python -m onnxsim candy-9.onnx sim.onnx") 22 | 23 | # 3. onnx --> ncnn 24 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 25 | 26 | # 4. ncnn --> optmize ---> ncnn 27 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 28 | ``` 29 | 30 | 31 | ## Reference 32 | 33 | - [https://arxiv.org/abs/1603.08155](https://arxiv.org/abs/1603.08155) 34 | - [style_transfer/fast_neural_style](https://github.com/onnx/models/tree/main/vision/style_transfer/fast_neural_style) 35 | 36 | 37 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/style_transfer/styletransfer/models/.DS_Store -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/candy9.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 70 75 3 | Input input1 0 1 input1 4 | Padding 63 1 1 input1 63 0=4 1=4 2=4 3=4 4=2 5 | Convolution 64 1 1 63 64 0=32 1=9 5=1 6=7776 6 | InstanceNorm 65 1 1 64 65 0=32 1=1.000000e-05 7 | ReLU 66 1 1 65 66 8 | Padding 67 1 1 66 67 0=1 1=1 2=1 3=1 4=2 9 | Convolution 68 1 1 67 68 0=64 1=3 3=2 5=1 6=18432 10 | InstanceNorm 69 1 1 68 69 0=64 1=1.000000e-05 11 | ReLU 70 1 1 69 70 12 | Padding 71 1 1 70 71 0=1 1=1 2=1 3=1 4=2 13 | Convolution 72 1 1 71 72 0=128 1=3 3=2 5=1 6=73728 14 | InstanceNorm 73 1 1 72 73 0=128 1=1.000000e-05 15 | ReLU 74 1 1 73 74 16 | Split splitncnn_0 1 2 74 74_splitncnn_0 74_splitncnn_1 17 | Padding 75 1 1 74_splitncnn_1 75 0=1 1=1 2=1 3=1 4=2 18 | Convolution 76 1 1 75 76 0=128 1=3 5=1 6=147456 19 | InstanceNorm 77 1 1 76 77 0=128 1=1.000000e-05 20 | ReLU 78 1 1 77 78 21 | Padding 79 1 1 78 79 0=1 1=1 2=1 3=1 4=2 22 | Convolution 80 1 1 79 80 0=128 1=3 5=1 6=147456 23 | InstanceNorm 81 1 1 80 81 0=128 1=1.000000e-05 24 | BinaryOp 82 2 1 81 74_splitncnn_0 82 25 | Split splitncnn_1 1 2 82 82_splitncnn_0 82_splitncnn_1 26 | Padding 83 1 1 82_splitncnn_1 83 0=1 1=1 2=1 3=1 4=2 27 | Convolution 84 1 1 83 84 0=128 1=3 5=1 6=147456 28 | InstanceNorm 85 1 1 84 85 0=128 1=1.000000e-05 29 | ReLU 86 1 1 85 86 30 | Padding 87 1 1 86 87 0=1 1=1 2=1 3=1 4=2 31 | Convolution 88 1 1 87 88 0=128 1=3 5=1 6=147456 32 | InstanceNorm 89 1 1 88 89 0=128 1=1.000000e-05 33 | BinaryOp 90 2 1 89 82_splitncnn_0 90 34 | Split splitncnn_2 1 2 90 90_splitncnn_0 90_splitncnn_1 35 | Padding 91 1 1 90_splitncnn_1 91 0=1 1=1 2=1 3=1 4=2 36 | Convolution 92 1 1 91 92 0=128 1=3 5=1 6=147456 37 | InstanceNorm 93 1 1 92 93 0=128 1=1.000000e-05 38 | ReLU 94 1 1 93 94 39 | Padding 95 1 1 94 95 0=1 1=1 2=1 3=1 4=2 40 | Convolution 96 1 1 95 96 0=128 1=3 5=1 6=147456 41 | InstanceNorm 97 1 1 96 97 0=128 1=1.000000e-05 42 | BinaryOp 98 2 1 97 90_splitncnn_0 98 43 | Split splitncnn_3 1 2 98 98_splitncnn_0 98_splitncnn_1 44 | Padding 99 1 1 98_splitncnn_1 99 0=1 1=1 2=1 3=1 4=2 45 | Convolution 100 1 1 99 100 0=128 1=3 5=1 6=147456 46 | InstanceNorm 101 1 1 100 101 0=128 1=1.000000e-05 47 | ReLU 102 1 1 101 102 48 | Padding 103 1 1 102 103 0=1 1=1 2=1 3=1 4=2 49 | Convolution 104 1 1 103 104 0=128 1=3 5=1 6=147456 50 | InstanceNorm 105 1 1 104 105 0=128 1=1.000000e-05 51 | BinaryOp 106 2 1 105 98_splitncnn_0 106 52 | Split splitncnn_4 1 2 106 106_splitncnn_0 106_splitncnn_1 53 | Padding 107 1 1 106_splitncnn_1 107 0=1 1=1 2=1 3=1 4=2 54 | Convolution 108 1 1 107 108 0=128 1=3 5=1 6=147456 55 | InstanceNorm 109 1 1 108 109 0=128 1=1.000000e-05 56 | ReLU 110 1 1 109 110 57 | Padding 111 1 1 110 111 0=1 1=1 2=1 3=1 4=2 58 | Convolution 112 1 1 111 112 0=128 1=3 5=1 6=147456 59 | InstanceNorm 113 1 1 112 113 0=128 1=1.000000e-05 60 | BinaryOp 114 2 1 113 106_splitncnn_0 114 61 | Interp 139 1 1 114 139 0=1 1=2.000000e+00 2=2.000000e+00 62 | Padding 140 1 1 139 140 0=1 1=1 2=1 3=1 4=2 63 | Convolution 141 1 1 140 141 0=64 1=3 5=1 6=73728 64 | InstanceNorm 142 1 1 141 142 0=64 1=1.000000e-05 65 | ReLU 143 1 1 142 143 66 | Interp 168 1 1 143 168 0=1 1=2.000000e+00 2=2.000000e+00 67 | Padding 169 1 1 168 169 0=1 1=1 2=1 3=1 4=2 68 | Convolution 170 1 1 169 170 0=32 1=3 5=1 6=18432 69 | InstanceNorm 171 1 1 170 171 0=32 1=1.000000e-05 70 | ReLU 172 1 1 171 172 71 | Padding 173 1 1 172 173 0=4 1=4 2=4 3=4 4=2 72 | Convolution output1 1 1 173 output1 0=3 1=9 5=1 6=7776 73 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/convert.py: -------------------------------------------------------------------------------- 1 | import os 2 | # 1. download models 3 | # download_url = https://github.com/onnx/models/tree/main/vision/style_transfer/fast_neural_style/model 4 | os.system("") 5 | 6 | # 2. onnx --> onnxsim 7 | os.system("python -m onnxsim candy-9.onnx sim.onnx") 8 | 9 | # 3. onnx --> ncnn 10 | os.system("onnx2ncnn sim.onnx ncnn.param ncnn.bin") 11 | 12 | # 4. ncnn --> optmize ---> ncnn 13 | os.system("ncnnoptimize ncnn.param ncnn.bin opt.param opt.bin 1") # 数字0 代表fp32 ;1代表fp16 14 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/mosaic-9.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 70 75 3 | Input input1 0 1 input1 4 | Padding 63 1 1 input1 63 0=4 1=4 2=4 3=4 4=2 5 | Convolution 64 1 1 63 64 0=32 1=9 5=1 6=7776 6 | InstanceNorm 65 1 1 64 65 0=32 1=1.000000e-05 7 | ReLU 66 1 1 65 66 8 | Padding 67 1 1 66 67 0=1 1=1 2=1 3=1 4=2 9 | Convolution 68 1 1 67 68 0=64 1=3 3=2 5=1 6=18432 10 | InstanceNorm 69 1 1 68 69 0=64 1=1.000000e-05 11 | ReLU 70 1 1 69 70 12 | Padding 71 1 1 70 71 0=1 1=1 2=1 3=1 4=2 13 | Convolution 72 1 1 71 72 0=128 1=3 3=2 5=1 6=73728 14 | InstanceNorm 73 1 1 72 73 0=128 1=1.000000e-05 15 | ReLU 74 1 1 73 74 16 | Split splitncnn_0 1 2 74 74_splitncnn_0 74_splitncnn_1 17 | Padding 75 1 1 74_splitncnn_1 75 0=1 1=1 2=1 3=1 4=2 18 | Convolution 76 1 1 75 76 0=128 1=3 5=1 6=147456 19 | InstanceNorm 77 1 1 76 77 0=128 1=1.000000e-05 20 | ReLU 78 1 1 77 78 21 | Padding 79 1 1 78 79 0=1 1=1 2=1 3=1 4=2 22 | Convolution 80 1 1 79 80 0=128 1=3 5=1 6=147456 23 | InstanceNorm 81 1 1 80 81 0=128 1=1.000000e-05 24 | BinaryOp 82 2 1 81 74_splitncnn_0 82 25 | Split splitncnn_1 1 2 82 82_splitncnn_0 82_splitncnn_1 26 | Padding 83 1 1 82_splitncnn_1 83 0=1 1=1 2=1 3=1 4=2 27 | Convolution 84 1 1 83 84 0=128 1=3 5=1 6=147456 28 | InstanceNorm 85 1 1 84 85 0=128 1=1.000000e-05 29 | ReLU 86 1 1 85 86 30 | Padding 87 1 1 86 87 0=1 1=1 2=1 3=1 4=2 31 | Convolution 88 1 1 87 88 0=128 1=3 5=1 6=147456 32 | InstanceNorm 89 1 1 88 89 0=128 1=1.000000e-05 33 | BinaryOp 90 2 1 89 82_splitncnn_0 90 34 | Split splitncnn_2 1 2 90 90_splitncnn_0 90_splitncnn_1 35 | Padding 91 1 1 90_splitncnn_1 91 0=1 1=1 2=1 3=1 4=2 36 | Convolution 92 1 1 91 92 0=128 1=3 5=1 6=147456 37 | InstanceNorm 93 1 1 92 93 0=128 1=1.000000e-05 38 | ReLU 94 1 1 93 94 39 | Padding 95 1 1 94 95 0=1 1=1 2=1 3=1 4=2 40 | Convolution 96 1 1 95 96 0=128 1=3 5=1 6=147456 41 | InstanceNorm 97 1 1 96 97 0=128 1=1.000000e-05 42 | BinaryOp 98 2 1 97 90_splitncnn_0 98 43 | Split splitncnn_3 1 2 98 98_splitncnn_0 98_splitncnn_1 44 | Padding 99 1 1 98_splitncnn_1 99 0=1 1=1 2=1 3=1 4=2 45 | Convolution 100 1 1 99 100 0=128 1=3 5=1 6=147456 46 | InstanceNorm 101 1 1 100 101 0=128 1=1.000000e-05 47 | ReLU 102 1 1 101 102 48 | Padding 103 1 1 102 103 0=1 1=1 2=1 3=1 4=2 49 | Convolution 104 1 1 103 104 0=128 1=3 5=1 6=147456 50 | InstanceNorm 105 1 1 104 105 0=128 1=1.000000e-05 51 | BinaryOp 106 2 1 105 98_splitncnn_0 106 52 | Split splitncnn_4 1 2 106 106_splitncnn_0 106_splitncnn_1 53 | Padding 107 1 1 106_splitncnn_1 107 0=1 1=1 2=1 3=1 4=2 54 | Convolution 108 1 1 107 108 0=128 1=3 5=1 6=147456 55 | InstanceNorm 109 1 1 108 109 0=128 1=1.000000e-05 56 | ReLU 110 1 1 109 110 57 | Padding 111 1 1 110 111 0=1 1=1 2=1 3=1 4=2 58 | Convolution 112 1 1 111 112 0=128 1=3 5=1 6=147456 59 | InstanceNorm 113 1 1 112 113 0=128 1=1.000000e-05 60 | BinaryOp 114 2 1 113 106_splitncnn_0 114 61 | Interp 139 1 1 114 139 0=1 1=2.000000e+00 2=2.000000e+00 62 | Padding 140 1 1 139 140 0=1 1=1 2=1 3=1 4=2 63 | Convolution 141 1 1 140 141 0=64 1=3 5=1 6=73728 64 | InstanceNorm 142 1 1 141 142 0=64 1=1.000000e-05 65 | ReLU 143 1 1 142 143 66 | Interp 168 1 1 143 168 0=1 1=2.000000e+00 2=2.000000e+00 67 | Padding 169 1 1 168 169 0=1 1=1 2=1 3=1 4=2 68 | Convolution 170 1 1 169 170 0=32 1=3 5=1 6=18432 69 | InstanceNorm 171 1 1 170 171 0=32 1=1.000000e-05 70 | ReLU 172 1 1 171 172 71 | Padding 173 1 1 172 173 0=4 1=4 2=4 3=4 4=2 72 | Convolution output1 1 1 173 output1 0=3 1=9 5=1 6=7776 73 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/pointilism-9.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 70 75 3 | Input input1 0 1 input1 4 | Padding 63 1 1 input1 63 0=4 1=4 2=4 3=4 4=2 5 | Convolution 64 1 1 63 64 0=32 1=9 5=1 6=7776 6 | InstanceNorm 65 1 1 64 65 0=32 1=1.000000e-05 7 | ReLU 66 1 1 65 66 8 | Padding 67 1 1 66 67 0=1 1=1 2=1 3=1 4=2 9 | Convolution 68 1 1 67 68 0=64 1=3 3=2 5=1 6=18432 10 | InstanceNorm 69 1 1 68 69 0=64 1=1.000000e-05 11 | ReLU 70 1 1 69 70 12 | Padding 71 1 1 70 71 0=1 1=1 2=1 3=1 4=2 13 | Convolution 72 1 1 71 72 0=128 1=3 3=2 5=1 6=73728 14 | InstanceNorm 73 1 1 72 73 0=128 1=1.000000e-05 15 | ReLU 74 1 1 73 74 16 | Split splitncnn_0 1 2 74 74_splitncnn_0 74_splitncnn_1 17 | Padding 75 1 1 74_splitncnn_1 75 0=1 1=1 2=1 3=1 4=2 18 | Convolution 76 1 1 75 76 0=128 1=3 5=1 6=147456 19 | InstanceNorm 77 1 1 76 77 0=128 1=1.000000e-05 20 | ReLU 78 1 1 77 78 21 | Padding 79 1 1 78 79 0=1 1=1 2=1 3=1 4=2 22 | Convolution 80 1 1 79 80 0=128 1=3 5=1 6=147456 23 | InstanceNorm 81 1 1 80 81 0=128 1=1.000000e-05 24 | BinaryOp 82 2 1 81 74_splitncnn_0 82 25 | Split splitncnn_1 1 2 82 82_splitncnn_0 82_splitncnn_1 26 | Padding 83 1 1 82_splitncnn_1 83 0=1 1=1 2=1 3=1 4=2 27 | Convolution 84 1 1 83 84 0=128 1=3 5=1 6=147456 28 | InstanceNorm 85 1 1 84 85 0=128 1=1.000000e-05 29 | ReLU 86 1 1 85 86 30 | Padding 87 1 1 86 87 0=1 1=1 2=1 3=1 4=2 31 | Convolution 88 1 1 87 88 0=128 1=3 5=1 6=147456 32 | InstanceNorm 89 1 1 88 89 0=128 1=1.000000e-05 33 | BinaryOp 90 2 1 89 82_splitncnn_0 90 34 | Split splitncnn_2 1 2 90 90_splitncnn_0 90_splitncnn_1 35 | Padding 91 1 1 90_splitncnn_1 91 0=1 1=1 2=1 3=1 4=2 36 | Convolution 92 1 1 91 92 0=128 1=3 5=1 6=147456 37 | InstanceNorm 93 1 1 92 93 0=128 1=1.000000e-05 38 | ReLU 94 1 1 93 94 39 | Padding 95 1 1 94 95 0=1 1=1 2=1 3=1 4=2 40 | Convolution 96 1 1 95 96 0=128 1=3 5=1 6=147456 41 | InstanceNorm 97 1 1 96 97 0=128 1=1.000000e-05 42 | BinaryOp 98 2 1 97 90_splitncnn_0 98 43 | Split splitncnn_3 1 2 98 98_splitncnn_0 98_splitncnn_1 44 | Padding 99 1 1 98_splitncnn_1 99 0=1 1=1 2=1 3=1 4=2 45 | Convolution 100 1 1 99 100 0=128 1=3 5=1 6=147456 46 | InstanceNorm 101 1 1 100 101 0=128 1=1.000000e-05 47 | ReLU 102 1 1 101 102 48 | Padding 103 1 1 102 103 0=1 1=1 2=1 3=1 4=2 49 | Convolution 104 1 1 103 104 0=128 1=3 5=1 6=147456 50 | InstanceNorm 105 1 1 104 105 0=128 1=1.000000e-05 51 | BinaryOp 106 2 1 105 98_splitncnn_0 106 52 | Split splitncnn_4 1 2 106 106_splitncnn_0 106_splitncnn_1 53 | Padding 107 1 1 106_splitncnn_1 107 0=1 1=1 2=1 3=1 4=2 54 | Convolution 108 1 1 107 108 0=128 1=3 5=1 6=147456 55 | InstanceNorm 109 1 1 108 109 0=128 1=1.000000e-05 56 | ReLU 110 1 1 109 110 57 | Padding 111 1 1 110 111 0=1 1=1 2=1 3=1 4=2 58 | Convolution 112 1 1 111 112 0=128 1=3 5=1 6=147456 59 | InstanceNorm 113 1 1 112 113 0=128 1=1.000000e-05 60 | BinaryOp 114 2 1 113 106_splitncnn_0 114 61 | Interp 139 1 1 114 139 0=1 1=2.000000e+00 2=2.000000e+00 62 | Padding 140 1 1 139 140 0=1 1=1 2=1 3=1 4=2 63 | Convolution 141 1 1 140 141 0=64 1=3 5=1 6=73728 64 | InstanceNorm 142 1 1 141 142 0=64 1=1.000000e-05 65 | ReLU 143 1 1 142 143 66 | Interp 168 1 1 143 168 0=1 1=2.000000e+00 2=2.000000e+00 67 | Padding 169 1 1 168 169 0=1 1=1 2=1 3=1 4=2 68 | Convolution 170 1 1 169 170 0=32 1=3 5=1 6=18432 69 | InstanceNorm 171 1 1 170 171 0=32 1=1.000000e-05 70 | ReLU 172 1 1 171 172 71 | Padding 173 1 1 172 173 0=4 1=4 2=4 3=4 4=2 72 | Convolution output1 1 1 173 output1 0=3 1=9 5=1 6=7776 73 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/rain-princess-9.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 70 75 3 | Input input1 0 1 input1 4 | Padding 63 1 1 input1 63 0=4 1=4 2=4 3=4 4=2 5 | Convolution 64 1 1 63 64 0=32 1=9 5=1 6=7776 6 | InstanceNorm 65 1 1 64 65 0=32 1=1.000000e-05 7 | ReLU 66 1 1 65 66 8 | Padding 67 1 1 66 67 0=1 1=1 2=1 3=1 4=2 9 | Convolution 68 1 1 67 68 0=64 1=3 3=2 5=1 6=18432 10 | InstanceNorm 69 1 1 68 69 0=64 1=1.000000e-05 11 | ReLU 70 1 1 69 70 12 | Padding 71 1 1 70 71 0=1 1=1 2=1 3=1 4=2 13 | Convolution 72 1 1 71 72 0=128 1=3 3=2 5=1 6=73728 14 | InstanceNorm 73 1 1 72 73 0=128 1=1.000000e-05 15 | ReLU 74 1 1 73 74 16 | Split splitncnn_0 1 2 74 74_splitncnn_0 74_splitncnn_1 17 | Padding 75 1 1 74_splitncnn_1 75 0=1 1=1 2=1 3=1 4=2 18 | Convolution 76 1 1 75 76 0=128 1=3 5=1 6=147456 19 | InstanceNorm 77 1 1 76 77 0=128 1=1.000000e-05 20 | ReLU 78 1 1 77 78 21 | Padding 79 1 1 78 79 0=1 1=1 2=1 3=1 4=2 22 | Convolution 80 1 1 79 80 0=128 1=3 5=1 6=147456 23 | InstanceNorm 81 1 1 80 81 0=128 1=1.000000e-05 24 | BinaryOp 82 2 1 81 74_splitncnn_0 82 25 | Split splitncnn_1 1 2 82 82_splitncnn_0 82_splitncnn_1 26 | Padding 83 1 1 82_splitncnn_1 83 0=1 1=1 2=1 3=1 4=2 27 | Convolution 84 1 1 83 84 0=128 1=3 5=1 6=147456 28 | InstanceNorm 85 1 1 84 85 0=128 1=1.000000e-05 29 | ReLU 86 1 1 85 86 30 | Padding 87 1 1 86 87 0=1 1=1 2=1 3=1 4=2 31 | Convolution 88 1 1 87 88 0=128 1=3 5=1 6=147456 32 | InstanceNorm 89 1 1 88 89 0=128 1=1.000000e-05 33 | BinaryOp 90 2 1 89 82_splitncnn_0 90 34 | Split splitncnn_2 1 2 90 90_splitncnn_0 90_splitncnn_1 35 | Padding 91 1 1 90_splitncnn_1 91 0=1 1=1 2=1 3=1 4=2 36 | Convolution 92 1 1 91 92 0=128 1=3 5=1 6=147456 37 | InstanceNorm 93 1 1 92 93 0=128 1=1.000000e-05 38 | ReLU 94 1 1 93 94 39 | Padding 95 1 1 94 95 0=1 1=1 2=1 3=1 4=2 40 | Convolution 96 1 1 95 96 0=128 1=3 5=1 6=147456 41 | InstanceNorm 97 1 1 96 97 0=128 1=1.000000e-05 42 | BinaryOp 98 2 1 97 90_splitncnn_0 98 43 | Split splitncnn_3 1 2 98 98_splitncnn_0 98_splitncnn_1 44 | Padding 99 1 1 98_splitncnn_1 99 0=1 1=1 2=1 3=1 4=2 45 | Convolution 100 1 1 99 100 0=128 1=3 5=1 6=147456 46 | InstanceNorm 101 1 1 100 101 0=128 1=1.000000e-05 47 | ReLU 102 1 1 101 102 48 | Padding 103 1 1 102 103 0=1 1=1 2=1 3=1 4=2 49 | Convolution 104 1 1 103 104 0=128 1=3 5=1 6=147456 50 | InstanceNorm 105 1 1 104 105 0=128 1=1.000000e-05 51 | BinaryOp 106 2 1 105 98_splitncnn_0 106 52 | Split splitncnn_4 1 2 106 106_splitncnn_0 106_splitncnn_1 53 | Padding 107 1 1 106_splitncnn_1 107 0=1 1=1 2=1 3=1 4=2 54 | Convolution 108 1 1 107 108 0=128 1=3 5=1 6=147456 55 | InstanceNorm 109 1 1 108 109 0=128 1=1.000000e-05 56 | ReLU 110 1 1 109 110 57 | Padding 111 1 1 110 111 0=1 1=1 2=1 3=1 4=2 58 | Convolution 112 1 1 111 112 0=128 1=3 5=1 6=147456 59 | InstanceNorm 113 1 1 112 113 0=128 1=1.000000e-05 60 | BinaryOp 114 2 1 113 106_splitncnn_0 114 61 | Interp 139 1 1 114 139 0=1 1=2.000000e+00 2=2.000000e+00 62 | Padding 140 1 1 139 140 0=1 1=1 2=1 3=1 4=2 63 | Convolution 141 1 1 140 141 0=64 1=3 5=1 6=73728 64 | InstanceNorm 142 1 1 141 142 0=64 1=1.000000e-05 65 | ReLU 143 1 1 142 143 66 | Interp 168 1 1 143 168 0=1 1=2.000000e+00 2=2.000000e+00 67 | Padding 169 1 1 168 169 0=1 1=1 2=1 3=1 4=2 68 | Convolution 170 1 1 169 170 0=32 1=3 5=1 6=18432 69 | InstanceNorm 171 1 1 170 171 0=32 1=1.000000e-05 70 | ReLU 172 1 1 171 172 71 | Padding 173 1 1 172 173 0=4 1=4 2=4 3=4 4=2 72 | Convolution output1 1 1 173 output1 0=3 1=9 5=1 6=7776 73 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/models/udnie-9.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 70 75 3 | Input input1 0 1 input1 4 | Padding 63 1 1 input1 63 0=4 1=4 2=4 3=4 4=2 5 | Convolution 64 1 1 63 64 0=32 1=9 5=1 6=7776 6 | InstanceNorm 65 1 1 64 65 0=32 1=1.000000e-05 7 | ReLU 66 1 1 65 66 8 | Padding 67 1 1 66 67 0=1 1=1 2=1 3=1 4=2 9 | Convolution 68 1 1 67 68 0=64 1=3 3=2 5=1 6=18432 10 | InstanceNorm 69 1 1 68 69 0=64 1=1.000000e-05 11 | ReLU 70 1 1 69 70 12 | Padding 71 1 1 70 71 0=1 1=1 2=1 3=1 4=2 13 | Convolution 72 1 1 71 72 0=128 1=3 3=2 5=1 6=73728 14 | InstanceNorm 73 1 1 72 73 0=128 1=1.000000e-05 15 | ReLU 74 1 1 73 74 16 | Split splitncnn_0 1 2 74 74_splitncnn_0 74_splitncnn_1 17 | Padding 75 1 1 74_splitncnn_1 75 0=1 1=1 2=1 3=1 4=2 18 | Convolution 76 1 1 75 76 0=128 1=3 5=1 6=147456 19 | InstanceNorm 77 1 1 76 77 0=128 1=1.000000e-05 20 | ReLU 78 1 1 77 78 21 | Padding 79 1 1 78 79 0=1 1=1 2=1 3=1 4=2 22 | Convolution 80 1 1 79 80 0=128 1=3 5=1 6=147456 23 | InstanceNorm 81 1 1 80 81 0=128 1=1.000000e-05 24 | BinaryOp 82 2 1 81 74_splitncnn_0 82 25 | Split splitncnn_1 1 2 82 82_splitncnn_0 82_splitncnn_1 26 | Padding 83 1 1 82_splitncnn_1 83 0=1 1=1 2=1 3=1 4=2 27 | Convolution 84 1 1 83 84 0=128 1=3 5=1 6=147456 28 | InstanceNorm 85 1 1 84 85 0=128 1=1.000000e-05 29 | ReLU 86 1 1 85 86 30 | Padding 87 1 1 86 87 0=1 1=1 2=1 3=1 4=2 31 | Convolution 88 1 1 87 88 0=128 1=3 5=1 6=147456 32 | InstanceNorm 89 1 1 88 89 0=128 1=1.000000e-05 33 | BinaryOp 90 2 1 89 82_splitncnn_0 90 34 | Split splitncnn_2 1 2 90 90_splitncnn_0 90_splitncnn_1 35 | Padding 91 1 1 90_splitncnn_1 91 0=1 1=1 2=1 3=1 4=2 36 | Convolution 92 1 1 91 92 0=128 1=3 5=1 6=147456 37 | InstanceNorm 93 1 1 92 93 0=128 1=1.000000e-05 38 | ReLU 94 1 1 93 94 39 | Padding 95 1 1 94 95 0=1 1=1 2=1 3=1 4=2 40 | Convolution 96 1 1 95 96 0=128 1=3 5=1 6=147456 41 | InstanceNorm 97 1 1 96 97 0=128 1=1.000000e-05 42 | BinaryOp 98 2 1 97 90_splitncnn_0 98 43 | Split splitncnn_3 1 2 98 98_splitncnn_0 98_splitncnn_1 44 | Padding 99 1 1 98_splitncnn_1 99 0=1 1=1 2=1 3=1 4=2 45 | Convolution 100 1 1 99 100 0=128 1=3 5=1 6=147456 46 | InstanceNorm 101 1 1 100 101 0=128 1=1.000000e-05 47 | ReLU 102 1 1 101 102 48 | Padding 103 1 1 102 103 0=1 1=1 2=1 3=1 4=2 49 | Convolution 104 1 1 103 104 0=128 1=3 5=1 6=147456 50 | InstanceNorm 105 1 1 104 105 0=128 1=1.000000e-05 51 | BinaryOp 106 2 1 105 98_splitncnn_0 106 52 | Split splitncnn_4 1 2 106 106_splitncnn_0 106_splitncnn_1 53 | Padding 107 1 1 106_splitncnn_1 107 0=1 1=1 2=1 3=1 4=2 54 | Convolution 108 1 1 107 108 0=128 1=3 5=1 6=147456 55 | InstanceNorm 109 1 1 108 109 0=128 1=1.000000e-05 56 | ReLU 110 1 1 109 110 57 | Padding 111 1 1 110 111 0=1 1=1 2=1 3=1 4=2 58 | Convolution 112 1 1 111 112 0=128 1=3 5=1 6=147456 59 | InstanceNorm 113 1 1 112 113 0=128 1=1.000000e-05 60 | BinaryOp 114 2 1 113 106_splitncnn_0 114 61 | Interp 139 1 1 114 139 0=1 1=2.000000e+00 2=2.000000e+00 62 | Padding 140 1 1 139 140 0=1 1=1 2=1 3=1 4=2 63 | Convolution 141 1 1 140 141 0=64 1=3 5=1 6=73728 64 | InstanceNorm 142 1 1 141 142 0=64 1=1.000000e-05 65 | ReLU 143 1 1 142 143 66 | Interp 168 1 1 143 168 0=1 1=2.000000e+00 2=2.000000e+00 67 | Padding 169 1 1 168 169 0=1 1=1 2=1 3=1 4=2 68 | Convolution 170 1 1 169 170 0=32 1=3 5=1 6=18432 69 | InstanceNorm 171 1 1 170 171 0=32 1=1.000000e-05 70 | ReLU 172 1 1 171 172 71 | Padding 173 1 1 172 173 0=4 1=4 2=4 3=4 4=2 72 | Convolution output1 1 1 173 output1 0=3 1=9 5=1 6=7776 73 | -------------------------------------------------------------------------------- /style_transfer/styletransfer/styletransfer.cpp: -------------------------------------------------------------------------------- 1 | // opencv 2 | #include 3 | #include 4 | #include 5 | 6 | // ncnn 7 | #include "gpu.h" 8 | #include "net.h" 9 | #include 10 | 11 | int main(int argc, char **argv) 12 | { 13 | cv::Mat image = cv::imread("input.png"); 14 | if (image.empty()) 15 | { 16 | std::cout << "cv::imread failed" << std::endl; 17 | return -1; 18 | } 19 | ncnn::Net net; 20 | net.opt.use_vulkan_compute = true; // 开启GPU加速 21 | net.load_param("models/candy9.param"); 22 | net.load_model("models/candy9.bin"); 23 | // 前处理 24 | const int w = image.cols; 25 | const int h = image.rows; 26 | const int target_size = 1000; // 值越大细节表现越好,但是速度越慢 27 | int target_w = w; 28 | int target_h = h; 29 | if (w < h) 30 | { 31 | target_h = target_size; 32 | target_w = target_size * w / h; 33 | } 34 | else 35 | { 36 | target_w = target_size; 37 | target_h = target_size * h / w; 38 | } 39 | ncnn::Mat in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR2RGB, w, h, target_w, target_h); 40 | ncnn::Mat out; 41 | ncnn::Extractor ex = net.create_extractor(); 42 | ex.input("input1", in); 43 | ex.extract("output1", out); 44 | // 后处理 45 | cv::Mat outbgr; 46 | outbgr.create(out.h, out.w, CV_8UC3); 47 | out.to_pixels(outbgr.data, ncnn::Mat::PIXEL_RGB2BGR); 48 | 49 | // cv::imwrite("out.png", outbgr); // 保存图片 50 | cv::imshow("out", outbgr); 51 | cv::waitKey(0); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /tts/sherpa/README.md: -------------------------------------------------------------------------------- 1 | # sherpa 2 | 3 | ## Input --> Output 4 | 5 | You can use sherpa-ncnn for real-time speech recognition 6 | 7 | - English: https://www.bilibili.com/video/BV1TP411p7dh/ 8 | - Chinese: https://www.bilibili.com/video/BV1214y177vu 9 | 10 | ## 出入信息 11 | 12 | ### 输入尺寸 13 | 14 | ## 模型转换 15 | 16 | ```python 17 | 18 | ``` 19 | 20 | ## c++实现 21 | 22 | 参见 https://github.com/k2-fsa/sherpa-ncnn 23 | 24 | ## Example project 25 | 26 | ## Reference 27 | 28 | - [k2-fsa/sherpa-ncnn](https://github.com/k2-fsa/sherpa-ncnn) 29 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 30 | -------------------------------------------------------------------------------- /tts/vits/README.md: -------------------------------------------------------------------------------- 1 | # vits 2 | 3 | ## Input --> Output 4 | 5 | ![](https://user-images.githubusercontent.com/57377927/213869491-daef85ca-7ea6-4e1f-b075-a71f415e8fc6.png) 6 | 7 | ## 出入信息 8 | 9 | ## 模型转换 10 | 11 | ```python 12 | https://github.com/weirdseed/vits-ncnn-convert-tool 13 | ``` 14 | 15 | ## c++实现 16 | 17 | 参见 https://github.com/weirdseed/Vits-Android-ncnn 18 | 19 | ## Example project 20 | 21 | ## Reference 22 | 23 | - [weirdseed/Vits-Android-ncnn](https://github.com/weirdseed/Vits-Android-ncnn) 24 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 25 | -------------------------------------------------------------------------------- /video/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/video/.DS_Store -------------------------------------------------------------------------------- /video/ifrnet/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/video/ifrnet/0.png -------------------------------------------------------------------------------- /video/ifrnet/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/video/ifrnet/1.png -------------------------------------------------------------------------------- /video/ifrnet/README.md: -------------------------------------------------------------------------------- 1 | # IFRNet: Intermediate Feature Refine Network for Efficient Frame Interpolation 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/ltkong218/IFRNet/raw/main/figures/vimeo90k.png) 6 | ![](https://github.com/ltkong218/IFRNet/raw/main/figures/fig2_2.gif) 7 | 8 | ## Convert 9 | 10 | pytorch --> torchscript --> pnnx --> ncnn 11 | 12 | ```bash 13 | 14 | ``` 15 | 16 | ## Notice 17 | 18 | Currently only normal in cpu mode, GPU fails for unknown reasons 19 | 20 | ## Example 21 | 22 | 23 | 24 | ## Reference 25 | 26 | - [ltkong218/IFRNet](https://github.com/ltkong218/IFRNet) 27 | - [nihui/ifrnet-ncnn-vulkan](https://github.com/nihui/ifrnet-ncnn-vulkan) 28 | 29 | 30 | -------------------------------------------------------------------------------- /video/nerf/README.md: -------------------------------------------------------------------------------- 1 | # taichi-nerfs 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/EdVince/NeRF-NCNN/raw/main/resources/Lego.gif) 6 | 7 | ## 出入信息 8 | 9 | ### 输入尺寸 10 | 11 | ## 模型转换 12 | 13 | ```python 14 | 欢迎pr 15 | 16 | ``` 17 | 18 | ## c++实现 19 | 20 | 参见 21 | 22 | ## Example project 23 | 24 | ## Reference 25 | 26 | - [taichi-dev/taichi-nerfs](https://github.com/taichi-dev/taichi-nerfs) 27 | - [EdVince/NeRF-NCNN](https://github.com/EdVince/NeRF-NCNN) 28 | - [Tencent/ncnn](https://github.com/Tencent/ncnn) 29 | -------------------------------------------------------------------------------- /video/rife/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/video/rife/.DS_Store -------------------------------------------------------------------------------- /video/rife/README.md: -------------------------------------------------------------------------------- 1 | # RIFE - Real-Time Intermediate Flow Estimation for Video Frame Interpolation 2 | 3 | ## Input --> Output 4 | 5 | ![](https://github.com/hzwer/arXiv2021-RIFE/raw/main/demo/I2_slomo_clipped.gif) 6 | 7 | ## Convert 8 | 9 | onnx --> simpleonnx --> onnx2ncnn --> ncnnOptimize --> ncnn 10 | 11 | ```bash 12 | 13 | ``` 14 | 15 | ## Example 16 | 17 | - [RIFE-GUI](https://github.com/Baiyuetribe/paper2gui) 18 | 19 | ## Reference 20 | 21 | - [hzwer/arXiv2021-RIFE](https://github.com/hzwer/arXiv2021-RIFE) 22 | 23 | 24 | -------------------------------------------------------------------------------- /video/rife/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baiyuetribe/ncnn-models/7fff7655adef31b39f93653d4e73251c2dc9ba00/video/rife/models/.DS_Store -------------------------------------------------------------------------------- /video/rife/rife.cpp: -------------------------------------------------------------------------------- 1 | // opencv2 2 | #include 3 | #include 4 | #include 5 | // c++ 6 | #include 7 | #include 8 | #include 9 | // ncnn 10 | #include "net.h" 11 | // #include "layer.h" // 用于处理自定义的层 12 | // #include "pipeline.h" 13 | #include "rife_ops.h" 14 | 15 | DEFINE_LAYER_CREATOR(Warp) 16 | // 自定义层 17 | // class Warp : public ncnn::Layer 18 | // { 19 | // public: 20 | // Warp(); 21 | // virtual int create_pipeline(const ncnn::Option &opt); 22 | // virtual int destroy_pipeline(const ncnn::Option &opt); 23 | // virtual int forward(const std::vector &bottom_blobs, std::vector &top_blobs, const ncnn::Option &opt) const; 24 | // virtual int forward(const std::vector &bottom_blobs, std::vector &top_blobs, ncnn::VkCompute &cmd, const ncnn::Option &opt) const; 25 | 26 | // private: 27 | // ncnn::Pipeline *pipeline_warp; 28 | // ncnn::Pipeline *pipeline_warp_pack4; 29 | // ncnn::Pipeline *pipeline_warp_pack8; 30 | // }; 31 | 32 | int rife4(const cv::Mat &in0image, const cv::Mat &in1image, float timestep, cv::Mat &outcv) 33 | { 34 | if (timestep == 0.f) 35 | { 36 | // outimage = in0image; 37 | return 0; 38 | } 39 | 40 | if (timestep == 1.f) 41 | { 42 | // outimage = in1image; 43 | return 0; 44 | } 45 | // 推理网络定义 46 | ncnn::Net net; 47 | net.opt.use_vulkan_compute = true; 48 | net.register_custom_layer("rife.Warp", Warp_layer_creator); // 特殊处理 开头加上 DEFINE_LAYER_CREATOR(Warp) 49 | net.load_param("models/flownet.param"); 50 | net.load_model("models/flownet.bin"); 51 | // 特殊图层处理 52 | // 图像转换 53 | const unsigned char *pixel0data = (const unsigned char *)in0image.data; 54 | const unsigned char *pixel1data = (const unsigned char *)in1image.data; 55 | const int w = in0image.cols; 56 | const int h = in0image.rows; 57 | const int channels = 3; // in0image.elempack; 58 | // pad to 32n 59 | int w_padded = (w + 31) / 32 * 32; 60 | int h_padded = (h + 31) / 32 * 32; 61 | // 入口处理 62 | ncnn::Mat in0; 63 | ncnn::Mat in1; 64 | in0 = ncnn::Mat::from_pixels(pixel0data, ncnn::Mat::PIXEL_BGR2RGB, w, h); 65 | in1 = ncnn::Mat::from_pixels(pixel1data, ncnn::Mat::PIXEL_BGR2RGB, w, h); 66 | // 输出 67 | ncnn::Mat out; 68 | { 69 | // preproc and border padding 70 | ncnn::Mat in0_padded; 71 | ncnn::Mat in1_padded; 72 | ncnn::Mat timestep_padded; 73 | { 74 | in0_padded.create(w_padded, h_padded, 3); 75 | for (int q = 0; q < 3; q++) 76 | { 77 | float *outptr = in0_padded.channel(q); 78 | 79 | int i = 0; 80 | for (; i < h; i++) 81 | { 82 | const float *ptr = in0.channel(q).row(i); 83 | 84 | int j = 0; 85 | for (; j < w; j++) 86 | { 87 | *outptr++ = *ptr++ * (1 / 255.f); 88 | } 89 | for (; j < w_padded; j++) 90 | { 91 | *outptr++ = 0.f; 92 | } 93 | } 94 | for (; i < h_padded; i++) 95 | { 96 | for (int j = 0; j < w_padded; j++) 97 | { 98 | *outptr++ = 0.f; 99 | } 100 | } 101 | } 102 | } 103 | { 104 | in1_padded.create(w_padded, h_padded, 3); 105 | for (int q = 0; q < 3; q++) 106 | { 107 | float *outptr = in1_padded.channel(q); 108 | 109 | int i = 0; 110 | for (; i < h; i++) 111 | { 112 | const float *ptr = in1.channel(q).row(i); 113 | 114 | int j = 0; 115 | for (; j < w; j++) 116 | { 117 | *outptr++ = *ptr++ * (1 / 255.f); 118 | } 119 | for (; j < w_padded; j++) 120 | { 121 | *outptr++ = 0.f; 122 | } 123 | } 124 | for (; i < h_padded; i++) 125 | { 126 | for (int j = 0; j < w_padded; j++) 127 | { 128 | *outptr++ = 0.f; 129 | } 130 | } 131 | } 132 | } 133 | { 134 | timestep_padded.create(w_padded, h_padded, 1); 135 | timestep_padded.fill(timestep); 136 | } 137 | // flownet 138 | ncnn::Mat out_padded; 139 | ncnn::Extractor ex = net.create_extractor(); 140 | ex.input("in0", in0_padded); 141 | ex.input("in1", in1_padded); 142 | ex.input("in2", timestep_padded); 143 | ex.extract("out0", out_padded); 144 | // std::cout << "out shape" << out_padded.w << " " << out_padded.h << std::endl; 145 | // cut padding and postproc 146 | out.create(w, h, 3); 147 | { 148 | for (int q = 0; q < 3; q++) 149 | { 150 | float *outptr = out.channel(q); 151 | const float *ptr = out_padded.channel(q); 152 | 153 | for (int i = 0; i < h; i++) 154 | { 155 | for (int j = 0; j < w; j++) 156 | { 157 | *outptr++ = *ptr++ * 255.f + 0.5f; 158 | } 159 | } 160 | } 161 | } 162 | } 163 | // outcv(in.h, in.w, CV_8UC3); 164 | out.to_pixels(outcv.data, ncnn::Mat::PIXEL_BGR2RGB); // 纯RGB现实偏蓝色 165 | return 0; 166 | } 167 | 168 | int main(int argc, char **argv) 169 | { 170 | cv::Mat image0 = cv::imread("0.png"); // 输入一张图片,BGR格式 171 | cv::Mat image1 = cv::imread("1.png"); // 输入一张图片,BGR格式 172 | if (image0.empty() || image1.empty()) 173 | { 174 | std::cout << "read image failed" << std::endl; 175 | return -1; 176 | } 177 | cv::Mat out(image0.rows, image0.cols, CV_8UC3); 178 | rife4(image0, image1, 0.5, out); 179 | cv::imshow("out", out); 180 | cv::waitKey(0); 181 | return 0; 182 | } 183 | -------------------------------------------------------------------------------- /video/rife/rife_ops.h: -------------------------------------------------------------------------------- 1 | // rife implemented with ncnn library 2 | 3 | #ifndef RIFE_OPS_H 4 | #define RIFE_OPS_H 5 | 6 | #include 7 | 8 | // ncnn 9 | #include "layer.h" 10 | #include "pipeline.h" 11 | 12 | class Warp : public ncnn::Layer 13 | { 14 | public: 15 | Warp(); 16 | virtual int create_pipeline(const ncnn::Option &opt); 17 | virtual int destroy_pipeline(const ncnn::Option &opt); 18 | virtual int forward(const std::vector &bottom_blobs, std::vector &top_blobs, const ncnn::Option &opt) const; 19 | virtual int forward(const std::vector &bottom_blobs, std::vector &top_blobs, ncnn::VkCompute &cmd, const ncnn::Option &opt) const; 20 | 21 | private: 22 | ncnn::Pipeline *pipeline_warp; 23 | ncnn::Pipeline *pipeline_warp_pack4; 24 | ncnn::Pipeline *pipeline_warp_pack8; 25 | }; 26 | 27 | #endif // RIFE_OPS_H 28 | -------------------------------------------------------------------------------- /video/rife/warp.cpp: -------------------------------------------------------------------------------- 1 | // rife implemented with ncnn library 2 | 3 | #include "rife_ops.h" 4 | 5 | #include "warp.comp.hex.h" 6 | #include "warp_pack4.comp.hex.h" 7 | #include "warp_pack8.comp.hex.h" 8 | 9 | using namespace ncnn; 10 | 11 | Warp::Warp() 12 | { 13 | support_vulkan = true; 14 | 15 | pipeline_warp = 0; 16 | pipeline_warp_pack4 = 0; 17 | pipeline_warp_pack8 = 0; 18 | } 19 | 20 | int Warp::create_pipeline(const Option& opt) 21 | { 22 | if (!vkdev) 23 | return 0; 24 | 25 | std::vector specializations(0 + 0); 26 | 27 | // pack1 28 | { 29 | static std::vector spirv; 30 | static ncnn::Mutex lock; 31 | { 32 | ncnn::MutexLockGuard guard(lock); 33 | if (spirv.empty()) 34 | { 35 | compile_spirv_module(warp_comp_data, sizeof(warp_comp_data), opt, spirv); 36 | } 37 | } 38 | 39 | pipeline_warp = new Pipeline(vkdev); 40 | pipeline_warp->set_optimal_local_size_xyz(); 41 | pipeline_warp->create(spirv.data(), spirv.size() * 4, specializations); 42 | } 43 | 44 | // pack4 45 | { 46 | static std::vector spirv; 47 | static ncnn::Mutex lock; 48 | { 49 | ncnn::MutexLockGuard guard(lock); 50 | if (spirv.empty()) 51 | { 52 | compile_spirv_module(warp_pack4_comp_data, sizeof(warp_pack4_comp_data), opt, spirv); 53 | } 54 | } 55 | 56 | pipeline_warp_pack4 = new Pipeline(vkdev); 57 | pipeline_warp_pack4->set_optimal_local_size_xyz(); 58 | pipeline_warp_pack4->create(spirv.data(), spirv.size() * 4, specializations); 59 | } 60 | 61 | // pack8 62 | if (opt.use_shader_pack8) 63 | { 64 | static std::vector spirv; 65 | static ncnn::Mutex lock; 66 | { 67 | ncnn::MutexLockGuard guard(lock); 68 | if (spirv.empty()) 69 | { 70 | compile_spirv_module(warp_pack8_comp_data, sizeof(warp_pack8_comp_data), opt, spirv); 71 | } 72 | } 73 | 74 | pipeline_warp_pack8 = new Pipeline(vkdev); 75 | pipeline_warp_pack8->set_optimal_local_size_xyz(); 76 | pipeline_warp_pack8->create(spirv.data(), spirv.size() * 4, specializations); 77 | } 78 | 79 | return 0; 80 | } 81 | 82 | int Warp::destroy_pipeline(const Option& opt) 83 | { 84 | delete pipeline_warp; 85 | pipeline_warp = 0; 86 | 87 | delete pipeline_warp_pack4; 88 | pipeline_warp_pack4 = 0; 89 | 90 | delete pipeline_warp_pack8; 91 | pipeline_warp_pack8 = 0; 92 | 93 | return 0; 94 | } 95 | 96 | int Warp::forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const 97 | { 98 | const Mat& image_blob = bottom_blobs[0]; 99 | const Mat& flow_blob = bottom_blobs[1]; 100 | 101 | int w = image_blob.w; 102 | int h = image_blob.h; 103 | int channels = image_blob.c; 104 | 105 | Mat& top_blob = top_blobs[0]; 106 | top_blob.create(w, h, channels); 107 | if (top_blob.empty()) 108 | return -100; 109 | 110 | #pragma omp parallel for num_threads(opt.num_threads) 111 | for (int q = 0; q < channels; q++) 112 | { 113 | float* outptr = top_blob.channel(q); 114 | 115 | const Mat image = image_blob.channel(q); 116 | 117 | const float* fxptr = flow_blob.channel(0); 118 | const float* fyptr = flow_blob.channel(1); 119 | 120 | for (int y = 0; y < h; y++) 121 | { 122 | for (int x = 0; x < w; x++) 123 | { 124 | float flow_x = fxptr[0]; 125 | float flow_y = fyptr[0]; 126 | 127 | float sample_x = x + flow_x; 128 | float sample_y = y + flow_y; 129 | 130 | // bilinear interpolate 131 | float v; 132 | { 133 | int x0 = floor(sample_x); 134 | int y0 = floor(sample_y); 135 | int x1 = x0 + 1; 136 | int y1 = y0 + 1; 137 | 138 | x0 = std::min(std::max(x0, 0), w - 1); 139 | y0 = std::min(std::max(y0, 0), h - 1); 140 | x1 = std::min(std::max(x1, 0), w - 1); 141 | y1 = std::min(std::max(y1, 0), h - 1); 142 | 143 | float alpha = sample_x - x0; 144 | float beta = sample_y - y0; 145 | 146 | float v0 = image.row(y0)[x0]; 147 | float v1 = image.row(y0)[x1]; 148 | float v2 = image.row(y1)[x0]; 149 | float v3 = image.row(y1)[x1]; 150 | 151 | float v4 = v0 * (1 - alpha) + v1 * alpha; 152 | float v5 = v2 * (1 - alpha) + v3 * alpha; 153 | 154 | v = v4 * (1 - beta) + v5 * beta; 155 | } 156 | 157 | outptr[0] = v; 158 | 159 | outptr += 1; 160 | 161 | fxptr += 1; 162 | fyptr += 1; 163 | } 164 | } 165 | } 166 | 167 | return 0; 168 | } 169 | 170 | int Warp::forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const 171 | { 172 | const VkMat& image_blob = bottom_blobs[0]; 173 | const VkMat& flow_blob = bottom_blobs[1]; 174 | 175 | int w = image_blob.w; 176 | int h = image_blob.h; 177 | int channels = image_blob.c; 178 | size_t elemsize = image_blob.elemsize; 179 | int elempack = image_blob.elempack; 180 | 181 | VkMat& top_blob = top_blobs[0]; 182 | top_blob.create(w, h, channels, elemsize, elempack, opt.blob_vkallocator); 183 | if (top_blob.empty()) 184 | return -100; 185 | 186 | std::vector bindings(3); 187 | bindings[0] = image_blob; 188 | bindings[1] = flow_blob; 189 | bindings[2] = top_blob; 190 | 191 | std::vector constants(4); 192 | constants[0].i = top_blob.w; 193 | constants[1].i = top_blob.h; 194 | constants[2].i = top_blob.c; 195 | constants[3].i = top_blob.cstep; 196 | 197 | if (elempack == 8) 198 | { 199 | cmd.record_pipeline(pipeline_warp_pack8, bindings, constants, top_blob); 200 | } 201 | else if (elempack == 4) 202 | { 203 | cmd.record_pipeline(pipeline_warp_pack4, bindings, constants, top_blob); 204 | } 205 | else // if (elempack == 1) 206 | { 207 | cmd.record_pipeline(pipeline_warp, bindings, constants, top_blob); 208 | } 209 | 210 | return 0; 211 | } 212 | --------------------------------------------------------------------------------