ModelBox-AI应用开发:动物分类【玩转华为云】
ModelBox-AI应用开发:动物分类
一、准备环境
ModelBox端云协同AI开发套件(Windows)环境准备【视频教程】
二、应用开发
1. 创建工程
在ModelBox
sdk目录下使用create.bat
创建animal_mbv2
工程
(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t server -n animal_mbv2
(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH=
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME=
(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t server -n animal_mbv2
sdk version is modelbox-win10-x64-1.5.3
dos2unix: converting file D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2/graph\animal_mbv2.toml to Unix format...
dos2unix: converting file D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2/graph\modelbox.conf to Unix format...
dos2unix: converting file D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2/bin\mock_task.toml to Unix format...
success: create animal_mbv2 in D:\modelbox-win10-x64-1.5.3\workspace
create.bat
工具的参数中,-t
表示所创建实例的类型,包括server
(ModelBox
工程)、python
(Python功能单元)、c++
(C++功能单元)、infer
(推理功能单元)等;-n
表示所创建实例的名称,开发者自行命名。
2. 创建推理功能单元
在ModelBox
sdk目录下使用create.bat
创建mbv2_infer
推理功能单元
(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t infer -n mbv2_infer -p animal_mbv2
(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH=
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME=
(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t infer -n mbv2_infer -p animal_mbv2
sdk version is modelbox-win10-x64-1.5.3
success: create infer mbv2_infer in D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2/model/mbv2_infer
create.bat
工具使用时,-t infer
即表示创建的是推理功能单元;-n xxx_infer
表示创建的功能单元名称为xxx_infer
;-p animal_mbv2
表示所创建的功能单元属于animal_mbv2
应用。
a. 下载转换好的模型
运行此Notebook下载转换好的ONNX格式模型
b. 修改模型配置文件
模型和配置文件保持在同级目录下
# Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
[base]
name = "mbv2_infer"
device = "cpu"
version = "1.0.0"
description = "your description"
entry = "./mbv2.onnx" # model file path, use relative path
type = "inference"
virtual_type = "onnx" # inference engine type: win10 now only support onnx
group_type = "Inference" # flowunit group attribution, do not change
# Input ports description
[input]
[input.input1] # input port number, Format is input.input[N]
name = "Input" # input port name
type = "float" # input port data type ,e.g. float or uint8
device = "cpu" # input buffer type: cpu, win10 now copy input from cpu
# Output ports description
[output]
[output.output1] # output port number, Format is output.output[N]
name = "Output" # output port name
type = "float" # output port data type ,e.g. float or uint8
3. 创建预处理功能单元
在ModelBox
sdk目录下使用create.bat
创建image_preprocess
预处理功能单元
(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t python -n image_preprocess -p animal_mbv2
(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH=
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME=
(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t python -n image_preprocess -p animal_mbv2
sdk version is modelbox-win10-x64-1.5.3
success: create python image_preprocess in D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2/etc/flowunit/image_preprocess
a. 修改配置文件
# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
# Basic config
[base]
name = "image_preprocess" # The FlowUnit name
device = "cpu" # The flowunit runs on cpu
version = "1.0.0" # The version of the flowunit
type = "python" # Fixed value, do not change
description = "description" # The description of the flowunit
entry = "image_preprocess@image_preprocessFlowUnit" # Python flowunit entry function
group_type = "Generic" # flowunit group attribution, change as Input/Output/Image/Generic ...
# Flowunit Type
stream = false # Whether the flowunit is a stream flowunit
condition = false # Whether the flowunit is a condition flowunit
collapse = false # Whether the flowunit is a collapse flowunit
collapse_all = false # Whether the flowunit will collapse all the data
expand = false # Whether the flowunit is a expand flowunit
# The default Flowunit config
[config]
item = "value"
# Input ports description
[input]
[input.input1] # Input port number, the format is input.input[N]
name = "in_data" # Input port name
type = "float" # Input port type
# Output ports description
[output]
[output.output1] # Output port number, the format is output.output[N]
name = "out_data" # Output port name
type = "float" # Output port type
b. 补充逻辑代码
# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import _flowunit as modelbox
import numpy as np
import base64
import json
import cv2
class image_preprocessFlowUnit(modelbox.FlowUnit):
# Derived from modelbox.FlowUnit
def __init__(self):
super().__init__()
def open(self, config):
# Open the flowunit to obtain configuration information
return modelbox.Status.StatusCode.STATUS_SUCCESS
def process(self, data_context):
# Process the data
in_data = data_context.input("in_data")
out_data = data_context.output("out_data")
# image_preprocess process code.
# Remove the following code and add your own code here.
for buffer in in_data:
# get image from request body
request_body = json.loads(buffer.as_object().strip(chr(0)))
if request_body.get("image_base64"):
img_base64 = request_body["image_base64"]
img_file = base64.b64decode(img_base64)
# reshape img
img_data = cv2.imdecode(np.fromstring(img_file, np.uint8), cv2.IMREAD_COLOR)
img_data = cv2.resize(img_data, (224, 224))
infer_data = np.float32(img_data) / 127.5 - 1
# build buffer
add_buffer = modelbox.Buffer(self.get_bind_device(), infer_data)
out_data.push_back(add_buffer)
else:
error_msg = "wrong key of request_body"
modelbox.error(error_msg)
add_buffer = modelbox.Buffer(self.get_bind_device(), "")
add_buffer.set_error("ImagePreprocess.BadRequest", error_msg)
out_data.push_back(add_buffer)
return modelbox.Status.StatusCode.STATUS_SUCCESS
def close(self):
# Close the flowunit
return modelbox.Status()
def data_pre(self, data_context):
# Before streaming data starts
return modelbox.Status()
def data_post(self, data_context):
# After streaming data ends
return modelbox.Status()
def data_group_pre(self, data_context):
# Before all streaming data starts
return modelbox.Status()
def data_group_post(self, data_context):
# After all streaming data ends
return modelbox.Status()
4. 创建后处理功能单元
在ModelBox
sdk目录下使用create.bat
创建mbv2_post
后处理功能单元
(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t python -n mbv2_post -p animal_mbv2
(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH=
(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME=
(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t python -n mbv2_post -p animal_mbv2
sdk version is modelbox-win10-x64-1.5.3
success: create python mbv2_post in D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2/etc/flowunit/mbv2_post
a. 修改配置文件
# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
# Basic config
[base]
name = "mbv2_post" # The FlowUnit name
device = "cpu" # The flowunit runs on cpu
version = "1.0.0" # The version of the flowunit
type = "python" # Fixed value, do not change
description = "description" # The description of the flowunit
entry = "mbv2_post@mbv2_postFlowUnit" # Python flowunit entry function
group_type = "Generic" # flowunit group attribution, change as Input/Output/Image/Generic ...
# Flowunit Type
stream = false # Whether the flowunit is a stream flowunit
condition = false # Whether the flowunit is a condition flowunit
collapse = false # Whether the flowunit is a collapse flowunit
collapse_all = false # Whether the flowunit will collapse all the data
expand = false # Whether the flowunit is a expand flowunit
# The default Flowunit config
[config]
num_classes = 90
# Input ports description
[input]
[input.input1] # Input port number, the format is input.input[N]
name = "in_feat" # Input port name
type = "float" # Input port type
# Output ports description
[output]
[output.output1] # Output port number, the format is output.output[N]
name = "out_data" # Output port name
type = "string" # Output port type
b. 修改逻辑代码
# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import _flowunit as modelbox
import numpy as np
import json
class mbv2_postFlowUnit(modelbox.FlowUnit):
# Derived from modelbox.FlowUnit
def __init__(self):
super().__init__()
def open(self, config):
# Open the flowunit to obtain configuration information
self.params = {}
self.params['num_classes'] = config.get_int('num_classes')
return modelbox.Status.StatusCode.STATUS_SUCCESS
def process(self, data_context):
# Process the data
in_feat = data_context.input("in_feat")
out_data = data_context.output("out_data")
# mbv2_post process code.
# Remove the following code and add your own code here.
for buffer_feat in in_feat:
feat_data = np.array(buffer_feat.as_object(), copy=False)
result = np.argmax(feat_data)
result = {"det_result": str(result)}
result_str = json.dumps(result)
out_buffer = modelbox.Buffer(self.get_bind_device(), result_str)
out_data.push_back(out_buffer)
return modelbox.Status.StatusCode.STATUS_SUCCESS
def close(self):
# Close the flowunit
return modelbox.Status()
def data_pre(self, data_context):
# Before streaming data starts
return modelbox.Status()
def data_post(self, data_context):
# After streaming data ends
return modelbox.Status()
def data_group_pre(self, data_context):
# Before all streaming data starts
return modelbox.Status()
def data_group_post(self, data_context):
# After all streaming data ends
return modelbox.Status()
5. 修改流程图
animal_mbv2
工程graph
目录下存放流程图,默认的流程图animal_mbv2.toml
与工程同名,其内容为(以Windows版ModelBox
为例):
# Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
[driver]
dir = ["${HILENS_APP_ROOT}/etc/flowunit",
"${HILENS_APP_ROOT}/etc/flowunit/cpp",
"${HILENS_APP_ROOT}/model",
"${HILENS_MB_SDK_PATH}/flowunit"]
skip-default = true
[profile]
profile=false
trace=false
dir="${HILENS_DATA_DIR}/mb_profile"
[graph]
format = "graphviz"
graphconf = """digraph animal_mbv2 {
node [shape=Mrecord]
queue_size = 4
batch_size = 1
input1[type=input,flowunit=input,device=cpu,deviceid=0]
httpserver_sync_receive[type=flowunit, flowunit=httpserver_sync_receive_v2, device=cpu, deviceid=0, time_out_ms=5000, endpoint="http://0.0.0.0:8083/v1/animal_mbv2", max_requests=100]
image_preprocess[type=flowunit, flowunit=image_preprocess, device=cpu, deviceid=0]
mbv2_infer[type=flowunit, flowunit=mbv2_infer, device=cpu, deviceid=0, batch_size=1]
mbv2_post[type=flowunit, flowunit=mbv2_post, device=cpu, deviceid=0]
httpserver_sync_reply[type=flowunit, flowunit=httpserver_sync_reply_v2, device=cpu, deviceid=0]
input1:input -> httpserver_sync_receive:in_url
httpserver_sync_receive:out_request_info -> image_preprocess:in_data
image_preprocess:out_data -> mbv2_infer:Input
mbv2_infer:Output -> mbv2_post:in_feat
mbv2_post:out_data -> httpserver_sync_reply:in_reply_info
}"""
[flow]
desc = "animal_mbv2 run in modelbox-win10-x64"
6. 准备动物图片和测试脚本
a. 动物图片
animal_mbv2
工程data
目录下存放动物图片文件夹test_imgs
b. 测试脚本
animal_mbv2
工程data
目录下存放测试脚本test_http.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
import os
import cv2
import json
import base64
import http.client
class HttpConfig:
'''http调用的参数配置'''
def __init__(self, host_ip, port, url, img_base64_str):
self.hostIP = host_ip
self.Port = port
self.httpMethod = "POST"
self.requstURL = url
self.headerdata = {
"Content-Type": "application/json"
}
self.test_data = {
"image_base64": img_base64_str
}
self.body = json.dumps(self.test_data)
def read_image(img_path):
'''读取图片数据并转为base64编码的字符串'''
img_data = cv2.imread(img_path, 0)
img_str = cv2.imencode('.jpg', img_data)[1].tostring()
img_bin = base64.b64encode(img_str)
img_base64_str = str(img_bin, encoding='utf8')
return img_data, img_base64_str
def decode_result_str(result_str):
try:
result = json.loads(json.loads(result_str)['det_result'])
except Exception as ex:
print(str(ex))
return []
else:
return result
names = ['antelope', 'badger', 'bat', 'bear', 'bee', 'beetle', 'bison',
'boar', 'butterfly', 'cat', 'caterpillar', 'chimpanzee',
'cockroach', 'cow', 'coyote', 'crab', 'crow', 'deer', 'dog',
'dolphin', 'donkey', 'dragonfly', 'duck', 'eagle', 'elephant',
'flamingo', 'fly', 'fox', 'goat', 'goldfish', 'goose', 'gorilla',
'grasshopper', 'hamster', 'hare', 'hedgehog', 'hippopotamus',
'hornbill', 'horse', 'hummingbird', 'hyena', 'jellyfish',
'kangaroo', 'koala', 'ladybugs', 'leopard', 'lion', 'lizard',
'lobster', 'mosquito', 'moth', 'mouse', 'octopus', 'okapi',
'orangutan', 'otter', 'owl', 'ox', 'oyster', 'panda', 'parrot',
'pelecaniformes', 'penguin', 'pig', 'pigeon', 'porcupine',
'possum', 'raccoon', 'rat', 'reindeer', 'rhinoceros', 'sandpiper',
'seahorse', 'seal', 'shark', 'sheep', 'snake', 'sparrow', 'squid',
'squirrel', 'starfish', 'swan', 'tiger', 'turkey', 'turtle',
'whale', 'wolf', 'wombat', 'woodpecker', 'zebra']
def test_image(img_path, ip, port, url):
'''单张图片测试'''
img_data, img_base64_str = read_image(img_path)
http_config = HttpConfig(ip, port, url, img_base64_str)
conn = http.client.HTTPConnection(host=http_config.hostIP, port=http_config.Port)
conn.request(method=http_config.httpMethod, url=http_config.requstURL,
body=http_config.body, headers=http_config.headerdata)
response = conn.getresponse().read().decode()
print('response: ', response)
result = decode_result_str(response)
print(names[result])
if __name__ == "__main__":
port = 8083
ip = "127.0.0.1"
url = "/v1/animal_mbv2"
img_folder = './test_imgs'
file_list = os.listdir(img_folder)
for img_file in file_list:
print("\n================ {} ================".format(img_file))
img_path = os.path.join(img_folder, img_file)
test_image(img_path, ip, port, url)
三、运行应用
在animal_mbv2
工程目录下执行.\bin\main.bat
运行应用:
(tensorflow) PS D:\modelbox-win10-x64-1.5.3> cd D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2
(tensorflow) PS D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2> .\bin\main.bat
(tensorflow) D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2>set PATH=D:/modelbox-win10-x64-1.5.3/workspace/animal_mbv2/bin/../../../python-embed;D:/modelbox-win10-x64-1.5.3/workspace/animal_mbv2/bin/../../../modelbox-win10-x64/bin;D:/modelbox-win10-x64-1.5.3/workspace/animal_mbv2/bin/../dependence/lib;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin
(tensorflow) D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2>modelbox.exe -c D:/modelbox-win10-x64-1.5.3/workspace/animal_mbv2/bin/../graph/modelbox.conf
[2024-06-07 03:58:55,435][ WARN][ iva_config.cc:143 ] update vas url failed. Fault, no vas projectid or iva endpoint
open log file D:/modelbox-win10-x64-1.5.3/workspace/animal_mbv2/bin/../hilens_data_dir/log/modelbox.log failed, No error
input dims is:1,224,224,3,
output dims is:1,90,
HTTP服务启动后可以在另一个终端进行请求测试,进入animal_mbv2
工程目录data
文件夹中使用test_http.py
脚本发起HTTP请求进行测试:
(tensorflow) PS D:\modelbox-win10-x64-1.5.3> cd D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2\data
(tensorflow) PS D:\modelbox-win10-x64-1.5.3\workspace\animal_mbv2\data> python .\test_http.py
================ Abyssinian_1.jpg ================
.\test_http.py:33: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
img_str = cv2.imencode('.jpg', img_data)[1].tostring()
response: {"det_result": "9"}
cat
================ saint_bernard_143.jpg ================
response: {"det_result": "18"}
dog
四、小结
本章我们介绍了如何使用ModelBox开发一个动物图片分类的AI应用,我们只需要准备模型文件以及简单的配置即可创建一个HTTP服务。同时我们可以了解到CNN网络的基本结构、数据处理和模型训练方法,以及对应的推理应用逻辑。
- 点赞
- 收藏
- 关注作者
评论(0)