├── .gitignore ├── Dockerfile ├── README.md ├── app ├── app_compat.py ├── index.html └── start.sh ├── deploy └── ImageStyleTransfer │ ├── hook │ └── index.js │ ├── publish.yaml │ ├── readme.md │ ├── readme_en.md │ ├── src │ └── s.yaml │ └── version.md ├── httpapi_test.py ├── main.py ├── result.png ├── run_batch_push.sh ├── run_build.sh └── test.png /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM registry.cn-hongkong.aliyuncs.com/duolabmeng/pyrun:paddlehub222 2 | 3 | COPY app /app 4 | WORKDIR /app 5 | 6 | ADD /app/app_compat.py /usr/local/lib/python3.7/site-packages/paddlehub/serving/app_compat.py 7 | RUN hub install animegan_v2_hayao_99 8 | 9 | EXPOSE 9000 10 | CMD ["/bin/bash","-c","hub serving start -m animegan_v2_hayao_99 -p 9000"] 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 基于 Serverless 图像转换为宫崎骏动漫风格案例 2 | 3 | 大家可以通过本项目提供的镜像,快速发布成可调用的Restful API服务。 4 | 5 | # animegan_v2_hayao_99 6 | 7 | [使用模型 animegan_v2_hayao_99](https://github.com/PaddlePaddle/PaddleHub/tree/release/v2.2/modules/image/Image_gan/style_transfer/animegan_v2_hayao_99) 8 | 9 | |模型名称|animegan_v2_hayao_99| 10 | | :--- | :---: | 11 | |类别|图像 - 图像生成| 12 | |网络|AnimeGAN| 13 | |数据集|The Wind Rises| 14 | |是否支持Fine-tuning|否| 15 | |模型大小|9.4MB| 16 | |最新更新日期|2021-07-30| 17 | |数据指标|-| 18 | 19 | ## 一、模型基本信息 20 | 21 | - ### 应用效果展示 22 | - 样例结果示例: 23 |

24 | 25 |
26 | 输入图像 27 |
28 | 29 |
30 | 输出图像 31 |
32 |

33 | 34 | 35 | - ### 模型介绍 36 | 37 | - AnimeGAN V2 图像风格转换模型, 模型可将输入的图像转换成宫崎骏动漫风格,模型权重转换自[AnimeGAN V2官方开源项目](https://github.com/TachibanaYoshino/AnimeGANv2)。 38 | 39 | # 部署方法 40 | 41 | # 1. 在阿里云函数计算应用中心里立即创建 42 | 43 | [阿里云Serverless 应用中心一键体验 ](https://fcnext.console.aliyun.com/applications/create?template=paddleImageStyleTransfer) 44 | 45 | # 2. 终端上输入命令创建 46 | 47 | ```shell 48 | 49 | s init paddleImageStyleTransfer # 初始化项目 50 | s deploy # 部署项目 51 | 52 | ``` 53 | 54 | # 调用方法 55 | 56 | ```python 57 | import requests 58 | import json 59 | import cv2 60 | import base64 61 | 62 | 63 | def cv2_to_base64(image): 64 | data = cv2.imencode('.jpg', image)[1] 65 | return base64.b64encode(data.tostring()).decode('utf8') 66 | 67 | 68 | def getResult(imagePath): 69 | data = json.dumps({'images': [cv2_to_base64(cv2.imread(imagePath))]}) 70 | r = requests.post("http://127.0.0.1:9000/predict/animegan_v2_hayao_99", data=data, 71 | headers={'Content-Type': 'application/json'}) 72 | return r.json()["results"] 73 | 74 | 75 | print(getResult("./test.png")) 76 | ``` 77 | 78 | # 调用失败的请看这里 79 | 80 | * 500x300 分辨率 需要 7秒 2.6gb内存 所以函数设置 3gb 运行内存才可以 81 | * 720x500 分辨率 需要 15秒 5.76gb内存 所以函数设置 8gb 运行内存才可以 82 | * 2000x1280 分辨率 需要 30秒 6.77g内存 所以设置 8gb 运行内存才可以 83 | 84 | 我是直接设置了16gb内存测试的 85 | 86 | 87 | # 本应用的镜像开发教程 88 | 89 | https://github.com/duolabmeng6/paddlehub_ppocr 90 | 91 | 阅读本文你将学会: 92 | 93 | 在 Serverless 架构中 docker 镜像制作的最佳实践,游刃有余的部署复杂场景下的深度学习模型 94 | 95 | 熟练的使用各厂商提供的 Serverless 服务,部署。 96 | 97 | 制作小巧精良的 docker 镜像 98 | 99 | # 常用命令 100 | ```shell 101 | s cli registry login # 登录授权 一次就行 102 | s cli registry publish # 发布包 103 | s cli registry list # 查看子机已发布的包 104 | 105 | s init paddleImageStyleTransfer # 自己测试应用的效果 106 | s deploy # 部署项目试试 107 | ``` -------------------------------------------------------------------------------- /app/app_compat.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License" 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import traceback 17 | import time 18 | 19 | from flask import Flask, request 20 | 21 | from paddlehub.serving.model_service.base_model_service import cv_module_info 22 | from paddlehub.serving.model_service.base_model_service import nlp_module_info 23 | from paddlehub.serving.model_service.base_model_service import v2_module_info 24 | from paddlehub.utils import utils, log 25 | 26 | filename = 'HubServing-%s.log' % time.strftime("%Y_%m_%d", time.localtime()) 27 | 28 | 29 | def package_result(status: str, msg: str, data: dict): 30 | ''' 31 | Package message of response. 32 | 33 | Args: 34 | status(str): Error code 35 | ======== ============================================================================================== 36 | Code Meaning 37 | -------- ---------------------------------------------------------------------------------------------- 38 | '000' Return results normally 39 | '101' An error occurred in the predicting method 40 | '111' Module is not available 41 | '112' Use outdated and abandoned HTTP protocol format 42 | ======== =============================================================================================== 43 | msg(str): Detailed info for error 44 | data(dict): Result of predict api. 45 | 46 | Returns: 47 | dict: Message of response 48 | 49 | Examples: 50 | .. code-block:: python 51 | 52 | data = {'result': 0.002} 53 | package_result(status='000', msg='', data=data) 54 | ''' 55 | return {"status": status, "msg": msg, "results": data} 56 | 57 | 58 | def predict_v2(module_info: dict, input: dict): 59 | ''' 60 | 61 | Predict with `serving` API of module. 62 | 63 | Args: 64 | module_info(dict): Module info include module name, method name and 65 | other info. 66 | input(dict): Data to input to predict API. 67 | 68 | Returns: 69 | dict: Response after packaging by func `package_result` 70 | 71 | Examples: 72 | .. code-block:: python 73 | 74 | module_info = {'module_name': 'lac'}} 75 | data = {'text': ['今天天气很好']} 76 | predict_v2(module_info=module_info, input=data) 77 | ''' 78 | serving_method_name = module_info["method_name"] 79 | serving_method = getattr(module_info["module"], serving_method_name) 80 | predict_args = module_info["predict_args"].copy() 81 | predict_args.update(input) 82 | 83 | for item in serving_method.__code__.co_varnames: 84 | if item in module_info.keys(): 85 | predict_args.update({item: module_info[item]}) 86 | try: 87 | output = serving_method(**predict_args) 88 | except Exception as err: 89 | log.logger.error(traceback.format_exc()) 90 | return package_result("101", str(err), "") 91 | 92 | return package_result("000", "", output) 93 | 94 | 95 | def create_app(init_flag: bool = False, configs: dict = None): 96 | ''' 97 | Start one flask instance and ready for HTTP requests. 98 | 99 | Args: 100 | init_flag(bool): Whether the instance need to be initialized with 101 | `configs` or not 102 | configs(dict): Module configs for initializing. 103 | 104 | Returns: 105 | One flask instance. 106 | 107 | Examples: 108 | .. code-block:: python 109 | 110 | create_app(init_flag=False, configs=None) 111 | ''' 112 | if init_flag is False: 113 | if configs is None: 114 | raise RuntimeError("Lack of necessary configs.") 115 | config_with_file(configs) 116 | 117 | app_instance = Flask(__name__) 118 | app_instance.config["JSON_AS_ASCII"] = False 119 | app_instance.logger = log.get_file_logger(filename) 120 | 121 | @app_instance.route("/", methods=["GET", "POST"]) 122 | def index(): 123 | ''' 124 | Provide index page. 125 | ''' 126 | return open('/app/index.html').read() 127 | 128 | return '暂不提供可视化界面,请直接使用脚本进行请求。
No visual ' \ 129 | 'interface is provided for the time being, please use the' \ 130 | ' python script to make a request directly.' 131 | 132 | @app_instance.before_request 133 | def before_request(): 134 | ''' 135 | Add id info to `request.data` before request. 136 | ''' 137 | request.data = {"id": utils.md5(request.remote_addr + str(time.time()))} 138 | 139 | @app_instance.route("/predict/", methods=["POST"]) 140 | def predict_serving_v2(module_name: str): 141 | ''' 142 | Http api for predicting. 143 | 144 | Args: 145 | module_name(str): Module name for predicting. 146 | 147 | Returns: 148 | Result of predicting after packaging. 149 | ''' 150 | if module_name in v2_module_info.modules: 151 | module_info = v2_module_info.get_module_info(module_name) 152 | else: 153 | msg = "Module {} is not available.".format(module_name) 154 | return package_result("111", msg, "") 155 | inputs = request.json 156 | if inputs is None: 157 | results = "This usage is out of date, please use 'application/json' as content-type to post to /predict/%s. See 'https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/tutorial/serving.md' for more details." % ( 158 | module_name) 159 | return package_result("112", results, "") 160 | 161 | results = predict_v2(module_info, inputs) 162 | return results 163 | 164 | return app_instance 165 | 166 | 167 | def config_with_file(configs: dict): 168 | ''' 169 | Config `cv_module_info` and `nlp_module_info` by configs. 170 | 171 | Args: 172 | configs(dict): Module info and configs 173 | 174 | Examples: 175 | .. code-block:: python 176 | 177 | configs = {'lac': {'version': 1.0.0, 'category': nlp}} 178 | config_with_file(configs=configs) 179 | ''' 180 | for key, value in configs.items(): 181 | if "CV" == value["category"]: 182 | cv_module_info.add_module(key, {key: value}) 183 | elif "NLP" == value["category"]: 184 | nlp_module_info.add_module(key, {key: value}) 185 | v2_module_info.add_module(key, {key: value}) 186 | logger = log.get_file_logger(filename) 187 | logger.info("%s==%s" % (key, value["version"])) 188 | 189 | 190 | def run(configs: dict = None, port: int = 8866): 191 | ''' 192 | Run flask instance for PaddleHub-Serving 193 | 194 | Args: 195 | configs(dict): module info and configs 196 | port(int): the port of the webserver 197 | 198 | Examples: 199 | .. code-block:: python 200 | 201 | configs = {'lac': {'version': 1.0.0, 'category': nlp}} 202 | run(configs=configs, port=8866) 203 | ''' 204 | logger = log.get_file_logger(filename) 205 | if configs is not None: 206 | config_with_file(configs) 207 | else: 208 | logger.error("Start failed cause of missing configuration.") 209 | return 210 | my_app = create_app(init_flag=True) 211 | my_app.run(host="0.0.0.0", port=port, debug=False, threaded=False) 212 | log.logger.info("PaddleHub-Serving has been stopped.") 213 | -------------------------------------------------------------------------------- /app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 基于 Serverless 图像转换为宫崎骏动漫风格案例 6 | 7 | 40 | 41 | 42 |
43 |
44 |
45 |
46 |

47 | 基于 Serverless 图像转换为宫崎骏动漫风格案例 48 |

49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | 说明: 58 | 本应用基于 Serverless Devs 进行部署,可通过 Serverless 应用中心一键体验。 59 |
60 |
61 | 本应用的使用帮助基于 Serverless 图像转换为宫崎骏动漫风格案例图像转换为宫崎骏动漫风格案例 62 |
接口文档:
63 |

地址::

64 |

 65 |                 

参数:

66 |

 67 |                 

案例:

68 |

 69 |             
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 | 80 | 81 |
82 |
83 | 体验: 84 | 85 | 86 |
87 | 88 |
89 |
90 |
91 |
92 |
93 | 117 | 118 | -------------------------------------------------------------------------------- /app/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | hub serving start -m animegan_v2_hayao_99 -p 9000 -------------------------------------------------------------------------------- /deploy/ImageStyleTransfer/hook/index.js: -------------------------------------------------------------------------------- 1 | async function preInit(inputObj) { 2 | console.log("项目帮助 https://github.com/duolabmeng6/paddlehub_ppocr") 3 | } 4 | 5 | async function postInit(inputObj) { 6 | console.log("项目帮助 https://github.com/duolabmeng6/paddlehub_ppocr") 7 | } 8 | 9 | module.exports = { 10 | postInit, 11 | preInit 12 | } 13 | -------------------------------------------------------------------------------- /deploy/ImageStyleTransfer/publish.yaml: -------------------------------------------------------------------------------- 1 | # 规范文档参考: 2 | # 中文:https://github.com/Serverless-Devs/Serverless-Devs/blob/master/spec/zh/0.0.2/serverless_package_model/3.package_model.md#应用模型规范 3 | # English: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/spec/en/0.0.2/serverless_package_model/3.package_model.md#Application-model-specification 4 | # -------------- 5 | # Package 开发发布最佳实践: 6 | # - [社区讨论 #62](https://github.com/Serverless-Devs/Serverless-Devs/discussions/62); 7 | # - [社区讨论 #407](https://github.com/Serverless-Devs/Serverless-Devs/discussions/407); 8 | # - [社区讨论 #439](https://github.com/Serverless-Devs/Serverless-Devs/discussions/439); 9 | 10 | Type: Application 11 | Name: paddleImageStyleTransfer 12 | Provider: 13 | - 阿里云 14 | Version: 0.0.28 15 | Description: 本应用基于百度开源的 PaddlePaddle 项目,AnimeGAN V2 图像风格转换模型, 可将输入的图像转换成宫崎骏动漫风格。 16 | HomePage: https://github.com/duolabmeng6/paddleImageStyleTransfer 17 | Tags: #标签详情 18 | - 人工智能 19 | - 图像转换 20 | Category: 人工智能 # 基础云服务/Web框架/Web应用/人工智能/音视频处理/图文处理/监控告警/大数据/IoT/新手入门/其他 21 | Service: 22 | 函数计算: 23 | Authorities: 24 | - AliyunFCFullAccess 25 | - AliyunContainerRegistryFullAccess 26 | Runtime: custom container 27 | Parameters: 28 | type: object 29 | additionalProperties: false # 不允许增加其他属性 30 | required: # 必填项 31 | - region 32 | - serviceName 33 | - functionName 34 | - memorySize 35 | properties: 36 | region: 37 | title: 地域 38 | type: string 39 | default: cn-shenzhen 40 | description: 创建应用所在的地区 41 | enum: 42 | - cn-shenzhen 43 | - cn-beijing 44 | - cn-hangzhou 45 | - cn-shanghai 46 | - cn-qingdao 47 | - cn-zhangjiakou 48 | - cn-huhehaote 49 | - cn-chengdu 50 | - cn-hongkong 51 | serviceName: 52 | title: 服务名 53 | type: string 54 | default: paddleImageStyleTransfer 55 | description: 服务名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-128 之间 56 | functionName: 57 | title: 函数名 58 | type: string 59 | default: paddleImageStyleTransfer 60 | description: 函数名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-64 之间 61 | memorySize: 62 | title: 内存规格 63 | type: string 64 | default: 3072 65 | description: 内存规格,单位为 MB,默认为 3072 MB -------------------------------------------------------------------------------- /deploy/ImageStyleTransfer/readme.md: -------------------------------------------------------------------------------- 1 | # 基于 Serverless 图像转换为宫崎骏动漫风格案例 2 | 3 | 大家可以通过本项目提供的镜像,快速发布成可调用的Restful API服务。 4 | 5 | # 部署方法 6 | 7 | # 1. 在阿里云函数计算应用中心里立即创建 8 | 9 | [阿里云Serverless 应用中心一键体验 ](https://fcnext.console.aliyun.com/applications/create?template=paddleImageStyleTransfer) 10 | 11 | 12 | # 2. 终端上输入命令创建 13 | 14 | ```shell 15 | 16 | s init paddleImageStyleTransfer # 初始化项目 17 | s deploy # 部署项目 18 | 19 | ``` 20 | 21 | # 调用方法 22 | 23 | ```python 24 | import requests 25 | import json 26 | import cv2 27 | import base64 28 | 29 | 30 | def cv2_to_base64(image): 31 | data = cv2.imencode('.jpg', image)[1] 32 | return base64.b64encode(data.tostring()).decode('utf8') 33 | 34 | 35 | def getResult(imagePath): 36 | data = json.dumps({'images': [cv2_to_base64(cv2.imread(imagePath))]}) 37 | r = requests.post("http://127.0.0.1:9000/predict/animegan_v2_hayao_99", data=data, 38 | headers={'Content-Type': 'application/json'}) 39 | return r.json()["results"] 40 | 41 | 42 | print(getResult("./test.png")) 43 | ``` 44 | 45 | 46 | # 调用失败的请看这里 47 | 48 | * 500x300 分辨率 需要 7秒 2.6gb内存 所以函数设置 3gb 运行内存才可以 49 | * 720x500 分辨率 需要 15秒 5.76gb内存 所以函数设置 8gb 运行内存才可以 50 | * 2000x1280 分辨率 需要 30秒 6.77g内存 所以设置 8gb 运行内存才可以 51 | 52 | 我是直接设置了16gb内存测试的 53 | 54 | # 本应用的镜像开发教程 55 | 56 | https://github.com/duolabmeng6/paddlehub_ppocr 57 | 58 | 阅读本文你将学会: 59 | 60 | 在 Serverless 架构中 docker 镜像制作的最佳实践,游刃有余的部署复杂场景下的深度学习模型 61 | 62 | 熟练的使用各厂商提供的 Serverless 服务,部署。 63 | 64 | 制作小巧精良的 docker 镜像 -------------------------------------------------------------------------------- /deploy/ImageStyleTransfer/readme_en.md: -------------------------------------------------------------------------------- 1 | # 基于 Serverless 图像转换为宫崎骏动漫风格案例 2 | 3 | 大家可以通过本项目提供的镜像,快速发布成可调用的Restful API服务。 4 | 5 | # 部署方法 6 | 7 | # 1. 在阿里云函数计算应用中心里立即创建 8 | 9 | [阿里云Serverless 应用中心一键体验 ](https://fcnext.console.aliyun.com/applications/create?template=paddleImageStyleTransfer) 10 | 11 | 12 | # 2. 终端上输入命令创建 13 | 14 | ```shell 15 | 16 | s init paddleImageStyleTransfer # 初始化项目 17 | s deploy # 部署项目 18 | 19 | ``` 20 | 21 | # 调用方法 22 | 23 | ```python 24 | import requests 25 | import json 26 | import cv2 27 | import base64 28 | 29 | 30 | def cv2_to_base64(image): 31 | data = cv2.imencode('.jpg', image)[1] 32 | return base64.b64encode(data.tostring()).decode('utf8') 33 | 34 | 35 | def getResult(imagePath): 36 | data = json.dumps({'images': [cv2_to_base64(cv2.imread(imagePath))]}) 37 | r = requests.post("http://127.0.0.1:9000/predict/animegan_v2_hayao_99", data=data, 38 | headers={'Content-Type': 'application/json'}) 39 | return r.json()["results"] 40 | 41 | 42 | print(getResult("./test.png")) 43 | ``` 44 | 45 | 46 | # 调用失败的请看这里 47 | 48 | * 500x300 分辨率 需要 7秒 2.6gb内存 所以函数设置 3gb 运行内存才可以 49 | * 720x500 分辨率 需要 15秒 5.76gb内存 所以函数设置 8gb 运行内存才可以 50 | * 2000x1280 分辨率 需要 30秒 6.77g内存 所以设置 8gb 运行内存才可以 51 | 52 | 我是直接设置了16gb内存测试的 53 | 54 | # 本应用的镜像开发教程 55 | 56 | https://github.com/duolabmeng6/paddlehub_ppocr 57 | 58 | 阅读本文你将学会: 59 | 60 | 在 Serverless 架构中 docker 镜像制作的最佳实践,游刃有余的部署复杂场景下的深度学习模型 61 | 62 | 熟练的使用各厂商提供的 Serverless 服务,部署。 63 | 64 | 制作小巧精良的 docker 镜像 -------------------------------------------------------------------------------- /deploy/ImageStyleTransfer/src/s.yaml: -------------------------------------------------------------------------------- 1 | edition: 1.0.0 2 | name: web-framework-app 3 | access: "{{ access }}" 4 | 5 | vars: 6 | region: "{{ region }}" 7 | memorySize: "{{ memorySize }}" 8 | service: 9 | name: "{{ serviceName }}" 10 | description: '图像转换成宫崎骏动漫风格API 项目地址 https://github.com/duolabmeng6/paddleImageStyleTransfer' 11 | 12 | services: 13 | framework: 14 | component: fc 15 | props: 16 | region: ${vars.region} 17 | service: 18 | name: ${vars.service.name} 19 | description: ${vars.service.description} 20 | internetAccess: true 21 | role: 22 | name: paddlerole 23 | policies: 24 | - AliyunContainerRegistryReadOnlyAccess 25 | function: 26 | name: "{{ functionName }}" 27 | description: ${vars.service.description} 28 | runtime: custom-container 29 | memorySize: ${vars.memorySize} 30 | caPort: 9000 31 | codeUri: ./ 32 | timeout: 60 33 | customContainerConfig: 34 | image: registry-vpc.${vars.region}.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 35 | command: '["sh","/app/start.sh"]' 36 | environmentVariables: 37 | testEnv: true 38 | triggers: 39 | - name: httpTrigger 40 | type: http 41 | config: 42 | authType: anonymous 43 | methods: 44 | - GET 45 | - POST 46 | - PUT 47 | customDomains: 48 | - domainName: auto 49 | protocol: HTTP 50 | routeConfigs: 51 | - path: /* 52 | -------------------------------------------------------------------------------- /deploy/ImageStyleTransfer/version.md: -------------------------------------------------------------------------------- 1 | # 基于 Serverless 图像转换为宫崎骏动漫风格案例 2 | 3 | 大家可以通过本项目提供的镜像,快速发布成可调用的Restful API服务。 4 | 5 | # 部署方法 6 | 7 | # 1. 在阿里云函数计算应用中心里立即创建 8 | 9 | [阿里云Serverless 应用中心一键体验 ](https://fcnext.console.aliyun.com/applications/create?template=paddleImageStyleTransfer) 10 | 11 | 12 | # 2. 终端上输入命令创建 13 | 14 | ```shell 15 | 16 | s init paddleImageStyleTransfer # 初始化项目 17 | s deploy # 部署项目 18 | 19 | ``` 20 | 21 | # 调用方法 22 | 23 | ```python 24 | import requests 25 | import json 26 | import cv2 27 | import base64 28 | 29 | 30 | def cv2_to_base64(image): 31 | data = cv2.imencode('.jpg', image)[1] 32 | return base64.b64encode(data.tostring()).decode('utf8') 33 | 34 | 35 | def getResult(imagePath): 36 | data = json.dumps({'images': [cv2_to_base64(cv2.imread(imagePath))]}) 37 | r = requests.post("http://127.0.0.1:9000/predict/animegan_v2_hayao_99", data=data, 38 | headers={'Content-Type': 'application/json'}) 39 | return r.json()["results"] 40 | 41 | 42 | print(getResult("./test.png")) 43 | ``` 44 | 45 | 46 | # 调用失败的请看这里 47 | 48 | * 500x300 分辨率 需要 7秒 2.6gb内存 所以函数设置 3gb 运行内存才可以 49 | * 720x500 分辨率 需要 15秒 5.76gb内存 所以函数设置 8gb 运行内存才可以 50 | * 2000x1280 分辨率 需要 30秒 6.77g内存 所以设置 8gb 运行内存才可以 51 | 52 | 我是直接设置了16gb内存测试的 53 | 54 | # 本应用的镜像开发教程 55 | 56 | https://github.com/duolabmeng6/paddlehub_ppocr 57 | 58 | 阅读本文你将学会: 59 | 60 | 在 Serverless 架构中 docker 镜像制作的最佳实践,游刃有余的部署复杂场景下的深度学习模型 61 | 62 | 熟练的使用各厂商提供的 Serverless 服务,部署。 63 | 64 | 制作小巧精良的 docker 镜像 -------------------------------------------------------------------------------- /httpapi_test.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | import cv2 4 | import base64 5 | 6 | 7 | def cv2_to_base64(image): 8 | data = cv2.imencode('.jpg', image)[1] 9 | return base64.b64encode(data.tostring()).decode('utf8') 10 | 11 | 12 | def getResult(imagePath): 13 | data = json.dumps({'images': [cv2_to_base64(cv2.imread(imagePath))]}) 14 | r = requests.post("http://127.0.0.1:9000/predict/animegan_v2_hayao_99", data=data, 15 | headers={'Content-Type': 'application/json'}) 16 | return r.json()["results"] 17 | 18 | 19 | print(getResult("./test.png")) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import paddlehub as hub 2 | import cv2 3 | from pyefun import * 4 | 5 | model = hub.Module(name="animegan_v2_hayao_99") 6 | 7 | with 计时器() as t: 8 | result = model.style_transfer(images=[cv2.imread('test.png')]) 9 | cv2.imwrite('./result.png', result[0]) 10 | print(t.取总耗时()) 11 | -------------------------------------------------------------------------------- /result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/duolabmeng6/paddleImageStyleTransfer/c8968980de7e21d683709bbc3d6512bf9e5168e0/result.png -------------------------------------------------------------------------------- /run_batch_push.sh: -------------------------------------------------------------------------------- 1 | docker tag animegan_v2_hayao_99:1.0 registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 2 | docker push registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 3 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-beijing.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 4 | docker push registry.cn-beijing.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 5 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-hangzhou.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 6 | docker push registry.cn-hangzhou.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 7 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-shanghai.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 8 | docker push registry.cn-shanghai.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 9 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-qingdao.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 10 | docker push registry.cn-qingdao.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 11 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-zhangjiakou.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 12 | docker push registry.cn-zhangjiakou.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 13 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-huhehaote.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 14 | docker push registry.cn-huhehaote.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 15 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-chengdu.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 16 | docker push registry.cn-chengdu.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 17 | docker tag registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 registry.cn-hongkong.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 18 | docker push registry.cn-hongkong.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 -------------------------------------------------------------------------------- /run_build.sh: -------------------------------------------------------------------------------- 1 | docker build -f ./Dockerfile -t animegan_v2_hayao_99:1.0 . 2 | docker rm -f animegan_v2_hayao_99 3 | docker run -itd --name animegan_v2_hayao_99 -p 9022:9000 animegan_v2_hayao_99:1.0 4 | docker logs animegan_v2_hayao_99 5 | docker diff animegan_v2_hayao_99 6 | 7 | #没问题的话就可以执行推送命令 8 | docker tag animegan_v2_hayao_99:1.0 registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 9 | docker push registry.cn-shenzhen.aliyuncs.com/duolabmeng/animegan_v2_hayao_99:1.1 10 | 11 | 12 | -------------------------------------------------------------------------------- /test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/duolabmeng6/paddleImageStyleTransfer/c8968980de7e21d683709bbc3d6512bf9e5168e0/test.png --------------------------------------------------------------------------------