diff --git a/official/cv/deeplabv3/infer/Dockerfile b/official/cv/deeplabv3/infer/Dockerfile index 487c5864ffbdc30aa951e5226a32114056e1fe11..b0f31f18f55f18e79e93a9bbafdc18e29ad9553b 100644 --- a/official/cv/deeplabv3/infer/Dockerfile +++ b/official/cv/deeplabv3/infer/Dockerfile @@ -1,5 +1,6 @@ ARG FROM_IMAGE_NAME FROM ${FROM_IMAGE_NAME} +USER root COPY requirements.txt . RUN pip3 install -r requirements.txt diff --git a/official/cv/deeplabv3/infer/convert/convert_om.sh b/official/cv/deeplabv3/infer/convert/convert_om.sh index 39cd519422de53f0d5174f7dc49eab9d834c333a..0ec6c789ca077b518e9fd7cded606ca24b28f14c 100644 --- a/official/cv/deeplabv3/infer/convert/convert_om.sh +++ b/official/cv/deeplabv3/infer/convert/convert_om.sh @@ -34,5 +34,6 @@ atc --framework=1 \ --model=$model_path \ --output=$output_model_name \ --fusion_switch_file=fusion_switch.cfg \ + --precision_mode=allow_fp32_to_fp16 \ --op_select_implmode=high_precision \ --soc_version=Ascend310 diff --git a/official/cv/deeplabv3/infer/data/config/deeplabv3.pipeline b/official/cv/deeplabv3/infer/data/config/deeplabv3.pipeline index af4c94d6808ecc4a81b9e2b033f28a7caf18a2f5..84685018e855a90333eaeaaf89adb7cb572ecb18 100644 --- a/official/cv/deeplabv3/infer/data/config/deeplabv3.pipeline +++ b/official/cv/deeplabv3/infer/data/config/deeplabv3.pipeline @@ -3,52 +3,12 @@ "stream_config": { "deviceId": "0" }, - "mxpi_imagedecoder0": { - "props": { - "handleMethod": "opencv" - }, - "factory": "mxpi_imagedecoder", - "next": "mxpi_imageresize0" - }, - "mxpi_imageresize0": { - "props": { - "handleMethod": "opencv", - "resizeType": "Resizer_KeepAspectRatio_Long", - "scaleValue": "513" - }, - "factory": "mxpi_imageresize", - "next": "opencv_normalize0" - }, - "opencv_normalize0": { - "props": { - "alpha": "123.675, 116.28, 103.53", - "beta": "58.395, 57.120, 57.375", - "dataType": "FLOAT32" - }, - "factory": "mxpi_imagenormalize", - "next": "mxpi_imageresize1" - }, - "mxpi_imageresize1": { - "props": { - "handleMethod": "opencv", - "resizeType": "Resizer_OnlyPadding", - "scaleValue": "513", - "paddingType": "Padding_RightDown", - "paddingHeight": "513", - "paddingWidth": "513", - "paddingColorB": "0", - "paddingColorG": "0", - "paddingColorR": "0" - }, - "factory": "mxpi_imageresize", - "next": "mxpi_tensorinfer0" - }, "mxpi_tensorinfer0": { "props": { - "dataSource": "mxpi_imageresize1", + "dataSource": "appsrc0", "modelPath": "../data/model/deeplabv3.om" }, - "factory": "mxpi_modelinfer", + "factory": "mxpi_tensorinfer", "next": "mxpi_semanticsegpostprocessor0" }, "mxpi_semanticsegpostprocessor0": { @@ -73,7 +33,7 @@ "blocksize": "409600" }, "factory": "appsrc", - "next": "mxpi_imagedecoder0" + "next": "mxpi_tensorinfer0" }, "appsink0": { "props": { diff --git a/official/cv/deeplabv3/infer/sdk/main.py b/official/cv/deeplabv3/infer/sdk/main.py index 31c434af2291000e453504003d5fb2dfd86d00f2..b55446f0a8c2e432bbd9865dbed0d8333d7f6135 100644 --- a/official/cv/deeplabv3/infer/sdk/main.py +++ b/official/cv/deeplabv3/infer/sdk/main.py @@ -20,8 +20,9 @@ import json import os import cv2 import numpy as np -from StreamManagerApi import MxDataInput from StreamManagerApi import StreamManagerApi +from StreamManagerApi import InProtobufVector, MxProtobufIn, MxDataInput +import MxpiDataType_pb2 as MxpiDataType from get_dataset_colormap import label_to_color_image PIPELINE_PATH = "../data/config/deeplabv3.pipeline" @@ -58,16 +59,35 @@ def _init_stream(pipeline_path): return stream_manager_api -def _do_infer(stream_manager_api, data_input): +def _do_infer(stream_manager_api, data_input, origin_img): stream_name = b'segmentation' - unique_id = stream_manager_api.SendDataWithUniqueId( - stream_name, 0, data_input) + vision_list = MxpiDataType.MxpiVisionList() + vision_vec = vision_list.visionVec.add() + vision_vec.visionInfo.format = 0 + vision_vec.visionInfo.width = origin_img.shape[1] + vision_vec.visionInfo.height = origin_img.shape[0] + vision_vec.visionInfo.widthAligned = origin_img.shape[1] + vision_vec.visionInfo.heightAligned = origin_img.shape[0] + + vision_vec.visionData.memType = 0 + + vision_vec.visionData.dataStr = data_input.tobytes() + vision_vec.visionData.dataSize = len(data_input) + + protobuf = MxProtobufIn() + protobuf.key = b"appsrc0" + protobuf.type = b'MxTools.MxpiVisionList' + protobuf.protobuf = vision_list.SerializeToString() + protobuf_vec = InProtobufVector() + + protobuf_vec.push_back(protobuf) + unique_id = stream_manager_api.SendProtobuf(stream_name, 0, protobuf_vec) + if unique_id < 0: raise RuntimeError("Failed to send data to stream.") - timeout = 3000 - infer_result = stream_manager_api.GetResultWithUniqueId( - stream_name, unique_id, timeout) + infer_result = stream_manager_api.GetResult(stream_name, unique_id) + if infer_result.errorCode != 0: raise RuntimeError( "GetResultWithUniqueId error, errorCode=%d, errorMsg=%s" % ( @@ -80,6 +100,37 @@ def _do_infer(stream_manager_api, data_input): return np.frombuffer(data_str, dtype=np.uint8).reshape(shape) +def resize_long(img, long_size=513): + h, w, _ = img.shape + if h > w: + new_h = long_size + new_w = int(1.0 * long_size * w / h) + else: + new_w = long_size + new_h = int(1.0 * long_size * h / w) + imo = cv2.resize(img, (new_w, new_h)) + return imo + + +def pre_process(img_, crop_size=513): + image_mean = [103.53, 116.28, 123.675] + image_std = [57.375, 57.120, 58.395] + # resize + img_ = resize_long(img_, crop_size) + + # mean, std + image_mean = np.array(image_mean) + image_std = np.array(image_std) + img_ = (img_ - image_mean) / image_std + + # pad to crop_size + pad_h = crop_size - img_.shape[0] + pad_w = crop_size - img_.shape[1] + if pad_h > 0 or pad_w > 0: + img_ = cv2.copyMakeBorder(img_, 0, pad_h, 0, pad_w, cv2.BORDER_CONSTANT, value=0) + + return img_ + def main(): args = _parse_args() # init stream manager @@ -97,9 +148,15 @@ def main(): img_path = os.path.join(args.data_root, img_path) msk_path = os.path.join(args.data_root, msk_path) msk_ = cv2.imread(msk_path, cv2.IMREAD_GRAYSCALE) - with open(img_path, 'rb') as f: - data_input.data = f.read() - each_array = _do_infer(stream_manager_api, data_input) + img_ = cv2.imread(img_path) + re_img = img_ + + crop_size = 513 + img_ = pre_process(img_, crop_size) + + data_input = img_.astype("float32") + + each_array = _do_infer(stream_manager_api, data_input, re_img) hist += _cal_hist( msk_.flatten(), each_array.flatten(), args.num_classes) @@ -116,6 +173,5 @@ def main(): stream_manager_api.DestroyAllStreams() - if __name__ == '__main__': main()