Update tutorials for encryption model

This commit is contained in:
felixhjh
2023-02-10 05:06:20 +00:00
parent 5160771a1c
commit 9d8b084155
3 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
English | [中文](README_CN.md)
# FastDeploy generates an encrypted model
This directory provides `encrypt.py` to quickly complete the encryption of the model and parameter files of ResNet50_vd
## encryption
```bash
# Download deployment example code
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy/tutorials/encrypt_model
# Download the ResNet50_vd model file
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
tar -xvf ResNet50_vd_infer.tgz
python encrypt.py --model ResNet50_vd_infer
```
>> **Note** After the encryption is completed, the ResNet50_vd_infer_encrypt folder will be generated, including `__model__.encrypted`, `__params__.encrypted`, `encryption_key.txt` three files, where `encryption_key.txt` contains the encrypted key. At the same time, you need to copy the `inference_cls.yaml` configuration file in the original folder to the ResNet50_vd_infer_encrypt folder for subsequent deployment
### Python encryption interface
Use the encrypted interface through the following interface settings
```python
import fastdeploy as fd
import os
# when key is not given, key will be automatically generated.
# otherwise, the file will be encrypted by specific key
encrypted_model, key = fd.encryption.encrypt(model_file.read())
encrypted_params, key= fd.encryption.encrypt(params_file.read(), key)
```
### FastDeploy deployment encryption model (decryption)
Through the setting of the following interface, FastDeploy can deploy the encryption model
```python
import fastdeploy as fd
option = fd.RuntimeOption()
option.set_encryption_key(key)
```
```C++
fastdeploy::RuntimeOption option;
option.SetEncryptionKey(key)
```
>> **Note** For more details about RuntimeOption, please refer to [RuntimeOption Python Documentation](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/python/html/runtime_option.html), [ RuntimeOption C++ Documentation](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/cpp/html/structfastdeploy_1_1RuntimeOption.html)

View File

@@ -0,0 +1,48 @@
[English](README.md) | 中文
# 使用FastDeploy生成加密模型
本目录下提供`encrypt.py`快速完成ResNet50_vd的模型和参数文件加密
FastDeploy支持对称加密的方案通过调用OpenSSL中的对称加密算法AES对模型进行加密并产生密钥
## 加密
```bash
#下载加密示例代码
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy/tutorials/encrypt_model
# 下载ResNet50_vd模型文件
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
tar -xvf ResNet50_vd_infer.tgz
python encrypt.py --model ResNet50_vd_infer
```
>> **注意** 加密完成后会生成ResNet50_vd_infer_encrypt文件夹包含`__model__.encrypted`,`__params__.encrypted`,`encryption_key.txt`三个文件,其中`encryption_key.txt`包含加密后的秘钥,同时需要将原文件夹中的、`inference_cls.yaml`配置文件 拷贝至ResNet50_vd_infer_encrypt文件夹以便后续部署使用
### Python加密接口
通过如下接口的设定,使用加密接口(解密)
```python
import fastdeploy as fd
import os
# when key is not given, key will be automatically generated.
# otherwise, the file will be encrypted by specific key
encrypted_model, key = fd.encryption.encrypt(model_file.read())
encrypted_params, key= fd.encryption.encrypt(params_file.read(), key)
```
### FastDeploy 部署加密模型
通过如下接口的设定,完成加密模型的推理
```python
import fastdeploy as fd
option = fd.RuntimeOption()
option.set_encryption_key(key)
```
```C++
fastdeploy::RuntimeOption option;
option.SetEncryptionKey(key)
```
>> **注意** RuntimeOption的更多详细信息请参考[RuntimeOption Python文档](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/python/html/runtime_option.html)[RuntimeOption C++文档](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/cpp/html/structfastdeploy_1_1RuntimeOption.html)

View File

@@ -0,0 +1,33 @@
import fastdeploy as fd
import os
def parse_arguments():
import argparse
import ast
parser = argparse.ArgumentParser()
parser.add_argument(
"--model", required=True, help="Path of model directory.")
return parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
model_file = os.path.join(args.model, "inference.pdmodel")
params_file = os.path.join(args.model, "inference.pdiparams")
config_file = os.path.join(args.model, "inference_cls.yaml")
model_buffer = open(model_file, 'rb')
params_buffer = open(params_file, 'rb')
encrypted_model, key = fd.encryption.encrypt(model_buffer.read())
encrypted_params, key= fd.encryption.encrypt(params_buffer.read(), key)
encrypted_model_dir = args.model + "_encrypt"
model_buffer.close()
params_buffer.close()
os.mkdir(encrypted_model_dir)
with open(os.path.join(encrypted_model_dir, "__model__.encrypted"), "w") as f:
f.write(encrypted_model)
with open(os.path.join(encrypted_model_dir, "__params__.encrypted"), "w") as f:
f.write(encrypted_params)
with open(os.path.join(encrypted_model_dir, "encryption_key.txt"), "w") as f:
f.write(key)
print("encryption success")