forked from mindspore-Ecosystem/mindspore
modify for fasterrcnn, maskrcnn to support aipp+dvpp 310 infer
This commit is contained in:
parent
f80b28313f
commit
118c72ea32
model_zoo/official/cv
|
@ -108,6 +108,9 @@ sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
|
|||
|
||||
# eval
|
||||
sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
||||
|
||||
# inference
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
# Run in docker
|
||||
|
@ -143,6 +146,13 @@ sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
|
|||
sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
||||
```
|
||||
|
||||
5. Inference
|
||||
|
||||
```shell
|
||||
# inference
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
# Script Description
|
||||
|
||||
## Script and Sample Code
|
||||
|
@ -151,9 +161,11 @@ sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
.
|
||||
└─faster_rcnn
|
||||
├─README.md // descriptions about fasterrcnn
|
||||
├─ascend310_infer //application for 310 inference
|
||||
├─scripts
|
||||
├─run_standalone_train_ascend.sh // shell script for standalone on ascend
|
||||
├─run_distribute_train_ascend.sh // shell script for distributed on ascend
|
||||
├─run_infer_310.sh // shell script for 310 inference
|
||||
└─run_eval_ascend.sh // shell script for eval on ascend
|
||||
├─src
|
||||
├─FasterRcnn
|
||||
|
@ -168,12 +180,15 @@ sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
├─resnet50.py // backbone network
|
||||
├─roi_align.py // roi align network
|
||||
└─rpn.py // region proposal network
|
||||
├─aipp.cfg // aipp config file
|
||||
├─config.py // total config
|
||||
├─dataset.py // create dataset and process dataset
|
||||
├─lr_schedule.py // learning ratio generator
|
||||
├─network_define.py // network define for fasterrcnn
|
||||
└─util.py // routine operation
|
||||
├─export.py // script to export AIR,MINDIR,ONNX model
|
||||
├─eval.py //eval scripts
|
||||
├─postprogress.py // post process for 310 inference
|
||||
└─train.py // train scripts
|
||||
```
|
||||
|
||||
|
@ -265,6 +280,44 @@ Eval result will be stored in the example path, whose folder name is "eval". Und
|
|||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.631
|
||||
```
|
||||
|
||||
## Model Export
|
||||
|
||||
```shell
|
||||
python export.py --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT]
|
||||
```
|
||||
|
||||
`EXPORT_FORMAT` shoule be in ["AIR", "ONNX", "MINDIR"]
|
||||
|
||||
## Inference Process
|
||||
|
||||
### Usage
|
||||
|
||||
Before performing inference, the air file must bu exported by export script on the Ascend910 environment.
|
||||
|
||||
```shell
|
||||
# Ascend310 inference
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
### result
|
||||
|
||||
Inference result is saved in current path, you can find result like this in log file.
|
||||
|
||||
```log
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.349
|
||||
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.570
|
||||
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.369
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.211
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.391
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.435
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.295
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.476
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.503
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.330
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.547
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.622
|
||||
```
|
||||
|
||||
# Model Description
|
||||
|
||||
## Performance
|
||||
|
|
|
@ -111,6 +111,9 @@ sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
|
|||
|
||||
# 评估
|
||||
sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
||||
|
||||
#推理
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
# 在docker上运行
|
||||
|
@ -146,6 +149,13 @@ sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
|
|||
sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
||||
```
|
||||
|
||||
5. 推理
|
||||
|
||||
```shell
|
||||
# 推理
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
# 脚本说明
|
||||
|
||||
## 脚本及样例代码
|
||||
|
@ -154,9 +164,11 @@ sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
.
|
||||
└─faster_rcnn
|
||||
├─README.md // Faster R-CNN相关说明
|
||||
├─ascend310_infer //实现310推理源代码
|
||||
├─scripts
|
||||
├─run_standalone_train_ascend.sh // Ascend单机shell脚本
|
||||
├─run_distribute_train_ascend.sh // Ascend分布式shell脚本
|
||||
├─run_infer_310.sh // Ascend推理shell脚本
|
||||
└─run_eval_ascend.sh // Ascend评估shell脚本
|
||||
├─src
|
||||
├─FasterRcnn
|
||||
|
@ -171,12 +183,15 @@ sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
├─resnet50.py // 骨干网络
|
||||
├─roi_align.py // ROI对齐网络
|
||||
└─rpn.py // 区域候选网络
|
||||
├─aipp.cfg // aipp 配置文件
|
||||
├─config.py // 总配置
|
||||
├─dataset.py // 创建并处理数据集
|
||||
├─lr_schedule.py // 学习率生成器
|
||||
├─network_define.py // Faster R-CNN网络定义
|
||||
└─util.py // 例行操作
|
||||
├─export.py // 导出 AIR,MINDIR,ONNX模型的脚本
|
||||
├─eval.py // 评估脚本
|
||||
├─postprogress.py // 310推理后处理脚本
|
||||
└─train.py // 训练脚本
|
||||
```
|
||||
|
||||
|
@ -268,6 +283,44 @@ sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.631
|
||||
```
|
||||
|
||||
## 模型导出
|
||||
|
||||
```shell
|
||||
python export.py --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT]
|
||||
```
|
||||
|
||||
`EXPORT_FORMAT` 可选 ["AIR", "ONNX", "MINDIR"]
|
||||
|
||||
## 推理过程
|
||||
|
||||
### 使用方法
|
||||
|
||||
在推理之前需要在昇腾910环境上完成模型的导出。
|
||||
|
||||
```shell
|
||||
# Ascend310 inference
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
### 结果
|
||||
|
||||
推理的结果保存在当前目录下,在日志文件中可以找到类似以下的结果。
|
||||
|
||||
```log
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.349
|
||||
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.570
|
||||
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.369
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.211
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.391
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.435
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.295
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.476
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.503
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.330
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.547
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.622
|
||||
```
|
||||
|
||||
# 模型描述
|
||||
|
||||
## 性能
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 ACLMANAGER_H
|
||||
#define ACLMANAGER_H
|
||||
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "acl/acl.h"
|
||||
#include "CommonDataType.h"
|
||||
#include "ModelProcess.h"
|
||||
#include "DvppCommon.h"
|
||||
|
||||
struct ModelInfo {
|
||||
std::string modelPath;
|
||||
uint32_t modelWidth;
|
||||
uint32_t modelHeight;
|
||||
uint32_t outputNum;
|
||||
};
|
||||
|
||||
class AclProcess {
|
||||
public:
|
||||
AclProcess(int deviceId, const std::string &om_path, uint32_t width, uint32_t height);
|
||||
~AclProcess() {}
|
||||
void Release();
|
||||
int InitResource();
|
||||
int Process(const std::string& imageFile, std::map<double, double> *costTime_map);
|
||||
|
||||
private:
|
||||
int InitModule();
|
||||
int Preprocess(const std::string& imageFile);
|
||||
int ModelInfer(std::map<double, double> *costTime_map);
|
||||
int WriteResult(const std::string& imageFile);
|
||||
int ReadFile(const std::string &filePath, RawData *fileData);
|
||||
|
||||
int32_t deviceId_;
|
||||
ModelInfo modelInfo_;
|
||||
aclrtContext context_;
|
||||
aclrtStream stream_;
|
||||
std::shared_ptr<ModelProcess> modelProcess_;
|
||||
std::shared_ptr<DvppCommon> dvppCommon_;
|
||||
bool keepRatio_;
|
||||
std::vector<void *> outputBuffers_;
|
||||
std::vector<size_t> outputSizes_;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 COMMONDATATYPE_H
|
||||
#define COMMONDATATYPE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "acl/acl.h"
|
||||
#include "acl/ops/acl_dvpp.h"
|
||||
|
||||
#define DVPP_ALIGN_UP(x, align) ((((x) + ((align)-1)) / (align)) * (align))
|
||||
|
||||
#define OK 0
|
||||
#define ERROR -1
|
||||
#define INVALID_POINTER -2
|
||||
#define READ_FILE_FAIL -3
|
||||
#define OPEN_FILE_FAIL -4
|
||||
#define INIT_FAIL -5
|
||||
#define INVALID_PARAM -6
|
||||
#define DECODE_FAIL -7
|
||||
|
||||
const float SEC2MS = 1000.0;
|
||||
const int YUV_BGR_SIZE_CONVERT_3 = 3;
|
||||
const int YUV_BGR_SIZE_CONVERT_2 = 2;
|
||||
const int VPC_WIDTH_ALIGN = 16;
|
||||
const int VPC_HEIGHT_ALIGN = 2;
|
||||
|
||||
// Description of image data
|
||||
struct ImageInfo {
|
||||
uint32_t width; // Image width
|
||||
uint32_t height; // Image height
|
||||
uint32_t lenOfByte; // Size of image data, bytes
|
||||
std::shared_ptr<uint8_t> data; // Smart pointer of image data
|
||||
};
|
||||
|
||||
// Description of data in device
|
||||
struct RawData {
|
||||
size_t lenOfByte; // Size of memory, bytes
|
||||
std::shared_ptr<void> data; // Smart pointer of data
|
||||
};
|
||||
|
||||
// define the structure of an rectangle
|
||||
struct Rectangle {
|
||||
uint32_t leftTopX;
|
||||
uint32_t leftTopY;
|
||||
uint32_t rightBottomX;
|
||||
uint32_t rightBottomY;
|
||||
};
|
||||
|
||||
enum VpcProcessType {
|
||||
VPC_PT_DEFAULT = 0,
|
||||
VPC_PT_PADDING, // Resize with locked ratio and paste on upper left corner
|
||||
VPC_PT_FIT, // Resize with locked ratio and paste on middle location
|
||||
VPC_PT_FILL, // Resize with locked ratio and paste on whole locatin, the input image may be cropped
|
||||
};
|
||||
|
||||
struct DvppDataInfo {
|
||||
uint32_t width = 0; // Width of image
|
||||
uint32_t height = 0; // Height of image
|
||||
uint32_t widthStride = 0; // Width after align up
|
||||
uint32_t heightStride = 0; // Height after align up
|
||||
acldvppPixelFormat format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; // Format of image
|
||||
uint32_t frameId = 0; // Needed by video
|
||||
uint32_t dataSize = 0; // Size of data in byte
|
||||
uint8_t *data = nullptr; // Image data
|
||||
};
|
||||
|
||||
struct CropRoiConfig {
|
||||
uint32_t left;
|
||||
uint32_t right;
|
||||
uint32_t down;
|
||||
uint32_t up;
|
||||
};
|
||||
|
||||
struct DvppCropInputInfo {
|
||||
DvppDataInfo dataInfo;
|
||||
CropRoiConfig roi;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 DVPP_COMMON_H
|
||||
#define DVPP_COMMON_H
|
||||
#include <memory>
|
||||
|
||||
#include "CommonDataType.h"
|
||||
#include "acl/ops/acl_dvpp.h"
|
||||
|
||||
const int MODULUS_NUM_2 = 2;
|
||||
const uint32_t ODD_NUM_1 = 1;
|
||||
|
||||
const uint32_t JPEGD_STRIDE_WIDTH = 128; // Jpegd module output width need to align up to 128
|
||||
const uint32_t JPEGD_STRIDE_HEIGHT = 16; // Jpegd module output height need to align up to 16
|
||||
const uint32_t VPC_STRIDE_WIDTH = 16; // Vpc module output width need to align up to 16
|
||||
const uint32_t VPC_STRIDE_HEIGHT = 2; // Vpc module output height need to align up to 2
|
||||
const uint32_t YUV422_WIDTH_NU = 2; // Width of YUV422, WidthStride = Width * 2
|
||||
const uint32_t YUV444_RGB_WIDTH_NU = 3; // Width of YUV444 and RGB888, WidthStride = Width * 3
|
||||
const uint32_t XRGB_WIDTH_NU = 4; // Width of XRGB8888, WidthStride = Width * 4
|
||||
const uint32_t JPEG_OFFSET = 8; // Offset of input file for jpegd module
|
||||
const uint32_t MAX_JPEGD_WIDTH = 8192; // Max width of jpegd module
|
||||
const uint32_t MAX_JPEGD_HEIGHT = 8192; // Max height of jpegd module
|
||||
const uint32_t MIN_JPEGD_WIDTH = 32; // Min width of jpegd module
|
||||
const uint32_t MIN_JPEGD_HEIGHT = 32; // Min height of jpegd module
|
||||
const uint32_t MAX_RESIZE_WIDTH = 4096; // Max width stride of resize module
|
||||
const uint32_t MAX_RESIZE_HEIGHT = 4096; // Max height stride of resize module
|
||||
const uint32_t MIN_RESIZE_WIDTH = 32; // Min width stride of resize module
|
||||
const uint32_t MIN_RESIZE_HEIGHT = 6; // Min height stride of resize module
|
||||
const float MIN_RESIZE_SCALE = 0.03125; // Min resize scale of resize module
|
||||
const float MAX_RESIZE_SCALE = 16.0; // Min resize scale of resize module
|
||||
const uint32_t MAX_VPC_WIDTH = 4096; // Max width of picture to VPC(resize/crop)
|
||||
const uint32_t MAX_VPC_HEIGHT = 4096; // Max height of picture to VPC(resize/crop)
|
||||
const uint32_t MIN_VPC_WIDTH = 32; // Min width of picture to VPC(resize/crop)
|
||||
const uint32_t MIN_VPC_HEIGHT = 6; // Min height of picture to VPC(resize/crop)
|
||||
const uint32_t MIN_CROP_WIDTH = 10; // Min width of crop area
|
||||
const uint32_t MIN_CROP_HEIGHT = 6; // Min height of crop area
|
||||
const uint8_t YUV_GREYER_VALUE = 128; // Filling value of the resized YUV image
|
||||
|
||||
#define CONVERT_TO_ODD(NUM) (((NUM) % MODULUS_NUM_2 != 0) ? (NUM) : ((NUM) - 1))
|
||||
#define CONVERT_TO_EVEN(NUM) (((NUM) % MODULUS_NUM_2 == 0) ? (NUM) : ((NUM) - 1))
|
||||
#define CHECK_ODD(num) ((num) % MODULUS_NUM_2 != 0)
|
||||
#define CHECK_EVEN(num) ((num) % MODULUS_NUM_2 == 0)
|
||||
#define RELEASE_DVPP_DATA(dvppDataPtr) do { \
|
||||
int retMacro; \
|
||||
if (dvppDataPtr != nullptr) { \
|
||||
retMacro = acldvppFree(dvppDataPtr); \
|
||||
if (retMacro != OK) { \
|
||||
std::cout << "Failed to free memory on dvpp, ret = " << retMacro << "." << std::endl; \
|
||||
} \
|
||||
dvppDataPtr = nullptr; \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
class DvppCommon {
|
||||
public:
|
||||
explicit DvppCommon(aclrtStream dvppStream);
|
||||
~DvppCommon();
|
||||
int Init(void);
|
||||
int DeInit(void);
|
||||
|
||||
static int GetVpcDataSize(uint32_t widthVpc, uint32_t heightVpc, acldvppPixelFormat format,
|
||||
uint32_t *vpcSize);
|
||||
|
||||
static int GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride);
|
||||
|
||||
static int GetVpcOutputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride);
|
||||
|
||||
static void GetJpegDecodeStrideSize(uint32_t width, uint32_t height, uint32_t *widthStride, uint32_t *heightStride);
|
||||
static int GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t *width, uint32_t *height,
|
||||
int32_t *components);
|
||||
|
||||
static int GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format,
|
||||
uint32_t *decSize);
|
||||
|
||||
int VpcResize(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output, bool withSynchronize,
|
||||
VpcProcessType processType = VPC_PT_DEFAULT);
|
||||
|
||||
int JpegDecode(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output, bool withSynchronize);
|
||||
|
||||
int CombineResizeProcess(std::shared_ptr<DvppDataInfo> input, const DvppDataInfo &output, bool withSynchronize,
|
||||
VpcProcessType processType = VPC_PT_DEFAULT);
|
||||
|
||||
int CombineJpegdProcess(const RawData& imageInfo, acldvppPixelFormat format, bool withSynchronize);
|
||||
|
||||
std::shared_ptr<DvppDataInfo> GetInputImage();
|
||||
std::shared_ptr<DvppDataInfo> GetDecodedImage();
|
||||
std::shared_ptr<DvppDataInfo> GetResizedImage();
|
||||
|
||||
void ReleaseDvppBuffer();
|
||||
|
||||
private:
|
||||
int SetDvppPicDescData(std::shared_ptr<DvppDataInfo> dataInfo, std::shared_ptr<acldvppPicDesc>picDesc);
|
||||
int ResizeProcess(std::shared_ptr<acldvppPicDesc> inputDesc,
|
||||
std::shared_ptr<acldvppPicDesc> outputDesc, bool withSynchronize);
|
||||
|
||||
int ResizeWithPadding(std::shared_ptr<acldvppPicDesc> inputDesc, std::shared_ptr<acldvppPicDesc> outputDesc,
|
||||
const CropRoiConfig &cropRoi, const CropRoiConfig &pasteRoi, bool withSynchronize);
|
||||
|
||||
void GetCropRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *cropRoi);
|
||||
|
||||
void GetPasteRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *pasteRoi);
|
||||
|
||||
int CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output);
|
||||
int TransferImageH2D(const RawData& imageInfo, const std::shared_ptr<DvppDataInfo>& jpegInput);
|
||||
int CreateStreamDesc(std::shared_ptr<DvppDataInfo> data);
|
||||
int DestroyResource();
|
||||
|
||||
std::shared_ptr<acldvppRoiConfig> cropAreaConfig_ = nullptr;
|
||||
std::shared_ptr<acldvppRoiConfig> pasteAreaConfig_ = nullptr;
|
||||
|
||||
std::shared_ptr<acldvppPicDesc> resizeInputDesc_ = nullptr;
|
||||
std::shared_ptr<acldvppPicDesc> resizeOutputDesc_ = nullptr;
|
||||
std::shared_ptr<acldvppPicDesc> decodeOutputDesc_ = nullptr;
|
||||
std::shared_ptr<acldvppResizeConfig> resizeConfig_ = nullptr;
|
||||
|
||||
acldvppChannelDesc *dvppChannelDesc_ = nullptr;
|
||||
aclrtStream dvppStream_ = nullptr;
|
||||
std::shared_ptr<DvppDataInfo> inputImage_ = nullptr;
|
||||
std::shared_ptr<DvppDataInfo> decodedImage_ = nullptr;
|
||||
std::shared_ptr<DvppDataInfo> resizedImage_ = nullptr;
|
||||
};
|
||||
#endif
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright(C) 2020. Huawei Technologies Co.,Ltd. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 MODELPROCSS_H
|
||||
#define MODELPROCSS_H
|
||||
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "acl/acl.h"
|
||||
#include "CommonDataType.h"
|
||||
|
||||
class ModelProcess {
|
||||
public:
|
||||
explicit ModelProcess(const int deviceId);
|
||||
ModelProcess();
|
||||
~ModelProcess();
|
||||
|
||||
int Init(const std::string &modelPath);
|
||||
int DeInit();
|
||||
|
||||
int ModelInference(const std::vector<void *> &inputBufs,
|
||||
const std::vector<size_t> &inputSizes,
|
||||
const std::vector<void *> &ouputBufs,
|
||||
const std::vector<size_t> &outputSizes,
|
||||
std::map<double, double> *costTime_map);
|
||||
aclmdlDesc *GetModelDesc();
|
||||
int ReadBinaryFile(const std::string &fileName, uint8_t **buffShared, int *buffLength);
|
||||
|
||||
private:
|
||||
aclmdlDataset *CreateAndFillDataset(const std::vector<void *> &bufs, const std::vector<size_t> &sizes);
|
||||
void DestroyDataset(aclmdlDataset *dataset);
|
||||
|
||||
std::mutex mtx_ = {};
|
||||
int deviceId_ = 0;
|
||||
uint32_t modelId_ = 0;
|
||||
void *modelDevPtr_ = nullptr;
|
||||
size_t modelDevPtrSize_ = 0;
|
||||
void *weightDevPtr_ = nullptr;
|
||||
size_t weightDevPtrSize_ = 0;
|
||||
aclrtContext contextModel_ = nullptr;
|
||||
std::shared_ptr<aclmdlDesc> modelDesc_ = nullptr;
|
||||
bool isDeInit_ = false;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,355 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 "AclProcess.h"
|
||||
#include <sys/time.h>
|
||||
#include <thread>
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
* @description Implementation of constructor for class AclProcess with parameter list
|
||||
* @attention context is passed in as a parameter after being created in ResourceManager::InitResource
|
||||
*/
|
||||
AclProcess::AclProcess(int deviceId, const std::string &om_path, uint32_t width, uint32_t height)
|
||||
: deviceId_(deviceId), stream_(nullptr), modelProcess_(nullptr), dvppCommon_(nullptr), keepRatio_(false) {
|
||||
modelInfo_.modelPath = om_path;
|
||||
modelInfo_.modelWidth = width;
|
||||
modelInfo_.modelHeight = height;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Release all the resource
|
||||
* @attention context will be released in ResourceManager::Release
|
||||
*/
|
||||
void AclProcess::Release() {
|
||||
// Synchronize stream and release Dvpp channel
|
||||
dvppCommon_->DeInit();
|
||||
// Release stream
|
||||
if (stream_ != nullptr) {
|
||||
int ret = aclrtDestroyStream(stream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to destroy the stream, ret = " << ret << ".";
|
||||
}
|
||||
stream_ = nullptr;
|
||||
}
|
||||
// Destroy resources of modelProcess_
|
||||
modelProcess_->DeInit();
|
||||
|
||||
// Release Dvpp buffer
|
||||
dvppCommon_->ReleaseDvppBuffer();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Initialize the modules used by this sample
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::InitModule() {
|
||||
// Create Dvpp common object
|
||||
if (dvppCommon_ == nullptr) {
|
||||
dvppCommon_ = std::make_shared<DvppCommon>(stream_);
|
||||
int retDvppCommon = dvppCommon_->Init();
|
||||
if (retDvppCommon != OK) {
|
||||
std::cout << "Failed to initialize dvppCommon, ret = " << retDvppCommon << std::endl;
|
||||
return retDvppCommon;
|
||||
}
|
||||
}
|
||||
// Create model inference object
|
||||
if (modelProcess_ == nullptr) {
|
||||
modelProcess_ = std::make_shared<ModelProcess>(deviceId_);
|
||||
}
|
||||
// Initialize ModelProcess module
|
||||
int ret = modelProcess_->Init(modelInfo_.modelPath);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to initialize the model process module, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "Initialized the model process module successfully." << std::endl;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Create resource for this sample
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::InitResource() {
|
||||
int ret = aclInit(nullptr); // Initialize ACL
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to init acl, ret = " << ret << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = aclrtSetDevice(deviceId_);
|
||||
if (ret != ACL_SUCCESS) {
|
||||
std::cout << "acl set device " << deviceId_ << "intCode = "<< static_cast<int32_t>(ret) << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "set device "<< deviceId_ << " success" << std::endl;
|
||||
|
||||
// create context (set current)
|
||||
ret = aclrtCreateContext(&context_, deviceId_);
|
||||
if (ret != ACL_SUCCESS) {
|
||||
std::cout << "acl create context failed, deviceId = " << deviceId_ <<
|
||||
"intCode = "<< static_cast<int32_t>(ret) << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "create context success" << std::endl;
|
||||
|
||||
ret = aclrtCreateStream(&stream_); // Create stream for application
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to create the acl stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "Created the acl stream successfully." << std::endl;
|
||||
// Initialize dvpp module
|
||||
if (InitModule() != OK) {
|
||||
return INIT_FAIL;
|
||||
}
|
||||
|
||||
aclmdlDesc *modelDesc = modelProcess_->GetModelDesc();
|
||||
size_t outputSize = aclmdlGetNumOutputs(modelDesc);
|
||||
modelInfo_.outputNum = outputSize;
|
||||
for (size_t i = 0; i < outputSize; i++) {
|
||||
size_t bufferSize = aclmdlGetOutputSizeByIndex(modelDesc, i);
|
||||
void *outputBuffer = nullptr;
|
||||
ret = aclrtMalloc(&outputBuffer, bufferSize, ACL_MEM_MALLOC_NORMAL_ONLY);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc buffer, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
outputBuffers_.push_back(outputBuffer);
|
||||
outputSizes_.push_back(bufferSize);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int AclProcess::WriteResult(const std::string& imageFile) {
|
||||
std::string homePath = "./result_Files";
|
||||
void *resHostBuf = nullptr;
|
||||
for (size_t i = 0; i < outputBuffers_.size(); ++i) {
|
||||
size_t output_size;
|
||||
void * netOutput;
|
||||
netOutput = outputBuffers_[i];
|
||||
output_size = outputSizes_[i];
|
||||
int ret = aclrtMallocHost(&resHostBuf, output_size);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to print the result, malloc host failed, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = aclrtMemcpy(resHostBuf, output_size, netOutput,
|
||||
output_size, ACL_MEMCPY_DEVICE_TO_HOST);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to print result, memcpy device to host failed, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int pos = imageFile.rfind('/');
|
||||
std::string fileName(imageFile, pos + 1);
|
||||
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), "_" + std::to_string(i) + ".bin");
|
||||
|
||||
std::string outFileName = homePath + "/" + fileName;
|
||||
FILE * outputFile = fopen(outFileName.c_str(), "wb");
|
||||
fwrite(resHostBuf, output_size, sizeof(char), outputFile);
|
||||
|
||||
fclose(outputFile);
|
||||
outputFile = nullptr;
|
||||
ret = aclrtFreeHost(resHostBuf);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree host output memory failed" << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file, store it into the RawData structure
|
||||
*
|
||||
* @param filePath file to read to
|
||||
* @param fileData RawData structure to store in
|
||||
* @return OK if create success, int code otherwise
|
||||
*/
|
||||
int AclProcess::ReadFile(const std::string &filePath, RawData *fileData) {
|
||||
// Open file with reading mode
|
||||
FILE *fp = fopen(filePath.c_str(), "rb");
|
||||
if (fp == nullptr) {
|
||||
std::cout << "Failed to open file, filePath = " << filePath << std::endl;
|
||||
return OPEN_FILE_FAIL;
|
||||
}
|
||||
// Get the length of input file
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size_t fileSize = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
// If file not empty, read it into FileInfo and return it
|
||||
if (fileSize > 0) {
|
||||
fileData->lenOfByte = fileSize;
|
||||
fileData->data = std::make_shared<uint8_t>();
|
||||
fileData->data.reset(new uint8_t[fileSize], std::default_delete<uint8_t[]>());
|
||||
uint32_t readRet = fread(fileData->data.get(), 1, fileSize, fp);
|
||||
if (readRet == 0) {
|
||||
fclose(fp);
|
||||
return READ_FILE_FAIL;
|
||||
}
|
||||
fclose(fp);
|
||||
return OK;
|
||||
}
|
||||
fclose(fp);
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
/*
|
||||
* @description Preprocess the input image
|
||||
* @param imageFile input image path
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::Preprocess(const std::string& imageFile) {
|
||||
RawData imageInfo;
|
||||
int ret = ReadFile(imageFile, &imageInfo); // Read image data from input image file
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to read file, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
// Run process of jpegD
|
||||
ret = dvppCommon_->CombineJpegdProcess(imageInfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420, true);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to execute image decoded of preprocess module, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
// Get output of decode jpeg image
|
||||
std::shared_ptr<DvppDataInfo> decodeOutData = dvppCommon_->GetDecodedImage();
|
||||
// Run resize application function
|
||||
DvppDataInfo resizeOutData;
|
||||
resizeOutData.height = modelInfo_.modelHeight;
|
||||
resizeOutData.width = modelInfo_.modelWidth;
|
||||
resizeOutData.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
|
||||
ret = dvppCommon_->CombineResizeProcess(decodeOutData, resizeOutData, true, VPC_PT_DEFAULT);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to execute image resized of preprocess module, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
RELEASE_DVPP_DATA(decodeOutData->data);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Inference of model
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::ModelInfer(std::map<double, double> *costTime_map) {
|
||||
// Get output of resize module
|
||||
std::shared_ptr<DvppDataInfo> resizeOutData = dvppCommon_->GetResizedImage();
|
||||
std::shared_ptr<DvppDataInfo> inputImg = dvppCommon_->GetInputImage();
|
||||
|
||||
float widthScale, heightScale;
|
||||
if (keepRatio_) {
|
||||
widthScale = static_cast<float>(resizeOutData->width) / inputImg->width;
|
||||
if (widthScale > static_cast<float>(resizeOutData->height) / inputImg->height) {
|
||||
widthScale = static_cast<float>(resizeOutData->height) / inputImg->height;
|
||||
}
|
||||
heightScale = widthScale;
|
||||
} else {
|
||||
widthScale = static_cast<float>(resizeOutData->width) / inputImg->width;
|
||||
heightScale = static_cast<float>(resizeOutData->height) / inputImg->height;
|
||||
}
|
||||
|
||||
aclFloat16 inputWidth = aclFloatToFloat16(static_cast<float>(inputImg->width));
|
||||
aclFloat16 inputHeight = aclFloatToFloat16(static_cast<float>(inputImg->height));
|
||||
aclFloat16 resizeWidthRatioFp16 = aclFloatToFloat16(widthScale);
|
||||
aclFloat16 resizeHeightRatioFp16 = aclFloatToFloat16(heightScale);
|
||||
|
||||
aclFloat16 *im_info = reinterpret_cast<aclFloat16 *>(malloc(sizeof(aclFloat16) * 4));
|
||||
im_info[0] = inputHeight;
|
||||
im_info[1] = inputWidth;
|
||||
im_info[2] = resizeHeightRatioFp16;
|
||||
im_info[3] = resizeWidthRatioFp16;
|
||||
void *imInfo_dst = nullptr;
|
||||
int ret = aclrtMalloc(&imInfo_dst, 8, ACL_MEM_MALLOC_NORMAL_ONLY);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
std::cout << "aclrtMalloc failed, ret = " << ret << std::endl;
|
||||
aclrtFree(imInfo_dst);
|
||||
return ret;
|
||||
}
|
||||
ret = aclrtMemcpy(reinterpret_cast<uint8_t *>(imInfo_dst), 8, im_info, 8, ACL_MEMCPY_HOST_TO_DEVICE);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
std::cout << "aclrtMemcpy failed, ret = " << ret << std::endl;
|
||||
aclrtFree(imInfo_dst);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<void *> inputBuffers({resizeOutData->data, imInfo_dst});
|
||||
std::vector<size_t> inputSizes({resizeOutData->dataSize, 4*2});
|
||||
|
||||
for (size_t i = 0; i < modelInfo_.outputNum; i++) {
|
||||
aclrtMemset(outputBuffers_[i], outputSizes_[i], 0, outputSizes_[i]);
|
||||
}
|
||||
// Execute classification model
|
||||
ret = modelProcess_->ModelInference(inputBuffers, inputSizes, outputBuffers_, outputSizes_, costTime_map);
|
||||
if (ret != OK) {
|
||||
aclrtFree(imInfo_dst);
|
||||
std::cout << "Failed to execute the classification model, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = aclrtFree(imInfo_dst);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree image info failed" << std::endl;
|
||||
return ret;
|
||||
}
|
||||
RELEASE_DVPP_DATA(resizeOutData->data);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Process classification
|
||||
*
|
||||
* @par Function
|
||||
* 1.Dvpp module preprocess
|
||||
* 2.Execute classification model
|
||||
* 3.Execute single operator
|
||||
* 4.Write result
|
||||
*
|
||||
* @param imageFile input file path
|
||||
* @return int int code
|
||||
*/
|
||||
|
||||
int AclProcess::Process(const std::string& imageFile, std::map<double, double> *costTime_map) {
|
||||
struct timeval begin = {0};
|
||||
struct timeval end = {0};
|
||||
gettimeofday(&begin, nullptr);
|
||||
|
||||
int ret = Preprocess(imageFile);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = ModelInfer(costTime_map);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = WriteResult(imageFile);
|
||||
if (ret != OK) {
|
||||
std::cout << "write result failed." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
gettimeofday(&end, nullptr);
|
||||
|
||||
const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
|
||||
std::cout << "[Process Delay] cost: " << costMs << "ms." << std::endl;
|
||||
return OK;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
|
||||
|
||||
# CMake lowest version requirement
|
||||
cmake_minimum_required(VERSION 3.5.1)
|
||||
# Add definitions ENABLE_DVPP_INTERFACE to use dvpp api
|
||||
add_definitions(-DENABLE_DVPP_INTERFACE)
|
||||
# project information
|
||||
project(InferClassification)
|
||||
# Check environment variable
|
||||
if(NOT DEFINED ENV{ASCEND_HOME})
|
||||
message(FATAL_ERROR "please define environment variable:ASCEND_HOME")
|
||||
endif()
|
||||
|
||||
# Compile options
|
||||
add_compile_options(-std=c++11 -fPIE -g -fstack-protector-all -Werror -Wreturn-type)
|
||||
|
||||
# Skip build rpath
|
||||
set(CMAKE_SKIP_BUILD_RPATH True)
|
||||
|
||||
# Set output directory
|
||||
set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/out)
|
||||
|
||||
# Set include directory and library directory
|
||||
#set(ACL_INC_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/include)
|
||||
#set(ACL_LIB_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/lib64/stub)
|
||||
set(ACL_INC_DIR $ENV{ASCEND_HOME}/acllib/include)
|
||||
set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib/lib64/stub)
|
||||
# Header path
|
||||
include_directories(${ACL_INC_DIR})
|
||||
include_directories(${PROJECT_SRC_ROOT}/../inc)
|
||||
|
||||
# add host lib path
|
||||
link_directories(${ACL_LIB_DIR})
|
||||
|
||||
add_executable(main AclProcess.cpp
|
||||
DvppCommon.cpp
|
||||
ModelProcess.cpp
|
||||
main.cpp)
|
||||
|
||||
target_link_libraries(main ascendcl gflags acl_dvpp pthread)
|
|
@ -0,0 +1,735 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "../inc/DvppCommon.h"
|
||||
#include "../inc/CommonDataType.h"
|
||||
|
||||
static auto g_resizeConfigDeleter = [](acldvppResizeConfig *p) { acldvppDestroyResizeConfig(p); };
|
||||
static auto g_picDescDeleter = [](acldvppPicDesc *picDesc) { acldvppDestroyPicDesc(picDesc); };
|
||||
static auto g_roiConfigDeleter = [](acldvppRoiConfig *p) { acldvppDestroyRoiConfig(p); };
|
||||
|
||||
DvppCommon::DvppCommon(aclrtStream dvppStream):dvppStream_(dvppStream) {}
|
||||
|
||||
/*
|
||||
* @description: Create a channel for processing image data,
|
||||
* the channel description is created by acldvppCreateChannelDesc
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::Init(void) {
|
||||
dvppChannelDesc_ = acldvppCreateChannelDesc();
|
||||
if (dvppChannelDesc_ == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
int ret = acldvppCreateChannel(dvppChannelDesc_);
|
||||
if (ret != 0) {
|
||||
std::cout << "Failed to create dvpp channel, ret = " << ret << "." << std::endl;
|
||||
acldvppDestroyChannelDesc(dvppChannelDesc_);
|
||||
dvppChannelDesc_ = nullptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: destroy the channel and the channel description used by image.
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::DeInit(void) {
|
||||
int ret = aclrtSynchronizeStream(dvppStream_); // int ret
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppDestroyChannel(dvppChannelDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to destory dvpp channel, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppDestroyChannelDesc(dvppChannelDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to destroy dvpp channel description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Release the memory that is allocated in the interfaces which are started with "Combine"
|
||||
*/
|
||||
void DvppCommon::ReleaseDvppBuffer() {
|
||||
if (resizedImage_ != nullptr) {
|
||||
RELEASE_DVPP_DATA(resizedImage_->data);
|
||||
}
|
||||
if (decodedImage_ != nullptr) {
|
||||
RELEASE_DVPP_DATA(decodedImage_->data);
|
||||
}
|
||||
if (inputImage_ != nullptr) {
|
||||
RELEASE_DVPP_DATA(inputImage_->data);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the size of buffer used to save image for VPC according to width, height and format
|
||||
* @param width specifies the width of the output image
|
||||
* @param height specifies the height of the output image
|
||||
* @param format specifies the format of the output image
|
||||
* @param: vpcSize is used to save the result size
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetVpcDataSize(uint32_t width, uint32_t height, acldvppPixelFormat format, uint32_t *vpcSize) {
|
||||
if (format != PIXEL_FORMAT_YUV_SEMIPLANAR_420 && format != PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
|
||||
std::cout << "Format[" << format << "] for VPC is not supported, just support NV12 or NV21." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
uint32_t widthStride = DVPP_ALIGN_UP(width, VPC_WIDTH_ALIGN);
|
||||
uint32_t heightStride = DVPP_ALIGN_UP(height, VPC_HEIGHT_ALIGN);
|
||||
*vpcSize = widthStride * heightStride * YUV_BGR_SIZE_CONVERT_3 / YUV_BGR_SIZE_CONVERT_2;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the aligned width and height of the input image according to the image format
|
||||
* @param: width specifies the width before alignment
|
||||
* @param: height specifies the height before alignment
|
||||
* @param: format specifies the image format
|
||||
* @param: widthStride is used to save the width after alignment
|
||||
* @param: heightStride is used to save the height after alignment
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride) {
|
||||
uint32_t inputWidthStride;
|
||||
if (format >= PIXEL_FORMAT_YUV_400 && format <= PIXEL_FORMAT_YVU_SEMIPLANAR_444) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH);
|
||||
} else if (format >= PIXEL_FORMAT_YUYV_PACKED_422 && format <= PIXEL_FORMAT_VYUY_PACKED_422) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH) * YUV422_WIDTH_NU;
|
||||
} else if (format >= PIXEL_FORMAT_YUV_PACKED_444 && format <= PIXEL_FORMAT_BGR_888) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH) * YUV444_RGB_WIDTH_NU;
|
||||
} else if (format >= PIXEL_FORMAT_ARGB_8888 && format <= PIXEL_FORMAT_BGRA_8888) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH) * XRGB_WIDTH_NU;
|
||||
} else {
|
||||
std::cout << "Input format[" << format << "] for VPC is invalid, please check it." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
uint32_t inputHeightStride = DVPP_ALIGN_UP(height, VPC_STRIDE_HEIGHT);
|
||||
if (inputWidthStride > MAX_RESIZE_WIDTH || inputWidthStride < MIN_RESIZE_WIDTH) {
|
||||
std::cout << "Input width stride " << inputWidthStride << " is invalid, not in [" << MIN_RESIZE_WIDTH \
|
||||
<< ", " << MAX_RESIZE_WIDTH << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
if (inputHeightStride > MAX_RESIZE_HEIGHT || inputHeightStride < MIN_RESIZE_HEIGHT) {
|
||||
std::cout << "Input height stride " << inputHeightStride << " is invalid, not in [" << MIN_RESIZE_HEIGHT \
|
||||
<< ", " << MAX_RESIZE_HEIGHT << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
*widthStride = inputWidthStride;
|
||||
*heightStride = inputHeightStride;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the aligned width and height of the output image according to the image format
|
||||
* @param: width specifies the width before alignment
|
||||
* @param: height specifies the height before alignment
|
||||
* @param: format specifies the image format
|
||||
* @param: widthStride is used to save the width after alignment
|
||||
* @param: heightStride is used to save the height after alignment
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetVpcOutputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride) {
|
||||
if (format != PIXEL_FORMAT_YUV_SEMIPLANAR_420 && format != PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
|
||||
std::cout << "Output format[" << format << "] is not supported, just support NV12 or NV21." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
*widthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH);
|
||||
*heightStride = DVPP_ALIGN_UP(height, VPC_STRIDE_HEIGHT);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Set picture description information and execute resize function
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @param: processType specifies whether to perform proportional scaling, default is non-proportional resize
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::VpcResize(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
bool withSynchronize, VpcProcessType processType) {
|
||||
acldvppPicDesc *inputDesc = acldvppCreatePicDesc();
|
||||
acldvppPicDesc *outputDesc = acldvppCreatePicDesc();
|
||||
resizeInputDesc_.reset(inputDesc, g_picDescDeleter);
|
||||
resizeOutputDesc_.reset(outputDesc, g_picDescDeleter);
|
||||
|
||||
// Set dvpp picture descriptin info of input image
|
||||
int ret = SetDvppPicDescData(input, resizeInputDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set dvpp input picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Set dvpp picture descriptin info of output image
|
||||
ret = SetDvppPicDescData(output, resizeOutputDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set dvpp output picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (processType == VPC_PT_DEFAULT) {
|
||||
return ResizeProcess(resizeInputDesc_, resizeOutputDesc_, withSynchronize);
|
||||
}
|
||||
|
||||
// Get crop area according to the processType
|
||||
CropRoiConfig cropRoi = {0};
|
||||
GetCropRoi(input, output, processType, &cropRoi);
|
||||
|
||||
// The width and height of the original image will be resized by the same ratio
|
||||
CropRoiConfig pasteRoi = {0};
|
||||
GetPasteRoi(input, output, processType, &pasteRoi);
|
||||
|
||||
return ResizeWithPadding(resizeInputDesc_, resizeOutputDesc_, cropRoi, pasteRoi, withSynchronize);
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Set image description information
|
||||
* @param: dataInfo specifies the image information
|
||||
* @param: picsDesc specifies the picture description information to be set
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::SetDvppPicDescData(std::shared_ptr<DvppDataInfo> dataInfo, std::shared_ptr<acldvppPicDesc>picDesc) {
|
||||
int ret = acldvppSetPicDescData(picDesc.get(), dataInfo->data);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set data for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescSize(picDesc.get(), dataInfo->dataSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set size for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescFormat(picDesc.get(), dataInfo->format);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set format for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescWidth(picDesc.get(), dataInfo->width);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set width for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescHeight(picDesc.get(), dataInfo->height);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set height for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppSetPicDescWidthStride(picDesc.get(), dataInfo->widthStride);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set aligned width for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescHeightStride(picDesc.get(), dataInfo->heightStride);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set aligned height for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Check whether the image format and zoom ratio meet the requirements
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output) {
|
||||
if (output.format != PIXEL_FORMAT_YUV_SEMIPLANAR_420 && output.format != PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
|
||||
std::cout << "Output format[" << output.format << "]is not supported, just support NV12 or NV21." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
float heightScale = static_cast<float>(output.height) / input.height;
|
||||
if (heightScale < MIN_RESIZE_SCALE || heightScale > MAX_RESIZE_SCALE) {
|
||||
std::cout << "Resize scale should be in range [1/16, 32], which is " << heightScale << "." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
float widthScale = static_cast<float>(output.width) / input.width;
|
||||
if (widthScale < MIN_RESIZE_SCALE || widthScale > MAX_RESIZE_SCALE) {
|
||||
std::cout << "Resize scale should be in range [1/16, 32], which is " << widthScale << "." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Scale the input image to the size specified by the output image and
|
||||
* saves the result to the output image (non-proportionate scaling)
|
||||
* @param: inputDesc specifies the description information of the input image
|
||||
* @param: outputDesc specifies the description information of the output image
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::ResizeProcess(std::shared_ptr<acldvppPicDesc>inputDesc,
|
||||
std::shared_ptr<acldvppPicDesc>outputDesc,
|
||||
bool withSynchronize) {
|
||||
acldvppResizeConfig *resizeConfig = acldvppCreateResizeConfig();
|
||||
if (resizeConfig == nullptr) {
|
||||
std::cout << "Failed to create dvpp resize config." << std::endl;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
|
||||
resizeConfig_.reset(resizeConfig, g_resizeConfigDeleter);
|
||||
int ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc.get(), outputDesc.get(),
|
||||
resizeConfig_.get(), dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to resize asynchronously, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (withSynchronize) {
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Crop the image from the input image based on the specified area and
|
||||
* paste the cropped image to the specified position of the target image
|
||||
* as the output image
|
||||
* @param: inputDesc specifies the description information of the input image
|
||||
* @param: outputDesc specifies the description information of the output image
|
||||
* @param: cropRoi specifies the cropped area
|
||||
* @param: pasteRoi specifies the pasting area
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: If the width and height of the crop area are different from those of the
|
||||
* paste area, the image is scaled again
|
||||
*/
|
||||
int DvppCommon::ResizeWithPadding(std::shared_ptr<acldvppPicDesc> inputDesc,
|
||||
std::shared_ptr<acldvppPicDesc> outputDesc,
|
||||
const CropRoiConfig &cropRoi, const CropRoiConfig &pasteRoi, bool withSynchronize) {
|
||||
acldvppRoiConfig *cropRoiCfg = acldvppCreateRoiConfig(cropRoi.left, cropRoi.right, cropRoi.up, cropRoi.down);
|
||||
if (cropRoiCfg == nullptr) {
|
||||
std::cout << "Failed to create dvpp roi config for corp area." << std::endl;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
cropAreaConfig_.reset(cropRoiCfg, g_roiConfigDeleter);
|
||||
|
||||
acldvppRoiConfig *pastRoiCfg = acldvppCreateRoiConfig(pasteRoi.left, pasteRoi.right, pasteRoi.up, pasteRoi.down);
|
||||
if (pastRoiCfg == nullptr) {
|
||||
std::cout << "Failed to create dvpp roi config for paster area." << std::endl;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
pasteAreaConfig_.reset(pastRoiCfg, g_roiConfigDeleter);
|
||||
|
||||
int ret = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, inputDesc.get(), outputDesc.get(), cropAreaConfig_.get(),
|
||||
pasteAreaConfig_.get(), dvppStream_);
|
||||
if (ret != OK) {
|
||||
// release resource.
|
||||
std::cout << "Failed to crop and paste asynchronously, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (withSynchronize) {
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed tp synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get crop area
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: processType specifies whether to perform proportional scaling
|
||||
* @param: cropRoi is used to save the info of the crop roi area
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
void DvppCommon::GetCropRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *cropRoi) {
|
||||
// When processType is not VPC_PT_FILL, crop area is the whole input image
|
||||
if (processType != VPC_PT_FILL) {
|
||||
cropRoi->right = CONVERT_TO_ODD(input->width - ODD_NUM_1);
|
||||
cropRoi->down = CONVERT_TO_ODD(input->height - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
bool widthRatioSmaller = true;
|
||||
// The scaling ratio is based on the smaller ratio to ensure the smallest edge to fill the targe edge
|
||||
float resizeRatio = static_cast<float>(input->width) / output->width;
|
||||
if (resizeRatio > (static_cast<float>(input->height) / output->height)) {
|
||||
resizeRatio = static_cast<float>(input->height) / output->height;
|
||||
widthRatioSmaller = false;
|
||||
}
|
||||
|
||||
const int halfValue = 2;
|
||||
// The left and up must be even, right and down must be odd which is required by acl
|
||||
if (widthRatioSmaller) {
|
||||
cropRoi->left = 0;
|
||||
cropRoi->right = CONVERT_TO_ODD(input->width - ODD_NUM_1);
|
||||
cropRoi->up = CONVERT_TO_EVEN(static_cast<uint32_t>((input->height - output->height * resizeRatio) /
|
||||
halfValue));
|
||||
cropRoi->down = CONVERT_TO_ODD(input->height - cropRoi->up - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
cropRoi->up = 0;
|
||||
cropRoi->down = CONVERT_TO_ODD(input->height - ODD_NUM_1);
|
||||
cropRoi->left = CONVERT_TO_EVEN(static_cast<uint32_t>((input->width - output->width * resizeRatio) / halfValue));
|
||||
cropRoi->right = CONVERT_TO_ODD(input->width - cropRoi->left - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get paste area
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: processType specifies whether to perform proportional scaling
|
||||
* @param: pasteRio is used to save the info of the paste area
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
void DvppCommon::GetPasteRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *pasteRoi) {
|
||||
if (processType == VPC_PT_FILL) {
|
||||
pasteRoi->right = CONVERT_TO_ODD(output->width - ODD_NUM_1);
|
||||
pasteRoi->down = CONVERT_TO_ODD(output->height - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
bool widthRatioLarger = true;
|
||||
// The scaling ratio is based on the larger ratio to ensure the largest edge to fill the targe edge
|
||||
float resizeRatio = static_cast<float>(input->width) / output->width;
|
||||
if (resizeRatio < (static_cast<float>(input->height) / output->height)) {
|
||||
resizeRatio = static_cast<float>(input->height) / output->height;
|
||||
widthRatioLarger = false;
|
||||
}
|
||||
|
||||
// Left and up is 0 when the roi paste on the upper left corner
|
||||
if (processType == VPC_PT_PADDING) {
|
||||
pasteRoi->right = (input->width / resizeRatio) - ODD_NUM_1;
|
||||
pasteRoi->down = (input->height / resizeRatio) - ODD_NUM_1;
|
||||
pasteRoi->right = CONVERT_TO_ODD(pasteRoi->right);
|
||||
pasteRoi->down = CONVERT_TO_ODD(pasteRoi->down);
|
||||
return;
|
||||
}
|
||||
|
||||
const int halfValue = 2;
|
||||
// Left and up is 0 when the roi paste on the middler location
|
||||
if (widthRatioLarger) {
|
||||
pasteRoi->left = 0;
|
||||
pasteRoi->right = output->width - ODD_NUM_1;
|
||||
pasteRoi->up = (output->height - (input->height / resizeRatio)) / halfValue;
|
||||
pasteRoi->down = output->height - pasteRoi->up - ODD_NUM_1;
|
||||
} else {
|
||||
pasteRoi->up = 0;
|
||||
pasteRoi->down = output->height - ODD_NUM_1;
|
||||
pasteRoi->left = (output->width - (input->width / resizeRatio)) / halfValue;
|
||||
pasteRoi->right = output->width - pasteRoi->left - ODD_NUM_1;
|
||||
}
|
||||
|
||||
// The left must be even and align to 16, up must be even, right and down must be odd which is required by acl
|
||||
pasteRoi->left = DVPP_ALIGN_UP(CONVERT_TO_EVEN(pasteRoi->left), VPC_WIDTH_ALIGN);
|
||||
pasteRoi->right = CONVERT_TO_ODD(pasteRoi->right);
|
||||
pasteRoi->up = CONVERT_TO_EVEN(pasteRoi->up);
|
||||
pasteRoi->down = CONVERT_TO_ODD(pasteRoi->down);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Resize the image specified by input and save the result to member variable resizedImage_
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @param: processType specifies whether to perform proportional scaling, default is non-proportional resize
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::CombineResizeProcess(std::shared_ptr<DvppDataInfo> input, const DvppDataInfo &output,
|
||||
bool withSynchronize, VpcProcessType processType) {
|
||||
int ret = CheckResizeParams(*input, output);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
// Get widthStride and heightStride for input and output image according to the format
|
||||
ret = GetVpcInputStrideSize(input->widthStride, input->heightStride, input->format,
|
||||
&(input->widthStride), &(input->heightStride));
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
resizedImage_ = std::make_shared<DvppDataInfo>();
|
||||
resizedImage_->width = output.width;
|
||||
resizedImage_->height = output.height;
|
||||
resizedImage_->format = output.format;
|
||||
ret = GetVpcOutputStrideSize(output.width, output.height, output.format, &(resizedImage_->widthStride),
|
||||
&(resizedImage_->heightStride));
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
// Get output buffer size for resize output
|
||||
ret = GetVpcDataSize(output.width, output.height, output.format, &(resizedImage_->dataSize));
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
// Malloc buffer for output of resize module
|
||||
// Need to pay attention to release of the buffer
|
||||
ret = acldvppMalloc(reinterpret_cast<void **>(&(resizedImage_->data)), resizedImage_->dataSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc " << resizedImage_->dataSize << " bytes on dvpp for resize" << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
aclrtMemset(resizedImage_->data, resizedImage_->dataSize, YUV_GREYER_VALUE, resizedImage_->dataSize);
|
||||
resizedImage_->frameId = input->frameId;
|
||||
ret = VpcResize(input, resizedImage_, withSynchronize, processType);
|
||||
if (ret != OK) {
|
||||
// Release the output buffer when resize failed, otherwise release it after use
|
||||
RELEASE_DVPP_DATA(resizedImage_->data);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Set the description of the output image and decode
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::JpegDecode(std::shared_ptr<DvppDataInfo> input,
|
||||
std::shared_ptr<DvppDataInfo> output,
|
||||
bool withSynchronize) {
|
||||
acldvppPicDesc *outputDesc = acldvppCreatePicDesc();
|
||||
decodeOutputDesc_.reset(outputDesc, g_picDescDeleter);
|
||||
|
||||
int ret = SetDvppPicDescData(output, decodeOutputDesc_);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppJpegDecodeAsync(dvppChannelDesc_, input->data, input->dataSize, decodeOutputDesc_.get(), dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to decode jpeg, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (withSynchronize) {
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return DECODE_FAIL;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the aligned width and height of the image after decoding
|
||||
* @param: width specifies the width before alignment
|
||||
* @param: height specifies the height before alignment
|
||||
* @param: widthStride is used to save the width after alignment
|
||||
* @param: heightStride is used to save the height after alignment
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
void DvppCommon::GetJpegDecodeStrideSize(uint32_t width, uint32_t height,
|
||||
uint32_t *widthStride, uint32_t *heightStride) {
|
||||
*widthStride = DVPP_ALIGN_UP(width, JPEGD_STRIDE_WIDTH);
|
||||
*heightStride = DVPP_ALIGN_UP(height, JPEGD_STRIDE_HEIGHT);
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get picture width and height and number of channels from image data
|
||||
* @param: data specifies the memory to store the image data
|
||||
* @param: dataSize specifies the size of the image data
|
||||
* @param: width is used to save the image width
|
||||
* @param: height is used to save the image height
|
||||
* @param: components is used to save the number of channels
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t *width, uint32_t *height,
|
||||
int32_t *components) {
|
||||
uint32_t widthTmp;
|
||||
uint32_t heightTmp;
|
||||
int32_t componentsTmp;
|
||||
int ret = acldvppJpegGetImageInfo(data, dataSize, &widthTmp, &heightTmp, &componentsTmp);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to get image info of jpeg, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (widthTmp > MAX_JPEGD_WIDTH || widthTmp < MIN_JPEGD_WIDTH) {
|
||||
std::cout << "Input width is invalid, not in [" << MIN_JPEGD_WIDTH << ", "
|
||||
<< MAX_JPEGD_WIDTH << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
if (heightTmp > MAX_JPEGD_HEIGHT || heightTmp < MIN_JPEGD_HEIGHT) {
|
||||
std::cout << "Input height is invalid, not in [" << MIN_JPEGD_HEIGHT << ", "
|
||||
<< MAX_JPEGD_HEIGHT << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
*width = widthTmp;
|
||||
*height = heightTmp;
|
||||
*components = componentsTmp;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the size of the buffer for storing decoded images based on the image data, size, and format
|
||||
* @param: data specifies the memory to store the image data
|
||||
* @param: dataSize specifies the size of the image data
|
||||
* @param: format specifies the image format
|
||||
* @param: decSize is used to store the result size
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format,
|
||||
uint32_t *decSize) {
|
||||
uint32_t outputSize;
|
||||
int ret = acldvppJpegPredictDecSize(data, dataSize, format, &outputSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to predict decode size of jpeg image, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
*decSize = outputSize;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Decode the image specified by imageInfo and save the result to member variable decodedImage_
|
||||
* @param: imageInfo specifies image information
|
||||
* @param: format specifies the image format
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::CombineJpegdProcess(const RawData& imageInfo, acldvppPixelFormat format, bool withSynchronize) {
|
||||
int32_t components;
|
||||
inputImage_ = std::make_shared<DvppDataInfo>();
|
||||
inputImage_->format = format;
|
||||
int ret = GetJpegImageInfo(imageInfo.data.get(), imageInfo.lenOfByte, &(inputImage_->width), &(inputImage_->height),
|
||||
&components);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to get input image info, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Get the buffer size of decode output according to the input data and output format
|
||||
uint32_t outBuffSize;
|
||||
ret = GetJpegDecodeDataSize(imageInfo.data.get(), imageInfo.lenOfByte, format, &outBuffSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to get size of decode output buffer, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// In TransferImageH2D function, device buffer will be alloced to store the input image
|
||||
// Need to pay attention to release of the buffer
|
||||
ret = TransferImageH2D(imageInfo, inputImage_);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
decodedImage_ = std::make_shared<DvppDataInfo>();
|
||||
decodedImage_->format = format;
|
||||
decodedImage_->width = inputImage_->width;
|
||||
decodedImage_->height = inputImage_->height;
|
||||
GetJpegDecodeStrideSize(inputImage_->width, inputImage_->height, &(decodedImage_->widthStride),
|
||||
&(decodedImage_->heightStride));
|
||||
decodedImage_->dataSize = outBuffSize;
|
||||
// Need to pay attention to release of the buffer
|
||||
ret = acldvppMalloc(reinterpret_cast<void **>(&decodedImage_->data), decodedImage_->dataSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc memory on dvpp, ret = " << ret << "." << std::endl;
|
||||
RELEASE_DVPP_DATA(inputImage_->data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = JpegDecode(inputImage_, decodedImage_, withSynchronize);
|
||||
if (ret != OK) {
|
||||
RELEASE_DVPP_DATA(inputImage_->data);
|
||||
inputImage_->data = nullptr;
|
||||
RELEASE_DVPP_DATA(decodedImage_->data);
|
||||
decodedImage_->data = nullptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Transfer data from host to device
|
||||
* @param: imageInfo specifies the image data on the host
|
||||
* @param: jpegInput is used to save the buffer and its size which is allocate on the device
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::TransferImageH2D(const RawData& imageInfo, const std::shared_ptr<DvppDataInfo>& jpegInput) {
|
||||
if (imageInfo.lenOfByte == 0) {
|
||||
std::cout << "The input buffer size on host should not be empty." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
uint8_t* inDevBuff = nullptr;
|
||||
int ret = acldvppMalloc(reinterpret_cast<void **>(&inDevBuff), imageInfo.lenOfByte);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc " << imageInfo.lenOfByte << " bytes on dvpp, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Copy the image data from host to device
|
||||
ret = aclrtMemcpyAsync(inDevBuff, imageInfo.lenOfByte, imageInfo.data.get(), imageInfo.lenOfByte,
|
||||
ACL_MEMCPY_HOST_TO_DEVICE, dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to copy " << imageInfo.lenOfByte << " bytes from host to device" << std::endl;
|
||||
RELEASE_DVPP_DATA(inDevBuff);
|
||||
return ret;
|
||||
}
|
||||
// Attention: We must call the aclrtSynchronizeStream to ensure the task of memory replication has been completed
|
||||
// after calling aclrtMemcpyAsync
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
RELEASE_DVPP_DATA(inDevBuff);
|
||||
return ret;
|
||||
}
|
||||
jpegInput->data = inDevBuff;
|
||||
jpegInput->dataSize = imageInfo.lenOfByte;
|
||||
return OK;
|
||||
}
|
||||
|
||||
std::shared_ptr<DvppDataInfo> DvppCommon::GetInputImage() {
|
||||
return inputImage_;
|
||||
}
|
||||
|
||||
std::shared_ptr<DvppDataInfo> DvppCommon::GetDecodedImage() {
|
||||
return decodedImage_;
|
||||
}
|
||||
|
||||
std::shared_ptr<DvppDataInfo> DvppCommon::GetResizedImage() {
|
||||
return resizedImage_;
|
||||
}
|
||||
|
||||
DvppCommon::~DvppCommon() {}
|
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
* Copyright(C) 2020. Huawei Technologies Co.,Ltd. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 <sys/time.h>
|
||||
#include <fstream>
|
||||
#include "../inc/ModelProcess.h"
|
||||
|
||||
ModelProcess::ModelProcess(const int deviceId) {
|
||||
deviceId_ = deviceId;
|
||||
}
|
||||
|
||||
ModelProcess::ModelProcess() {}
|
||||
|
||||
ModelProcess::~ModelProcess() {
|
||||
if (!isDeInit_) {
|
||||
DeInit();
|
||||
}
|
||||
}
|
||||
|
||||
void ModelProcess::DestroyDataset(aclmdlDataset *dataset) {
|
||||
// Just release the DataBuffer object and DataSet object, remain the buffer, because it is managerd by user
|
||||
if (dataset != nullptr) {
|
||||
for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(dataset); i++) {
|
||||
aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(dataset, i);
|
||||
if (dataBuffer != nullptr) {
|
||||
aclDestroyDataBuffer(dataBuffer);
|
||||
dataBuffer = nullptr;
|
||||
}
|
||||
}
|
||||
aclmdlDestroyDataset(dataset);
|
||||
}
|
||||
}
|
||||
|
||||
aclmdlDesc *ModelProcess::GetModelDesc() {
|
||||
return modelDesc_.get();
|
||||
}
|
||||
|
||||
int ModelProcess::ModelInference(const std::vector<void *> &inputBufs,
|
||||
const std::vector<size_t> &inputSizes,
|
||||
const std::vector<void *> &ouputBufs,
|
||||
const std::vector<size_t> &outputSizes,
|
||||
std::map<double, double> *costTime_map) {
|
||||
std::cout << "ModelProcess:Begin to inference." << std::endl;
|
||||
aclmdlDataset *input = nullptr;
|
||||
input = CreateAndFillDataset(inputBufs, inputSizes);
|
||||
if (input == nullptr) {
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
int ret = 0;
|
||||
|
||||
aclmdlDataset *output = nullptr;
|
||||
output = CreateAndFillDataset(ouputBufs, outputSizes);
|
||||
if (output == nullptr) {
|
||||
DestroyDataset(input);
|
||||
input = nullptr;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
struct timeval start;
|
||||
struct timeval end;
|
||||
double startTime_ms;
|
||||
double endTime_ms;
|
||||
mtx_.lock();
|
||||
gettimeofday(&start, NULL);
|
||||
ret = aclmdlExecute(modelId_, input, output);
|
||||
gettimeofday(&end, NULL);
|
||||
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
|
||||
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
|
||||
costTime_map->insert(std::pair<double, double>(startTime_ms, endTime_ms));
|
||||
mtx_.unlock();
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlExecute failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
DestroyDataset(input);
|
||||
DestroyDataset(output);
|
||||
return OK;
|
||||
}
|
||||
|
||||
int ModelProcess::DeInit() {
|
||||
isDeInit_ = true;
|
||||
int ret = aclmdlUnload(modelId_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlUnload failed, ret["<< ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (modelDevPtr_ != nullptr) {
|
||||
ret = aclrtFree(modelDevPtr_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
modelDevPtr_ = nullptr;
|
||||
}
|
||||
if (weightDevPtr_ != nullptr) {
|
||||
ret = aclrtFree(weightDevPtr_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
weightDevPtr_ = nullptr;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
/**
|
||||
* Read a binary file, store the data into a uint8_t array
|
||||
*
|
||||
* @param fileName the file for reading
|
||||
* @param buffShared a shared pointer to a uint8_t array for storing file
|
||||
* @param buffLength the length of the array
|
||||
* @return OK if create success, error code otherwise
|
||||
*/
|
||||
int ModelProcess::ReadBinaryFile(const std::string &fileName, uint8_t **buffShared, int *buffLength) {
|
||||
std::ifstream inFile(fileName, std::ios::in | std::ios::binary);
|
||||
if (!inFile) {
|
||||
std::cout << "FaceFeatureLib: read file " << fileName << " fail." <<std::endl;
|
||||
return READ_FILE_FAIL;
|
||||
}
|
||||
|
||||
inFile.seekg(0, inFile.end);
|
||||
*buffLength = inFile.tellg();
|
||||
inFile.seekg(0, inFile.beg);
|
||||
|
||||
uint8_t *tempShared = reinterpret_cast<uint8_t *>(malloc(*buffLength));
|
||||
inFile.read(reinterpret_cast<char *>(tempShared), *buffLength);
|
||||
inFile.close();
|
||||
*buffShared = tempShared;
|
||||
|
||||
std::cout << "read file: fileName=" << fileName << ", size=" << *buffLength << "." << std::endl;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int ModelProcess::Init(const std::string &modelPath) {
|
||||
std::cout << "ModelProcess:Begin to init instance." << std::endl;
|
||||
int modelSize = 0;
|
||||
uint8_t *modelData = nullptr;
|
||||
int ret = ReadBinaryFile(modelPath, &modelData, &modelSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "read model file failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclmdlQuerySizeFromMem(modelData, modelSize, &modelDevPtrSize_, &weightDevPtrSize_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlQuerySizeFromMem failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "modelDevPtrSize_[" << modelDevPtrSize_ << "]" << std::endl;
|
||||
std::cout << " weightDevPtrSize_[" << weightDevPtrSize_ << "]." << std::endl;
|
||||
|
||||
ret = aclrtMalloc(&modelDevPtr_, modelDevPtrSize_, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtMalloc dev_ptr failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclrtMalloc(&weightDevPtr_, weightDevPtrSize_, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtMalloc weight_ptr failed, ret[" << ret << "] " << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclmdlLoadFromMemWithMem(modelData, modelSize, &modelId_, modelDevPtr_, modelDevPtrSize_,
|
||||
weightDevPtr_, weightDevPtrSize_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlLoadFromMemWithMem failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclrtGetCurrentContext(&contextModel_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtMalloc weight_ptr failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
aclmdlDesc *modelDesc = aclmdlCreateDesc();
|
||||
if (modelDesc == nullptr) {
|
||||
std::cout << "aclmdlCreateDesc failed." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclmdlGetDesc(modelDesc, modelId_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlGetDesc ret fail, ret:" << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
modelDesc_.reset(modelDesc, aclmdlDestroyDesc);
|
||||
free(modelData);
|
||||
return OK;
|
||||
}
|
||||
|
||||
aclmdlDataset *ModelProcess::CreateAndFillDataset(const std::vector<void *> &bufs, const std::vector<size_t> &sizes) {
|
||||
aclmdlDataset *dataset = aclmdlCreateDataset();
|
||||
if (dataset == nullptr) {
|
||||
std::cout << "ACL_ModelInputCreate failed." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bufs.size(); ++i) {
|
||||
aclDataBuffer *data = aclCreateDataBuffer(bufs[i], sizes[i]);
|
||||
if (data == nullptr) {
|
||||
DestroyDataset(dataset);
|
||||
std::cout << "aclCreateDataBuffer failed." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ret = aclmdlAddDatasetBuffer(dataset, data);
|
||||
if (ret != OK) {
|
||||
DestroyDataset(dataset);
|
||||
std::cout << "ACL_ModelInputDataAdd failed, ret[" << ret << "]." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return dataset;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#!/bin/bash
|
||||
# Copyright 2020 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
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
path_cur=$(cd "`dirname $0`"; pwd)
|
||||
build_type="Release"
|
||||
|
||||
function preparePath() {
|
||||
rm -rf $1
|
||||
mkdir -p $1
|
||||
cd $1
|
||||
}
|
||||
|
||||
function buildA300() {
|
||||
if [ ! "${ARCH_PATTERN}" ]; then
|
||||
# set ARCH_PATTERN to acllib when it was not specified by user
|
||||
export ARCH_PATTERN=acllib
|
||||
echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}"
|
||||
else
|
||||
echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib"
|
||||
export ARCH_PATTERN=${ARCH_PATTERN}/acllib
|
||||
fi
|
||||
|
||||
path_build=$path_cur/build
|
||||
preparePath $path_build
|
||||
cmake -DCMAKE_BUILD_TYPE=$build_type ..
|
||||
make -j
|
||||
ret=$?
|
||||
cd ..
|
||||
return ${ret}
|
||||
}
|
||||
|
||||
# set ASCEND_VERSION to ascend-toolkit/latest when it was not specified by user
|
||||
if [ ! "${ASCEND_VERSION}" ]; then
|
||||
export ASCEND_VERSION=ascend-toolkit/latest
|
||||
echo "Set ASCEND_VERSION to the default value: ${ASCEND_VERSION}"
|
||||
else
|
||||
echo "ASCEND_VERSION is set to ${ASCEND_VERSION} by user"
|
||||
fi
|
||||
|
||||
buildA300
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gflags/gflags.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include "../inc/AclProcess.h"
|
||||
#include "../inc/CommonDataType.h"
|
||||
|
||||
DEFINE_string(om_path, "./fasterrcnn.om", "om model path.");
|
||||
DEFINE_string(data_path, "./test.jpg", "om model path.");
|
||||
DEFINE_int32(width, 1280, "width");
|
||||
DEFINE_int32(height, 768, "height");
|
||||
DEFINE_int32(device_id, 0, "height");
|
||||
|
||||
static bool is_file(const std::string &filename) {
|
||||
struct stat buffer;
|
||||
return (stat(filename.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
|
||||
}
|
||||
|
||||
static bool is_dir(const std::string &filefodler) {
|
||||
struct stat buffer;
|
||||
return (stat(filefodler.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
|
||||
}
|
||||
/*
|
||||
* @description Initialize and run AclProcess module
|
||||
* @param resourceInfo resource info of deviceIds, model info, single Operator Path, etc
|
||||
* @param file the absolute path of input file
|
||||
* @return int int code
|
||||
*/
|
||||
int main(int argc, char* argv[]) {
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
std::cout << "OM File Path :" << FLAGS_om_path << std::endl;
|
||||
std::cout << "data Path :" << FLAGS_data_path << std::endl;
|
||||
std::cout << "width :" << FLAGS_width << std::endl;
|
||||
std::cout << "height :" << FLAGS_height << std::endl;
|
||||
std::cout << "deviceId :" << FLAGS_device_id << std::endl;
|
||||
|
||||
char omAbsPath[PATH_MAX];
|
||||
if (realpath(FLAGS_om_path.c_str(), omAbsPath) == nullptr) {
|
||||
std::cout << "Failed to get the om real path." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
if (access(omAbsPath, R_OK) == -1) {
|
||||
std::cout << "ModelPath " << omAbsPath << " doesn't exist or read failed." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
char dataAbsPath[PATH_MAX];
|
||||
if (realpath(FLAGS_data_path.c_str(), dataAbsPath) == nullptr) {
|
||||
std::cout << "Failed to get the data real path." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
if (access(dataAbsPath, R_OK) == -1) {
|
||||
std::cout << "data paeh " << dataAbsPath << " doesn't exist or read failed." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
std::map<double, double> costTime_map;
|
||||
AclProcess aclProcess(FLAGS_device_id, FLAGS_om_path, FLAGS_width, FLAGS_height);
|
||||
int ret = aclProcess.InitResource();
|
||||
if (ret != OK) {
|
||||
aclProcess.Release();
|
||||
return ret;
|
||||
}
|
||||
if (is_file(FLAGS_data_path)) {
|
||||
aclProcess.Process(FLAGS_data_path, &costTime_map);
|
||||
} else if (is_dir(FLAGS_data_path)) {
|
||||
struct dirent * filename;
|
||||
DIR * dir;
|
||||
dir = opendir(FLAGS_data_path.c_str());
|
||||
if (dir == nullptr) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
if (strcmp(filename->d_name, ".") == 0 || strcmp(filename->d_name, "..") == 0) {
|
||||
continue;
|
||||
}
|
||||
std::string wholePath = FLAGS_data_path + "/" + filename->d_name;
|
||||
aclProcess.Process(wholePath, &costTime_map);
|
||||
}
|
||||
} else {
|
||||
std::cout << " input image path error" << std::endl;
|
||||
}
|
||||
|
||||
double average = 0.0;
|
||||
int infer_cnt = 0;
|
||||
char tmpCh[256];
|
||||
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
|
||||
double diff = 0.0;
|
||||
diff = iter->second - iter->first;
|
||||
average += diff;
|
||||
infer_cnt++;
|
||||
}
|
||||
average = average/infer_cnt;
|
||||
memset(tmpCh, 0, sizeof(tmpCh));
|
||||
snprintf(tmpCh, sizeof(tmpCh), "NN inference cost average time: %4.3f ms of infer_count %d \n", average, infer_cnt);
|
||||
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl;
|
||||
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt");
|
||||
std::ofstream file_stream(file_name.c_str(), std::ios::trunc);
|
||||
file_stream << tmpCh;
|
||||
file_stream.close();
|
||||
costTime_map.clear();
|
||||
|
||||
aclProcess.Release();
|
||||
return OK;
|
||||
}
|
|
@ -19,7 +19,7 @@ import numpy as np
|
|||
import mindspore as ms
|
||||
from mindspore import Tensor, load_checkpoint, load_param_into_net, export, context
|
||||
|
||||
from src.FasterRcnn.faster_rcnn_r50 import Faster_Rcnn_Resnet50
|
||||
from src.FasterRcnn.faster_rcnn_r50 import FasterRcnn_Infer
|
||||
from src.config import config
|
||||
|
||||
parser = argparse.ArgumentParser(description='fasterrcnn_export')
|
||||
|
@ -34,15 +34,17 @@ args = parser.parse_args()
|
|||
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id)
|
||||
|
||||
if __name__ == '__main__':
|
||||
net = Faster_Rcnn_Resnet50(config=config)
|
||||
net = FasterRcnn_Infer(config=config)
|
||||
|
||||
param_dict = load_checkpoint(args.ckpt_file)
|
||||
load_param_into_net(net, param_dict)
|
||||
|
||||
param_dict_new = {}
|
||||
for key, value in param_dict.items():
|
||||
param_dict_new["network." + key] = value
|
||||
|
||||
load_param_into_net(net, param_dict_new)
|
||||
|
||||
img = Tensor(np.zeros([config.test_batch_size, 3, config.img_height, config.img_width]), ms.float16)
|
||||
img_metas = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, 4]), ms.float16)
|
||||
gt_bboxes = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, config.num_gts]), ms.float16)
|
||||
gt_label = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, config.num_gts]), ms.int32)
|
||||
gt_num = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, config.num_gts]), ms.bool_)
|
||||
|
||||
export(net, img, img_metas, gt_bboxes, gt_label, gt_num, file_name=args.file_name, file_format=args.file_format)
|
||||
export(net, img, img_metas, file_name=args.file_name, file_format=args.file_format)
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
# Copyright 2020 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.
|
||||
# ============================================================================
|
||||
"""post process for 310 inference"""
|
||||
import argparse
|
||||
import numpy as np
|
||||
from pycocotools.coco import COCO
|
||||
|
||||
from src.config import config
|
||||
from src.util import coco_eval, bbox2result_1image, results2json
|
||||
|
||||
dst_width = 1280
|
||||
dst_height = 768
|
||||
|
||||
parser = argparse.ArgumentParser(description="FasterRcnn inference")
|
||||
parser.add_argument("--ann_file", type=str, required=True, help="ann file.")
|
||||
parser.add_argument("--img_path", type=str, required=True, help="image file path.")
|
||||
args = parser.parse_args()
|
||||
|
||||
def get_eval_result(ann_file, img_path):
|
||||
max_num = 128
|
||||
result_path = "./result_Files/"
|
||||
|
||||
outputs = []
|
||||
|
||||
dataset_coco = COCO(ann_file)
|
||||
img_ids = dataset_coco.getImgIds()
|
||||
|
||||
for img_id in img_ids:
|
||||
file_id = str(img_id).zfill(12)
|
||||
|
||||
bbox_result_file = result_path + file_id + "_0.bin"
|
||||
label_result_file = result_path + file_id + "_1.bin"
|
||||
mask_result_file = result_path + file_id + "_2.bin"
|
||||
|
||||
all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5)
|
||||
all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1)
|
||||
all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1)
|
||||
|
||||
all_bbox_squee = np.squeeze(all_bbox)
|
||||
all_label_squee = np.squeeze(all_label)
|
||||
all_mask_squee = np.squeeze(all_mask)
|
||||
|
||||
all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
|
||||
all_labels_tmp_mask = all_label_squee[all_mask_squee]
|
||||
|
||||
if all_bboxes_tmp_mask.shape[0] > max_num:
|
||||
inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
|
||||
inds = inds[:max_num]
|
||||
all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
|
||||
all_labels_tmp_mask = all_labels_tmp_mask[inds]
|
||||
|
||||
outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes)
|
||||
outputs.append(outputs_tmp)
|
||||
|
||||
eval_types = ["bbox"]
|
||||
result_files = results2json(dataset_coco, outputs, "./results.pkl")
|
||||
coco_eval(result_files, eval_types, dataset_coco, single_result=False)
|
||||
|
||||
if __name__ == '__main__':
|
||||
get_eval_result(args.ann_file, args.img_path)
|
|
@ -0,0 +1,92 @@
|
|||
#!/bin/bash
|
||||
# Copyright 2020 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
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
if [ $# != 3 ]
|
||||
then
|
||||
echo "Usage: sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
get_real_path(){
|
||||
if [ "${1:0:1}" == "/" ]; then
|
||||
echo "$1"
|
||||
else
|
||||
echo "$(realpath -m $PWD/$1)"
|
||||
fi
|
||||
}
|
||||
model=$(get_real_path $1)
|
||||
data_path=$(get_real_path $2)
|
||||
ann_file=$(get_real_path $3)
|
||||
echo $model
|
||||
echo $data_path
|
||||
echo $ann_file
|
||||
export ASCEND_HOME=/usr/local/Ascend/
|
||||
export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH
|
||||
export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ones:$LD_LIBRARY_PATH
|
||||
export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages/te.egg:$ASCEND_HOME/atc/python/site-packages/topi.egg:$ASCEND_HOME/atc/python/site-packages/auto_tune.egg::$ASCEND_HOME/atc/python/site-packages/schedule_search.egg:$PYTHONPATH
|
||||
export ASCEND_OPP_PATH=$ASCEND_HOME/opp
|
||||
|
||||
function air_to_om()
|
||||
{
|
||||
atc --input_format=NCHW --framework=1 --model=$model --input_shape="x:1, 3, 768, 1280; im_info: 1, 4" --output=fasterrcnn --insert_op_conf=../src/aipp.cfg --precision_mode=allow_fp32_to_fp16 --soc_version=Ascend310
|
||||
}
|
||||
|
||||
function compile_app()
|
||||
{
|
||||
cd ../ascend310_infer/src
|
||||
sh build.sh
|
||||
cd -
|
||||
}
|
||||
|
||||
function infer()
|
||||
{
|
||||
if [ -d result_Files ]; then
|
||||
rm -rf ./result_Files
|
||||
fi
|
||||
if [ -d time_Result ]; then
|
||||
rm -rf ./time_Result
|
||||
fi
|
||||
mkdir result_Files
|
||||
mkdir time_Result
|
||||
../ascend310_infer/src/out/main --om_path=./fasterrcnn.om --data_path=$data_path
|
||||
}
|
||||
|
||||
function cal_acc()
|
||||
{
|
||||
python ../postprocess.py --ann_file=$ann_file --img_path=$data_path &> log &
|
||||
}
|
||||
|
||||
air_to_om
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "air to om failed"
|
||||
exit 1
|
||||
fi
|
||||
compile_app
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "compile app code failed"
|
||||
exit 1
|
||||
fi
|
||||
infer
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "excute inference failed"
|
||||
exit 1
|
||||
fi
|
||||
cal_acc
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "calculate accuracy failed"
|
||||
exit 1
|
||||
fi
|
|
@ -423,3 +423,13 @@ class Faster_Rcnn_Resnet50(nn.Cell):
|
|||
multi_level_anchors += (Tensor(anchors.astype(np.float16)),)
|
||||
|
||||
return multi_level_anchors
|
||||
|
||||
class FasterRcnn_Infer(nn.Cell):
|
||||
def __init__(self, config):
|
||||
super(FasterRcnn_Infer, self).__init__()
|
||||
self.network = Faster_Rcnn_Resnet50(config)
|
||||
self.network.set_train(False)
|
||||
|
||||
def construct(self, img_data, img_metas):
|
||||
output = self.network(img_data, img_metas, None, None, None)
|
||||
return output
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
aipp_op {
|
||||
aipp_mode : static
|
||||
input_format : YUV420SP_U8
|
||||
related_input_rank : 0
|
||||
csc_switch : true
|
||||
rbuv_swap_switch : false
|
||||
matrix_r0c0 : 256
|
||||
matrix_r0c1 : 0
|
||||
matrix_r0c2 : 359
|
||||
matrix_r1c0 : 256
|
||||
matrix_r1c1 : -88
|
||||
matrix_r1c2 : -183
|
||||
matrix_r2c0 : 256
|
||||
matrix_r2c1 : 454
|
||||
matrix_r2c2 : 0
|
||||
input_bias_0 : 0
|
||||
input_bias_1 : 128
|
||||
input_bias_2 : 128
|
||||
|
||||
mean_chn_0 : 124
|
||||
mean_chn_1 : 117
|
||||
mean_chn_2 : 104
|
||||
var_reci_chn_0 : 0.0171247538316637
|
||||
var_reci_chn_1 : 0.0175070028011204
|
||||
var_reci_chn_2 : 0.0174291938997821
|
||||
}
|
|
@ -122,6 +122,18 @@ pip install mmcv=0.2.14
|
|||
Note:
|
||||
1. VALIDATION_JSON_FILE is a label json file for evaluation.
|
||||
|
||||
5. Execute inference script.
|
||||
After training, you can start inference as follows:
|
||||
|
||||
```shell
|
||||
# inference
|
||||
bash run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
Note:
|
||||
1. AIR_PATH is a model file, exported by export script file on the Ascend910 environment.
|
||||
2. ANN_FILE_PATH is a annotation file for inference.
|
||||
|
||||
# Run in docker
|
||||
|
||||
1. Build docker images
|
||||
|
@ -155,6 +167,13 @@ bash run_distribute_train.sh [RANK_TABLE_FILE] [PRETRAINED_CKPT]
|
|||
bash run_eval.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
||||
```
|
||||
|
||||
5. Inference.
|
||||
|
||||
```shell
|
||||
# inference
|
||||
bash run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
# [Script Description](#contents)
|
||||
|
||||
## [Script and Sample Code](#contents)
|
||||
|
@ -163,9 +182,11 @@ bash run_eval.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
.
|
||||
└─MaskRcnn
|
||||
├─README.md # README
|
||||
├─ascend310_infer #application for 310 inference
|
||||
├─scripts # shell script
|
||||
├─run_standalone_train.sh # training in standalone mode(1pcs)
|
||||
├─run_distribute_train.sh # training in parallel mode(8 pcs)
|
||||
├─run_infer_310.sh #shell script for 310 inference
|
||||
└─run_eval.sh # evaluation
|
||||
├─src
|
||||
├─maskrcnn
|
||||
|
@ -181,13 +202,16 @@ bash run_eval.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
|
|||
├─resnet50.py # backbone network
|
||||
├─roi_align.py # roi align network
|
||||
└─rpn.py # reagion proposal network
|
||||
├─aipp.cfg #aipp config file
|
||||
├─config.py # network configuration
|
||||
├─dataset.py # dataset utils
|
||||
├─lr_schedule.py # leanring rate geneatore
|
||||
├─network_define.py # network define for maskrcnn
|
||||
└─util.py # routine operation
|
||||
├─mindspore_hub_conf.py # mindspore hub interface
|
||||
├─export.py #script to export AIR,MINDIR,ONNX model
|
||||
├─eval.py # evaluation scripts
|
||||
├─postprogress.py #post process for 310 inference
|
||||
└─train.py # training scripts
|
||||
```
|
||||
|
||||
|
@ -468,6 +492,61 @@ Accumulating evaluation results...
|
|||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.586
|
||||
```
|
||||
|
||||
## Model Export
|
||||
|
||||
```shell
|
||||
python export.py --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT]
|
||||
```
|
||||
|
||||
`EXPORT_FORMAT` shoule be in ["AIR", "ONNX", "MINDIR"]
|
||||
|
||||
## Inference Process
|
||||
|
||||
### Usage
|
||||
|
||||
Before performing inference, the air file must bu exported by export script on the 910 environment.
|
||||
|
||||
```shell
|
||||
# Ascend310 inference
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
### result
|
||||
|
||||
Inference result is saved in current path, you can find result like this in log file.
|
||||
|
||||
```bash
|
||||
Evaluate annotation type *bbox*
|
||||
Accumulating evaluation results...
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.3368
|
||||
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.589
|
||||
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.394
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.218
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.411
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.476
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.305
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.489
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.514
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.323
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.562
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.657
|
||||
|
||||
Evaluate annotation type *segm*
|
||||
Accumulating evaluation results...
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.323
|
||||
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.544
|
||||
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.336
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.147
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.353
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.479
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.278
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.422
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.439
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.248
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.478
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.594
|
||||
```
|
||||
|
||||
# Model Description
|
||||
|
||||
## Performance
|
||||
|
|
|
@ -122,6 +122,18 @@ pip install mmcv=0.2.14
|
|||
注:
|
||||
1. VALIDATION_JSON_FILE是用于评估的标签JSON文件。
|
||||
|
||||
5. 执行推理脚本。
|
||||
训练结束后,按照如下步骤启动推理:
|
||||
|
||||
```bash
|
||||
# 评估
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
注:
|
||||
1. AIR_PATH是在910上使用export脚本导出的模型。
|
||||
2. ANN_FILE_PATH是推理使用的标注文件。
|
||||
|
||||
# 脚本说明
|
||||
|
||||
## 脚本和样例代码
|
||||
|
@ -130,9 +142,11 @@ pip install mmcv=0.2.14
|
|||
.
|
||||
└─MaskRcnn
|
||||
├─README.md # README
|
||||
├─ascend310_infer #实现310推理源代码
|
||||
├─scripts # shell脚本
|
||||
├─run_standalone_train.sh # 单机模式训练(单卡)
|
||||
├─run_distribute_train.sh # 并行模式训练(8卡)
|
||||
├─run_infer_310.sh # Ascend推理shell脚本
|
||||
└─run_eval.sh # 评估
|
||||
├─src
|
||||
├─maskrcnn
|
||||
|
@ -148,13 +162,16 @@ pip install mmcv=0.2.14
|
|||
├─resnet50.py # 骨干网
|
||||
├─roi_align.py # 兴趣点对齐网络
|
||||
└─rpn.py # 区域候选网络
|
||||
├─aipp.cfg #aipp 配置文件
|
||||
├─config.py # 网络配置
|
||||
├─dataset.py # 数据集工具
|
||||
├─lr_schedule.py # 学习率生成器
|
||||
├─network_define.py # MaskRCNN的网络定义
|
||||
└─util.py # 例行操作
|
||||
├─mindspore_hub_conf.py # MindSpore hub接口
|
||||
├─export.py #导出 AIR,MINDIR,ONNX模型的脚本
|
||||
├─eval.py # 评估脚本
|
||||
├─postprogress.py #310推理后处理脚本
|
||||
└─train.py # 训练脚本
|
||||
```
|
||||
|
||||
|
@ -410,6 +427,61 @@ sh run_eval.sh [VALIDATION_ANN_FILE_JSON] [CHECKPOINT_PATH]
|
|||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.558
|
||||
```
|
||||
|
||||
## 模型导出
|
||||
|
||||
```shell
|
||||
python export.py --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT]
|
||||
```
|
||||
|
||||
`EXPORT_FORMAT` 选项 ["AIR", "ONNX", "MINDIR"]
|
||||
|
||||
## 推理过程
|
||||
|
||||
### 使用方法
|
||||
|
||||
在推理之前需要在昇腾910环境上完成模型的导出。
|
||||
|
||||
```shell
|
||||
# Ascend310 推理
|
||||
sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
|
||||
```
|
||||
|
||||
### 结果
|
||||
|
||||
推理的结果保存在当前目录下,在日志文件中可以找到类似以下的结果。
|
||||
|
||||
```bash
|
||||
Evaluate annotation type *bbox*
|
||||
Accumulating evaluation results...
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.3368
|
||||
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.589
|
||||
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.394
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.218
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.411
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.476
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.305
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.489
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.514
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.323
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.562
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.657
|
||||
|
||||
Evaluate annotation type *segm*
|
||||
Accumulating evaluation results...
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.323
|
||||
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.544
|
||||
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.336
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.147
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.353
|
||||
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.479
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.278
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.422
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.439
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.248
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.478
|
||||
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.594
|
||||
```
|
||||
|
||||
# 模型说明
|
||||
|
||||
## 性能
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 ACLMANAGER_H
|
||||
#define ACLMANAGER_H
|
||||
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "acl/acl.h"
|
||||
#include "CommonDataType.h"
|
||||
#include "ModelProcess.h"
|
||||
#include "DvppCommon.h"
|
||||
|
||||
struct ModelInfo {
|
||||
std::string modelPath;
|
||||
uint32_t modelWidth;
|
||||
uint32_t modelHeight;
|
||||
uint32_t outputNum;
|
||||
};
|
||||
|
||||
class AclProcess {
|
||||
public:
|
||||
AclProcess(int deviceId, const std::string &om_path, uint32_t width, uint32_t height);
|
||||
~AclProcess() {}
|
||||
void Release();
|
||||
int InitResource();
|
||||
int Process(const std::string& imageFile, std::map<double, double> *costTime_map);
|
||||
|
||||
private:
|
||||
int InitModule();
|
||||
int Preprocess(const std::string& imageFile);
|
||||
int ModelInfer(std::map<double, double> *costTime_map);
|
||||
int WriteResult(const std::string& imageFile);
|
||||
int ReadFile(const std::string &filePath, RawData *fileData);
|
||||
|
||||
int32_t deviceId_;
|
||||
ModelInfo modelInfo_;
|
||||
aclrtContext context_;
|
||||
aclrtStream stream_;
|
||||
std::shared_ptr<ModelProcess> modelProcess_;
|
||||
std::shared_ptr<DvppCommon> dvppCommon_;
|
||||
bool keepRatio_;
|
||||
std::vector<void *> outputBuffers_;
|
||||
std::vector<size_t> outputSizes_;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 COMMONDATATYPE_H
|
||||
#define COMMONDATATYPE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "acl/acl.h"
|
||||
#include "acl/ops/acl_dvpp.h"
|
||||
|
||||
#define DVPP_ALIGN_UP(x, align) ((((x) + ((align)-1)) / (align)) * (align))
|
||||
|
||||
#define OK 0
|
||||
#define ERROR -1
|
||||
#define INVALID_POINTER -2
|
||||
#define READ_FILE_FAIL -3
|
||||
#define OPEN_FILE_FAIL -4
|
||||
#define INIT_FAIL -5
|
||||
#define INVALID_PARAM -6
|
||||
#define DECODE_FAIL -7
|
||||
|
||||
const float SEC2MS = 1000.0;
|
||||
const int YUV_BGR_SIZE_CONVERT_3 = 3;
|
||||
const int YUV_BGR_SIZE_CONVERT_2 = 2;
|
||||
const int VPC_WIDTH_ALIGN = 16;
|
||||
const int VPC_HEIGHT_ALIGN = 2;
|
||||
|
||||
// Description of image data
|
||||
struct ImageInfo {
|
||||
uint32_t width; // Image width
|
||||
uint32_t height; // Image height
|
||||
uint32_t lenOfByte; // Size of image data, bytes
|
||||
std::shared_ptr<uint8_t> data; // Smart pointer of image data
|
||||
};
|
||||
|
||||
// Description of data in device
|
||||
struct RawData {
|
||||
size_t lenOfByte; // Size of memory, bytes
|
||||
std::shared_ptr<void> data; // Smart pointer of data
|
||||
};
|
||||
|
||||
// define the structure of an rectangle
|
||||
struct Rectangle {
|
||||
uint32_t leftTopX;
|
||||
uint32_t leftTopY;
|
||||
uint32_t rightBottomX;
|
||||
uint32_t rightBottomY;
|
||||
};
|
||||
|
||||
enum VpcProcessType {
|
||||
VPC_PT_DEFAULT = 0,
|
||||
VPC_PT_PADDING, // Resize with locked ratio and paste on upper left corner
|
||||
VPC_PT_FIT, // Resize with locked ratio and paste on middle location
|
||||
VPC_PT_FILL, // Resize with locked ratio and paste on whole locatin, the input image may be cropped
|
||||
};
|
||||
|
||||
struct DvppDataInfo {
|
||||
uint32_t width = 0; // Width of image
|
||||
uint32_t height = 0; // Height of image
|
||||
uint32_t widthStride = 0; // Width after align up
|
||||
uint32_t heightStride = 0; // Height after align up
|
||||
acldvppPixelFormat format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; // Format of image
|
||||
uint32_t frameId = 0; // Needed by video
|
||||
uint32_t dataSize = 0; // Size of data in byte
|
||||
uint8_t *data = nullptr; // Image data
|
||||
};
|
||||
|
||||
struct CropRoiConfig {
|
||||
uint32_t left;
|
||||
uint32_t right;
|
||||
uint32_t down;
|
||||
uint32_t up;
|
||||
};
|
||||
|
||||
struct DvppCropInputInfo {
|
||||
DvppDataInfo dataInfo;
|
||||
CropRoiConfig roi;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 DVPP_COMMON_H
|
||||
#define DVPP_COMMON_H
|
||||
#include <memory>
|
||||
|
||||
#include "CommonDataType.h"
|
||||
#include "acl/ops/acl_dvpp.h"
|
||||
|
||||
const int MODULUS_NUM_2 = 2;
|
||||
const uint32_t ODD_NUM_1 = 1;
|
||||
|
||||
const uint32_t JPEGD_STRIDE_WIDTH = 128; // Jpegd module output width need to align up to 128
|
||||
const uint32_t JPEGD_STRIDE_HEIGHT = 16; // Jpegd module output height need to align up to 16
|
||||
const uint32_t VPC_STRIDE_WIDTH = 16; // Vpc module output width need to align up to 16
|
||||
const uint32_t VPC_STRIDE_HEIGHT = 2; // Vpc module output height need to align up to 2
|
||||
const uint32_t YUV422_WIDTH_NU = 2; // Width of YUV422, WidthStride = Width * 2
|
||||
const uint32_t YUV444_RGB_WIDTH_NU = 3; // Width of YUV444 and RGB888, WidthStride = Width * 3
|
||||
const uint32_t XRGB_WIDTH_NU = 4; // Width of XRGB8888, WidthStride = Width * 4
|
||||
const uint32_t JPEG_OFFSET = 8; // Offset of input file for jpegd module
|
||||
const uint32_t MAX_JPEGD_WIDTH = 8192; // Max width of jpegd module
|
||||
const uint32_t MAX_JPEGD_HEIGHT = 8192; // Max height of jpegd module
|
||||
const uint32_t MIN_JPEGD_WIDTH = 32; // Min width of jpegd module
|
||||
const uint32_t MIN_JPEGD_HEIGHT = 32; // Min height of jpegd module
|
||||
const uint32_t MAX_RESIZE_WIDTH = 4096; // Max width stride of resize module
|
||||
const uint32_t MAX_RESIZE_HEIGHT = 4096; // Max height stride of resize module
|
||||
const uint32_t MIN_RESIZE_WIDTH = 32; // Min width stride of resize module
|
||||
const uint32_t MIN_RESIZE_HEIGHT = 6; // Min height stride of resize module
|
||||
const float MIN_RESIZE_SCALE = 0.03125; // Min resize scale of resize module
|
||||
const float MAX_RESIZE_SCALE = 16.0; // Min resize scale of resize module
|
||||
const uint32_t MAX_VPC_WIDTH = 4096; // Max width of picture to VPC(resize/crop)
|
||||
const uint32_t MAX_VPC_HEIGHT = 4096; // Max height of picture to VPC(resize/crop)
|
||||
const uint32_t MIN_VPC_WIDTH = 32; // Min width of picture to VPC(resize/crop)
|
||||
const uint32_t MIN_VPC_HEIGHT = 6; // Min height of picture to VPC(resize/crop)
|
||||
const uint32_t MIN_CROP_WIDTH = 10; // Min width of crop area
|
||||
const uint32_t MIN_CROP_HEIGHT = 6; // Min height of crop area
|
||||
const uint8_t YUV_GREYER_VALUE = 128; // Filling value of the resized YUV image
|
||||
|
||||
#define CONVERT_TO_ODD(NUM) (((NUM) % MODULUS_NUM_2 != 0) ? (NUM) : ((NUM) - 1))
|
||||
#define CONVERT_TO_EVEN(NUM) (((NUM) % MODULUS_NUM_2 == 0) ? (NUM) : ((NUM) - 1))
|
||||
#define CHECK_ODD(num) ((num) % MODULUS_NUM_2 != 0)
|
||||
#define CHECK_EVEN(num) ((num) % MODULUS_NUM_2 == 0)
|
||||
#define RELEASE_DVPP_DATA(dvppDataPtr) do { \
|
||||
int retMacro; \
|
||||
if (dvppDataPtr != nullptr) { \
|
||||
retMacro = acldvppFree(dvppDataPtr); \
|
||||
if (retMacro != OK) { \
|
||||
std::cout << "Failed to free memory on dvpp, ret = " << retMacro << "." << std::endl; \
|
||||
} \
|
||||
dvppDataPtr = nullptr; \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
class DvppCommon {
|
||||
public:
|
||||
explicit DvppCommon(aclrtStream dvppStream);
|
||||
~DvppCommon();
|
||||
int Init(void);
|
||||
int DeInit(void);
|
||||
|
||||
static int GetVpcDataSize(uint32_t widthVpc, uint32_t heightVpc, acldvppPixelFormat format,
|
||||
uint32_t *vpcSize);
|
||||
|
||||
static int GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride);
|
||||
|
||||
static int GetVpcOutputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride);
|
||||
|
||||
static void GetJpegDecodeStrideSize(uint32_t width, uint32_t height, uint32_t *widthStride, uint32_t *heightStride);
|
||||
static int GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t *width, uint32_t *height,
|
||||
int32_t *components);
|
||||
|
||||
static int GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format,
|
||||
uint32_t *decSize);
|
||||
|
||||
int VpcResize(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output, bool withSynchronize,
|
||||
VpcProcessType processType = VPC_PT_DEFAULT);
|
||||
|
||||
int JpegDecode(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output, bool withSynchronize);
|
||||
|
||||
int CombineResizeProcess(std::shared_ptr<DvppDataInfo> input, const DvppDataInfo &output, bool withSynchronize,
|
||||
VpcProcessType processType = VPC_PT_DEFAULT);
|
||||
|
||||
int CombineJpegdProcess(const RawData& imageInfo, acldvppPixelFormat format, bool withSynchronize);
|
||||
|
||||
std::shared_ptr<DvppDataInfo> GetInputImage();
|
||||
std::shared_ptr<DvppDataInfo> GetDecodedImage();
|
||||
std::shared_ptr<DvppDataInfo> GetResizedImage();
|
||||
|
||||
void ReleaseDvppBuffer();
|
||||
|
||||
private:
|
||||
int SetDvppPicDescData(std::shared_ptr<DvppDataInfo> dataInfo, std::shared_ptr<acldvppPicDesc>picDesc);
|
||||
int ResizeProcess(std::shared_ptr<acldvppPicDesc> inputDesc,
|
||||
std::shared_ptr<acldvppPicDesc> outputDesc, bool withSynchronize);
|
||||
|
||||
int ResizeWithPadding(std::shared_ptr<acldvppPicDesc> inputDesc, std::shared_ptr<acldvppPicDesc> outputDesc,
|
||||
const CropRoiConfig &cropRoi, const CropRoiConfig &pasteRoi, bool withSynchronize);
|
||||
|
||||
void GetCropRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *cropRoi);
|
||||
|
||||
void GetPasteRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *pasteRoi);
|
||||
|
||||
int CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output);
|
||||
int TransferImageH2D(const RawData& imageInfo, const std::shared_ptr<DvppDataInfo>& jpegInput);
|
||||
int CreateStreamDesc(std::shared_ptr<DvppDataInfo> data);
|
||||
int DestroyResource();
|
||||
|
||||
std::shared_ptr<acldvppRoiConfig> cropAreaConfig_ = nullptr;
|
||||
std::shared_ptr<acldvppRoiConfig> pasteAreaConfig_ = nullptr;
|
||||
|
||||
std::shared_ptr<acldvppPicDesc> resizeInputDesc_ = nullptr;
|
||||
std::shared_ptr<acldvppPicDesc> resizeOutputDesc_ = nullptr;
|
||||
std::shared_ptr<acldvppPicDesc> decodeOutputDesc_ = nullptr;
|
||||
std::shared_ptr<acldvppResizeConfig> resizeConfig_ = nullptr;
|
||||
|
||||
acldvppChannelDesc *dvppChannelDesc_ = nullptr;
|
||||
aclrtStream dvppStream_ = nullptr;
|
||||
std::shared_ptr<DvppDataInfo> inputImage_ = nullptr;
|
||||
std::shared_ptr<DvppDataInfo> decodedImage_ = nullptr;
|
||||
std::shared_ptr<DvppDataInfo> resizedImage_ = nullptr;
|
||||
};
|
||||
#endif
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright(C) 2020. Huawei Technologies Co.,Ltd. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 MODELPROCSS_H
|
||||
#define MODELPROCSS_H
|
||||
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "acl/acl.h"
|
||||
#include "CommonDataType.h"
|
||||
|
||||
class ModelProcess {
|
||||
public:
|
||||
explicit ModelProcess(const int deviceId);
|
||||
ModelProcess();
|
||||
~ModelProcess();
|
||||
|
||||
int Init(const std::string &modelPath);
|
||||
int DeInit();
|
||||
|
||||
int ModelInference(const std::vector<void *> &inputBufs,
|
||||
const std::vector<size_t> &inputSizes,
|
||||
const std::vector<void *> &ouputBufs,
|
||||
const std::vector<size_t> &outputSizes,
|
||||
std::map<double, double> *costTime_map);
|
||||
aclmdlDesc *GetModelDesc();
|
||||
int ReadBinaryFile(const std::string &fileName, uint8_t **buffShared, int *buffLength);
|
||||
|
||||
private:
|
||||
aclmdlDataset *CreateAndFillDataset(const std::vector<void *> &bufs, const std::vector<size_t> &sizes);
|
||||
void DestroyDataset(aclmdlDataset *dataset);
|
||||
|
||||
std::mutex mtx_ = {};
|
||||
int deviceId_ = 0;
|
||||
uint32_t modelId_ = 0;
|
||||
void *modelDevPtr_ = nullptr;
|
||||
size_t modelDevPtrSize_ = 0;
|
||||
void *weightDevPtr_ = nullptr;
|
||||
size_t weightDevPtrSize_ = 0;
|
||||
aclrtContext contextModel_ = nullptr;
|
||||
std::shared_ptr<aclmdlDesc> modelDesc_ = nullptr;
|
||||
bool isDeInit_ = false;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,355 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 "AclProcess.h"
|
||||
#include <sys/time.h>
|
||||
#include <thread>
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
* @description Implementation of constructor for class AclProcess with parameter list
|
||||
* @attention context is passed in as a parameter after being created in ResourceManager::InitResource
|
||||
*/
|
||||
AclProcess::AclProcess(int deviceId, const std::string &om_path, uint32_t width, uint32_t height)
|
||||
: deviceId_(deviceId), stream_(nullptr), modelProcess_(nullptr), dvppCommon_(nullptr), keepRatio_(true) {
|
||||
modelInfo_.modelPath = om_path;
|
||||
modelInfo_.modelWidth = width;
|
||||
modelInfo_.modelHeight = height;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Release all the resource
|
||||
* @attention context will be released in ResourceManager::Release
|
||||
*/
|
||||
void AclProcess::Release() {
|
||||
// Synchronize stream and release Dvpp channel
|
||||
dvppCommon_->DeInit();
|
||||
// Release stream
|
||||
if (stream_ != nullptr) {
|
||||
int ret = aclrtDestroyStream(stream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to destroy the stream, ret = " << ret << ".";
|
||||
}
|
||||
stream_ = nullptr;
|
||||
}
|
||||
// Destroy resources of modelProcess_
|
||||
modelProcess_->DeInit();
|
||||
|
||||
// Release Dvpp buffer
|
||||
dvppCommon_->ReleaseDvppBuffer();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Initialize the modules used by this sample
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::InitModule() {
|
||||
// Create Dvpp common object
|
||||
if (dvppCommon_ == nullptr) {
|
||||
dvppCommon_ = std::make_shared<DvppCommon>(stream_);
|
||||
int retDvppCommon = dvppCommon_->Init();
|
||||
if (retDvppCommon != OK) {
|
||||
std::cout << "Failed to initialize dvppCommon, ret = " << retDvppCommon << std::endl;
|
||||
return retDvppCommon;
|
||||
}
|
||||
}
|
||||
// Create model inference object
|
||||
if (modelProcess_ == nullptr) {
|
||||
modelProcess_ = std::make_shared<ModelProcess>(deviceId_);
|
||||
}
|
||||
// Initialize ModelProcess module
|
||||
int ret = modelProcess_->Init(modelInfo_.modelPath);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to initialize the model process module, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "Initialized the model process module successfully." << std::endl;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Create resource for this sample
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::InitResource() {
|
||||
int ret = aclInit(nullptr); // Initialize ACL
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to init acl, ret = " << ret << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = aclrtSetDevice(deviceId_);
|
||||
if (ret != ACL_SUCCESS) {
|
||||
std::cout << "acl set device " << deviceId_ << "intCode = "<< static_cast<int32_t>(ret) << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "set device "<< deviceId_ << " success" << std::endl;
|
||||
|
||||
// create context (set current)
|
||||
ret = aclrtCreateContext(&context_, deviceId_);
|
||||
if (ret != ACL_SUCCESS) {
|
||||
std::cout << "acl create context failed, deviceId = " << deviceId_ <<
|
||||
"intCode = "<< static_cast<int32_t>(ret) << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "create context success" << std::endl;
|
||||
|
||||
ret = aclrtCreateStream(&stream_); // Create stream for application
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to create the acl stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "Created the acl stream successfully." << std::endl;
|
||||
// Initialize dvpp module
|
||||
if (InitModule() != OK) {
|
||||
return INIT_FAIL;
|
||||
}
|
||||
|
||||
aclmdlDesc *modelDesc = modelProcess_->GetModelDesc();
|
||||
size_t outputSize = aclmdlGetNumOutputs(modelDesc);
|
||||
modelInfo_.outputNum = outputSize;
|
||||
for (size_t i = 0; i < outputSize; i++) {
|
||||
size_t bufferSize = aclmdlGetOutputSizeByIndex(modelDesc, i);
|
||||
void *outputBuffer = nullptr;
|
||||
ret = aclrtMalloc(&outputBuffer, bufferSize, ACL_MEM_MALLOC_NORMAL_ONLY);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc buffer, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
outputBuffers_.push_back(outputBuffer);
|
||||
outputSizes_.push_back(bufferSize);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int AclProcess::WriteResult(const std::string& imageFile) {
|
||||
std::string homePath = "./result_Files";
|
||||
void *resHostBuf = nullptr;
|
||||
for (size_t i = 0; i < outputBuffers_.size(); ++i) {
|
||||
size_t output_size;
|
||||
void * netOutput;
|
||||
netOutput = outputBuffers_[i];
|
||||
output_size = outputSizes_[i];
|
||||
int ret = aclrtMallocHost(&resHostBuf, output_size);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to print the result, malloc host failed, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = aclrtMemcpy(resHostBuf, output_size, netOutput,
|
||||
output_size, ACL_MEMCPY_DEVICE_TO_HOST);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to print result, memcpy device to host failed, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int pos = imageFile.rfind('/');
|
||||
std::string fileName(imageFile, pos + 1);
|
||||
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), "_" + std::to_string(i) + ".bin");
|
||||
|
||||
std::string outFileName = homePath + "/" + fileName;
|
||||
FILE * outputFile = fopen(outFileName.c_str(), "wb");
|
||||
fwrite(resHostBuf, output_size, sizeof(char), outputFile);
|
||||
|
||||
fclose(outputFile);
|
||||
outputFile = nullptr;
|
||||
ret = aclrtFreeHost(resHostBuf);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree host output memory failed" << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file, store it into the RawData structure
|
||||
*
|
||||
* @param filePath file to read to
|
||||
* @param fileData RawData structure to store in
|
||||
* @return OK if create success, int code otherwise
|
||||
*/
|
||||
int AclProcess::ReadFile(const std::string &filePath, RawData *fileData) {
|
||||
// Open file with reading mode
|
||||
FILE *fp = fopen(filePath.c_str(), "rb");
|
||||
if (fp == nullptr) {
|
||||
std::cout << "Failed to open file, filePath = " << filePath << std::endl;
|
||||
return OPEN_FILE_FAIL;
|
||||
}
|
||||
// Get the length of input file
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size_t fileSize = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
// If file not empty, read it into FileInfo and return it
|
||||
if (fileSize > 0) {
|
||||
fileData->lenOfByte = fileSize;
|
||||
fileData->data = std::make_shared<uint8_t>();
|
||||
fileData->data.reset(new uint8_t[fileSize], std::default_delete<uint8_t[]>());
|
||||
uint32_t readRet = fread(fileData->data.get(), 1, fileSize, fp);
|
||||
if (readRet == 0) {
|
||||
fclose(fp);
|
||||
return READ_FILE_FAIL;
|
||||
}
|
||||
fclose(fp);
|
||||
return OK;
|
||||
}
|
||||
fclose(fp);
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
/*
|
||||
* @description Preprocess the input image
|
||||
* @param imageFile input image path
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::Preprocess(const std::string& imageFile) {
|
||||
RawData imageInfo;
|
||||
int ret = ReadFile(imageFile, &imageInfo); // Read image data from input image file
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to read file, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
// Run process of jpegD
|
||||
ret = dvppCommon_->CombineJpegdProcess(imageInfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420, true);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to execute image decoded of preprocess module, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
// Get output of decode jpeg image
|
||||
std::shared_ptr<DvppDataInfo> decodeOutData = dvppCommon_->GetDecodedImage();
|
||||
// Run resize application function
|
||||
DvppDataInfo resizeOutData;
|
||||
resizeOutData.height = modelInfo_.modelHeight;
|
||||
resizeOutData.width = modelInfo_.modelWidth;
|
||||
resizeOutData.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
|
||||
ret = dvppCommon_->CombineResizeProcess(decodeOutData, resizeOutData, true, VPC_PT_PADDING);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to execute image resized of preprocess module, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
RELEASE_DVPP_DATA(decodeOutData->data);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Inference of model
|
||||
* @return int int code
|
||||
*/
|
||||
int AclProcess::ModelInfer(std::map<double, double> *costTime_map) {
|
||||
// Get output of resize module
|
||||
std::shared_ptr<DvppDataInfo> resizeOutData = dvppCommon_->GetResizedImage();
|
||||
std::shared_ptr<DvppDataInfo> inputImg = dvppCommon_->GetInputImage();
|
||||
|
||||
float widthScale, heightScale;
|
||||
if (keepRatio_) {
|
||||
widthScale = static_cast<float>(resizeOutData->width) / inputImg->width;
|
||||
if (widthScale > static_cast<float>(resizeOutData->height) / inputImg->height) {
|
||||
widthScale = static_cast<float>(resizeOutData->height) / inputImg->height;
|
||||
}
|
||||
heightScale = widthScale;
|
||||
} else {
|
||||
widthScale = static_cast<float>(resizeOutData->width) / inputImg->width;
|
||||
heightScale = static_cast<float>(resizeOutData->height) / inputImg->height;
|
||||
}
|
||||
|
||||
aclFloat16 inputWidth = aclFloatToFloat16(static_cast<float>(inputImg->width));
|
||||
aclFloat16 inputHeight = aclFloatToFloat16(static_cast<float>(inputImg->height));
|
||||
aclFloat16 resizeWidthRatioFp16 = aclFloatToFloat16(widthScale);
|
||||
aclFloat16 resizeHeightRatioFp16 = aclFloatToFloat16(heightScale);
|
||||
|
||||
aclFloat16 *im_info = reinterpret_cast<aclFloat16 *>(malloc(sizeof(aclFloat16) * 4));
|
||||
im_info[0] = inputHeight;
|
||||
im_info[1] = inputWidth;
|
||||
im_info[2] = resizeHeightRatioFp16;
|
||||
im_info[3] = resizeWidthRatioFp16;
|
||||
void *imInfo_dst = nullptr;
|
||||
int ret = aclrtMalloc(&imInfo_dst, 8, ACL_MEM_MALLOC_NORMAL_ONLY);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
std::cout << "aclrtMalloc failed, ret = " << ret << std::endl;
|
||||
aclrtFree(imInfo_dst);
|
||||
return ret;
|
||||
}
|
||||
ret = aclrtMemcpy(reinterpret_cast<uint8_t *>(imInfo_dst), 8, im_info, 8, ACL_MEMCPY_HOST_TO_DEVICE);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
std::cout << "aclrtMemcpy failed, ret = " << ret << std::endl;
|
||||
aclrtFree(imInfo_dst);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<void *> inputBuffers({resizeOutData->data, imInfo_dst});
|
||||
std::vector<size_t> inputSizes({resizeOutData->dataSize, 4*2});
|
||||
|
||||
for (size_t i = 0; i < modelInfo_.outputNum; i++) {
|
||||
aclrtMemset(outputBuffers_[i], outputSizes_[i], 0, outputSizes_[i]);
|
||||
}
|
||||
// Execute classification model
|
||||
ret = modelProcess_->ModelInference(inputBuffers, inputSizes, outputBuffers_, outputSizes_, costTime_map);
|
||||
if (ret != OK) {
|
||||
aclrtFree(imInfo_dst);
|
||||
std::cout << "Failed to execute the classification model, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = aclrtFree(imInfo_dst);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree image info failed" << std::endl;
|
||||
return ret;
|
||||
}
|
||||
RELEASE_DVPP_DATA(resizeOutData->data);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description Process classification
|
||||
*
|
||||
* @par Function
|
||||
* 1.Dvpp module preprocess
|
||||
* 2.Execute classification model
|
||||
* 3.Execute single operator
|
||||
* 4.Write result
|
||||
*
|
||||
* @param imageFile input file path
|
||||
* @return int int code
|
||||
*/
|
||||
|
||||
int AclProcess::Process(const std::string& imageFile, std::map<double, double> *costTime_map) {
|
||||
struct timeval begin = {0};
|
||||
struct timeval end = {0};
|
||||
gettimeofday(&begin, nullptr);
|
||||
|
||||
int ret = Preprocess(imageFile);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = ModelInfer(costTime_map);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = WriteResult(imageFile);
|
||||
if (ret != OK) {
|
||||
std::cout << "write result failed." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
gettimeofday(&end, nullptr);
|
||||
|
||||
const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
|
||||
std::cout << "[Process Delay] cost: " << costMs << "ms." << std::endl;
|
||||
return OK;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
|
||||
|
||||
# CMake lowest version requirement
|
||||
cmake_minimum_required(VERSION 3.5.1)
|
||||
# Add definitions ENABLE_DVPP_INTERFACE to use dvpp api
|
||||
add_definitions(-DENABLE_DVPP_INTERFACE)
|
||||
# project information
|
||||
project(InferClassification)
|
||||
# Check environment variable
|
||||
if(NOT DEFINED ENV{ASCEND_HOME})
|
||||
message(FATAL_ERROR "please define environment variable:ASCEND_HOME")
|
||||
endif()
|
||||
|
||||
# Compile options
|
||||
add_compile_options(-std=c++11 -fPIE -g -fstack-protector-all -Werror -Wreturn-type)
|
||||
|
||||
# Skip build rpath
|
||||
set(CMAKE_SKIP_BUILD_RPATH True)
|
||||
|
||||
# Set output directory
|
||||
set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/out)
|
||||
|
||||
# Set include directory and library directory
|
||||
#set(ACL_INC_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/include)
|
||||
#set(ACL_LIB_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/lib64/stub)
|
||||
set(ACL_INC_DIR $ENV{ASCEND_HOME}/acllib/include)
|
||||
set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib/lib64/stub)
|
||||
# Header path
|
||||
include_directories(${ACL_INC_DIR})
|
||||
include_directories(${PROJECT_SRC_ROOT}/../inc)
|
||||
|
||||
# add host lib path
|
||||
link_directories(${ACL_LIB_DIR})
|
||||
|
||||
add_executable(main AclProcess.cpp
|
||||
DvppCommon.cpp
|
||||
ModelProcess.cpp
|
||||
main.cpp)
|
||||
|
||||
target_link_libraries(main ascendcl gflags acl_dvpp pthread)
|
|
@ -0,0 +1,735 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "../inc/DvppCommon.h"
|
||||
#include "../inc/CommonDataType.h"
|
||||
|
||||
static auto g_resizeConfigDeleter = [](acldvppResizeConfig *p) { acldvppDestroyResizeConfig(p); };
|
||||
static auto g_picDescDeleter = [](acldvppPicDesc *picDesc) { acldvppDestroyPicDesc(picDesc); };
|
||||
static auto g_roiConfigDeleter = [](acldvppRoiConfig *p) { acldvppDestroyRoiConfig(p); };
|
||||
|
||||
DvppCommon::DvppCommon(aclrtStream dvppStream):dvppStream_(dvppStream) {}
|
||||
|
||||
/*
|
||||
* @description: Create a channel for processing image data,
|
||||
* the channel description is created by acldvppCreateChannelDesc
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::Init(void) {
|
||||
dvppChannelDesc_ = acldvppCreateChannelDesc();
|
||||
if (dvppChannelDesc_ == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
int ret = acldvppCreateChannel(dvppChannelDesc_);
|
||||
if (ret != 0) {
|
||||
std::cout << "Failed to create dvpp channel, ret = " << ret << "." << std::endl;
|
||||
acldvppDestroyChannelDesc(dvppChannelDesc_);
|
||||
dvppChannelDesc_ = nullptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: destroy the channel and the channel description used by image.
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::DeInit(void) {
|
||||
int ret = aclrtSynchronizeStream(dvppStream_); // int ret
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppDestroyChannel(dvppChannelDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to destory dvpp channel, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppDestroyChannelDesc(dvppChannelDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to destroy dvpp channel description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Release the memory that is allocated in the interfaces which are started with "Combine"
|
||||
*/
|
||||
void DvppCommon::ReleaseDvppBuffer() {
|
||||
if (resizedImage_ != nullptr) {
|
||||
RELEASE_DVPP_DATA(resizedImage_->data);
|
||||
}
|
||||
if (decodedImage_ != nullptr) {
|
||||
RELEASE_DVPP_DATA(decodedImage_->data);
|
||||
}
|
||||
if (inputImage_ != nullptr) {
|
||||
RELEASE_DVPP_DATA(inputImage_->data);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the size of buffer used to save image for VPC according to width, height and format
|
||||
* @param width specifies the width of the output image
|
||||
* @param height specifies the height of the output image
|
||||
* @param format specifies the format of the output image
|
||||
* @param: vpcSize is used to save the result size
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetVpcDataSize(uint32_t width, uint32_t height, acldvppPixelFormat format, uint32_t *vpcSize) {
|
||||
if (format != PIXEL_FORMAT_YUV_SEMIPLANAR_420 && format != PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
|
||||
std::cout << "Format[" << format << "] for VPC is not supported, just support NV12 or NV21." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
uint32_t widthStride = DVPP_ALIGN_UP(width, VPC_WIDTH_ALIGN);
|
||||
uint32_t heightStride = DVPP_ALIGN_UP(height, VPC_HEIGHT_ALIGN);
|
||||
*vpcSize = widthStride * heightStride * YUV_BGR_SIZE_CONVERT_3 / YUV_BGR_SIZE_CONVERT_2;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the aligned width and height of the input image according to the image format
|
||||
* @param: width specifies the width before alignment
|
||||
* @param: height specifies the height before alignment
|
||||
* @param: format specifies the image format
|
||||
* @param: widthStride is used to save the width after alignment
|
||||
* @param: heightStride is used to save the height after alignment
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride) {
|
||||
uint32_t inputWidthStride;
|
||||
if (format >= PIXEL_FORMAT_YUV_400 && format <= PIXEL_FORMAT_YVU_SEMIPLANAR_444) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH);
|
||||
} else if (format >= PIXEL_FORMAT_YUYV_PACKED_422 && format <= PIXEL_FORMAT_VYUY_PACKED_422) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH) * YUV422_WIDTH_NU;
|
||||
} else if (format >= PIXEL_FORMAT_YUV_PACKED_444 && format <= PIXEL_FORMAT_BGR_888) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH) * YUV444_RGB_WIDTH_NU;
|
||||
} else if (format >= PIXEL_FORMAT_ARGB_8888 && format <= PIXEL_FORMAT_BGRA_8888) {
|
||||
inputWidthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH) * XRGB_WIDTH_NU;
|
||||
} else {
|
||||
std::cout << "Input format[" << format << "] for VPC is invalid, please check it." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
uint32_t inputHeightStride = DVPP_ALIGN_UP(height, VPC_STRIDE_HEIGHT);
|
||||
if (inputWidthStride > MAX_RESIZE_WIDTH || inputWidthStride < MIN_RESIZE_WIDTH) {
|
||||
std::cout << "Input width stride " << inputWidthStride << " is invalid, not in [" << MIN_RESIZE_WIDTH \
|
||||
<< ", " << MAX_RESIZE_WIDTH << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
if (inputHeightStride > MAX_RESIZE_HEIGHT || inputHeightStride < MIN_RESIZE_HEIGHT) {
|
||||
std::cout << "Input height stride " << inputHeightStride << " is invalid, not in [" << MIN_RESIZE_HEIGHT \
|
||||
<< ", " << MAX_RESIZE_HEIGHT << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
*widthStride = inputWidthStride;
|
||||
*heightStride = inputHeightStride;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the aligned width and height of the output image according to the image format
|
||||
* @param: width specifies the width before alignment
|
||||
* @param: height specifies the height before alignment
|
||||
* @param: format specifies the image format
|
||||
* @param: widthStride is used to save the width after alignment
|
||||
* @param: heightStride is used to save the height after alignment
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetVpcOutputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||
uint32_t *widthStride, uint32_t *heightStride) {
|
||||
if (format != PIXEL_FORMAT_YUV_SEMIPLANAR_420 && format != PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
|
||||
std::cout << "Output format[" << format << "] is not supported, just support NV12 or NV21." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
*widthStride = DVPP_ALIGN_UP(width, VPC_STRIDE_WIDTH);
|
||||
*heightStride = DVPP_ALIGN_UP(height, VPC_STRIDE_HEIGHT);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Set picture description information and execute resize function
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @param: processType specifies whether to perform proportional scaling, default is non-proportional resize
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::VpcResize(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
bool withSynchronize, VpcProcessType processType) {
|
||||
acldvppPicDesc *inputDesc = acldvppCreatePicDesc();
|
||||
acldvppPicDesc *outputDesc = acldvppCreatePicDesc();
|
||||
resizeInputDesc_.reset(inputDesc, g_picDescDeleter);
|
||||
resizeOutputDesc_.reset(outputDesc, g_picDescDeleter);
|
||||
|
||||
// Set dvpp picture descriptin info of input image
|
||||
int ret = SetDvppPicDescData(input, resizeInputDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set dvpp input picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Set dvpp picture descriptin info of output image
|
||||
ret = SetDvppPicDescData(output, resizeOutputDesc_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set dvpp output picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (processType == VPC_PT_DEFAULT) {
|
||||
return ResizeProcess(resizeInputDesc_, resizeOutputDesc_, withSynchronize);
|
||||
}
|
||||
|
||||
// Get crop area according to the processType
|
||||
CropRoiConfig cropRoi = {0};
|
||||
GetCropRoi(input, output, processType, &cropRoi);
|
||||
|
||||
// The width and height of the original image will be resized by the same ratio
|
||||
CropRoiConfig pasteRoi = {0};
|
||||
GetPasteRoi(input, output, processType, &pasteRoi);
|
||||
|
||||
return ResizeWithPadding(resizeInputDesc_, resizeOutputDesc_, cropRoi, pasteRoi, withSynchronize);
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Set image description information
|
||||
* @param: dataInfo specifies the image information
|
||||
* @param: picsDesc specifies the picture description information to be set
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::SetDvppPicDescData(std::shared_ptr<DvppDataInfo> dataInfo, std::shared_ptr<acldvppPicDesc>picDesc) {
|
||||
int ret = acldvppSetPicDescData(picDesc.get(), dataInfo->data);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set data for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescSize(picDesc.get(), dataInfo->dataSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set size for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescFormat(picDesc.get(), dataInfo->format);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set format for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescWidth(picDesc.get(), dataInfo->width);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set width for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescHeight(picDesc.get(), dataInfo->height);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set height for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppSetPicDescWidthStride(picDesc.get(), dataInfo->widthStride);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set aligned width for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = acldvppSetPicDescHeightStride(picDesc.get(), dataInfo->heightStride);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to set aligned height for dvpp picture description, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Check whether the image format and zoom ratio meet the requirements
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output) {
|
||||
if (output.format != PIXEL_FORMAT_YUV_SEMIPLANAR_420 && output.format != PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
|
||||
std::cout << "Output format[" << output.format << "]is not supported, just support NV12 or NV21." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
float heightScale = static_cast<float>(output.height) / input.height;
|
||||
if (heightScale < MIN_RESIZE_SCALE || heightScale > MAX_RESIZE_SCALE) {
|
||||
std::cout << "Resize scale should be in range [1/16, 32], which is " << heightScale << "." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
float widthScale = static_cast<float>(output.width) / input.width;
|
||||
if (widthScale < MIN_RESIZE_SCALE || widthScale > MAX_RESIZE_SCALE) {
|
||||
std::cout << "Resize scale should be in range [1/16, 32], which is " << widthScale << "." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Scale the input image to the size specified by the output image and
|
||||
* saves the result to the output image (non-proportionate scaling)
|
||||
* @param: inputDesc specifies the description information of the input image
|
||||
* @param: outputDesc specifies the description information of the output image
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::ResizeProcess(std::shared_ptr<acldvppPicDesc>inputDesc,
|
||||
std::shared_ptr<acldvppPicDesc>outputDesc,
|
||||
bool withSynchronize) {
|
||||
acldvppResizeConfig *resizeConfig = acldvppCreateResizeConfig();
|
||||
if (resizeConfig == nullptr) {
|
||||
std::cout << "Failed to create dvpp resize config." << std::endl;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
|
||||
resizeConfig_.reset(resizeConfig, g_resizeConfigDeleter);
|
||||
int ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc.get(), outputDesc.get(),
|
||||
resizeConfig_.get(), dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to resize asynchronously, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (withSynchronize) {
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Crop the image from the input image based on the specified area and
|
||||
* paste the cropped image to the specified position of the target image
|
||||
* as the output image
|
||||
* @param: inputDesc specifies the description information of the input image
|
||||
* @param: outputDesc specifies the description information of the output image
|
||||
* @param: cropRoi specifies the cropped area
|
||||
* @param: pasteRoi specifies the pasting area
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: If the width and height of the crop area are different from those of the
|
||||
* paste area, the image is scaled again
|
||||
*/
|
||||
int DvppCommon::ResizeWithPadding(std::shared_ptr<acldvppPicDesc> inputDesc,
|
||||
std::shared_ptr<acldvppPicDesc> outputDesc,
|
||||
const CropRoiConfig &cropRoi, const CropRoiConfig &pasteRoi, bool withSynchronize) {
|
||||
acldvppRoiConfig *cropRoiCfg = acldvppCreateRoiConfig(cropRoi.left, cropRoi.right, cropRoi.up, cropRoi.down);
|
||||
if (cropRoiCfg == nullptr) {
|
||||
std::cout << "Failed to create dvpp roi config for corp area." << std::endl;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
cropAreaConfig_.reset(cropRoiCfg, g_roiConfigDeleter);
|
||||
|
||||
acldvppRoiConfig *pastRoiCfg = acldvppCreateRoiConfig(pasteRoi.left, pasteRoi.right, pasteRoi.up, pasteRoi.down);
|
||||
if (pastRoiCfg == nullptr) {
|
||||
std::cout << "Failed to create dvpp roi config for paster area." << std::endl;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
pasteAreaConfig_.reset(pastRoiCfg, g_roiConfigDeleter);
|
||||
|
||||
int ret = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, inputDesc.get(), outputDesc.get(), cropAreaConfig_.get(),
|
||||
pasteAreaConfig_.get(), dvppStream_);
|
||||
if (ret != OK) {
|
||||
// release resource.
|
||||
std::cout << "Failed to crop and paste asynchronously, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (withSynchronize) {
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed tp synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get crop area
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: processType specifies whether to perform proportional scaling
|
||||
* @param: cropRoi is used to save the info of the crop roi area
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
void DvppCommon::GetCropRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *cropRoi) {
|
||||
// When processType is not VPC_PT_FILL, crop area is the whole input image
|
||||
if (processType != VPC_PT_FILL) {
|
||||
cropRoi->right = CONVERT_TO_ODD(input->width - ODD_NUM_1);
|
||||
cropRoi->down = CONVERT_TO_ODD(input->height - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
bool widthRatioSmaller = true;
|
||||
// The scaling ratio is based on the smaller ratio to ensure the smallest edge to fill the targe edge
|
||||
float resizeRatio = static_cast<float>(input->width) / output->width;
|
||||
if (resizeRatio > (static_cast<float>(input->height) / output->height)) {
|
||||
resizeRatio = static_cast<float>(input->height) / output->height;
|
||||
widthRatioSmaller = false;
|
||||
}
|
||||
|
||||
const int halfValue = 2;
|
||||
// The left and up must be even, right and down must be odd which is required by acl
|
||||
if (widthRatioSmaller) {
|
||||
cropRoi->left = 0;
|
||||
cropRoi->right = CONVERT_TO_ODD(input->width - ODD_NUM_1);
|
||||
cropRoi->up = CONVERT_TO_EVEN(static_cast<uint32_t>((input->height - output->height * resizeRatio) /
|
||||
halfValue));
|
||||
cropRoi->down = CONVERT_TO_ODD(input->height - cropRoi->up - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
cropRoi->up = 0;
|
||||
cropRoi->down = CONVERT_TO_ODD(input->height - ODD_NUM_1);
|
||||
cropRoi->left = CONVERT_TO_EVEN(static_cast<uint32_t>((input->width - output->width * resizeRatio) / halfValue));
|
||||
cropRoi->right = CONVERT_TO_ODD(input->width - cropRoi->left - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get paste area
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: processType specifies whether to perform proportional scaling
|
||||
* @param: pasteRio is used to save the info of the paste area
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
void DvppCommon::GetPasteRoi(std::shared_ptr<DvppDataInfo> input, std::shared_ptr<DvppDataInfo> output,
|
||||
VpcProcessType processType, CropRoiConfig *pasteRoi) {
|
||||
if (processType == VPC_PT_FILL) {
|
||||
pasteRoi->right = CONVERT_TO_ODD(output->width - ODD_NUM_1);
|
||||
pasteRoi->down = CONVERT_TO_ODD(output->height - ODD_NUM_1);
|
||||
return;
|
||||
}
|
||||
|
||||
bool widthRatioLarger = true;
|
||||
// The scaling ratio is based on the larger ratio to ensure the largest edge to fill the targe edge
|
||||
float resizeRatio = static_cast<float>(input->width) / output->width;
|
||||
if (resizeRatio < (static_cast<float>(input->height) / output->height)) {
|
||||
resizeRatio = static_cast<float>(input->height) / output->height;
|
||||
widthRatioLarger = false;
|
||||
}
|
||||
|
||||
// Left and up is 0 when the roi paste on the upper left corner
|
||||
if (processType == VPC_PT_PADDING) {
|
||||
pasteRoi->right = (input->width / resizeRatio) - ODD_NUM_1;
|
||||
pasteRoi->down = (input->height / resizeRatio) - ODD_NUM_1;
|
||||
pasteRoi->right = CONVERT_TO_ODD(pasteRoi->right);
|
||||
pasteRoi->down = CONVERT_TO_ODD(pasteRoi->down);
|
||||
return;
|
||||
}
|
||||
|
||||
const int halfValue = 2;
|
||||
// Left and up is 0 when the roi paste on the middler location
|
||||
if (widthRatioLarger) {
|
||||
pasteRoi->left = 0;
|
||||
pasteRoi->right = output->width - ODD_NUM_1;
|
||||
pasteRoi->up = (output->height - (input->height / resizeRatio)) / halfValue;
|
||||
pasteRoi->down = output->height - pasteRoi->up - ODD_NUM_1;
|
||||
} else {
|
||||
pasteRoi->up = 0;
|
||||
pasteRoi->down = output->height - ODD_NUM_1;
|
||||
pasteRoi->left = (output->width - (input->width / resizeRatio)) / halfValue;
|
||||
pasteRoi->right = output->width - pasteRoi->left - ODD_NUM_1;
|
||||
}
|
||||
|
||||
// The left must be even and align to 16, up must be even, right and down must be odd which is required by acl
|
||||
pasteRoi->left = DVPP_ALIGN_UP(CONVERT_TO_EVEN(pasteRoi->left), VPC_WIDTH_ALIGN);
|
||||
pasteRoi->right = CONVERT_TO_ODD(pasteRoi->right);
|
||||
pasteRoi->up = CONVERT_TO_EVEN(pasteRoi->up);
|
||||
pasteRoi->down = CONVERT_TO_ODD(pasteRoi->down);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Resize the image specified by input and save the result to member variable resizedImage_
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @param: processType specifies whether to perform proportional scaling, default is non-proportional resize
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::CombineResizeProcess(std::shared_ptr<DvppDataInfo> input, const DvppDataInfo &output,
|
||||
bool withSynchronize, VpcProcessType processType) {
|
||||
int ret = CheckResizeParams(*input, output);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
// Get widthStride and heightStride for input and output image according to the format
|
||||
ret = GetVpcInputStrideSize(input->widthStride, input->heightStride, input->format,
|
||||
&(input->widthStride), &(input->heightStride));
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
resizedImage_ = std::make_shared<DvppDataInfo>();
|
||||
resizedImage_->width = output.width;
|
||||
resizedImage_->height = output.height;
|
||||
resizedImage_->format = output.format;
|
||||
ret = GetVpcOutputStrideSize(output.width, output.height, output.format, &(resizedImage_->widthStride),
|
||||
&(resizedImage_->heightStride));
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
// Get output buffer size for resize output
|
||||
ret = GetVpcDataSize(output.width, output.height, output.format, &(resizedImage_->dataSize));
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
// Malloc buffer for output of resize module
|
||||
// Need to pay attention to release of the buffer
|
||||
ret = acldvppMalloc(reinterpret_cast<void **>(&(resizedImage_->data)), resizedImage_->dataSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc " << resizedImage_->dataSize << " bytes on dvpp for resize" << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
aclrtMemset(resizedImage_->data, resizedImage_->dataSize, YUV_GREYER_VALUE, resizedImage_->dataSize);
|
||||
resizedImage_->frameId = input->frameId;
|
||||
ret = VpcResize(input, resizedImage_, withSynchronize, processType);
|
||||
if (ret != OK) {
|
||||
// Release the output buffer when resize failed, otherwise release it after use
|
||||
RELEASE_DVPP_DATA(resizedImage_->data);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Set the description of the output image and decode
|
||||
* @param: input specifies the input image information
|
||||
* @param: output specifies the output image information
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::JpegDecode(std::shared_ptr<DvppDataInfo> input,
|
||||
std::shared_ptr<DvppDataInfo> output,
|
||||
bool withSynchronize) {
|
||||
acldvppPicDesc *outputDesc = acldvppCreatePicDesc();
|
||||
decodeOutputDesc_.reset(outputDesc, g_picDescDeleter);
|
||||
|
||||
int ret = SetDvppPicDescData(output, decodeOutputDesc_);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = acldvppJpegDecodeAsync(dvppChannelDesc_, input->data, input->dataSize, decodeOutputDesc_.get(), dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to decode jpeg, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (withSynchronize) {
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
return DECODE_FAIL;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the aligned width and height of the image after decoding
|
||||
* @param: width specifies the width before alignment
|
||||
* @param: height specifies the height before alignment
|
||||
* @param: widthStride is used to save the width after alignment
|
||||
* @param: heightStride is used to save the height after alignment
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
void DvppCommon::GetJpegDecodeStrideSize(uint32_t width, uint32_t height,
|
||||
uint32_t *widthStride, uint32_t *heightStride) {
|
||||
*widthStride = DVPP_ALIGN_UP(width, JPEGD_STRIDE_WIDTH);
|
||||
*heightStride = DVPP_ALIGN_UP(height, JPEGD_STRIDE_HEIGHT);
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get picture width and height and number of channels from image data
|
||||
* @param: data specifies the memory to store the image data
|
||||
* @param: dataSize specifies the size of the image data
|
||||
* @param: width is used to save the image width
|
||||
* @param: height is used to save the image height
|
||||
* @param: components is used to save the number of channels
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t *width, uint32_t *height,
|
||||
int32_t *components) {
|
||||
uint32_t widthTmp;
|
||||
uint32_t heightTmp;
|
||||
int32_t componentsTmp;
|
||||
int ret = acldvppJpegGetImageInfo(data, dataSize, &widthTmp, &heightTmp, &componentsTmp);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to get image info of jpeg, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
if (widthTmp > MAX_JPEGD_WIDTH || widthTmp < MIN_JPEGD_WIDTH) {
|
||||
std::cout << "Input width is invalid, not in [" << MIN_JPEGD_WIDTH << ", "
|
||||
<< MAX_JPEGD_WIDTH << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
if (heightTmp > MAX_JPEGD_HEIGHT || heightTmp < MIN_JPEGD_HEIGHT) {
|
||||
std::cout << "Input height is invalid, not in [" << MIN_JPEGD_HEIGHT << ", "
|
||||
<< MAX_JPEGD_HEIGHT << "]." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
*width = widthTmp;
|
||||
*height = heightTmp;
|
||||
*components = componentsTmp;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Get the size of the buffer for storing decoded images based on the image data, size, and format
|
||||
* @param: data specifies the memory to store the image data
|
||||
* @param: dataSize specifies the size of the image data
|
||||
* @param: format specifies the image format
|
||||
* @param: decSize is used to store the result size
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format,
|
||||
uint32_t *decSize) {
|
||||
uint32_t outputSize;
|
||||
int ret = acldvppJpegPredictDecSize(data, dataSize, format, &outputSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to predict decode size of jpeg image, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
*decSize = outputSize;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Decode the image specified by imageInfo and save the result to member variable decodedImage_
|
||||
* @param: imageInfo specifies image information
|
||||
* @param: format specifies the image format
|
||||
* @param: withSynchronize specifies whether to execute synchronously
|
||||
* @return: OK if success, other values if failure
|
||||
* @attention: This function can be called only when the DvppCommon object is initialized with Init
|
||||
*/
|
||||
int DvppCommon::CombineJpegdProcess(const RawData& imageInfo, acldvppPixelFormat format, bool withSynchronize) {
|
||||
int32_t components;
|
||||
inputImage_ = std::make_shared<DvppDataInfo>();
|
||||
inputImage_->format = format;
|
||||
int ret = GetJpegImageInfo(imageInfo.data.get(), imageInfo.lenOfByte, &(inputImage_->width), &(inputImage_->height),
|
||||
&components);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to get input image info, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Get the buffer size of decode output according to the input data and output format
|
||||
uint32_t outBuffSize;
|
||||
ret = GetJpegDecodeDataSize(imageInfo.data.get(), imageInfo.lenOfByte, format, &outBuffSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to get size of decode output buffer, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// In TransferImageH2D function, device buffer will be alloced to store the input image
|
||||
// Need to pay attention to release of the buffer
|
||||
ret = TransferImageH2D(imageInfo, inputImage_);
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
decodedImage_ = std::make_shared<DvppDataInfo>();
|
||||
decodedImage_->format = format;
|
||||
decodedImage_->width = inputImage_->width;
|
||||
decodedImage_->height = inputImage_->height;
|
||||
GetJpegDecodeStrideSize(inputImage_->width, inputImage_->height, &(decodedImage_->widthStride),
|
||||
&(decodedImage_->heightStride));
|
||||
decodedImage_->dataSize = outBuffSize;
|
||||
// Need to pay attention to release of the buffer
|
||||
ret = acldvppMalloc(reinterpret_cast<void **>(&decodedImage_->data), decodedImage_->dataSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc memory on dvpp, ret = " << ret << "." << std::endl;
|
||||
RELEASE_DVPP_DATA(inputImage_->data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = JpegDecode(inputImage_, decodedImage_, withSynchronize);
|
||||
if (ret != OK) {
|
||||
RELEASE_DVPP_DATA(inputImage_->data);
|
||||
inputImage_->data = nullptr;
|
||||
RELEASE_DVPP_DATA(decodedImage_->data);
|
||||
decodedImage_->data = nullptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* @description: Transfer data from host to device
|
||||
* @param: imageInfo specifies the image data on the host
|
||||
* @param: jpegInput is used to save the buffer and its size which is allocate on the device
|
||||
* @return: OK if success, other values if failure
|
||||
*/
|
||||
int DvppCommon::TransferImageH2D(const RawData& imageInfo, const std::shared_ptr<DvppDataInfo>& jpegInput) {
|
||||
if (imageInfo.lenOfByte == 0) {
|
||||
std::cout << "The input buffer size on host should not be empty." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
uint8_t* inDevBuff = nullptr;
|
||||
int ret = acldvppMalloc(reinterpret_cast<void **>(&inDevBuff), imageInfo.lenOfByte);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to malloc " << imageInfo.lenOfByte << " bytes on dvpp, ret = " << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Copy the image data from host to device
|
||||
ret = aclrtMemcpyAsync(inDevBuff, imageInfo.lenOfByte, imageInfo.data.get(), imageInfo.lenOfByte,
|
||||
ACL_MEMCPY_HOST_TO_DEVICE, dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to copy " << imageInfo.lenOfByte << " bytes from host to device" << std::endl;
|
||||
RELEASE_DVPP_DATA(inDevBuff);
|
||||
return ret;
|
||||
}
|
||||
// Attention: We must call the aclrtSynchronizeStream to ensure the task of memory replication has been completed
|
||||
// after calling aclrtMemcpyAsync
|
||||
ret = aclrtSynchronizeStream(dvppStream_);
|
||||
if (ret != OK) {
|
||||
std::cout << "Failed to synchronize stream, ret = " << ret << "." << std::endl;
|
||||
RELEASE_DVPP_DATA(inDevBuff);
|
||||
return ret;
|
||||
}
|
||||
jpegInput->data = inDevBuff;
|
||||
jpegInput->dataSize = imageInfo.lenOfByte;
|
||||
return OK;
|
||||
}
|
||||
|
||||
std::shared_ptr<DvppDataInfo> DvppCommon::GetInputImage() {
|
||||
return inputImage_;
|
||||
}
|
||||
|
||||
std::shared_ptr<DvppDataInfo> DvppCommon::GetDecodedImage() {
|
||||
return decodedImage_;
|
||||
}
|
||||
|
||||
std::shared_ptr<DvppDataInfo> DvppCommon::GetResizedImage() {
|
||||
return resizedImage_;
|
||||
}
|
||||
|
||||
DvppCommon::~DvppCommon() {}
|
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
* Copyright(C) 2020. Huawei Technologies Co.,Ltd. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 <sys/time.h>
|
||||
#include <fstream>
|
||||
#include "../inc/ModelProcess.h"
|
||||
|
||||
ModelProcess::ModelProcess(const int deviceId) {
|
||||
deviceId_ = deviceId;
|
||||
}
|
||||
|
||||
ModelProcess::ModelProcess() {}
|
||||
|
||||
ModelProcess::~ModelProcess() {
|
||||
if (!isDeInit_) {
|
||||
DeInit();
|
||||
}
|
||||
}
|
||||
|
||||
void ModelProcess::DestroyDataset(aclmdlDataset *dataset) {
|
||||
// Just release the DataBuffer object and DataSet object, remain the buffer, because it is managerd by user
|
||||
if (dataset != nullptr) {
|
||||
for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(dataset); i++) {
|
||||
aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(dataset, i);
|
||||
if (dataBuffer != nullptr) {
|
||||
aclDestroyDataBuffer(dataBuffer);
|
||||
dataBuffer = nullptr;
|
||||
}
|
||||
}
|
||||
aclmdlDestroyDataset(dataset);
|
||||
}
|
||||
}
|
||||
|
||||
aclmdlDesc *ModelProcess::GetModelDesc() {
|
||||
return modelDesc_.get();
|
||||
}
|
||||
|
||||
int ModelProcess::ModelInference(const std::vector<void *> &inputBufs,
|
||||
const std::vector<size_t> &inputSizes,
|
||||
const std::vector<void *> &ouputBufs,
|
||||
const std::vector<size_t> &outputSizes,
|
||||
std::map<double, double> *costTime_map) {
|
||||
std::cout << "ModelProcess:Begin to inference." << std::endl;
|
||||
aclmdlDataset *input = nullptr;
|
||||
input = CreateAndFillDataset(inputBufs, inputSizes);
|
||||
if (input == nullptr) {
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
int ret = 0;
|
||||
|
||||
aclmdlDataset *output = nullptr;
|
||||
output = CreateAndFillDataset(ouputBufs, outputSizes);
|
||||
if (output == nullptr) {
|
||||
DestroyDataset(input);
|
||||
input = nullptr;
|
||||
return INVALID_POINTER;
|
||||
}
|
||||
struct timeval start;
|
||||
struct timeval end;
|
||||
double startTime_ms;
|
||||
double endTime_ms;
|
||||
mtx_.lock();
|
||||
gettimeofday(&start, NULL);
|
||||
ret = aclmdlExecute(modelId_, input, output);
|
||||
gettimeofday(&end, NULL);
|
||||
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
|
||||
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
|
||||
costTime_map->insert(std::pair<double, double>(startTime_ms, endTime_ms));
|
||||
mtx_.unlock();
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlExecute failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
DestroyDataset(input);
|
||||
DestroyDataset(output);
|
||||
return OK;
|
||||
}
|
||||
|
||||
int ModelProcess::DeInit() {
|
||||
isDeInit_ = true;
|
||||
int ret = aclmdlUnload(modelId_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlUnload failed, ret["<< ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (modelDevPtr_ != nullptr) {
|
||||
ret = aclrtFree(modelDevPtr_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
modelDevPtr_ = nullptr;
|
||||
}
|
||||
if (weightDevPtr_ != nullptr) {
|
||||
ret = aclrtFree(weightDevPtr_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtFree failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
weightDevPtr_ = nullptr;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
/**
|
||||
* Read a binary file, store the data into a uint8_t array
|
||||
*
|
||||
* @param fileName the file for reading
|
||||
* @param buffShared a shared pointer to a uint8_t array for storing file
|
||||
* @param buffLength the length of the array
|
||||
* @return OK if create success, error code otherwise
|
||||
*/
|
||||
int ModelProcess::ReadBinaryFile(const std::string &fileName, uint8_t **buffShared, int *buffLength) {
|
||||
std::ifstream inFile(fileName, std::ios::in | std::ios::binary);
|
||||
if (!inFile) {
|
||||
std::cout << "FaceFeatureLib: read file " << fileName << " fail." <<std::endl;
|
||||
return READ_FILE_FAIL;
|
||||
}
|
||||
|
||||
inFile.seekg(0, inFile.end);
|
||||
*buffLength = inFile.tellg();
|
||||
inFile.seekg(0, inFile.beg);
|
||||
|
||||
uint8_t *tempShared = reinterpret_cast<uint8_t *>(malloc(*buffLength));
|
||||
inFile.read(reinterpret_cast<char *>(tempShared), *buffLength);
|
||||
inFile.close();
|
||||
*buffShared = tempShared;
|
||||
|
||||
std::cout << "read file: fileName=" << fileName << ", size=" << *buffLength << "." << std::endl;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int ModelProcess::Init(const std::string &modelPath) {
|
||||
std::cout << "ModelProcess:Begin to init instance." << std::endl;
|
||||
int modelSize = 0;
|
||||
uint8_t *modelData = nullptr;
|
||||
int ret = ReadBinaryFile(modelPath, &modelData, &modelSize);
|
||||
if (ret != OK) {
|
||||
std::cout << "read model file failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclmdlQuerySizeFromMem(modelData, modelSize, &modelDevPtrSize_, &weightDevPtrSize_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlQuerySizeFromMem failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
std::cout << "modelDevPtrSize_[" << modelDevPtrSize_ << "]" << std::endl;
|
||||
std::cout << " weightDevPtrSize_[" << weightDevPtrSize_ << "]." << std::endl;
|
||||
|
||||
ret = aclrtMalloc(&modelDevPtr_, modelDevPtrSize_, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtMalloc dev_ptr failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclrtMalloc(&weightDevPtr_, weightDevPtrSize_, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtMalloc weight_ptr failed, ret[" << ret << "] " << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclmdlLoadFromMemWithMem(modelData, modelSize, &modelId_, modelDevPtr_, modelDevPtrSize_,
|
||||
weightDevPtr_, weightDevPtrSize_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlLoadFromMemWithMem failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclrtGetCurrentContext(&contextModel_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclrtMalloc weight_ptr failed, ret[" << ret << "]." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
aclmdlDesc *modelDesc = aclmdlCreateDesc();
|
||||
if (modelDesc == nullptr) {
|
||||
std::cout << "aclmdlCreateDesc failed." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
ret = aclmdlGetDesc(modelDesc, modelId_);
|
||||
if (ret != OK) {
|
||||
std::cout << "aclmdlGetDesc ret fail, ret:" << ret << "." << std::endl;
|
||||
return ret;
|
||||
}
|
||||
modelDesc_.reset(modelDesc, aclmdlDestroyDesc);
|
||||
free(modelData);
|
||||
return OK;
|
||||
}
|
||||
|
||||
aclmdlDataset *ModelProcess::CreateAndFillDataset(const std::vector<void *> &bufs, const std::vector<size_t> &sizes) {
|
||||
aclmdlDataset *dataset = aclmdlCreateDataset();
|
||||
if (dataset == nullptr) {
|
||||
std::cout << "ACL_ModelInputCreate failed." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bufs.size(); ++i) {
|
||||
aclDataBuffer *data = aclCreateDataBuffer(bufs[i], sizes[i]);
|
||||
if (data == nullptr) {
|
||||
DestroyDataset(dataset);
|
||||
std::cout << "aclCreateDataBuffer failed." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ret = aclmdlAddDatasetBuffer(dataset, data);
|
||||
if (ret != OK) {
|
||||
DestroyDataset(dataset);
|
||||
std::cout << "ACL_ModelInputDataAdd failed, ret[" << ret << "]." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return dataset;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#!/bin/bash
|
||||
# Copyright 2020 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
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
path_cur=$(cd "`dirname $0`"; pwd)
|
||||
build_type="Release"
|
||||
|
||||
function preparePath() {
|
||||
rm -rf $1
|
||||
mkdir -p $1
|
||||
cd $1
|
||||
}
|
||||
|
||||
function buildA300() {
|
||||
if [ ! "${ARCH_PATTERN}" ]; then
|
||||
# set ARCH_PATTERN to acllib when it was not specified by user
|
||||
export ARCH_PATTERN=acllib
|
||||
echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}"
|
||||
else
|
||||
echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib"
|
||||
export ARCH_PATTERN=${ARCH_PATTERN}/acllib
|
||||
fi
|
||||
|
||||
path_build=$path_cur/build
|
||||
preparePath $path_build
|
||||
cmake -DCMAKE_BUILD_TYPE=$build_type ..
|
||||
make -j
|
||||
ret=$?
|
||||
cd ..
|
||||
return ${ret}
|
||||
}
|
||||
|
||||
# set ASCEND_VERSION to ascend-toolkit/latest when it was not specified by user
|
||||
if [ ! "${ASCEND_VERSION}" ]; then
|
||||
export ASCEND_VERSION=ascend-toolkit/latest
|
||||
echo "Set ASCEND_VERSION to the default value: ${ASCEND_VERSION}"
|
||||
else
|
||||
echo "ASCEND_VERSION is set to ${ASCEND_VERSION} by user"
|
||||
fi
|
||||
|
||||
buildA300
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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
|
||||
*
|
||||
* 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 <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gflags/gflags.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include "../inc/AclProcess.h"
|
||||
#include "../inc/CommonDataType.h"
|
||||
|
||||
DEFINE_string(om_path, "./maskrcnn.om", "om model path.");
|
||||
DEFINE_string(data_path, "./test.jpg", "om model path.");
|
||||
DEFINE_int32(width, 1280, "width");
|
||||
DEFINE_int32(height, 768, "height");
|
||||
DEFINE_int32(device_id, 0, "height");
|
||||
|
||||
static bool is_file(const std::string &filename) {
|
||||
struct stat buffer;
|
||||
return (stat(filename.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
|
||||
}
|
||||
|
||||
static bool is_dir(const std::string &filefodler) {
|
||||
struct stat buffer;
|
||||
return (stat(filefodler.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
|
||||
}
|
||||
/*
|
||||
* @description Initialize and run AclProcess module
|
||||
* @param resourceInfo resource info of deviceIds, model info, single Operator Path, etc
|
||||
* @param file the absolute path of input file
|
||||
* @return int int code
|
||||
*/
|
||||
int main(int argc, char* argv[]) {
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
std::cout << "OM File Path :" << FLAGS_om_path << std::endl;
|
||||
std::cout << "data Path :" << FLAGS_data_path << std::endl;
|
||||
std::cout << "width :" << FLAGS_width << std::endl;
|
||||
std::cout << "height :" << FLAGS_height << std::endl;
|
||||
std::cout << "deviceId :" << FLAGS_device_id << std::endl;
|
||||
|
||||
char omAbsPath[PATH_MAX];
|
||||
if (realpath(FLAGS_om_path.c_str(), omAbsPath) == nullptr) {
|
||||
std::cout << "Failed to get the om real path." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
if (access(omAbsPath, R_OK) == -1) {
|
||||
std::cout << "ModelPath " << omAbsPath << " doesn't exist or read failed." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
char dataAbsPath[PATH_MAX];
|
||||
if (realpath(FLAGS_data_path.c_str(), dataAbsPath) == nullptr) {
|
||||
std::cout << "Failed to get the data real path." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
if (access(dataAbsPath, R_OK) == -1) {
|
||||
std::cout << "data paeh " << dataAbsPath << " doesn't exist or read failed." << std::endl;
|
||||
return INVALID_PARAM;
|
||||
}
|
||||
|
||||
std::map<double, double> costTime_map;
|
||||
AclProcess aclProcess(FLAGS_device_id, FLAGS_om_path, FLAGS_width, FLAGS_height);
|
||||
int ret = aclProcess.InitResource();
|
||||
if (ret != OK) {
|
||||
aclProcess.Release();
|
||||
return ret;
|
||||
}
|
||||
if (is_file(FLAGS_data_path)) {
|
||||
aclProcess.Process(FLAGS_data_path, &costTime_map);
|
||||
} else if (is_dir(FLAGS_data_path)) {
|
||||
struct dirent * filename;
|
||||
DIR * dir;
|
||||
dir = opendir(FLAGS_data_path.c_str());
|
||||
if (dir == nullptr) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
if (strcmp(filename->d_name, ".") == 0 || strcmp(filename->d_name, "..") == 0) {
|
||||
continue;
|
||||
}
|
||||
std::string wholePath = FLAGS_data_path + "/" + filename->d_name;
|
||||
aclProcess.Process(wholePath, &costTime_map);
|
||||
}
|
||||
} else {
|
||||
std::cout << " input image path error" << std::endl;
|
||||
}
|
||||
|
||||
double average = 0.0;
|
||||
int infer_cnt = 0;
|
||||
char tmpCh[256];
|
||||
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
|
||||
double diff = 0.0;
|
||||
diff = iter->second - iter->first;
|
||||
average += diff;
|
||||
infer_cnt++;
|
||||
}
|
||||
average = average/infer_cnt;
|
||||
memset(tmpCh, 0, sizeof(tmpCh));
|
||||
snprintf(tmpCh, sizeof(tmpCh), "NN inference cost average time: %4.3f ms of infer_count %d \n", average, infer_cnt);
|
||||
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl;
|
||||
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt");
|
||||
std::ofstream file_stream(file_name.c_str(), std::ios::trunc);
|
||||
file_stream << tmpCh;
|
||||
file_stream.close();
|
||||
costTime_map.clear();
|
||||
|
||||
aclProcess.Release();
|
||||
return OK;
|
||||
}
|
|
@ -18,7 +18,7 @@ import numpy as np
|
|||
|
||||
from mindspore import Tensor, context, load_checkpoint, load_param_into_net, export
|
||||
|
||||
from src.maskrcnn.mask_rcnn_r50 import Mask_Rcnn_Resnet50
|
||||
from src.maskrcnn.mask_rcnn_r50 import MaskRcnn_Infer
|
||||
from src.config import config
|
||||
|
||||
parser = argparse.ArgumentParser(description='maskrcnn export')
|
||||
|
@ -34,19 +34,20 @@ args = parser.parse_args()
|
|||
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id)
|
||||
|
||||
if __name__ == '__main__':
|
||||
net = Mask_Rcnn_Resnet50(config=config)
|
||||
net = MaskRcnn_Infer(config=config)
|
||||
param_dict = load_checkpoint(args.ckpt_file)
|
||||
load_param_into_net(net, param_dict)
|
||||
|
||||
param_dict_new = {}
|
||||
for key, value in param_dict.items():
|
||||
param_dict_new["network." + key] = value
|
||||
|
||||
load_param_into_net(net, param_dict_new)
|
||||
net.set_train(False)
|
||||
|
||||
bs = config.test_batch_size
|
||||
|
||||
img = Tensor(np.zeros([args.batch_size, 3, config.img_height, config.img_width], np.float16))
|
||||
img_metas = Tensor(np.zeros([args.batch_size, 4], np.float16))
|
||||
gt_bboxes = Tensor(np.zeros([args.batch_size, config.num_gts, 4], np.float16))
|
||||
gt_labels = Tensor(np.zeros([args.batch_size, config.num_gts], np.int32))
|
||||
gt_num = Tensor(np.zeros([args.batch_size, config.num_gts], np.bool))
|
||||
gt_mask = Tensor(np.zeros([args.batch_size, config.num_gts], np.bool))
|
||||
|
||||
input_data = [img, img_metas, gt_bboxes, gt_labels, gt_num, gt_mask]
|
||||
input_data = [img, img_metas]
|
||||
export(net, *input_data, file_name=args.file_name, file_format=args.file_format)
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
# Copyright 2020 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.
|
||||
# ============================================================================
|
||||
"""post process for 310 inference"""
|
||||
import argparse
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
from pycocotools.coco import COCO
|
||||
|
||||
from src.config import config
|
||||
from src.util import coco_eval, bbox2result_1image, results2json, get_seg_masks
|
||||
|
||||
dst_width = 1280
|
||||
dst_height = 768
|
||||
|
||||
parser = argparse.ArgumentParser(description="maskrcnn inference")
|
||||
parser.add_argument("--ann_file", type=str, required=True, help="ann file.")
|
||||
parser.add_argument("--img_path", type=str, required=True, help="image file path.")
|
||||
args = parser.parse_args()
|
||||
|
||||
def get_imgSize(file_name):
|
||||
img = Image.open(file_name)
|
||||
return img.size
|
||||
|
||||
def get_resizeRatio(img_size):
|
||||
org_width, org_height = img_size
|
||||
resize_ratio = dst_width / org_width
|
||||
if resize_ratio > dst_height / org_height:
|
||||
resize_ratio = dst_height / org_height
|
||||
|
||||
return resize_ratio
|
||||
|
||||
def get_eval_result(ann_file, img_path):
|
||||
max_num = 128
|
||||
result_path = "./result_Files/"
|
||||
outputs = []
|
||||
|
||||
dataset_coco = COCO(ann_file)
|
||||
img_ids = dataset_coco.getImgIds()
|
||||
|
||||
for img_id in img_ids:
|
||||
file_id = str(img_id).zfill(12)
|
||||
file = img_path + "/" + file_id + ".jpg"
|
||||
img_size = get_imgSize(file)
|
||||
resize_ratio = get_resizeRatio(img_size)
|
||||
|
||||
img_metas = np.array([img_size[1], img_size[0]] + [resize_ratio, resize_ratio])
|
||||
|
||||
bbox_result_file = result_path + file_id + "_0.bin"
|
||||
label_result_file = result_path + file_id + "_1.bin"
|
||||
mask_result_file = result_path + file_id + "_2.bin"
|
||||
mask_fb_result_file = result_path + file_id + "_3.bin"
|
||||
|
||||
all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5)
|
||||
all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1)
|
||||
all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1)
|
||||
all_mask_fb = np.fromfile(mask_fb_result_file, dtype=np.float16).reshape(80000, 28, 28)
|
||||
|
||||
all_bbox_squee = np.squeeze(all_bbox)
|
||||
all_label_squee = np.squeeze(all_label)
|
||||
all_mask_squee = np.squeeze(all_mask)
|
||||
all_mask_fb_squee = np.squeeze(all_mask_fb)
|
||||
|
||||
all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
|
||||
all_labels_tmp_mask = all_label_squee[all_mask_squee]
|
||||
all_mask_fb_tmp_mask = all_mask_fb_squee[all_mask_squee, :, :]
|
||||
|
||||
if all_bboxes_tmp_mask.shape[0] > max_num:
|
||||
inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
|
||||
inds = inds[:max_num]
|
||||
all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
|
||||
all_labels_tmp_mask = all_labels_tmp_mask[inds]
|
||||
all_mask_fb_tmp_mask = all_mask_fb_tmp_mask[inds]
|
||||
|
||||
bbox_results = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes)
|
||||
segm_results = get_seg_masks(all_mask_fb_tmp_mask, all_bboxes_tmp_mask, all_labels_tmp_mask, img_metas,
|
||||
True, config.num_classes)
|
||||
outputs.append((bbox_results, segm_results))
|
||||
|
||||
eval_types = ["bbox", "segm"]
|
||||
result_files = results2json(dataset_coco, outputs, "./results.pkl")
|
||||
coco_eval(result_files, eval_types, dataset_coco, single_result=False)
|
||||
|
||||
if __name__ == '__main__':
|
||||
get_eval_result(args.ann_file, args.img_path)
|
|
@ -0,0 +1,92 @@
|
|||
#!/bin/bash
|
||||
# Copyright 2020 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
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
if [ $# != 3 ]
|
||||
then
|
||||
echo "Usage: sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
get_real_path(){
|
||||
if [ "${1:0:1}" == "/" ]; then
|
||||
echo "$1"
|
||||
else
|
||||
echo "$(realpath -m $PWD/$1)"
|
||||
fi
|
||||
}
|
||||
model=$(get_real_path $1)
|
||||
data_path=$(get_real_path $2)
|
||||
ann_file=$(get_real_path $3)
|
||||
echo $model
|
||||
echo $data_path
|
||||
echo $ann_file
|
||||
export ASCEND_HOME=/usr/local/Ascend/
|
||||
export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH
|
||||
export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ones:$LD_LIBRARY_PATH
|
||||
export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages/te.egg:$ASCEND_HOME/atc/python/site-packages/topi.egg:$ASCEND_HOME/atc/python/site-packages/auto_tune.egg::$ASCEND_HOME/atc/python/site-packages/schedule_search.egg:$PYTHONPATH
|
||||
export ASCEND_OPP_PATH=$ASCEND_HOME/opp
|
||||
|
||||
function air_to_om()
|
||||
{
|
||||
atc --input_format=NCHW --framework=1 --model=$model --input_shape="x:1, 3, 768, 1280; im_info: 1, 4" --output=maskrcnn --insert_op_conf=../src/aipp.cfg --precision_mode=allow_fp32_to_fp16 --soc_version=Ascend310
|
||||
}
|
||||
|
||||
function compile_app()
|
||||
{
|
||||
cd ../ascend310_infer/src
|
||||
sh build.sh
|
||||
cd -
|
||||
}
|
||||
|
||||
function infer()
|
||||
{
|
||||
if [ -d result_Files ]; then
|
||||
rm -rf ./result_Files
|
||||
fi
|
||||
if [ -d time_Result ]; then
|
||||
rm -rf ./time_Result
|
||||
fi
|
||||
mkdir result_Files
|
||||
mkdir time_Result
|
||||
../ascend310_infer/src/out/main --om_path=./maskrcnn.om --data_path=$data_path
|
||||
}
|
||||
|
||||
function cal_acc()
|
||||
{
|
||||
python ../postprocess.py --ann_file=$ann_file --img_path=$data_path &> log &
|
||||
}
|
||||
|
||||
air_to_om
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "air to om failed"
|
||||
exit 1
|
||||
fi
|
||||
compile_app
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "compile app code failed"
|
||||
exit 1
|
||||
fi
|
||||
infer
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "excute inference failed"
|
||||
exit 1
|
||||
fi
|
||||
cal_acc
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "calculate accuracy failed"
|
||||
exit 1
|
||||
fi
|
|
@ -0,0 +1,26 @@
|
|||
aipp_op {
|
||||
aipp_mode : static
|
||||
input_format : YUV420SP_U8
|
||||
related_input_rank : 0
|
||||
csc_switch : true
|
||||
rbuv_swap_switch : false
|
||||
matrix_r0c0 : 256
|
||||
matrix_r0c1 : 0
|
||||
matrix_r0c2 : 359
|
||||
matrix_r1c0 : 256
|
||||
matrix_r1c1 : -88
|
||||
matrix_r1c2 : -183
|
||||
matrix_r2c0 : 256
|
||||
matrix_r2c1 : 454
|
||||
matrix_r2c2 : 0
|
||||
input_bias_0 : 0
|
||||
input_bias_1 : 128
|
||||
input_bias_2 : 128
|
||||
|
||||
mean_chn_0 : 124
|
||||
mean_chn_1 : 117
|
||||
mean_chn_2 : 104
|
||||
var_reci_chn_0 : 0.0171247538316637
|
||||
var_reci_chn_1 : 0.0175070028011204
|
||||
var_reci_chn_2 : 0.0174291938997821
|
||||
}
|
|
@ -567,3 +567,13 @@ class Mask_Rcnn_Resnet50(nn.Cell):
|
|||
roi_feats_mask_test = self.cast(roi_feats_mask_test, mstype.float16)
|
||||
mask_fb_pred_all = self.rcnn_mask(roi_feats_mask_test)
|
||||
return mask_fb_pred_all
|
||||
|
||||
class MaskRcnn_Infer(nn.Cell):
|
||||
def __init__(self, config):
|
||||
super(MaskRcnn_Infer, self).__init__()
|
||||
self.network = Mask_Rcnn_Resnet50(config)
|
||||
self.network.set_train(False)
|
||||
|
||||
def construct(self, img_data, img_metas):
|
||||
output = self.network(img_data, img_metas, None, None, None, None)
|
||||
return output
|
||||
|
|
Loading…
Reference in New Issue