diff --git a/tutorials/encrypt_model/README.md b/tutorials/encrypt_model/README.md index 8a49c107c..755671686 100644 --- a/tutorials/encrypt_model/README.md +++ b/tutorials/encrypt_model/README.md @@ -6,7 +6,7 @@ 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 @@ -14,7 +14,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 ``` >> **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 @@ -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) \ No newline at end of file +>> **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) diff --git a/tutorials/encrypt_model/README_CN.md b/tutorials/encrypt_model/README_CN.md index 8230f68e6..c2f80ffd4 100644 --- a/tutorials/encrypt_model/README_CN.md +++ b/tutorials/encrypt_model/README_CN.md @@ -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文件夹,以便后续部署使用 @@ -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) \ No newline at end of file +>> **注意** 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) diff --git a/tutorials/encrypt_model/encrypt.py b/tutorials/encrypt_model/encrypt.py index 380509042..f4d80ed2f 100644 --- a/tutorials/encrypt_model/encrypt.py +++ b/tutorials/encrypt_model/encrypt.py @@ -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") \ No newline at end of file + print("encryption key: ", key) + print("encryption success")