结合使用加密模型和 OpenVINO

将深度学习功能部署到边缘设备可能会带来安全 挑战,比如确保推理的完整性,或者为深度学习模型 提供版权保护。

一种可能的解决方案是使用加密技术来保护 部署和存储在边缘设备上的模型。模型加密、解密和 身份验证不是由 OpenVINO 提供的,但可以使用 第三方工具(即 OpenSSL)实现。实施加密时,请确保 使用最新版本的工具并遵循加密技术最佳实践。

本指南介绍如何通过受保护的模型安全地使用 OpenVINO。

安全部署模型

模型通过 OpenVINO 模型优化器优化后, 会以 OpenVINO 中间表示 (OpenVINO IR) 格式部署到目标设备中。优化 模型存储在边缘设备上,并通过 OpenVINO 运行时执行。 TensorFlow(请查阅 TensorFlow 前端功能和限制 )、ONNX 和 PaddlePaddle 模型也可以通过 OpenVINO 运行时本机读取。

在将模型部署到边缘设备之前可对其进行加密和优化, 以用于保护深度学习模型。边缘设备应始终保护存储的模型, 并**仅在运行时中**解密模型, 以供 OpenVINO 运行时使用。

../../_images/deploy_encrypted_model.svg

加载加密模型

OpenVINO™ 运行时在加载之前需要进行模型解密。为 模型解密分配一个临时内存块,并使用 ov::Core::read_model 方法从内存缓冲区加载模型。 有关更多信息,请参阅 ov::Core 类参考文档。

std::vector<uint8_t> model_data, weights_data;

std::string password; // taken from an user
std::ifstream model_file("model.xml"), weights_file("model.bin");

// Read model files and decrypt them into temporary memory block
decrypt_file(model_file, password, model_data);
decrypt_file(weights_file, password, weights_data);

英特尔® Software Guard Extensions(英特尔® SGX)等基于硬件的保护可用于保护解密操作机密,并将其绑定到设备上。有关更多信息,请参阅 英特尔® Software Guard Extensions

使用 ov::Core::read_model 分别设置模型表示和 权重。

目前,还没有办法从 ONNX 模型的内存中读取外部权重。 调用 ov::Core::read_model(const std::string& model, const Tensor& weights) 方法时, 应将 weights 作为空值 ov::Tensor 传递。

ov::Core core;
// Load model from temporary memory block
std::string str_model(model_data.begin(), model_data.end());
auto model = core.read_model(str_model,
    ov::Tensor(ov::element::u8, {weights_data.size()}, weights_data.data()));

其他资源