diff --git a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/README.md b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/README.md index 877f80b23d717bb64443d67c57b1e2cf50a3b79d..1cd3f20f31ff8fb247cf6a9be3a5b9ac5c4e2938 100644 --- a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/README.md +++ b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/README.md @@ -115,14 +115,42 @@ cd mmdetection bash ../test/train_full_8p.sh --data_path=./mmdetection/data/coco ``` +## FSAF Training Results + +| Acc@1 | FPS | # of NPU/GPU | Epochs | Opt-Level | Loss Scale | +| :------: | :------: | :------: | :------: | :------: | :------: | +| 36.0 | - | 8P GPU | 12 | FP32 | - | +| 9.4 | 11.39 | 1P GPU | 1 | O1 | dynamic | +| 37.5 | 70.84 | 8P GPU | 12 | O1 | dynamic | +| 10.3 | 1.26 | 1P NPU | 1 | O1 | 32.0 | +| 36.2 | 8.38 | 8P NPU | 12 | O1 | 32.0 | + + + ### Evaluation +环境: +``` +bash docker_start.sh ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:21.0.4 /home/manlin/FSAF_for_Pytorch_master /home/manlin/FSAF_for_Pytorch_master +192.168.99.107 +910B +version=22.0.2 +CANN:5.1.RC2 +镜像:ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:21.0.4 +``` ```sh +目前归档的模型为epoch_10.pth,下面在线评估和推理都用此模型验证 cd FSAF chmod +x ./test/train_eval_8p.sh chmod +x ./mmdetection/tools/dist_test.sh cd mmdetection -bash ../test/train_eval_8p.sh {你想测试的模型路径,可不传,默认为./work_dirs/fsaf_r50_fpn_1x_coco/latest.pth} +修改评估数据集路径: vim ./configs/_base_/datasets/coco_detection.py + data_root = '../coco2017/' +修改模型路径:vim ../test/train_eval_8p.sh + MODEL=${2-../epoch_10.pth} +bash ../test/train_eval_8p.sh --data_path=/home/manlin/coco2017/ +在线评估结果(epoch_10.pth) +Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.359 ``` ### Demo @@ -130,37 +158,116 @@ bash ../test/train_eval_8p.sh {你想测试的模型路径,可不传,默认 ```sh cd FSAF source ./test/env_npu.sh -python3.7.5 demo.py {the path of your FSAF checkpoint,可不传,默认为./mmdetection/work_dirs/fsaf_r50_fpn_1x_coco/latest.pth} +python3.7.5 demo.py {the path of your FSAF checkpoint,同上指定模型路径) ``` ### Exporting to the ONNX +``` +环境: +机器名:192.168.99.107 +启动另外一个容器 +vim docker_start.sh 修改成单卡即可 +bash docker_start.sh ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:21.0.4 /home/manlin/FSAF_for_Pytorch_master /home/manlin/FSAF_for_Pytorch_master + +下载拷贝ACL中代码 +https://gitee.com/ascend/ModelZoo-PyTorch/tree/master/ACL_PyTorch/contrib/cv/detection/Fsaf +cd Fsaf +先卸载自带opencv,mmcv +pip uninstall opencv +pip uninstall mmcv +安装依赖 +pip install install pyproject.toml +pip install protobuf +pip install cmake +pip install torch==1.8.1 +pip install torchvision +pip install onnx +pip install numpy==1.21.2 +pip install opencv-python==4.6.0.66 +pip install psutil +pip install absl-py +pip install tensorflow + +export GIT_SSL_NO_VERIFY=true +git clone https://github.com/open-mmlab/mmcv -b master +cd mmcv 版本为1.3.10如果安装完不是这个版本,需要重新安装 +git reset --hard 04daea425bcb0a104d8b4acbbc16bd31304cf168 +MMCV_WITH_OPS=1 pip3 install -e . + +安装mmdetection +cd .. +export GIT_SSL_NO_VERIFY=true +git clone https://github.com/open-mmlab/mmdetection -b master +cd mmdetection +git reset --hard 604bfe9618533949c74002a4e54f972e57ad0a7a +patch -p1 < ../fsaf.diff +pip install -r requirements/build.txt +pip install terminaltables +python3 setup.py develop + +python3 ./tools/deployment/pytorch2onnx.py ./configs/fsaf/fsaf_r50_fpn_1x_coco.py ./epoch_10.pth --output-file fsaf_acl_10.onnx --shape 800 1216 +转onnx若缺少lib2to3,需要将压缩包lib2to3.zip解压到对应的dist-packages目录下 +``` -We need to clone the MMDetection v2.11.0 for exporting FSAF checkpoint into the ONNX model due of some bugs in MMDetection v2.10.0 - -> 本次训练功能在 MMDetection v2.10 版本的基础上打通,经过测试,在 2.10 版本训练完成的 FSAF 权重文件可在 MMDetection v2.11.0 上正常推理,MMDetection v2.11.0 以上版本请自测。 -```sh -cd FSAF -source ./test/env_npu.sh -git clone https://github.com/open-mmlab/mmdetection.git mmdetection-2.11.0 -cd mmdetection-2.11.0 -git checkout 2894516 # 切换到 v2.11.0 版本 -python3.7.5 setup.py develop +##推理 +``` +环境: +机器:192.168.88.213 +镜像:ascendhub.huawei.com/public-ascendhub/infer-modelzoo 21.0.4 +Ascend 310 +启动推理容器进行推理 +``` +模型转换 +``` +cd ./infer/convert +bash atc.sh fsaf_acl_10.onnx ../convert/fsaf_acl_10.om ``` -```sh -# back to the path of the FSAF -cd FSAF -source ./test/env_npu.sh -python3.7.5 pytorch2onnx.py {config of FSAF,可不传,默认为./mmdetection/configs/fsaf/fsaf_r50_fpn_1x_coco.py} {the path of your FSAF checkpoint,可不传,默认为./mmdetection/work_dirs/fsaf_r50_fpn_1x_coco/latest.pth} +mxbase 推理 +``` +cd infer/mxbase +修改模型路径和数据集路径 +vim src/main.cpp +initParam.modelPath = 模型路径; +std::string imgPath = 数据集路径; + +新建output文件夹 +mkdir output +cd output +mkdir infer_result +mkdir show_result + +编译: +bash build.sh +./fsaf +开始评估: +python3 txt_to_json.py --infer_result_path ../mxbase/output/infer_result/ --json_output_filename ../mxbase/output/coco_detection_result +python3 coco_eval.py --ground_truth_file ../../../coco2017/annotations/instances_val2017.json --detection_result_file ../mxbase/output/coco_detection_result.json --eval_result_file ../mxbase/output/mx_eval_map_result.txt +mxbase精度: +Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.354 +``` +sdk 推理 +``` +安装相关依赖: +pip install imagecodecs +pip install cmake +pip install addict +pip install yapf + +cd sdk +新建output文件夹 +mkdir output +cd output +mkdir infer_result +mkdir show_result +启动 +bash run.sh ../../../coco2017/val2017/ /home/data/dzkd_mindx/manlin/coco2017/coco2017.info ../data/config/fsaf.pipeline ./output/infer_result/ ./output/show_result/ +开始评估: +python3 txt_to_json.py --infer_result_path ../sdk/output/infer_result/ --json_output_filename ../sdk/output/coco_detection_result +python3 coco_eval.py --ground_truth_file ../../../coco2017/annotations/instances_val2017.json --detection_result_file ../sdk/output/coco_detection_result.json --eval_result_file ../sdk/output/mx_eval_map_result.txt +sdk精度: +Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.354 ``` -## FSAF Training Results -| Acc@1 | FPS | # of NPU/GPU | Epochs | Opt-Level | Loss Scale | -| :------: | :------: | :------: | :------: | :------: | :------: | -| 36.0 | - | 8P GPU | 12 | FP32 | - | -| 9.4 | 11.39 | 1P GPU | 1 | O1 | dynamic | -| 37.5 | 70.84 | 8P GPU | 12 | O1 | dynamic | -| 10.3 | 1.26 | 1P NPU | 1 | O1 | 32.0 | -| 36.2 | 8.38 | 8P NPU | 12 | O1 | 32.0 | diff --git a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.cpp b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.cpp index 36b7c50fb852b488d86275a4e9af48438e048082..bc0f3591370aa91a7d7903ee566f2f856321bd34 100755 --- a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.cpp +++ b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.cpp @@ -1,427 +1,414 @@ -/* - * Copyright 2021 Huawei Technologies Co., Ltd - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - - * Unless 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. - */ - -#include "fsaf.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "MxBase/DeviceManager/DeviceManager.h" -#include "MxBase/DvppWrapper/DvppWrapper.h" -#include "MxBase/Log/Log.h" - -using namespace MxBase; - -// Complete all initialization work. When you are going to -// use FSAF class, you should invoke this method immediately. -// -// you need construct InitParam object for Init. -APP_ERROR FSAF::Init(const InitParam& initParam) { - // initialize device - APP_ERROR ret = MxBase::DeviceManager::GetInstance()->InitDevices(); - if (ret != APP_ERR_OK) { - LogError << "Init devices failed, ret=" << ret << "."; - return ret; - } - // initialize context - ret = MxBase::TensorContext::GetInstance()->SetContext(initParam.deviceId); - if (ret != APP_ERR_OK) { - LogError << "Set context failed, ret=" << ret << "."; - return ret; - } - - // load model - model_ = std::make_shared(); - ret = model_->Init(initParam.modelPath, modelDesc_); - if (ret != APP_ERR_OK) { - LogError << "ModelInferenceProcessor init failed, ret=" << ret << "."; - return ret; - } - - return APP_ERR_OK; -} - -APP_ERROR FSAF::DeInit() { - model_->DeInit(); - MxBase::DeviceManager::GetInstance()->DestroyDevices(); - return APP_ERR_OK; -} - -// Get all files under a directory. -// Parameters: -// dirPath: the directory path -// Return: -// a vector of filename, including the suffix. -std::vector FSAF::GetFileList(const std::string &dirPath) { - struct dirent *ptr; - DIR *dir = opendir(dirPath.c_str()); - std::vector files; - while ((ptr = readdir(dir)) != NULL) { - if (ptr->d_name[0] == '.') { - continue; - } - files.push_back(ptr->d_name); - } - closedir(dir); - return files; -} - -// Read image from a image path. -// Parameters: -// imgPath: string of image path -// imageMat: opencv Mat object, for storging image as matrix -// height: int, storge the image height -// width: int, storge the image width -// Return: -// APP_ERROR object, if read image successfully, return APP_ERR_OK. -APP_ERROR FSAF::ReadImage(const std::string& imgPath, cv::Mat& imageMat, int& height, int& width) { - imageMat = cv::imread(imgPath, cv::IMREAD_COLOR); - - // BGR -> RGB - cv::cvtColor(imageMat, imageMat, cv::COLOR_BGR2RGB); - - width = imageMat.cols; - height = imageMat.rows; - - return APP_ERR_OK; -} - -// Resize image to fix size. -// We use RightDown padding style. -// Parameters: -// srcImageMat: source opencv Mat object, image matrix -// dstImageMat: storge destination opencv Mat object -// resizedImageInfo: contain infomation about the image, including before and after scaling -//Return: -// APP_ERROR object, if resize image successfully, return APP_ERR_OK. -APP_ERROR FSAF::ResizeImage(const cv::Mat& srcImageMat, cv::Mat& dstImageMat, - MxBase::ResizedImageInfo& resizedImageInfo) { - float resizeHeight = 800; - float resizeWidth = 1216; - float scale = std::min(resizeWidth / srcImageMat.cols, resizeHeight / srcImageMat.rows); - int new_width = srcImageMat.cols * scale; - int new_height = srcImageMat.rows * scale; - - // calculate the padding - int pad_w = resizeWidth - new_width; - int pad_h = resizeHeight - new_height; - - resizedImageInfo.heightOriginal = srcImageMat.rows; - resizedImageInfo.heightResize = resizeHeight; - resizedImageInfo.widthOriginal = srcImageMat.cols; - resizedImageInfo.widthResize = resizeWidth; - resizedImageInfo.resizeType = RESIZER_MS_KEEP_ASPECT_RATIO; - - // invoke opencv method to resize and pad - cv::resize(srcImageMat, dstImageMat, cv::Size(new_width, new_height), 0, 0, cv::INTER_CUBIC); - cv::copyMakeBorder(dstImageMat, dstImageMat, 0, pad_h, 0, pad_w, - cv::BorderTypes::BORDER_CONSTANT, cv::Scalar(0, 0, 0)); - - return APP_ERR_OK; -} - -APP_ERROR FSAF::Normalize(const cv::Mat &srcImageMat, cv::Mat &dstImageMat) -{ - constexpr size_t ALPHA_AND_BETA_SIZE = 3; - cv::Mat float32Mat; - srcImageMat.convertTo(float32Mat, CV_32FC3); - - std::vector tmp; - cv::split(float32Mat, tmp); - - const std::vector mean = {103.53, 116.28, 123.675}; - const std::vector std = {57.375, 57.120, 58.395}; - for (size_t i = 0; i < ALPHA_AND_BETA_SIZE; ++i) { - tmp[i].convertTo(tmp[i], CV_32FC3, 1 / std[i], - mean[i] / std[i]); - } - cv::merge(tmp, dstImageMat); - return APP_ERR_OK; -} - -// Convert Mat to Tensor. -// Parameters: -// imageMat: input image matrix -// tensorBase: storge image as tensor -// Return: -// APP_ERROR object, if convert image successfully, return APP_ERR_OK. -APP_ERROR FSAF::CVMatToTensorBase(const cv::Mat &imageMat, MxBase::TensorBase &tensorBase) { - // calculate the data size: width * height * depth - const uint32_t dataSize = imageMat.cols * imageMat.rows * imageMat.channels(); - // allocate memory - MemoryData memoryDataDst(dataSize, MemoryData::MEMORY_DEVICE, deviceId_); - MemoryData memoryDataSrc(imageMat.data, dataSize, MemoryData::MEMORY_HOST_MALLOC); - - APP_ERROR ret = MemoryHelper::MxbsMallocAndCopy(memoryDataDst, memoryDataSrc); - if (ret != APP_ERR_OK) { - LogError << GetError(ret) << "Memory malloc failed."; - return ret; - } - // get tensor shape - // std::vector shape = {imageMat.rows * YUV444_RGB_WIDTH_NU, static_cast(imageMat.cols)}; - std::vector shape = { - static_cast(imageMat.rows), - static_cast(imageMat.cols), - static_cast(imageMat.channels())}; - - // tensorBase = TensorBase(memoryDataDst, false, shape, TENSOR_DTYPE_UINT8); - tensorBase = TensorBase(memoryDataDst, false, shape, TENSOR_DTYPE_FLOAT32); - return APP_ERR_OK; -} - -// Model inference. -// Parameters: -// inputs: input image tensor -// outputs: result tensor of inference result -// Return: -// APP_ERROR object, if convert image successfully, return APP_ERR_OK. -APP_ERROR FSAF::Inference(const std::vector& inputs, std::vector& outputs) { - auto dtypes = model_->GetOutputDataType(); - - // modelDesc_ get the output tensor size through Init() - for (size_t i = 0; i < modelDesc_.outputTensors.size(); ++i) { - std::vector shape = {}; - for (size_t j = 0; j < modelDesc_.outputTensors[i].tensorDims.size(); ++j) { - shape.push_back((uint32_t)modelDesc_.outputTensors[i].tensorDims[j]); - } - TensorBase tensor(shape, dtypes[i], MemoryData::MemoryType::MEMORY_DEVICE, deviceId_); - APP_ERROR ret = TensorBase::TensorBaseMalloc(tensor); - if (ret != APP_ERR_OK) { - LogError << "TensorBaseMalloc failed, ret=" << ret << "."; - return ret; - } - outputs.push_back(tensor); - } - - DynamicInfo dynamicInfo = {}; - dynamicInfo.dynamicType = DynamicType::STATIC_BATCH; - - // infer the result according to the input tensor - APP_ERROR ret = model_->ModelInference(inputs, outputs, dynamicInfo); - if (ret != APP_ERR_OK) { - LogError << "ModelInference failed, ret=" << ret << "."; - return ret; - } - - return APP_ERR_OK; -} - -// Post process for inference result. -// Scale the bbox to the origin image size. -// Parameters: -// imgPath: input image path -// inputs: tensor of image after inference -// resultPath: the path of storaging infer_result -// height, width: image's height and width -// name: image name, not including suffix -// showPath: the path of storaging visualizition result -// Return: -// APP_ERROR object, if post process image successfully, return APP_ERR_OK. -APP_ERROR FSAF::PostProcess(const std::string& imgPath, std::vector& inputs, - const std::string &resultPath, int& height, int& width, const std::string& name, std::string &showPath) { - // object num - int tensor_size = 100; - - MxBase::TensorBase& tensor = inputs[1]; // 1*100 - - int ret = tensor.ToHost(); - if (ret != APP_ERR_OK) { - LogError << GetError(ret) << "Tensor_1 deploy to host failed."; - return ret; - } - std::vector shape = tensor.GetShape(); - - auto labels = reinterpret_cast(tensor.GetBuffer()); // 1*100 - - std::cout << "---------------------------labels---------------------------" << std::endl; - int label[tensor_size] = {0}; - for(int i = 0; i < tensor_size; i++){ - std::cout << labels[i] << " "; - label[i] = labels[i]; - } - - tensor = inputs[0]; // 1*100*5 - ret = tensor.ToHost(); - if (ret != APP_ERR_OK) { - LogError << GetError(ret) << "Tensor_0 deploy to host failed."; - return ret; - } - - auto bbox = reinterpret_cast(tensor.GetBuffer()); - std::cout << "\n---------------------------bboxes--------------------------" << std::endl; - for(int i = 0; i < tensor_size; i++){ - std::cout << bbox[i][0] << ", " << bbox[i][1] << ", " << bbox[i][2] << ", " - << bbox[i][3] << ", " << bbox[i][4] << std::endl; - } - - // get infer coordinates - float image_size_w = width; - float image_size_h = height; - float net_input_width = 1216; - float net_input_height = 800; - float scale = std::min(net_input_width / (float)width, net_input_height / (float)height); - - int new_width = image_size_w * scale; - - float n_scale = (float)new_width / image_size_w; - - // probability threshold and all classes for label - float prob_thres = 0.05; - std::vector classes = {"person", "bicycle", "car", "motorcycle", "airplane", "bus", - "train", "truck", "boat", "traffic light", "fire hydrant", - "stop sign", "parking meter", "bench", "bird", "cat", "dog", - "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", - "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", - "skis", "snowboard", "sports ball", "kite", "baseball bat", - "baseball glove", "skateboard", "surfboard", "tennis racket", - "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", - "banana", "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", "couch", - "potted plant", "bed", "dining table", "toilet", "tv", "laptop", - "mouse", "remote", "keyboard", "cell phone", "microwave", - "oven", "toaster", "sink", "refrigerator", "book", "clock", - "vase", "scissors", "teddy bear", "hair drier", "toothbrush"}; - - // storge bbox after post processing - float ppbox[tensor_size][5] = {0}; - - for (int j = 0; j < tensor_size; ++j) { - // scale bbox - ppbox[j][0] = (bbox[j][0]) / n_scale; - ppbox[j][1] = (bbox[j][1]) / n_scale; - ppbox[j][2] = (bbox[j][2]) / n_scale; - ppbox[j][3] = (bbox[j][3]) / n_scale; - ppbox[j][4] = bbox[j][4]; - // limit bbox in a valid range - ppbox[j][0] = std::max((float)0, ppbox[j][0]); - ppbox[j][1] = std::max((float)0, ppbox[j][1]); - ppbox[j][2] = std::min(image_size_w, ppbox[j][2]); - ppbox[j][3] = std::min(image_size_h, ppbox[j][3]); - } - - std::ofstream out(resultPath); - cv::Mat imgCur = cv::imread(imgPath); - - for (int j = 0; j < tensor_size; ++j) { - if(float(ppbox[j][4]) < float(prob_thres)) { - continue; - } - if(label[j] < 0 || label[j] > 80) { - continue; - } - - // standard the output result - std::string class_name = classes[int(label[j])]; - std::string det_results_str = ""; - std::ostringstream oss; - oss << ppbox[j][4]; - std::string confidence(oss.str()); - char strff1[21], strff2[21], strff3[21], strff4[21], strff0[21]; - memset(strff1, 0, sizeof(strff1)); - memset(strff2, 0, sizeof(strff2)); - memset(strff3, 0, sizeof(strff3)); - memset(strff4, 0, sizeof(strff4)); - memset(strff0, 0, sizeof(strff0)); - // print ppbox to char* - sprintf(strff0, "%.8f", ppbox[j][0]); - sprintf(strff1, "%.8f", ppbox[j][1]); - sprintf(strff2, "%.8f", ppbox[j][2]); - sprintf(strff3, "%.8f", ppbox[j][3]); - sprintf(strff4, "%.8f", ppbox[j][4]); - det_results_str = det_results_str + class_name + " " + strff4 + " " + strff0 + " " + strff1 + " " - + strff2 + " " + strff3 + "\n"; - - out << det_results_str; - std::cout << det_results_str; - // visualization on the origin image - cv::Point p3((ppbox[j][0]), (ppbox[j][1])); - cv::Point p4((ppbox[j][2]), (ppbox[j][3])); - cv::Scalar colorRectangle1(0, 255, 1); - int thicknessRectangle1 = 1; - cv::rectangle(imgCur, p3, p4, colorRectangle1, thicknessRectangle1); - cv::putText(imgCur, class_name + "|" + confidence, p3, cv::FONT_HERSHEY_SIMPLEX, - 0.5, colorRectangle1, 1, 1, false); - } - out.close(); - cv::imwrite(showPath + "/" + name + ".jpg", imgCur); - - return APP_ERR_OK; -} - -// Primary method for process all images. -APP_ERROR FSAF::Process(const std::string &dirPath, std::string &resultPath, std::string &showPath) { - std::vector dirFileList = GetFileList(dirPath); - std::vector names, paths; - // for debug counting - int i = 0; - // process every image - for(auto imgFile : dirFileList) { - std::string imgPath = dirPath + "/" + imgFile; - std::string name = imgFile.substr(0, imgFile.find(".")); - std::string subresultPath = resultPath + "/" + name + ".txt"; - - cv::Mat imageMat; - int height = 0; - int width = 0; - // get image infomation - APP_ERROR ret = ReadImage(imgPath, imageMat, height, width); - if (ret != APP_ERR_OK) { - LogError << "ReadImage failed, ret=" << ret << "."; - return ret; - } - // resize image and pad it - ResizedImageInfo resizedImageInfo; - ResizeImage(imageMat, imageMat, resizedImageInfo); - - // convert image matrix to tensor - TensorBase tensorBase; - ret = CVMatToTensorBase(imageMat, tensorBase); - if (ret != APP_ERR_OK) { - LogError << "CVMatToTensorBase failed, ret=" << ret << "."; - return ret; - } - - std::vector inputs = {}; - std::vector outputs = {}; - inputs.push_back(tensorBase); - // infer and get output tensor - ret = Inference(inputs, outputs); - if (ret != APP_ERR_OK) { - LogError << "Inference failed, ret=" << ret << "."; - return ret; - } - // post process the bbox to the origin image, - // and implement visualizition. - ret = PostProcess(imgPath, outputs, subresultPath, height, width, name, showPath); - if (ret != APP_ERR_OK) { - LogError << "PostProcess failed, ret=" << ret << "."; - return ret; - } - // add count - i++; - std::cout << i << std::endl; - } - - return APP_ERR_OK; - -} +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + + * Unless 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. + */ + +#include "fsaf.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "MxBase/DeviceManager/DeviceManager.h" +#include "MxBase/DvppWrapper/DvppWrapper.h" +#include "MxBase/Log/Log.h" + +using namespace MxBase; + +// Complete all initialization work. When you are going to +// use FSAF class, you should invoke this method immediately. +// +// you need construct InitParam object for Init. +APP_ERROR FSAF::Init(const InitParam& initParam) { + // initialize device + APP_ERROR ret = MxBase::DeviceManager::GetInstance()->InitDevices(); + if (ret != APP_ERR_OK) { + LogError << "Init devices failed, ret=" << ret << "."; + return ret; + } + // initialize context + ret = MxBase::TensorContext::GetInstance()->SetContext(initParam.deviceId); + if (ret != APP_ERR_OK) { + LogError << "Set context failed, ret=" << ret << "."; + return ret; + } + + // load model + model_ = std::make_shared(); + ret = model_->Init(initParam.modelPath, modelDesc_); + if (ret != APP_ERR_OK) { + LogError << "ModelInferenceProcessor init failed, ret=" << ret << "."; + return ret; + } + + return APP_ERR_OK; +} + +APP_ERROR FSAF::DeInit() { + model_->DeInit(); + MxBase::DeviceManager::GetInstance()->DestroyDevices(); + return APP_ERR_OK; +} + +// Get all files under a directory. +// Parameters: +// dirPath: the directory path +// Return: +// a vector of filename, including the suffix. +std::vector FSAF::GetFileList(const std::string &dirPath) { + struct dirent *ptr; + DIR *dir = opendir(dirPath.c_str()); + std::vector files; + while ((ptr = readdir(dir)) != NULL) { + if (ptr->d_name[0] == '.') { + continue; + } + files.push_back(ptr->d_name); + } + closedir(dir); + return files; +} + +// Read image from a image path. +// Parameters: +// imgPath: string of image path +// imageMat: opencv Mat object, for storging image as matrix +// height: int, storge the image height +// width: int, storge the image width +// Return: +// APP_ERROR object, if read image successfully, return APP_ERR_OK. +APP_ERROR FSAF::ReadImage(const std::string& imgPath, cv::Mat& imageMat, int& height, int& width) { + std::cout<<"-----------------------------------------"< RGB + cv::cvtColor(imageMat, imageMat, cv::COLOR_BGR2RGB); //不加此行 0.354,加了0.354 + + width = imageMat.cols; + height = imageMat.rows; + + return APP_ERR_OK; +} + +// Resize image to fix size. +// We use RightDown padding style. +// Parameters: +// srcImageMat: source opencv Mat object, image matrix +// dstImageMat: storge destination opencv Mat object +// resizedImageInfo: contain infomation about the image, including before and after scaling +//Return: +// APP_ERROR object, if resize image successfully, return APP_ERR_OK. +APP_ERROR FSAF::ResizeImage(const cv::Mat& srcImageMat, cv::Mat& dstImageMat, + MxBase::ResizedImageInfo& resizedImageInfo) { + float resizeHeight = 800; + float resizeWidth = 1216; + float scale = std::min(resizeWidth / srcImageMat.cols, resizeHeight / srcImageMat.rows); + int new_width = srcImageMat.cols * scale + 0.5; + int new_height = srcImageMat.rows * scale + 0.5; + + // calculate the padding + int pad_w = resizeWidth - new_width; + int pad_h = resizeHeight - new_height; + + resizedImageInfo.heightOriginal = srcImageMat.rows; + resizedImageInfo.heightResize = resizeHeight; + resizedImageInfo.widthOriginal = srcImageMat.cols; + resizedImageInfo.widthResize = resizeWidth; + resizedImageInfo.resizeType = RESIZER_MS_KEEP_ASPECT_RATIO; + std::cout<<"src_width="< shape = {imageMat.rows * YUV444_RGB_WIDTH_NU, static_cast(imageMat.cols)}; + std::vector shape = { + static_cast(imageMat.rows), + static_cast(imageMat.cols), + static_cast(imageMat.channels())}; + + // tensorBase = TensorBase(memoryDataDst, false, shape, TENSOR_DTYPE_UINT8); + tensorBase = TensorBase(memoryDataDst, false, shape, TENSOR_DTYPE_FLOAT32); + return APP_ERR_OK; +} + +// Model inference. +// Parameters: +// inputs: input image tensor +// outputs: result tensor of inference result +// Return: +// APP_ERROR object, if convert image successfully, return APP_ERR_OK. +APP_ERROR FSAF::Inference(const std::vector& inputs, std::vector& outputs) { + auto dtypes = model_->GetOutputDataType(); + + // modelDesc_ get the output tensor size through Init() + for (size_t i = 0; i < modelDesc_.outputTensors.size(); ++i) { + std::vector shape = {}; + for (size_t j = 0; j < modelDesc_.outputTensors[i].tensorDims.size(); ++j) { + shape.push_back((uint32_t)modelDesc_.outputTensors[i].tensorDims[j]); + } + TensorBase tensor(shape, dtypes[i], MemoryData::MemoryType::MEMORY_DEVICE, deviceId_); + APP_ERROR ret = TensorBase::TensorBaseMalloc(tensor); + if (ret != APP_ERR_OK) { + LogError << "TensorBaseMalloc failed, ret=" << ret << "."; + return ret; + } + outputs.push_back(tensor); + } + + DynamicInfo dynamicInfo = {}; + dynamicInfo.dynamicType = DynamicType::STATIC_BATCH; + + // infer the result according to the input tensor + APP_ERROR ret = model_->ModelInference(inputs, outputs, dynamicInfo); + if (ret != APP_ERR_OK) { + LogError << "ModelInference failed, ret=" << ret << "."; + return ret; + } + + return APP_ERR_OK; +} + +// Post process for inference result. +// Scale the bbox to the origin image size. +// Parameters: +// imgPath: input image path +// inputs: tensor of image after inference +// resultPath: the path of storaging infer_result +// height, width: image's height and width +// name: image name, not including suffix +// showPath: the path of storaging visualizition result +// Return: +// APP_ERROR object, if post process image successfully, return APP_ERR_OK. +APP_ERROR FSAF::PostProcess(const std::string& imgPath, std::vector& inputs, + const std::string &resultPath, int& height, int& width, const std::string& name, std::string &showPath) { + // object num + int tensor_size = 100; + + MxBase::TensorBase& tensor = inputs[1]; // 1*100 + + int ret = tensor.ToHost(); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Tensor_1 deploy to host failed."; + return ret; + } + std::vector shape = tensor.GetShape(); + + auto labels = reinterpret_cast(tensor.GetBuffer()); // 1*100 + + std::cout << "---------------------------labels---------------------------" << std::endl; + int label[tensor_size] = {0}; + for(int i = 0; i < tensor_size; i++){ + std::cout << labels[i] << " "; + label[i] = labels[i]; + } + + tensor = inputs[0]; // 1*100*5 + ret = tensor.ToHost(); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Tensor_0 deploy to host failed."; + return ret; + } + + auto bbox = reinterpret_cast(tensor.GetBuffer()); + std::cout << "\n---------------------------bboxes--------------------------" << std::endl; + for(int i = 0; i < tensor_size; i++){ + std::cout << bbox[i][0] << ", " << bbox[i][1] << ", " << bbox[i][2] << ", " + << bbox[i][3] << ", " << bbox[i][4] << std::endl; + } + + // get infer coordinates + float image_size_w = width; + float image_size_h = height; + float net_input_width = 1216; + float net_input_height = 800; + float scale = std::min(net_input_width / (float)width, net_input_height / (float)height); + + int new_width = image_size_w * scale; + + float n_scale = (float)new_width / image_size_w; + + // probability threshold and all classes for label + float prob_thres = 0.05; + std::vector classes = {"person", "bicycle", "car", "motorcycle", "airplane", "bus", + "train", "truck", "boat", "traffic light", "fire hydrant", + "stop sign", "parking meter", "bench", "bird", "cat", "dog", + "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", + "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", + "skis", "snowboard", "sports ball", "kite", "baseball bat", + "baseball glove", "skateboard", "surfboard", "tennis racket", + "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", + "banana", "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", "couch", + "potted plant", "bed", "dining table", "toilet", "tv", "laptop", + "mouse", "remote", "keyboard", "cell phone", "microwave", + "oven", "toaster", "sink", "refrigerator", "book", "clock", + "vase", "scissors", "teddy bear", "hair drier", "toothbrush"}; + + // storge bbox after post processing + float ppbox[tensor_size][5] = {0}; + + for (int j = 0; j < tensor_size; ++j) { + // scale bbox + ppbox[j][0] = (bbox[j][0]) / n_scale; + ppbox[j][1] = (bbox[j][1]) / n_scale; + ppbox[j][2] = (bbox[j][2]) / n_scale; + ppbox[j][3] = (bbox[j][3]) / n_scale; + ppbox[j][4] = bbox[j][4]; + // limit bbox in a valid range + ppbox[j][0] = std::max((float)0, ppbox[j][0]); + ppbox[j][1] = std::max((float)0, ppbox[j][1]); + ppbox[j][2] = std::min(image_size_w, ppbox[j][2]); + ppbox[j][3] = std::min(image_size_h, ppbox[j][3]); + } + + std::ofstream out(resultPath); + cv::Mat imgCur = cv::imread(imgPath); + + for (int j = 0; j < tensor_size; ++j) { + if(float(ppbox[j][4]) < float(prob_thres)) { + continue; + } + if(label[j] < 0 || label[j] > 80) { + continue; + } + + // standard the output result + std::string class_name = classes[int(label[j])]; + std::string det_results_str = ""; + std::ostringstream oss; + oss << ppbox[j][4]; + std::string confidence(oss.str()); + char strff1[21], strff2[21], strff3[21], strff4[21], strff0[21]; + memset(strff1, 0, sizeof(strff1)); + memset(strff2, 0, sizeof(strff2)); + memset(strff3, 0, sizeof(strff3)); + memset(strff4, 0, sizeof(strff4)); + memset(strff0, 0, sizeof(strff0)); + // print ppbox to char* + sprintf(strff0, "%.8f", ppbox[j][0]); + sprintf(strff1, "%.8f", ppbox[j][1]); + sprintf(strff2, "%.8f", ppbox[j][2]); + sprintf(strff3, "%.8f", ppbox[j][3]); + sprintf(strff4, "%.8f", ppbox[j][4]); + det_results_str = det_results_str + class_name + " " + strff4 + " " + strff0 + " " + strff1 + " " + + strff2 + " " + strff3 + "\n"; + + out << det_results_str; + std::cout << det_results_str; + // visualization on the origin image + cv::Point p3((ppbox[j][0]), (ppbox[j][1])); + cv::Point p4((ppbox[j][2]), (ppbox[j][3])); + cv::Scalar colorRectangle1(0, 255, 1); + int thicknessRectangle1 = 1; + cv::rectangle(imgCur, p3, p4, colorRectangle1, thicknessRectangle1); + cv::putText(imgCur, class_name + "|" + confidence, p3, cv::FONT_HERSHEY_SIMPLEX, + 0.5, colorRectangle1, 1, 1, false); + } + out.close(); + cv::imwrite(showPath + "/" + name + ".jpg", imgCur); + + return APP_ERR_OK; +} + +// Primary method for process all images. +APP_ERROR FSAF::Process(const std::string &dirPath, std::string &resultPath, std::string &showPath) { + std::vector dirFileList = GetFileList(dirPath); + std::vector names, paths; + // for debug counting + int i = 0; + // process every image + for(auto imgFile : dirFileList) { + std::string imgPath = dirPath + "/" + imgFile; + std::string name = imgFile.substr(0, imgFile.find(".")); + std::string subresultPath = resultPath + "/" + name + ".txt"; + cv::Mat imageMat; + int height = 0; + int width = 0; + // get image infomation + APP_ERROR ret = ReadImage(imgPath, imageMat, height, width); + if (ret != APP_ERR_OK) { + LogError << "ReadImage failed, ret=" << ret << "."; + return ret; + } + // resize image and pad it + ResizedImageInfo resizedImageInfo; + ResizeImage(imageMat, imageMat, resizedImageInfo); + + // convert image matrix to tensor + TensorBase tensorBase; + ret = CVMatToTensorBase(imageMat, tensorBase); + if (ret != APP_ERR_OK) { + LogError << "CVMatToTensorBase failed, ret=" << ret << "."; + return ret; + } + + std::vector inputs = {}; + std::vector outputs = {}; + inputs.push_back(tensorBase); + // infer and get output tensor + ret = Inference(inputs, outputs); + if (ret != APP_ERR_OK) { + LogError << "Inference failed, ret=" << ret << "."; + return ret; + } + // post process the bbox to the origin image, + // and implement visualizition. + ret = PostProcess(imgPath, outputs, subresultPath, height, width, name, showPath); + if (ret != APP_ERR_OK) { + LogError << "PostProcess failed, ret=" << ret << "."; + return ret; + } + // add count + i++; + std::cout << i << std::endl; + } + + return APP_ERR_OK; + +} diff --git a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.h b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.h index 8f20a8db67955250538d95660e6cfd3ef0ff1e33..36bb294062a642b64cfe8766408d07b72cc63cf1 100755 --- a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.h +++ b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/fsaf.h @@ -1,65 +1,69 @@ -/* - * Copyright 2021 Huawei Technologies Co., Ltd - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - - * Unless 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. - */ - -#ifndef UNET_SEGMENTATION_H -#define UNET_SEGMENTATION_H - -#include - -#include "MxBase/ModelInfer/ModelInferenceProcessor.h" -#include "MxBase/PostProcessBases/PostProcessDataType.h" -#include "MxBase/Tensor/TensorContext/TensorContext.h" - -// parameters struct for initialize Model -struct InitParam { - // npu device id - uint32_t deviceId; - // model path of .om - std::string modelPath; -}; - -// FSAF Model class -// Example: -// ... -// FSAF fsaf; -// APP_ERROR ret = fsaf.Init(initParam); -// ret = fsaf.Process(imgPath,resultPath,showPath); -// ... -// fsaf.DeInit(); -class FSAF { -public: - APP_ERROR Init(const InitParam &initParam); - APP_ERROR DeInit(); - APP_ERROR ReadImage(const std::string &imgPath, cv::Mat &imageMat, int& height, int& width); - APP_ERROR ResizeImage(const cv::Mat &srcImageMat, cv::Mat &dstImageMat, - MxBase::ResizedImageInfo &resziedImageInfo); - APP_ERROR CVMatToTensorBase(const cv::Mat &imageMat, MxBase::TensorBase &tensorBase); - APP_ERROR VectorToTensorBase(int* transMat, MxBase::TensorBase& tensorBase); - APP_ERROR Inference(const std::vector &inputs, std::vector &outputs); - APP_ERROR PostProcess(const std::string& imgPath, std::vector &inputs, - const std::string &subresultPath,int& height, int& width,const std::string& name,std::string &showPath); - APP_ERROR Process(const std::string &dirPath, std::string &resultPath, std::string &showPath); - APP_ERROR Normalize(const cv::Mat &srcImageMat, cv::Mat &dstImageMat); - -private: - std::shared_ptr model_; - MxBase::ModelDesc modelDesc_; - uint32_t deviceId_ = 0; - - std::vector GetFileList(const std::string &dirPath); -}; - -#endif +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + + * Unless 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. + */ + +#ifndef UNET_SEGMENTATION_H +#define UNET_SEGMENTATION_H + +#include + +#include "MxBase/ModelInfer/ModelInferenceProcessor.h" +#include "MxBase/PostProcessBases/PostProcessDataType.h" +#include "MxBase/Tensor/TensorContext/TensorContext.h" + +// parameters struct for initialize Model +struct InitParam { + // npu device id + uint32_t deviceId; + // model path of .om + std::string modelPath; + +}; + +// FSAF Model class +// Example: +// ... +// FSAF fsaf; +// APP_ERROR ret = fsaf.Init(initParam); +// ret = fsaf.Process(imgPath,resultPath,showPath); +// ... +// fsaf.DeInit(); +class FSAF { +public: + APP_ERROR Init(const InitParam &initParam); + APP_ERROR DeInit(); + APP_ERROR ReadImage(const std::string &imgPath, cv::Mat &imageMat, int& height, int& width); + APP_ERROR ResizeImage(const cv::Mat &srcImageMat, cv::Mat &dstImageMat, + MxBase::ResizedImageInfo &resziedImageInfo); + APP_ERROR CVMatToTensorBase(const cv::Mat &imageMat, MxBase::TensorBase &tensorBase); + APP_ERROR VectorToTensorBase(int* transMat, MxBase::TensorBase& tensorBase); + //APP_ERROR CVMatToTensorBase(const cv::Mat &imageMat, MxBase::TensorBase &tensorBase,float &data) ; + APP_ERROR Inference(const std::vector &inputs, std::vector &outputs); + APP_ERROR PostProcess(const std::string& imgPath, std::vector &inputs, + const std::string &subresultPath,int& height, int& width,const std::string& name,std::string &showPath); + APP_ERROR Process(const std::string &dirPath, std::string &resultPath, std::string &showPath); + APP_ERROR Normalize(const cv::Mat &srcImageMat, cv::Mat &dstImageMat); + + APP_ERROR Pad(const cv::Mat& srcImageMat,cv::Mat& dstImageMat); + +private: + std::shared_ptr model_; + MxBase::ModelDesc modelDesc_; + uint32_t deviceId_ = 0; + + std::vector GetFileList(const std::string &dirPath); +}; + +#endif diff --git a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/main.cpp b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/main.cpp index 141ab3f167070a2dfe448b92cccef71ec32ab01c..876c833a37e60fe76829336f63dbac476d972d72 100755 --- a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/main.cpp +++ b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/mxbase/src/main.cpp @@ -1,53 +1,53 @@ -/* - * Copyright 2021 Huawei Technologies Co., Ltd - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - - * Unless 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. - */ - -#include "fsaf.h" - -#include "MxBase/Log/Log.h" - -int main() { - // config the parameters for fsaf model - InitParam initParam = {}; - initParam.deviceId = 0; - initParam.modelPath = "../../data/model/fsaf.om"; - - // declare and initialize the fsaf model - FSAF fsaf; - APP_ERROR ret = fsaf.Init(initParam); - if (ret != APP_ERR_OK) { - LogError << "FSAF init failed, ret=" << ret << "."; - return ret; - } - - // coco2017 validation set for object detection - std::string imgPath = "../../data/input/val2017"; - - // directories for saving result - std::string outputPath = "../output/"; - std::string resultPath = outputPath + "infer_result"; - std::string showPath = outputPath + "show_result"; - - // call the process of fsaf model - ret = fsaf.Process(imgPath, resultPath, showPath); - if (ret != APP_ERR_OK) { - LogError << "FSAF process failed, ret=" << ret << "."; - fsaf.DeInit(); - return ret; - } - - fsaf.DeInit(); - return APP_ERR_OK; -} +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + + * Unless 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. + */ + +#include "fsaf.h" + +#include "MxBase/Log/Log.h" + +int main() { + // config the parameters for fsaf model + InitParam initParam = {}; + initParam.deviceId = 0; + initParam.modelPath = "../../data/model/fsaf_acl_10.om"; + + // declare and initialize the fsaf model + FSAF fsaf; + APP_ERROR ret = fsaf.Init(initParam); + if (ret != APP_ERR_OK) { + LogError << "FSAF init failed, ret=" << ret << "."; + return ret; + } + + // coco2017 validation set for object detection + std::string imgPath = "../../../coco2017/val2017/"; + + // directories for saving result + std::string outputPath = "./output/"; + std::string resultPath = outputPath + "infer_result"; + std::string showPath = outputPath + "show_result"; + + // call the process of fsaf model + ret = fsaf.Process(imgPath, resultPath, showPath); + if (ret != APP_ERR_OK) { + LogError << "FSAF process failed, ret=" << ret << "."; + fsaf.DeInit(); + return ret; + } + + fsaf.DeInit(); + return APP_ERR_OK; +} diff --git a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/sdk/main.py b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/sdk/main.py index 23d7158364a14bf0f834991ebb6038d5de10b579..fd1668399cf5c42f101a608e775b9b3cdab4a389 100644 --- a/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/sdk/main.py +++ b/PyTorch/contrib/cv/detection/FSAF_for_Pytorch/infer/sdk/main.py @@ -107,10 +107,10 @@ def do_infer_image(stream_manager_api, image_path): f"errorMsg={infer_result.data.decode()}") exit() - infer_result_json = json.loads(infer_result.data.decode()) - content = json.loads(infer_result_json['metaData'][0]['content']) + # infer_result_json = json.loads(infer_result.data.decode()) + # content = json.loads(infer_result_json['metaData'][0]['content']) # print the infer data - print(infer_result.data.decode()) + print("infer_result.data.decode=", infer_result.data.decode()) infer_result_json = json.loads(infer_result.data.decode()) content = json.loads(infer_result_json['metaData'][0]['content']) @@ -119,9 +119,8 @@ def do_infer_image(stream_manager_api, image_path): tensor_shape = tensor_vec['tensorShape'] bboxes = np.frombuffer(base64.b64decode(data_str), dtype=np.float32) bboxes = np.reshape(bboxes, tensor_shape[1:]) - print("---------------------------bboxes---------------------------") - print(bboxes, '\n') + print("bboxes.shape=", bboxes.shape, '\n') tensor_vec = content['tensorPackageVec'][0]['tensorVec'][1] data_str = tensor_vec['dataStr'] @@ -130,10 +129,8 @@ def do_infer_image(stream_manager_api, image_path): labels = np.reshape(labels, tensor_shape[1:]) print("---------------------------labels---------------------------") - print(labels, '\n') - - # [bboxes,labels] (1,100,5);(1,100) - return bboxes, labels + print("labels.shape=", labels.shape, '\n') + return bboxes, labels # [bboxes,labels] (1,100,5);(1,100) CLASSES = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', @@ -162,19 +159,25 @@ def postprocess_bboxes(bboxes, image_size, net_input_width, net_input_height): w = image_size[0] h = image_size[1] scale = min(net_input_width / w, net_input_height / h) + new_input_width = int(w * scale) + scale = new_input_width / w + pbboxes = bboxes.copy() + + # scale_ws, scale_hs = net_input_width / w, net_input_height / h # cal predict box on the image src pbboxes[:, 0] = pbboxes[:, 0] / scale pbboxes[:, 1] = pbboxes[:, 1] / scale pbboxes[:, 2] = pbboxes[:, 2] / scale pbboxes[:, 3] = pbboxes[:, 3] / scale - + # print('pbboxes[:, 2]_2222=', pbboxes[:, 2]) # make pbboxes value in valid range pbboxes[:, 0] = np.maximum(pbboxes[:, 0], 0) pbboxes[:, 1] = np.maximum(pbboxes[:, 1], 0) - pbboxes[:, 2] = np.minimum(pbboxes[:, 2], net_input_width) - pbboxes[:, 3] = np.minimum(pbboxes[:, 3], net_input_height) + pbboxes[:, 2] = np.minimum(pbboxes[:, 2], w) + # print('pbboxes[:, 2]_33333=', pbboxes[:, 2]) + pbboxes[:, 3] = np.minimum(pbboxes[:, 3], h) return pbboxes @@ -274,13 +277,12 @@ def main(args): det_results_file = os.path.join(det_results_path, file_name + ".txt") with open(det_results_file, "w") as detf: detf.write(det_results_str) + print('the {}th jpg '.format(i)) + print("det_results_str= \n", det_results_str) - print(det_results_str) - print(i) - stream_manager_api.DestroyAllStreams() if __name__ == "__main__": args = parse_arg() - main(args) \ No newline at end of file + main(args)