使用 ncnn 将 pytorch 模型部署到安卓手机 在编译 NCNN 时开启图形卡支持。Vulkan 用于 GPU,设置 -DNCNN_VULKAN=ON。 MobileNetV3 在编译到 MT 时开启 CMAKE 0091 特性 cmake_minimum_required(VERSION 3.20.0) cmake_policy(SET CMP0091 NEW) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") project("client-project") 训练 YOLO \Envs\torch\Scripts\activate.ps1 python train.py --batch 6 --workers 2 --imgsz 960 --epochs 300 --data "\Core\yaml\data.yaml" --cfg "\Core\yaml\cfg.yaml" --weights \ Core\weights\best.pt --device 0 模型转换 from torch import nn import torch.utils.model_zoo as model_zoo import torch.onnx from libs import define from libs.net import Net from libs.dataset import ImageDataset import os test_data = ImageDataset(define.testPath,False) test_loader = torch.utils.data.DataLoader( test_data, batch_size=1, shuffle=True) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = Net(out_dim=19).to(device) model.load_state_dict(torch.load( "./widget/last.pt" )) model.eval() def saveOnnx(): for data, target in test_loader: data, target = data.to(device), target.to(device) label = target.long() y = model(data) # 导出模型 torch.onnx.export(model, # 正在运行的模型 data, # 模型输入(或多个输入的元组) "./widget/best.onnx", # 保存模型的位置(可以是文件或文件对象) export_params=True, # 将训练好的参数权重存储在模型文件中 opset_version=10, # 导出模型的 ONNX 版本 do_constant_folding=True, # 是否执行常量折叠以进行优化 input_names = ['input'], # 模型的输入名称 output_names = ['output'], # 模型的输出名称 dynamic_axes={'input': {0:'batch_size'}, # 可变长度轴 'output': {0:'batch_size'}}) traced_script_module = torch.jit.trace(model, data) return saveOnnx() # 转换 os.system("python -m onnxsim ./widget/best.onnx ./widgetbest-sim.onnx") os.system("./bin/onnx2ncnn.exe ./widget/best-sim.onnx ./widget/best.param ./widget/best.bin") os.system("./bin/ncnnoptimize.exe ./widget/best.param ./widget/best.bin ./widget/best-opt.param ./widget/best-opt.bin 65536") python .\export.py --weights weights/best.pt --img 960 --batch 1 --train python -m onnxsim best.onnx best-sim.onnx .\onnx2ncnn.exe best-sim.onnx best.param best.bin ncnnoptimize best.param best.bin best-opt.param best-opt.bin 65536 Git 克隆 ncnn 仓库及子模块 $ git clone https://github.com/Tencent/ncnn.git $ cd ncnn $ git submodule update --init 为 Linux 构建 使用 VS2017 为 Windows x64 构建 为 macOS 构建 使用交叉编译为 ARM Cortex-A 系列构建 使用交叉编译为 Hisilicon 平台构建 为 Android 构建 在 macOS 上使用 xcode 为 iOS 构建 为 WebAssembly 构建 为 AllWinner D1 构建 为 Loongson 2K1000 构建 为 Android 上的 Termux 构建 为 Linux 构建 安装所需的构建依赖项:
...