From d9c04133b152d22470f4a7c92b2fcff16ea45ffa Mon Sep 17 00:00:00 2001 From: zhuzizck <9637583+zhuzizck@user.noreply.gitee.com> Date: Mon, 30 Aug 2021 02:32:26 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=A7=E5=9B=BE?= =?UTF-8?q?=E5=88=86=E5=9D=97=E6=8F=92=E4=BB=B6=E6=A0=B7=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlockSeqmentationAndInfer.txt | 48 +++++++ .../pipeline/Sample.pipeline | 92 ++++++++++++ .../python/BlockSeqmentationAndInfer.py | 135 ++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt create mode 100644 tutorials/BlockSeqmentationAndInfer/pipeline/Sample.pipeline create mode 100644 tutorials/BlockSeqmentationAndInfer/python/BlockSeqmentationAndInfer.py diff --git a/tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt b/tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt new file mode 100644 index 000000000..1dd5a1ffb --- /dev/null +++ b/tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt @@ -0,0 +1,48 @@ +样例说明: +本样例将元数据(metadata)传入分块插件(mxpi_roigenerator)进行分块操作,对分块进行推理后,合并推理结果并返回。 +在运行目录下新建test_data_unet目录,按照2018 Data Science Bowl中stage1_train数据集格式准备数据并拷贝到目录下,在运行目录下执行run.sh +图片推理成功则会在当前目录下生成output目录,该目录下有对应图片的推理结果图片,推理结果图片为上传源图片经过分块、推理、合并分块推理结果后的输出结果。 + + +使用说明: +1. 模型准备: + ●前往昇腾社区获取U-Net Medical模型,单击“下载模型脚本”和“下载模型”,下载所需软件包。网址为:https://www.hiascend.com/zh/software/modelzoo/detail/C/5dd93b6b54df4e2ab02b4cfe13883784 +2. 安装MindX SDK开发套件: + ●下载路径:https://www.hiascend.com/software/mindx-sdk/sdk-detail + ● 配套版本:2.0.3 + 安装操作请参见《mxManufacture用户指南》 +3. 准备模型推理所需文件 + ● 进入模型所在目录,并新建“unet_simple”文件夹 + cd /home/HwHiAiUser/mxManufacture/samples/mxManufacture/models/ + mkdir unet_simple + ●拷贝推理所需文件到新建的unet_simple文件夹下 + root@ubuntu:/home/HwHiAiUser/mxManufacture/samples/mxManufacture/models# ll unet_simple/ + -rw-r--r-- 1 root root 25 Mar 22 07:11 unet_simple.cfg # 推理结果后处理所需的标签文件 + -rw-r--r-- 1 root root 17 Mar 22 07:11 unet_simple.names # 推理所需的标签文件,通过“下载模型脚本”获取 + -rw-r--r-- 1 root root 15926050 Mar 29 14:51 unet_simple_opencv.om # 推理所需的om文件,可通过“模型转换”生成或“下载模型”获取 + 配置文件unet_simple.cfg内容如下: + CLASS_NUM=2 + MODEL_TYPE=0 + unet_simple.names是标签文件,内容如下: + background + foreground +4. 进入“/home/HwHiAiUser/mxManufacture/samples/mxManufacture/pipeline”目录,放入Sample.pipeline文件 + 请根据实际情况修改图片规格、模型路径、配置文件路径和标签路径。更多介绍请参见《mxManufacture 用户指南》中“基础开发”章节。 +5. 运行推理服务: + ●进入“/home/HwHiAiUser/mxManufacture/samples/mxManufacture/python”目录,放入BlockSeqmentationAndInfer.py文件。 + ●在目录下新建test_data_unet文件夹,按照以下文件结构准备数据 + 文件结构如下 + test_data_unet + |__1 + | |__images + | | |__ 1.png + | | + | |__masks + | |__ 1.png + | |__ ... + |__... + ●打开run.sh文件,倒数第二行的 main.py 替换成BlockSeqmentationAndInfer.py。 + ●在当前目录下执行命令,“./run.sh” +6. 查看结果: + ●在当前目录下会自动创建output文件夹,其中有图片经过分块推理合并后的结果图。 + \ No newline at end of file diff --git a/tutorials/BlockSeqmentationAndInfer/pipeline/Sample.pipeline b/tutorials/BlockSeqmentationAndInfer/pipeline/Sample.pipeline new file mode 100644 index 000000000..1db310798 --- /dev/null +++ b/tutorials/BlockSeqmentationAndInfer/pipeline/Sample.pipeline @@ -0,0 +1,92 @@ +{ + "segmentation": { + "stream_config": { + "deviceId": "1" + }, + "appsrc0": { + "props": { + "blocksize": "409600" + }, + "factory": "appsrc", + "next": "mxpi_imagedecoder0" + }, + "mxpi_imagedecoder0": { + "props": { + "cvProcessor": "opencv" + }, + "factory": "mxpi_imagedecoder", + "next": "mxpi_block0" + }, + "mxpi_block0": { + "props":{ + "blockHeight" : "200", + "blockWidth" : "200", + "chessboardHeight":"1", + "chessboardWidth":"1", + "splitType":"Num_Block", + "overlapHeight":"100", + "overlapWidth":"100" + }, + "factory": "mxpi_roigenerator", + "next":["tee0", "mxpi_mergeblock0:2"] + }, + "tee0": { + "factory": "tee", + "next": ["mxpi_imagecrop0", "mxpi_mergeblock0:1"] + }, + "mxpi_imagecrop0": { + "props": { + "cvProcessor": "opencv" + }, + "factory":"mxpi_imagecrop", + "next":"mxpi_imageresize0" + }, + "mxpi_imageresize0": { + "props": { + "handleMethod": "opencv", + "resizeType": "Resizer_Stretch", + "resizeHeight": "96", + "resizeWidth": "96" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0": { + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/unet_simple/unet_simple_opencv.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_modelpostprocessor0" + }, +"mxpi_modelpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "dataSourceRoiBoxes":"mxpi_block0_0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/unet_simple/unet_simple.cfg", + "labelPath": "../models/unet_simple/unet_simple.names", + "postProcessLibPath": "libunetmindsporepostprocess.so" + }, + "factory": "mxpi_semanticsegpostprocessor", + "next": "mxpi_mergeblock0:0" + }, + "mxpi_mergeblock0": { + "factory": "mxpi_semanticsegstitcher", + "next": "mxpi_dumpdata0" + }, + "mxpi_dumpdata0": { + "props": { + "requiredMetaDataKeys": "mxpi_tensorinfer0" + }, + "factory": "mxpi_dumpdata", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} \ No newline at end of file diff --git a/tutorials/BlockSeqmentationAndInfer/python/BlockSeqmentationAndInfer.py b/tutorials/BlockSeqmentationAndInfer/python/BlockSeqmentationAndInfer.py new file mode 100644 index 000000000..1041281ca --- /dev/null +++ b/tutorials/BlockSeqmentationAndInfer/python/BlockSeqmentationAndInfer.py @@ -0,0 +1,135 @@ +# coding: utf-8 +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# less required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import argparse +import base64 +import json +import os + +import cv2 +import numpy as np +from StreamManagerApi import MxDataInput +from StreamManagerApi import StreamManagerApi + + +def _parse_arg(): + parser = argparse.ArgumentParser(description="SDK infer") + parser.add_argument("-d", "--dataset", type=str, + default="./test_data_unet", + help="Specify the directory of dataset") + parser.add_argument("-p", "--pipeline", type=str, + default="../pipeline/Sample.pipeline", + help="Specify the path of pipeline file") + parser.add_argument("-o", "--output", type=str, + default="./output", help="Specify the infer output directory") + return parser.parse_args() + + +def _get_dataset(dataset_dir): + img_ids = sorted(next(os.walk(dataset_dir))[1]) + for img_id in img_ids: + img_path = os.path.join(dataset_dir, img_id) + yield (img_id, + os.path.join(img_path, "images", f"{img_id}.png"), + os.path.join(img_path, "masks")) + + +def _process_mask(mask_dir): + mask = [] + for mask_path in os.listdir(mask_dir): + mask_cur = cv2.imread(os.path.join(mask_dir, mask_path), + cv2.IMREAD_GRAYSCALE) + mask.append(mask_cur) + mask = np.max(mask, axis=0) + return mask + + +def _get_stream_manager(pipeline_path): + stream_mgr_api = StreamManagerApi() + ret = stream_mgr_api.InitManager() + if ret != 0: + print(f"Failed to init Stream manager, ret={ret}") + exit(1) + + with open(pipeline_path, 'rb') as f: + pipeline_content = f.read() + + ret = stream_mgr_api.CreateMultipleStreams(pipeline_content) + if ret != 0: + print(f"Failed to create stream, ret={ret}") + exit(1) + return stream_mgr_api + + +def _do_infer_image(stream_mgr_api, image_path): + stream_name = b'segmentation' + data_input = MxDataInput() + with open(image_path, 'rb') as f: + data_input.data = f.read() + + unique_id = stream_mgr_api.SendData(stream_name, 0, data_input) + if unique_id < 0: + print("Failed to send data to stream.") + exit(1) + + infer_result = stream_mgr_api.GetResult(stream_name, 0,10000) + if infer_result.errorCode != 0: + print(f"GetResult error. errorCode={infer_result.errorCode}," + f"errorMsg={infer_result.data.decode()}") + exit(1) + + infer_result_data = json.loads(infer_result.data.decode()) + content = json.loads(infer_result_data['metaData'][0]['content']) + tensor_vec = content['tensorPackageVec'][0]['tensorVec'][0] + data_str = tensor_vec['dataStr'] + tensor_shape = tensor_vec['tensorShape'] + infer_array = np.frombuffer(base64.b64decode(data_str), dtype=np.float32) + return infer_array.reshape(tensor_shape) + + +def _save_output(output_dir, image_id, infer_image, mask_image): + if not output_dir: + return + + infer_image = np.squeeze(infer_image, axis=0) + infer_image = infer_image.transpose(2, 0, 1)[1] + infer_image = cv2.resize(infer_image, mask_image.shape[::-1], + interpolation=cv2.INTER_CUBIC) + + os.makedirs(output_dir, exist_ok=True) + infer_image_path = os.path.join(output_dir, f"{image_id}_infer.png") + mask_image_path = os.path.join(output_dir, f"{image_id}_mask.png") + cv2.imwrite(infer_image_path, + np.uint8(infer_image > 0.5).astype(np.uint8) * 255) + + +def main(_args): + streamManagerApi = StreamManagerApi() + streamManagerApi.InitManager() + with open(_args.pipeline, 'rb') as f: + pipelineStr = f.read() + streamManagerApi.CreateMultipleStreams(pipelineStr) + #stream_mgr_api = _get_stream_manager(_args.pipeline) + for image_id, image_path, mask_dir in _get_dataset(_args.dataset): + infer_image = _do_infer_image(streamManagerApi, image_path) + mask_image = _process_mask(mask_dir) + _save_output(_args.output, image_id, infer_image, mask_image) + streamManagerApi.DestroyAllStreams() + + +if __name__ == "__main__": + args = _parse_arg() + main(args) -- Gitee From c908a2281390203f8aa451ef500278ddb2c47831 Mon Sep 17 00:00:00 2001 From: zhuzizck <9637583+zhuzizck@user.noreply.gitee.com> Date: Mon, 30 Aug 2021 02:38:06 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\277\347\224\250\350\257\264\346\230\216.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt => "tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" (100%) diff --git a/tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt "b/tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" similarity index 100% rename from tutorials/BlockSeqmentationAndInfer/BlockSeqmentationAndInfer.txt rename to "tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" -- Gitee From 5f82447311213c3270534b3d61d3c936c578b42e Mon Sep 17 00:00:00 2001 From: zhuzizck <9637583+zhuzizck@user.noreply.gitee.com> Date: Mon, 30 Aug 2021 02:40:56 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0readme.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlockSeqmentationAndInfer/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" => tutorials/BlockSeqmentationAndInfer/readme.md (100%) diff --git "a/tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" b/tutorials/BlockSeqmentationAndInfer/readme.md similarity index 100% rename from "tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" rename to tutorials/BlockSeqmentationAndInfer/readme.md -- Gitee From 6c42fb3b3a728383c55053181ae415a7d3846e53 Mon Sep 17 00:00:00 2001 From: zhuzizck <9637583+zhuzizck@user.noreply.gitee.com> Date: Mon, 30 Aug 2021 02:43:40 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/BlockSeqmentationAndInfer/readme.md | 42 ------------------- 1 file changed, 42 deletions(-) diff --git a/tutorials/BlockSeqmentationAndInfer/readme.md b/tutorials/BlockSeqmentationAndInfer/readme.md index 1dd5a1ffb..fada2fd91 100644 --- a/tutorials/BlockSeqmentationAndInfer/readme.md +++ b/tutorials/BlockSeqmentationAndInfer/readme.md @@ -3,46 +3,4 @@ 在运行目录下新建test_data_unet目录,按照2018 Data Science Bowl中stage1_train数据集格式准备数据并拷贝到目录下,在运行目录下执行run.sh 图片推理成功则会在当前目录下生成output目录,该目录下有对应图片的推理结果图片,推理结果图片为上传源图片经过分块、推理、合并分块推理结果后的输出结果。 - -使用说明: -1. 模型准备: - ●前往昇腾社区获取U-Net Medical模型,单击“下载模型脚本”和“下载模型”,下载所需软件包。网址为:https://www.hiascend.com/zh/software/modelzoo/detail/C/5dd93b6b54df4e2ab02b4cfe13883784 -2. 安装MindX SDK开发套件: - ●下载路径:https://www.hiascend.com/software/mindx-sdk/sdk-detail - ● 配套版本:2.0.3 - 安装操作请参见《mxManufacture用户指南》 -3. 准备模型推理所需文件 - ● 进入模型所在目录,并新建“unet_simple”文件夹 - cd /home/HwHiAiUser/mxManufacture/samples/mxManufacture/models/ - mkdir unet_simple - ●拷贝推理所需文件到新建的unet_simple文件夹下 - root@ubuntu:/home/HwHiAiUser/mxManufacture/samples/mxManufacture/models# ll unet_simple/ - -rw-r--r-- 1 root root 25 Mar 22 07:11 unet_simple.cfg # 推理结果后处理所需的标签文件 - -rw-r--r-- 1 root root 17 Mar 22 07:11 unet_simple.names # 推理所需的标签文件,通过“下载模型脚本”获取 - -rw-r--r-- 1 root root 15926050 Mar 29 14:51 unet_simple_opencv.om # 推理所需的om文件,可通过“模型转换”生成或“下载模型”获取 - 配置文件unet_simple.cfg内容如下: - CLASS_NUM=2 - MODEL_TYPE=0 - unet_simple.names是标签文件,内容如下: - background - foreground -4. 进入“/home/HwHiAiUser/mxManufacture/samples/mxManufacture/pipeline”目录,放入Sample.pipeline文件 - 请根据实际情况修改图片规格、模型路径、配置文件路径和标签路径。更多介绍请参见《mxManufacture 用户指南》中“基础开发”章节。 -5. 运行推理服务: - ●进入“/home/HwHiAiUser/mxManufacture/samples/mxManufacture/python”目录,放入BlockSeqmentationAndInfer.py文件。 - ●在目录下新建test_data_unet文件夹,按照以下文件结构准备数据 - 文件结构如下 - test_data_unet - |__1 - | |__images - | | |__ 1.png - | | - | |__masks - | |__ 1.png - | |__ ... - |__... - ●打开run.sh文件,倒数第二行的 main.py 替换成BlockSeqmentationAndInfer.py。 - ●在当前目录下执行命令,“./run.sh” -6. 查看结果: - ●在当前目录下会自动创建output文件夹,其中有图片经过分块推理合并后的结果图。 \ No newline at end of file -- Gitee From c5f0d70661577c1b50cea24fd70897b332432d57 Mon Sep 17 00:00:00 2001 From: zhuzizck <9637583+zhuzizck@user.noreply.gitee.com> Date: Mon, 30 Aug 2021 02:44:38 +0000 Subject: [PATCH 5/6] update tutorials/BlockSeqmentationAndInfer/readme.md. --- tutorials/BlockSeqmentationAndInfer/readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/tutorials/BlockSeqmentationAndInfer/readme.md b/tutorials/BlockSeqmentationAndInfer/readme.md index fada2fd91..1028bd2c1 100644 --- a/tutorials/BlockSeqmentationAndInfer/readme.md +++ b/tutorials/BlockSeqmentationAndInfer/readme.md @@ -1,4 +1,5 @@ 样例说明: +=== 本样例将元数据(metadata)传入分块插件(mxpi_roigenerator)进行分块操作,对分块进行推理后,合并推理结果并返回。 在运行目录下新建test_data_unet目录,按照2018 Data Science Bowl中stage1_train数据集格式准备数据并拷贝到目录下,在运行目录下执行run.sh 图片推理成功则会在当前目录下生成output目录,该目录下有对应图片的推理结果图片,推理结果图片为上传源图片经过分块、推理、合并分块推理结果后的输出结果。 -- Gitee From de73324d9f9d626fec1d2576756a657774f2b9d5 Mon Sep 17 00:00:00 2001 From: zhuzizck <9637583+zhuzizck@user.noreply.gitee.com> Date: Mon, 30 Aug 2021 02:45:51 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\347\224\250\350\257\264\346\230\216.txt" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" diff --git "a/tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" "b/tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" new file mode 100644 index 000000000..44744b5a0 --- /dev/null +++ "b/tutorials/BlockSeqmentationAndInfer/\344\275\277\347\224\250\350\257\264\346\230\216.txt" @@ -0,0 +1,42 @@ +使用说明: +1. 模型准备: + ●前往昇腾社区获取U-Net Medical模型,单击“下载模型脚本”和“下载模型”,下载所需软件包。网址为:https://www.hiascend.com/zh/software/modelzoo/detail/C/5dd93b6b54df4e2ab02b4cfe13883784 +2. 安装MindX SDK开发套件: + ●下载路径:https://www.hiascend.com/software/mindx-sdk/sdk-detail + ● 配套版本:2.0.3 + 安装操作请参见《mxManufacture用户指南》 +3. 准备模型推理所需文件 + ● 进入模型所在目录,并新建“unet_simple”文件夹 + cd /home/HwHiAiUser/mxManufacture/samples/mxManufacture/models/ + mkdir unet_simple + ●拷贝推理所需文件到新建的unet_simple文件夹下 + root@ubuntu:/home/HwHiAiUser/mxManufacture/samples/mxManufacture/models# ll unet_simple/ + -rw-r--r-- 1 root root 25 Mar 22 07:11 unet_simple.cfg # 推理结果后处理所需的标签文件 + -rw-r--r-- 1 root root 17 Mar 22 07:11 unet_simple.names # 推理所需的标签文件,通过“下载模型脚本”获取 + -rw-r--r-- 1 root root 15926050 Mar 29 14:51 unet_simple_opencv.om # 推理所需的om文件,可通过“模型转换”生成或“下载模型”获取 + 配置文件unet_simple.cfg内容如下: + CLASS_NUM=2 + MODEL_TYPE=0 + unet_simple.names是标签文件,内容如下: + background + foreground +4. 进入“/home/HwHiAiUser/mxManufacture/samples/mxManufacture/pipeline”目录,放入Sample.pipeline文件 + 请根据实际情况修改图片规格、模型路径、配置文件路径和标签路径。更多介绍请参见《mxManufacture 用户指南》中“基础开发”章节。 +5. 运行推理服务: + ●进入“/home/HwHiAiUser/mxManufacture/samples/mxManufacture/python”目录,放入BlockSeqmentationAndInfer.py文件。 + ●在目录下新建test_data_unet文件夹,按照以下文件结构准备数据 + 文件结构如下 + test_data_unet + |__1 + | |__images + | | |__ 1.png + | | + | |__masks + | |__ 1.png + | |__ ... + |__... + ●打开run.sh文件,倒数第二行的 main.py 替换成BlockSeqmentationAndInfer.py。 + ●在当前目录下执行命令,“./run.sh” +6. 查看结果: + ●在当前目录下会自动创建output文件夹,其中有图片经过分块推理合并后的结果图。 + \ No newline at end of file -- Gitee