Skip to content

Commit

Permalink
Add some args in encrypt.py
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhjh committed Feb 13, 2023
1 parent 341258a commit bf61f84
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions tutorials/encrypt_model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ This directory provides `encrypt.py` to quickly complete the encryption of the m

## encryption
```bash
# Download deployment example code
# 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
python encrypt.py --model_file ResNet50_vd_infer/inference.pdmodel --params_file ResNet50_vd_infer/inference.pdiparams --encrypted_model_dir ResNet50_vd_infer_encrypt
```
>> **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
Expand Down Expand Up @@ -43,4 +43,4 @@ option.set_encryption_key(key)
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)
>> **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)
4 changes: 2 additions & 2 deletions tutorials/encrypt_model/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cd FastDeploy/tutorials/encrypt_model
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
tar -xvf ResNet50_vd_infer.tgz

python encrypt.py --model ResNet50_vd_infer
python encrypt.py --model_file ResNet50_vd_infer/inference.pdmodel --params_file ResNet50_vd_infer/inference.pdiparams --encrypted_model_dir ResNet50_vd_infer_encrypt
```
>> **注意** 加密完成后会生成ResNet50_vd_infer_encrypt文件夹,包含`__model__.encrypted`,`__params__.encrypted`,`encryption_key.txt`三个文件,其中`encryption_key.txt`包含加密后的秘钥,同时需要将原文件夹中的、`inference_cls.yaml`配置文件 拷贝至ResNet50_vd_infer_encrypt文件夹,以便后续部署使用
Expand Down Expand Up @@ -45,4 +45,4 @@ option.set_encryption_key(key)
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)
>> **注意** 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)
38 changes: 26 additions & 12 deletions tutorials/encrypt_model/encrypt.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
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.")
"--encrypted_model_dir",
required=False,
help="Path of model directory.")
parser.add_argument(
"--model_file", required=True, help="Path of model file directory.")
parser.add_argument(
"--params_file",
required=True,
help="Path of parameters file 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')
model_buffer = open(args.model_file, 'rb')
params_buffer = open(args.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"
# use the same key to encrypt parameter file
encrypted_params, key = fd.encryption.encrypt(params_buffer.read(), key)
encrypted_model_dir = "encrypt_model_dir"
if args.encrypted_model_dir:
encrypted_model_dir = args.encrypted_model_dir
model_buffer.close()
params_buffer.close()
os.mkdir(encrypted_model_dir)
with open(os.path.join(encrypted_model_dir, "__model__.encrypted"), "w") as f:
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:
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:
with open(os.path.join(encrypted_model_dir, "encryption_key.txt"),
"w") as f:
f.write(key)
print("encryption success")
print("encryption key: ", key)
print("encryption success")

0 comments on commit bf61f84

Please sign in to comment.