Add some args in encrypt.py

This commit is contained in:
felixhjh
2023-02-13 02:35:55 +00:00
parent 341258a02e
commit bf61f84d22
3 changed files with 31 additions and 17 deletions

View File

@@ -14,7 +14,7 @@ cd FastDeploy/tutorials/encrypt_model
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
tar -xvf 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 >> **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

View File

@@ -16,7 +16,7 @@ cd FastDeploy/tutorials/encrypt_model
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
tar -xvf 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文件夹以便后续部署使用 >> **注意** 加密完成后会生成ResNet50_vd_infer_encrypt文件夹包含`__model__.encrypted`,`__params__.encrypted`,`encryption_key.txt`三个文件,其中`encryption_key.txt`包含加密后的秘钥,同时需要将原文件夹中的、`inference_cls.yaml`配置文件 拷贝至ResNet50_vd_infer_encrypt文件夹以便后续部署使用

View File

@@ -1,33 +1,47 @@
import fastdeploy as fd import fastdeploy as fd
import os import os
def parse_arguments(): def parse_arguments():
import argparse import argparse
import ast import ast
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( 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() return parser.parse_args()
if __name__ == "__main__": if __name__ == "__main__":
args = parse_arguments() args = parse_arguments()
model_file = os.path.join(args.model, "inference.pdmodel") model_buffer = open(args.model_file, 'rb')
params_file = os.path.join(args.model, "inference.pdiparams") params_buffer = open(args.params_file, 'rb')
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_model, key = fd.encryption.encrypt(model_buffer.read())
encrypted_params, key= fd.encryption.encrypt(params_buffer.read(), key) # use the same key to encrypt parameter file
encrypted_model_dir = args.model + "_encrypt" 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() model_buffer.close()
params_buffer.close() params_buffer.close()
os.mkdir(encrypted_model_dir) 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) 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) 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) f.write(key)
print("encryption key: ", key)
print("encryption success") print("encryption success")