深度学习模型加密(pytorch,onnx,darknet)

更新时间

最近更新: 2022-09

环境

Python 3.8
opencv 4.5.2.54
onnxruntime 1.10.0
pytorch 1.10.2
cryptography 3.1.1

流程介绍

加密:

  1. 模型以二进制存储
  2. 以二进制形式读取
  3. 用cryptography对文件加密
  4. 保存加密后文件

解密:

  1. 读取加密文件
  2. 使用cryptography解密
  3. 转换为框架可读数据

代码(onnx,darknet)

# 生成密钥
from cryptography.fernet import Fernet

key = Fernet.generate_key()
#保存license
#with open('license', 'wb') as fw:
    #fw.write(key)
print(key)  
#解密时 key修改成自己的就行
f = Fernet(key)
#模型加密

#原始模型路径
model_file = ['./yolov4-tiny.weights','./yolov4-tiny.cfg','./yolov4-tiny.onnx']

#加密模型,名称根据自己需求更改
new_model_file = ['./yolov4-tiny.dll','./yolov4-tiny-cfg.dll','./yolov4-tiny-onnx.dll']

for i in range(2):
	#二进创建打开生成文件
    with open(new_model_file[i],'wb') as ew:
    	#二进制读取模型文件
        content = open(model_file[i],'rb').read()
        #根据密钥解密文件
        encrypted_content = f.encrypt(content)
        # print(encrypted_content)
        #保存到新文件
        ew.write(encrypted_content)
# 模型解密
#使用opencv dnn模块读取darknet模型测试
import cv2
#二进制读取加密后的文件
conf_file = open(new_model_file[1],'rb').read()
#解密
conf_file = f.decrypt(conf_file)
#转换数据格式
conf_file   = bytearray(conf_file)
# 与上一致
weight_flie = open(new_model_file[0],'rb').read()
weight_flie = f.decrypt(weight_flie)
weight_flie = bytearray(weight_flie)
#读取模型
net = cv2.dnn.readNetFromDarknet(conf_file,weight_flie)
print(net)

#使用onnxruntime模块读取onnx模型测试
import onnxruntime

onnx_file = open(new_model_file[2],'rb').read()
onnx_file = f.decrypt(onnx_file)
#onnx不需要转换数据格式
session = onnxruntime.InferenceSession(onnx_file)
print(session)

代码(pytorch)

# 生成密钥
from cryptography.fernet import Fernet

key = Fernet.generate_key()
#保存license
#with open('license', 'wb') as fw:
    #fw.write(key)
print(key)  
#解密时 key修改成自己的就行
f = Fernet(key)
#模型存储格式预处理
import io
import torch
#读取pytorch模型存为io.Bytes格式
model = torch.load('./model.pth',map_location='cpu')
#print(model)
byte = io.BytesIO()
torch.save(model, byte)
byte.seek(0) 
#加密
#二进制方式读取模型
pth_bytes = byte.read()
#加密
new_model = f.encrypt(pth_bytes)
#保存加密文件
with open('new_model.pth', 'wb') as fw:
    fw.write(new_model)
#解密
#读取加密后的文件
with open('new_model.pth', 'rb') as fr:
    new_model = fr.read()
# cryptography 解密
new_model = f.decrypt(new_model)
#解密后的byte数据转为io.bytes数据格式
model_byte = io.BytesIO(new_model)
model_byte.seek(0)
# torch.load读取解密后的io.Bytes格式数据
model = torch.load(model_byte)

print(model)

参考文章

Pytorch模型加密的方法
神经网络如何加密(Python)

  • 3
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Jetson Nano 是一款基于 NVIDIA Jetson 平台的小型开发板,可以用于部署深度学习模型PyTorch是一种非常流行的深度学习框架,而 ONNX是一种可互操作的深度学习模型格式,可以在不同的框架之间共享模型。 以下是在 Jetson Nano 上部署 PyTorch ONNX 模型的步骤: 1. 安装 PyTorchONNX 在 Jetson Nano 上安装 PyTorchONNX,可以使用 pip 命令: ``` pip3 install torch torchvision pip3 install onnx ``` 2. 导出 PyTorch 模型ONNX 格式 使用 PyTorch模型导出为 ONNX 格式,可以使用以下代码: ``` import torch import torchvision model = torchvision.models.resnet18(pretrained=True) model.eval() dummy_input = torch.randn(1, 3, 224, 224) input_names = ["input"] output_names = ["output"] torch.onnx.export(model, dummy_input, "resnet18.onnx", verbose=True, input_names=input_names, output_names=output_names) ``` 3. 在 Jetson Nano 上加载 ONNX 模型 使用 ONNX 运行时库在 Jetson Nano 上加载 ONNX 模型。可以使用以下代码: ``` import onnxruntime as ort session = ort.InferenceSession("resnet18.onnx") input_name = session.get_inputs()[0].name output_name = session.get_outputs()[0].name input_data = np.random.randn(1, 3, 224, 224).astype(np.float32) result = session.run([output_name], {input_name: input_data}) print(result) ``` 以上就是在 Jetson Nano 上部署 PyTorch ONNX 模型的步骤。需要注意的是,在 Jetson Nano 上运行深度学习模型需要使用 GPU 加速,因此应该确保已经安装了适当的 NVIDIA GPU 驱动程序和 CUDA 工具包。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值