!9844 New Dvpp operation for Ascend 310 chip

From: @lizhenglong1992
Reviewed-by: 
Signed-off-by:
pull/9844/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 280db3d651

@ -3,6 +3,13 @@ include_directories(${CMAKE_SOURCE_DIR}/mindspore/core)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_BINARY_DIR})
if (ENABLE_ACL)
set(ASCEND_PATH /usr/local/Ascend)
include_directories(${ASCEND_PATH}/acllib/include)
link_directories(${ASCEND_PATH}/acllib/lib64/)
find_library(ascendcl acl_dvpp ${ASCEND_PATH}/acllib/lib64)
endif ()
if (NOT(CMAKE_SYSTEM_NAME MATCHES "Darwin"))
link_directories(${CMAKE_SOURCE_DIR}/build/mindspore/graphengine)
endif ()

@ -17,6 +17,10 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-attributes")
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
add_definitions(-D _CRT_RAND_S)
endif ()
if (ENABLE_ACL)
add_definitions(-D ENABLE_ACL)
message(STATUS "ACL module is enabled")
endif ()
if (ENABLE_GPUQUE)
add_definitions(-D ENABLE_GPUQUE)
message(STATUS "GPU queue is enabled")
@ -114,6 +118,10 @@ if (NOT(${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
add_dependencies(text-kernels core)
endif ()
if (ENABLE_ACL)
add_dependencies(kernels-dvpp-image core dvpp-utils)
endif ()
if (ENABLE_PYTHON)
add_dependencies(APItoPython core)
endif ()
@ -163,6 +171,13 @@ if (NOT(${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
)
endif ()
if (ENABLE_ACL)
set(submodules
${submodules}
$<TARGET_OBJECTS:kernels-dvpp-image>
$<TARGET_OBJECTS:dvpp-utils>)
endif ()
if (ENABLE_PYTHON)
set(submodules
${submodules}
@ -186,6 +201,11 @@ endif ()
################# Link with external libraries ########################
target_link_libraries(_c_dataengine PRIVATE mindspore mindspore_gvar)
if (ENABLE_ACL)
target_link_libraries(_c_dataengine PRIVATE ascendcl acl_dvpp)
endif ()
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
if (ENABLE_PYTHON)
target_link_libraries(_c_dataengine PRIVATE mindspore::pybind11_module ${PYTHON_LIBRARIES} ${SECUREC_LIBRARY})

@ -31,6 +31,9 @@
#include "minddata/dataset/kernels/image/cut_out_op.h"
#endif
#include "minddata/dataset/kernels/image/decode_op.h"
#ifdef ENABLE_ACL
#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h"
#endif
#ifndef ENABLE_ANDROID
#include "minddata/dataset/kernels/image/equalize_op.h"
#include "minddata/dataset/kernels/image/hwc_to_chw_op.h"
@ -133,6 +136,16 @@ std::shared_ptr<DecodeOperation> Decode(bool rgb) {
return op->ValidateParams() ? op : nullptr;
}
#ifdef ENABLE_ACL
// Function to create DvppDecodeResizeCropOperation.
std::shared_ptr<DvppDecodeResizeCropOperation> DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop,
std::vector<uint32_t> resize) {
auto op = std::make_shared<DvppDecodeResizeCropOperation>(crop, resize);
// Input validation
return op->ValidateParams() ? op : nullptr;
}
#endif
// Function to create EqualizeOperation.
std::shared_ptr<EqualizeOperation> Equalize() {
auto op = std::make_shared<EqualizeOperation>();
@ -656,6 +669,78 @@ Status MixUpBatchOperation::ValidateParams() {
std::shared_ptr<TensorOp> MixUpBatchOperation::Build() { return std::make_shared<MixUpBatchOp>(alpha_); }
#endif
#ifdef ENABLE_ACL
// DvppDecodeResizeCropOperation
DvppDecodeResizeCropOperation::DvppDecodeResizeCropOperation(const std::vector<uint32_t> &crop,
const std::vector<uint32_t> &resize)
: crop_(crop), resize_(resize) {}
Status DvppDecodeResizeCropOperation::ValidateParams() {
// size
if (crop_.empty() || crop_.size() > 2) {
std::string err_msg = "DvppDecodeResizeCropJpeg: crop size must be a vector of one or two elements, got: " +
std::to_string(crop_.size());
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
if (resize_.empty() || resize_.size() > 2) {
std::string err_msg = "DvppDecodeResizeCropJpeg: resize size must be a vector of one or two elements, got: " +
std::to_string(resize_.size());
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
if (crop_.size() < resize_.size()) {
if (crop_[0] >= MIN(resize_[0], resize_[1])) {
std::string err_msg = "crop size must be smaller than resize size";
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
}
if (crop_.size() > resize_.size()) {
if (MAX(crop_[0], crop_[1]) >= resize_[0]) {
std::string err_msg = "crop size must be smaller than resize size";
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
}
if (crop_.size() == resize_.size()) {
for (int32_t i = 0; i < crop_.size(); ++i) {
if (crop_[i] >= resize_[i]) {
std::string err_msg = "crop size must be smaller than resize size";
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
}
}
return Status::OK();
}
std::shared_ptr<TensorOp> DvppDecodeResizeCropOperation::Build() {
// If size is a single value, the smaller edge of the image will be
// resized to this value with the same image aspect ratio.
uint32_t cropHeight, cropWidth, resizeHeight, resizeWidth;
if (crop_.size() == 1) {
cropHeight = crop_[0];
cropWidth = crop_[0];
} else {
cropHeight = crop_[0];
cropWidth = crop_[1];
}
// User specified the width value.
if (resize_.size() == 1) {
resizeHeight = resize_[0];
resizeWidth = resize_[0];
} else {
resizeHeight = resize_[0];
resizeWidth = resize_[1];
}
std::shared_ptr<DvppDecodeResizeCropJpegOp> tensor_op =
std::make_shared<DvppDecodeResizeCropJpegOp>(cropHeight, cropWidth, resizeHeight, resizeWidth);
return tensor_op;
}
#endif
// NormalizeOperation
NormalizeOperation::NormalizeOperation(std::vector<float> mean, std::vector<float> std) : mean_(mean), std_(std) {}

@ -38,6 +38,7 @@ constexpr char kAutoContrastOperation[] = "AutoContrast";
constexpr char kBoundingBoxAugmentOperation[] = "BoundingBoxAugment";
constexpr char kCutMixBatchOperation[] = "CutMixBatch";
constexpr char kCutOutOperation[] = "CutOut";
constexpr char kDvppDecodeResizeCropOperation[] = "DvppDecodeResizeCrop";
constexpr char kEqualizeOperation[] = "Equalize";
constexpr char kHwcToChwOperation[] = "HwcToChw";
constexpr char kInvertOperation[] = "Invert";
@ -76,6 +77,7 @@ class AutoContrastOperation;
class BoundingBoxAugmentOperation;
class CutMixBatchOperation;
class CutOutOperation;
class DvppDecodeResizeCropOperation;
class EqualizeOperation;
class HwcToChwOperation;
class InvertOperation;
@ -142,6 +144,22 @@ std::shared_ptr<CutMixBatchOperation> CutMixBatch(ImageBatchFormat image_batch_f
/// \return Shared pointer to the current TensorOp
std::shared_ptr<CutOutOperation> CutOut(int32_t length, int32_t num_patches = 1);
/// \brief Function to create a DvppDecodeResizeCropJpeg TensorOperation.
/// \notes Tensor operation to decode and resize JPEG image using the simulation algorithm of Ascend series
/// chip DVPP module. It is recommended to use this algorithm in the following scenarios:
/// When training, the DVPP of the Ascend chip is not used,
/// and the DVPP of the Ascend chip is used during inference,
/// and the accuracy of inference is lower than the accuracy of training;
/// and the input image size should be in range [16*16, 4096*4096].
/// Only images with an even resolution can be output. The output of odd resolution is not supported.
/// \param[in] crop vector representing the output size of the final crop image.
/// \param[in] size A vector representing the output size of the intermediate resized image.
/// If size is a single value, smaller edge of the image will be resized to this value with
/// the same image aspect ratio. If size has 2 values, it should be (height, width).
/// \return Shared pointer to the current TensorOperation.
std::shared_ptr<DvppDecodeResizeCropOperation> DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop = {224, 224},
std::vector<uint32_t> resize = {256, 256});
/// \brief Function to create a Equalize TensorOperation.
/// \notes Apply histogram equalization on input image.
/// \return Shared pointer to the current TensorOperation.
@ -553,6 +571,23 @@ class CutOutOperation : public TensorOperation {
ImageBatchFormat image_batch_format_;
};
class DvppDecodeResizeCropOperation : public TensorOperation {
public:
explicit DvppDecodeResizeCropOperation(const std::vector<uint32_t> &crop, const std::vector<uint32_t> &resize);
~DvppDecodeResizeCropOperation() = default;
std::shared_ptr<TensorOp> Build() override;
Status ValidateParams() override;
std::string Name() const override { return kDvppDecodeResizeCropOperation; }
private:
std::vector<uint32_t> crop_;
std::vector<uint32_t> resize_;
};
class EqualizeOperation : public TensorOperation {
public:
~EqualizeOperation() = default;

@ -2,6 +2,9 @@ file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc"
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
add_subdirectory(soft_dvpp)
add_subdirectory(lite_cv)
if (ENABLE_ACL)
add_subdirectory(dvpp)
endif ()
add_library(kernels-image OBJECT
affine_op.cc
auto_contrast_op.cc
@ -51,4 +54,8 @@ add_library(kernels-image OBJECT
random_resize_with_bbox_op.cc
random_color_op.cc
)
add_dependencies(kernels-image kernels-soft-dvpp-image )
if (ENABLE_ACL)
add_dependencies(kernels-image kernels-soft-dvpp-image kernels-dvpp-image)
else()
add_dependencies(kernels-image kernels-soft-dvpp-image)
endif ()

@ -0,0 +1,6 @@
file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc")
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
add_subdirectory(utils)
add_library(kernels-dvpp-image OBJECT
dvpp_decode_resize_crop_jpeg_op.cc)
add_dependencies(kernels-dvpp-image dvpp-utils)

@ -0,0 +1,104 @@
/**
* 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.
*/
#include <string>
#include <vector>
#include <iostream>
#include "minddata/dataset/kernels/image/dvpp/utils/AclProcess.h"
#include "minddata/dataset/core/cv_tensor.h"
#include "minddata/dataset/kernels/image/image_utils.h"
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
#include "minddata/dataset/core/data_type.h"
#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h"
namespace mindspore {
namespace dataset {
Status DvppDecodeResizeCropJpegOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
IO_CHECK(input, output);
if (!IsNonEmptyJPEG(input)) {
RETURN_STATUS_UNEXPECTED("SoftDvppDecodeReiszeJpegOp only support process jpeg image.");
}
try {
CHECK_FAIL_RETURN_UNEXPECTED(input->GetBuffer() != nullptr, "The input image buffer is empty.");
unsigned char *buffer = const_cast<unsigned char *>(input->GetBuffer());
RawData imageInfo;
uint32_t filesize = input->SizeInBytes();
imageInfo.lenOfByte = filesize;
imageInfo.data = std::make_shared<uint8_t>();
imageInfo.data.reset(new uint8_t[filesize], std::default_delete<uint8_t[]>());
memcpy_s(imageInfo.data.get(), filesize, buffer, filesize);
// First part end, whose function is to transform data from a Tensor to imageinfo data structure which can be
// applied on device
ResourceInfo resource;
resource.aclConfigPath = "";
resource.deviceIds.insert(0); // 0 is device id which should be refined later!
std::shared_ptr<ResourceManager> instance = ResourceManager::GetInstance();
APP_ERROR ret = instance->InitResource(resource);
if (ret != APP_ERR_OK) {
instance->Release();
std::string error = "Error in Init D-chip:" + std::to_string(ret);
RETURN_STATUS_UNEXPECTED(error);
}
int deviceId = *(resource.deviceIds.begin());
aclrtContext context = instance->GetContext(deviceId);
// Second part end where we initialize the resource of D chip and set up all configures
AclProcess process(resized_width_, resized_height_, crop_width_, crop_height_, context);
process.set_mode(true);
ret = process.InitResource();
if (ret != APP_ERR_OK) {
instance->Release();
std::string error = "Error in Init resource:" + std::to_string(ret);
RETURN_STATUS_UNEXPECTED(error);
}
ret = process.Process(imageInfo);
if (ret != APP_ERR_OK) {
instance->Release();
std::string error = "Error in dvpp processing:" + std::to_string(ret);
RETURN_STATUS_UNEXPECTED(error);
}
// Third part end where we execute the core function of dvpp
auto data = std::static_pointer_cast<unsigned char>(process.Get_Memory_Data());
unsigned char *ret_ptr = data.get();
std::shared_ptr(DvppDataInfo) CropOut = process.Get_Device_Memory_Data();
dsize_t dvpp_length = CropOut->dataSize;
const TensorShape dvpp_shape({dvpp_length, 1, 1});
const DataType dvpp_data_type(DataType::DE_UINT8);
mindspore::dataset::Tensor::CreateFromMemory(dvpp_shape, dvpp_data_type, ret_ptr, output);
if (!((*output)->HasData())) {
std::string error = "[ERROR] Fail to get the Output result from memory!";
RETURN_STATUS_UNEXPECTED(error);
}
process.device_memory_release();
// Last part end where we transform the processed data into a tensor which can be applied in later units.
} catch (const cv::Exception &e) {
std::string error = "[ERROR] Fail in DvppDecodeResizeCropJpegOp:" + std::string(e.what());
RETURN_STATUS_UNEXPECTED(error);
}
return Status::OK();
}
Status DvppDecodeResizeCropJpegOp::OutputShape(const std::vector<TensorShape> &inputs,
std::vector<TensorShape> &outputs) {
RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs));
outputs.clear();
TensorShape out({-1, 1, 1}); // we don't know what is output image size, but we know it should be 3 channels
if (inputs[0].Rank() == 1) outputs.emplace_back(out);
if (!outputs.empty()) return Status::OK();
return Status(StatusCode::kUnexpectedError, "Input has a wrong shape");
}
} // namespace dataset
} // namespace mindspore

@ -0,0 +1,60 @@
/**
* 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.
*/
#ifndef MINDSPORE_DVPP_DECODE_RESIZE_CROP_JPEG_OP_H
#define MINDSPORE_DVPP_DECODE_RESIZE_CROP_JPEG_OP_H
#include <memory>
#include <string>
#include <vector>
#include "minddata/dataset/core/tensor.h"
#include "minddata/dataset/kernels/tensor_op.h"
#include "minddata/dataset/util/status.h"
#include "minddata/dataset/core/data_type.h"
#include "mindspore/core/utils/log_adapter.h"
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
#include "acl/acl.h"
namespace mindspore {
namespace dataset {
class DvppDecodeResizeCropJpegOp : public TensorOp {
public:
DvppDecodeResizeCropJpegOp(int32_t crop_height, int32_t crop_width, int32_t resized_height, int32_t resized_width)
: crop_height_(crop_height),
crop_width_(crop_width),
resized_height_(resized_height),
resized_width_(resized_width) {}
/// \brief Destructor
~DvppDecodeResizeCropJpegOp() = default;
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
std::string Name() const override { return kDvppDecodeResizeCropJpegOp; }
private:
int32_t crop_height_;
int32_t crop_width_;
int32_t resized_height_;
int32_t resized_width_;
};
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_DVPP_DECODE_RESIZE_CROP_JPEG_OP_H

@ -0,0 +1,86 @@
#include <climits>
/*
* 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 <string>
#include <string.h>
#include <map>
#include <iostream>
#include <memory>
#include "acl/acl.h"
#include "CommonDataType.h"
#include "mindspore/core/utils/log_adapter.h"
#include "ErrorCode.h"
#include "DvppCommon.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
mode_t SetFileDefaultUmask();
class AclProcess {
public:
AclProcess(uint32_t resizeWidth, uint32_t resizeHeight, uint32_t cropWidth, uint32_t cropHeight, aclrtContext context,
aclrtStream stream = nullptr, std::shared_ptr<DvppCommon> dvppCommon = nullptr);
~AclProcess(){};
// Release all the resource
APP_ERROR Release();
// Create resource for this sample
APP_ERROR InitResource();
// Process the result
APP_ERROR Process(RawData &ImageInfo);
// API for access memory
std::shared_ptr<void> Get_Memory_Data();
// API for access device memory
std::shared_ptr<DvppDataInfo> Get_Device_Memory_Data();
// change output method
void set_mode(bool flag);
// Get the mode of Acl process
bool get_mode();
// Save the result
APP_ERROR WriteResult(uint32_t fileSize, std::shared_ptr<void> outBuf, std::string filename);
// Color space reform
void YUV420TOYUV444(unsigned char *inputBuffer, unsigned char *outputBuffer, int w, int h);
// Crop definition
void CropConfigFilter(CropRoiConfig &cfg, DvppCropInputInfo &cropinfo);
// D-chip memory release
void device_memory_release();
private:
// Initialize the modules used by this sample
APP_ERROR InitModule();
// Preprocess the input image
APP_ERROR Preprocess(RawData &ImageInfo);
// Filename process
APP_ERROR RenameFile(std::string &filename);
aclrtContext context_;
aclrtStream stream_;
std::shared_ptr<DvppCommon> dvppCommon_; // dvpp object
std::shared_ptr<void> processedInfo_; // processed data
uint32_t resizeWidth_; // dvpp resize width
uint32_t resizeHeight_; // dvpp resize height
uint32_t cropWidth_; // dvpp crop width
uint32_t cropHeight_; // dvpp crop height
bool repeat_; // Repeatly process image or not
};
#endif

@ -0,0 +1,11 @@
file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc")
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
add_definitions(-DENABLE_DVPP_INTERFACE)
add_library(dvpp-utils OBJECT
AclProcess.cc
DvppCommon.cc
ErrorCode.cpp
ResourceManager.cc
)

@ -0,0 +1,187 @@
/*
* 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
#ifndef ENABLE_DVPP_INTERFACE
#define ENABLE_DVPP_INTERFACE
#endif
#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))
const uint32_t VIDEO_H264 = 0;
const uint32_t VIDEO_H265 = 1;
const float SEC2MS = 1000.0;
const uint32_t VIDEO_PROCESS_THREAD = 16;
const int YUV_BGR_SIZE_CONVERT_3 = 3;
const int YUV_BGR_SIZE_CONVERT_2 = 2;
const int DVPP_JPEG_OFFSET = 8;
const int VPC_WIDTH_ALIGN = 16;
const int VPC_HEIGHT_ALIGN = 2;
const int JPEG_WIDTH_ALIGN = 128;
const int JPEG_HEIGHT_ALIGN = 16;
const int VPC_OFFSET_ALIGN = 2;
// Tensor Descriptor
struct Tensor {
aclDataType dataType; // Tensor data type
int numDim; // Number of dimensions of Tensor
std::vector<int64_t> dims; // Dimension vector
aclFormat format; // Format of tensor, e.g. ND, NCHW, NC1HWC0
std::string name; // Name of tensor
};
// Data type of tensor
enum OpAttrType {
BOOL = 0,
INT = 1,
FLOAT = 2,
STRING = 3,
LIST_BOOL = 4,
LIST_INT = 6,
LIST_FLOAT = 7,
LIST_STRING = 8,
LIST_LIST_INT = 9,
};
// operator attribution describe
// type decide whether the other attribute needed to set a value
struct OpAttr {
std::string name;
OpAttrType type;
int num; // LIST_BOOL/INT/FLOAT/STRING/LIST_LIST_INT need
uint8_t numBool; // BOOL need
int64_t numInt; // INT need
float numFloat; // FLOAT need
std::string numString; // STRING need
std::vector<uint8_t> valuesBool; // LIST_BOOL need
std::vector<int64_t> valuesInt; // LIST_INT need
std::vector<float> valuesFloat; // LIST_FLOAT need
std::vector<std::string> valuesString; // LIST_STRING need
std::vector<int> numLists; // LIST_LIST_INT need
std::vector<std::vector<int64_t>> valuesListList; // LIST_LIST_INT need
};
// 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
};
// Description of data in device
struct StreamData {
size_t size; // Size of memory, bytes
std::shared_ptr<void> data; // Smart pointer of data
};
// Description of stream data
struct StreamInfo {
std::string format;
uint32_t height;
uint32_t width;
uint32_t channelId;
std::string streamPath;
};
// define the structure of an rectangle
struct Rectangle {
uint32_t leftTopX;
uint32_t leftTopY;
uint32_t rightBottomX;
uint32_t rightBottomY;
};
struct ObjectDetectInfo {
int32_t classId;
float confidence;
Rectangle location;
};
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;
};
// Description of matrix info
struct MatrixInfo {
uint32_t row = 0; // row of matrix
uint32_t col = 0; // col of matrix
uint32_t dataSize = 0; // size of memory, bytes
std::shared_ptr<void> data = nullptr; // data of matrix
aclDataType dataType = ACL_FLOAT16; // data Type of matrix
};
// Description of coefficient info
struct CoefficientInfo {
std::shared_ptr<void> data = nullptr; // data of coefficient
aclDataType dataType = ACL_FLOAT16; // dataType
};
// define the input of BLAS operator such as producing:
// C = alpha * A * B + beta * C
struct BlasInput {
MatrixInfo A;
MatrixInfo B;
MatrixInfo C;
CoefficientInfo alpha;
CoefficientInfo beta;
};
extern bool g_vdecNotified[VIDEO_PROCESS_THREAD];
extern bool g_vpcNotified[VIDEO_PROCESS_THREAD];
extern bool g_inferNotified[VIDEO_PROCESS_THREAD];
extern bool g_postNotified[VIDEO_PROCESS_THREAD];
#endif

@ -0,0 +1,220 @@
/*
* 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 "CommonDataType.h"
#include "ErrorCode.h"
#include "acl/ops/acl_dvpp.h"
const int MODULUS_NUM_2 = 2;
const uint32_t ODD_NUM_1 = 1;
struct Rect {
/* left location of the rectangle */
uint32_t x;
/* top location of the rectangle */
uint32_t y;
/* with of the rectangle */
uint32_t width;
/* height of the rectangle */
uint32_t height;
};
struct DvppBaseData {
uint32_t dataSize; // Size of data in byte
uint8_t *data;
};
struct VdecConfig {
int inputWidth = 0;
int inputHeight = 0;
acldvppStreamFormat inFormat = H264_MAIN_LEVEL; // stream format renference acldvppStreamFormat
acldvppPixelFormat outFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420; // output format renference acldvppPixelFormat
uint32_t channelId = 0; // user define channelId: 0-15
uint32_t deviceId = 0;
pthread_t threadId = 0; // thread for callback
aclvdecCallback callback = {0}; // user define how to process vdec out data
bool runflag = true;
};
struct DeviceStreamData {
std::vector<ObjectDetectInfo> detectResult;
uint32_t framId;
uint32_t channelId;
};
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 JPEGE_STRIDE_WIDTH = 16; // Jpege module input width need to align up to 16
const uint32_t JPEGE_STRIDE_HEIGHT = 1; // Jpege module input height remains unchanged
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 VDEC_STRIDE_WIDTH = 16; // Vdec module output width need to align up to 16
const uint32_t VDEC_STRIDE_HEIGHT = 2; // Vdec module output width need to align up to 2
const uint32_t YUV_BYTES_NU = 3; // Numerator of yuv image, H x W x 3 / 2
const uint32_t YUV_BYTES_DE = 2; // Denominator of yuv image, H x W x 3 / 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_JPEGE_WIDTH = 8192; // Max width of jpege module
const uint32_t MAX_JPEGE_HEIGHT = 8192; // Max height of jpege module
const uint32_t MIN_JPEGE_WIDTH = 32; // Min width of jpege module
const uint32_t MIN_JPEGE_HEIGHT = 32; // Min height of jpege 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)) // Convert the input to odd num
#define CONVERT_TO_EVEN(NUM) (((NUM) % MODULUS_NUM_2 == 0) ? (NUM) : ((NUM)-1)) // Convert the input to even num
#define CHECK_ODD(num) ((num) % MODULUS_NUM_2 != 0)
#define CHECK_EVEN(num) ((num) % MODULUS_NUM_2 == 0)
#define RELEASE_DVPP_DATA(dvppDataPtr) \
do { \
APP_ERROR retMacro; \
if (dvppDataPtr != nullptr) { \
retMacro = acldvppFree(dvppDataPtr); \
if (retMacro != APP_ERR_OK) { \
MS_LOG(ERROR) << "Failed to free memory on dvpp, ret = " << retMacro << "."; \
} \
dvppDataPtr = nullptr; \
} \
} while (0);
class DvppCommon {
public:
explicit DvppCommon(aclrtStream dvppStream);
explicit DvppCommon(const VdecConfig &vdecConfig); // Need by vdec
~DvppCommon();
APP_ERROR Init(void);
APP_ERROR InitVdec(); // Needed by vdec
APP_ERROR DeInit(void);
static APP_ERROR GetVpcDataSize(uint32_t widthVpc, uint32_t heightVpc, acldvppPixelFormat format, uint32_t &vpcSize);
static APP_ERROR GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
uint32_t &widthStride, uint32_t &heightStride);
static APP_ERROR 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 APP_ERROR GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t &width, uint32_t &height,
int32_t &components);
static APP_ERROR GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format,
uint32_t &decSize);
static APP_ERROR GetJpegEncodeStrideSize(std::shared_ptr<DvppDataInfo> &input);
static APP_ERROR SetEncodeLevel(uint32_t level, acldvppJpegeConfig &jpegeConfig);
static APP_ERROR GetVideoDecodeStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
uint32_t &widthStride, uint32_t &heightStride);
static APP_ERROR GetVideoDecodeDataSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
uint32_t &vdecSize);
// The following interfaces can be called only when the DvppCommon object is initialized with Init
APP_ERROR VpcResize(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize,
VpcProcessType processType = VPC_PT_DEFAULT);
APP_ERROR VpcCrop(const DvppCropInputInfo &input, const DvppDataInfo &output, bool withSynchronize);
APP_ERROR JpegDecode(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize);
APP_ERROR JpegEncode(DvppDataInfo &input, DvppDataInfo &output, acldvppJpegeConfig *jpegeConfig,
bool withSynchronize);
APP_ERROR GetJpegEncodeDataSize(DvppDataInfo &input, acldvppJpegeConfig *jpegeConfig, uint32_t &encSize);
// These functions started with "Combine" encapsulate the DVPP process together, malloc DVPP memory,
// transfer pictures from host to device, and then execute the DVPP operation.
// The caller needs to pay attention to the release of the memory alloced in these functions.
// You can call the ReleaseDvppBuffer function to release memory after use completely.
APP_ERROR CombineResizeProcess(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize,
VpcProcessType processType = VPC_PT_DEFAULT);
APP_ERROR CombineCropProcess(DvppCropInputInfo &input, DvppDataInfo &output, bool withSynchronize);
APP_ERROR CombineJpegdProcess(const RawData &imageInfo, acldvppPixelFormat format, bool withSynchronize);
APP_ERROR CombineJpegeProcess(const RawData &imageInfo, uint32_t width, uint32_t height, acldvppPixelFormat format,
bool withSynchronize);
// The following interface can be called only when the DvppCommon object is initialized with InitVdec
APP_ERROR CombineVdecProcess(std::shared_ptr<DvppDataInfo> data, void *userData);
// Get the private member variables which are assigned in the interfaces which are started with "Combine"
std::shared_ptr<DvppDataInfo> GetInputImage();
std::shared_ptr<DvppDataInfo> GetDecodedImage();
std::shared_ptr<DvppDataInfo> GetResizedImage();
std::shared_ptr<DvppDataInfo> GetEncodedImage();
std::shared_ptr<DvppDataInfo> GetCropedImage();
// Release the memory that is allocated in the interfaces which are started with "Combine"
void ReleaseDvppBuffer();
APP_ERROR VdecSendEosFrame() const;
private:
APP_ERROR SetDvppPicDescData(const DvppDataInfo &dataInfo, acldvppPicDesc &picDesc);
APP_ERROR ResizeProcess(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, bool withSynchronize);
APP_ERROR ResizeWithPadding(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, CropRoiConfig &cropRoi,
CropRoiConfig &pasteRoi, bool withSynchronize);
void GetCropRoi(const DvppDataInfo &input, const DvppDataInfo &output, VpcProcessType processType,
CropRoiConfig &cropRoi);
void GetPasteRoi(const DvppDataInfo &input, const DvppDataInfo &output, VpcProcessType processType,
CropRoiConfig &pasteRoi);
APP_ERROR CropProcess(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, const CropRoiConfig &cropArea,
bool withSynchronize);
APP_ERROR CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output);
APP_ERROR CheckCropParams(const DvppCropInputInfo &input);
APP_ERROR TransferImageH2D(const RawData &imageInfo, const std::shared_ptr<DvppDataInfo> &jpegInput);
APP_ERROR CreateStreamDesc(std::shared_ptr<DvppDataInfo> data);
APP_ERROR DestroyResource();
std::shared_ptr<acldvppRoiConfig> cropAreaConfig_ = nullptr;
std::shared_ptr<acldvppRoiConfig> pasteAreaConfig_ = nullptr;
std::shared_ptr<acldvppPicDesc> cropInputDesc_ = nullptr;
std::shared_ptr<acldvppPicDesc> cropOutputDesc_ = nullptr;
std::shared_ptr<acldvppRoiConfig> cropRoiConfig_ = nullptr;
std::shared_ptr<acldvppPicDesc> encodeInputDesc_ = nullptr;
std::shared_ptr<acldvppJpegeConfig> jpegeConfig_ = nullptr;
std::shared_ptr<acldvppPicDesc> resizeInputDesc_ = nullptr;
std::shared_ptr<acldvppPicDesc> resizeOutputDesc_ = nullptr;
std::shared_ptr<acldvppResizeConfig> resizeConfig_ = nullptr;
std::shared_ptr<acldvppPicDesc> decodeOutputDesc_ = nullptr;
acldvppChannelDesc *dvppChannelDesc_ = nullptr;
aclrtStream dvppStream_ = nullptr;
std::shared_ptr<DvppDataInfo> inputImage_ = nullptr;
std::shared_ptr<DvppDataInfo> decodedImage_ = nullptr;
std::shared_ptr<DvppDataInfo> encodedImage_ = nullptr;
std::shared_ptr<DvppDataInfo> resizedImage_ = nullptr;
std::shared_ptr<DvppDataInfo> cropImage_ = nullptr;
bool isVdec_ = false;
aclvdecChannelDesc *vdecChannelDesc_ = nullptr;
acldvppStreamDesc *streamInputDesc_ = nullptr;
acldvppPicDesc *picOutputDesc_ = nullptr;
VdecConfig vdecConfig_;
};
#endif

@ -0,0 +1,51 @@
/*
* 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 "mindspore/core/utils/log_adapter.h"
#include "ErrorCode.h"
std::string GetAppErrCodeInfo(const APP_ERROR err) {
if ((err < APP_ERR_ACL_END) && (err >= APP_ERR_ACL_FAILURE)) {
return APP_ERR_ACL_LOG_STRING[((err < 0) ? (err + APP_ERR_ACL_END + 1) : err)];
} else if ((err < APP_ERR_COMM_END) && (err > APP_ERR_COMM_BASE)) {
return (err - APP_ERR_COMM_BASE) <
(int)sizeof(APP_ERR_COMMON_LOG_STRING) / (int)sizeof(APP_ERR_COMMON_LOG_STRING[0])
? APP_ERR_COMMON_LOG_STRING[err - APP_ERR_COMM_BASE]
: "Undefine the error code information";
} else if ((err < APP_ERR_DVPP_END) && (err > APP_ERR_DVPP_BASE)) {
return (err - APP_ERR_DVPP_BASE) < (int)sizeof(APP_ERR_DVPP_LOG_STRING) / (int)sizeof(APP_ERR_DVPP_LOG_STRING[0])
? APP_ERR_DVPP_LOG_STRING[err - APP_ERR_DVPP_BASE]
: "Undefine the error code information";
} else if ((err < APP_ERR_QUEUE_END) && (err > APP_ERR_QUEUE_BASE)) {
return (err - APP_ERR_QUEUE_BASE) < (int)sizeof(APP_ERR_QUEUE_LOG_STRING) / (int)sizeof(APP_ERR_QUEUE_LOG_STRING[0])
? APP_ERR_QUEUE_LOG_STRING[err - APP_ERR_QUEUE_BASE]
: "Undefine the error code information";
} else {
return "Error code unknown";
}
}
void AssertErrorCode(int code, std::string file, std::string function, int line) {
if (code != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed at " << file << "->" << function << "->" << line << ": error code=" << code;
exit(code);
}
}
void CheckErrorCode(int code, std::string file, std::string function, int line) {
if (code != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed at " << file << "->" << function << "->" << line << ": error code=" << code;
}
}

@ -0,0 +1,136 @@
/*
* 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 "ResourceManager.h"
#include <algorithm>
bool ResourceManager::initFlag_ = true;
std::shared_ptr<ResourceManager> ResourceManager::ptr_ = nullptr;
/**
* Check whether the file exists.
*
* @param filePath the file path we want to check
* @return APP_ERR_OK if file exists, error code otherwise
*/
APP_ERROR ExistFile(const std::string &filePath) {
struct stat fileSat = {0};
char c[PATH_MAX + 1] = {0x00};
size_t count = filePath.copy(c, PATH_MAX + 1);
if (count != filePath.length()) {
MS_LOG(ERROR) << "Failed to strcpy" << c;
return APP_ERR_COMM_FAILURE;
}
// Get the absolute path of input directory
char path[PATH_MAX + 1] = {0x00};
if ((strlen(c) > PATH_MAX) || (realpath(c, path) == nullptr)) {
MS_LOG(ERROR) << "Failed to get canonicalize path";
return APP_ERR_COMM_EXIST;
}
if (stat(c, &fileSat) == 0 && S_ISREG(fileSat.st_mode)) {
return APP_ERR_OK;
}
return APP_ERR_COMM_FAILURE;
}
void ResourceManager::Release() {
APP_ERROR ret;
for (size_t i = 0; i < deviceIds_.size(); i++) {
if (contexts_[i] != nullptr) {
ret = aclrtDestroyContext(contexts_[i]); // Destroy context
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to destroy context, ret = " << ret << ".";
return;
}
contexts_[i] = nullptr;
}
ret = aclrtResetDevice(deviceIds_[i]); // Reset device
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to reset device, ret = " << ret << ".";
return;
}
}
ret = aclFinalize();
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to finalize acl, ret = " << ret << ".";
return;
}
MS_LOG(INFO) << "Finalized acl successfully.";
}
std::shared_ptr<ResourceManager> ResourceManager::GetInstance() {
if (ptr_ == nullptr) {
ResourceManager *temp = new ResourceManager();
ptr_.reset(temp);
}
return ptr_;
}
APP_ERROR ResourceManager::InitResource(ResourceInfo &resourceInfo) {
if (!GetInitStatus()) {
return APP_ERR_OK;
}
std::string &aclConfigPath = resourceInfo.aclConfigPath;
APP_ERROR ret;
if (aclConfigPath.length() == 0) {
// Init acl without aclconfig
ret = aclInit(nullptr);
} else {
ret = ExistFile(aclConfigPath);
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Acl config file not exist, ret = " << ret << ".";
return ret;
}
ret = aclInit(aclConfigPath.c_str()); // Initialize ACL
}
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to init acl, ret = " << ret;
return ret;
}
std::copy(resourceInfo.deviceIds.begin(), resourceInfo.deviceIds.end(), std::back_inserter(deviceIds_));
MS_LOG(INFO) << "Initialized acl successfully.";
// Open device and create context for each chip, note: it create one context for each chip
for (size_t i = 0; i < deviceIds_.size(); i++) {
deviceIdMap_[deviceIds_[i]] = i;
ret = aclrtSetDevice(deviceIds_[i]);
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to open acl device: " << deviceIds_[i];
return ret;
}
MS_LOG(INFO) << "Open device " << deviceIds_[i] << " successfully.";
aclrtContext context;
ret = aclrtCreateContext(&context, deviceIds_[i]);
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to create acl context, ret = " << ret << ".";
return ret;
}
MS_LOG(INFO) << "Created context for device " << deviceIds_[i] << " successfully";
contexts_.push_back(context);
}
std::string singleOpPath = resourceInfo.singleOpFolderPath;
if (!singleOpPath.empty()) {
ret = aclopSetModelDir(singleOpPath.c_str()); // Set operator model directory for application
if (ret != APP_ERR_OK) {
MS_LOG(ERROR) << "Failed to aclopSetModelDir, ret = " << ret << ".";
return ret;
}
}
MS_LOG(INFO) << "Init resource successfully.";
ResourceManager::initFlag_ = false;
return APP_ERR_OK;
}
aclrtContext ResourceManager::GetContext(int deviceId) { return contexts_[deviceIdMap_[deviceId]]; }

@ -0,0 +1,88 @@
/*
* 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 RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#include <vector>
#include <set>
#include <cstring>
#include <unordered_map>
#include <mutex>
#include "CommonDataType.h"
#include "ErrorCode.h"
#include <sys/stat.h>
#include "mindspore/core/utils/log_adapter.h"
#define PATH_MAX 4096
enum ModelLoadMethod {
LOAD_FROM_FILE = 0, // Loading from file, memory of model and weights are managed by ACL
LOAD_FROM_MEM, // Loading from memory, memory of model and weights are managed by ACL
LOAD_FROM_FILE_WITH_MEM, // Loading from file, memory of model and weight are managed by user
LOAD_FROM_MEM_WITH_MEM // Loading from memory, memory of model and weight are managed by user
};
struct ModelInfo {
std::string modelName;
std::string modelPath; // Path of om model file
size_t modelFileSize; // Size of om model file
std::shared_ptr<void> modelFilePtr; // Smart pointer of model file data
uint32_t modelWidth; // Input width of model
uint32_t modelHeight; // Input height of model
ModelLoadMethod method; // Loading method of model
};
// Device resource info, such as model infos, etc
struct DeviceResInfo {
std::vector<ModelInfo> modelInfos;
};
struct ResourceInfo {
std::set<int> deviceIds;
std::string aclConfigPath;
std::string singleOpFolderPath;
std::unordered_map<int, DeviceResInfo> deviceResInfos; // map <deviceId, deviceResourceInfo>
};
APP_ERROR ExistFile(const std::string &filePath);
class ResourceManager {
public:
ResourceManager(){};
~ResourceManager(){};
// Get the Instance of resource manager
static std::shared_ptr<ResourceManager> GetInstance();
// Init the resource of resource manager
APP_ERROR InitResource(ResourceInfo &resourceInfo);
aclrtContext GetContext(int deviceId);
void Release();
static bool GetInitStatus() { return initFlag_; }
private:
static std::shared_ptr<ResourceManager> ptr_;
static bool initFlag_;
std::vector<int> deviceIds_;
std::vector<aclrtContext> contexts_;
std::unordered_map<int, int> deviceIdMap_; // Map of device to index
};
#endif

@ -57,6 +57,7 @@ constexpr char kCenterCropOp[] = "CenterCropOp";
constexpr char kCutMixBatchOp[] = "CutMixBatchOp";
constexpr char kCutOutOp[] = "CutOutOp";
constexpr char kCropOp[] = "CropOp";
constexpr char kDvppDecodeResizeCropJpegOp[] = "DvppDecodeResizeCropJpegOp";
constexpr char kEqualizeOp[] = "EqualizeOp";
constexpr char kHwcToChwOp[] = "HwcToChwOp";
constexpr char kInvertOp[] = "InvertOp";

@ -31,10 +31,10 @@ class TestDE : public ST::Common {
TEST_F(TestDE, ResNetPreprocess) {
std::vector<std::shared_ptr<Tensor>> images;
MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764", &images);
MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",
&images);
MindDataEager Compose({Decode(),
Resize({224, 224}),
MindDataEager Compose({Decode(), Resize({224, 224}),
Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255}),
HWC2CHW()});
@ -47,3 +47,19 @@ TEST_F(TestDE, ResNetPreprocess) {
ASSERT_EQ(images[0]->Shape()[1], 224);
ASSERT_EQ(images[0]->Shape()[2], 224);
}
TEST_F(TestDE, TestDvpp) {
std::vector<std::shared_ptr<Tensor>> images;
MindDataEager::LoadImageFromDir("/root/Dvpp_Unit_Dev/val2014_test/", &images);
MindDataEager Solo({DvppDecodeResizeCropJpeg({224, 224}, {256, 256})});
for (auto &img : images) {
img = Solo(img);
}
ASSERT_EQ(images[0]->Shape().size(), 3);
ASSERT_EQ(images[0]->Shape()[0], 224 * 224 * 1.5);
ASSERT_EQ(images[0]->Shape()[1], 1);
ASSERT_EQ(images[0]->Shape()[2], 1);
}

Loading…
Cancel
Save