diff --git a/mindspore/ccsrc/minddata/dataset/api/execute.cc b/mindspore/ccsrc/minddata/dataset/api/execute.cc index 93a1cbec465..7f27a2a07e5 100644 --- a/mindspore/ccsrc/minddata/dataset/api/execute.cc +++ b/mindspore/ccsrc/minddata/dataset/api/execute.cc @@ -16,8 +16,8 @@ #include "minddata/dataset/include/execute.h" #include "minddata/dataset/core/de_tensor.h" -#include "minddata/dataset/core/device_tensor.h" #include "minddata/dataset/core/device_resource.h" +#include "minddata/dataset/core/device_tensor.h" #include "minddata/dataset/core/tensor_row.h" #include "minddata/dataset/include/tensor.h" #include "minddata/dataset/include/type_id.h" @@ -35,12 +35,11 @@ namespace mindspore { namespace dataset { // FIXME - Temporarily overload Execute to support both TensorOperation and TensorTransform -Execute::Execute(std::shared_ptr op, std::string deviceType) { +Execute::Execute(std::shared_ptr op, MapTargetDevice deviceType) { ops_.emplace_back(std::move(op)); device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -51,14 +50,18 @@ Execute::Execute(std::shared_ptr op, std::string deviceType) { #endif } -Execute::Execute(std::shared_ptr op, std::string deviceType) { +Execute::Execute(std::shared_ptr op, MapTargetDevice deviceType) { // Convert op from TensorTransform to TensorOperation - std::shared_ptr operation = op->Parse(); + std::shared_ptr operation; + if (deviceType == MapTargetDevice::kCpu) { + operation = op->Parse(); + } else { + operation = op->Parse(deviceType); + } ops_.emplace_back(std::move(operation)); device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -70,14 +73,13 @@ Execute::Execute(std::shared_ptr op, std::string deviceType) { } /* -Execute::Execute(TensorTransform op, std::string deviceType) { +Execute::Execute(TensorTransform op, MapTargetDevice deviceType) { // Convert op from TensorTransform to TensorOperation std::shared_ptr operation = op.Parse(); ops_.emplace_back(std::move(operation)); device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -90,14 +92,18 @@ Execute::Execute(TensorTransform op, std::string deviceType) { */ // Execute function for the example case: auto decode(new vision::Decode()); -Execute::Execute(TensorTransform *op, std::string deviceType) { +Execute::Execute(TensorTransform *op, MapTargetDevice deviceType) { // Convert op from TensorTransform to TensorOperation - std::shared_ptr operation = op->Parse(); + std::shared_ptr operation; + if (deviceType == MapTargetDevice::kCpu) { + operation = op->Parse(); + } else { + operation = op->Parse(deviceType); + } ops_.emplace_back(std::move(operation)); device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -108,11 +114,10 @@ Execute::Execute(TensorTransform *op, std::string deviceType) { #endif } -Execute::Execute(std::vector> ops, std::string deviceType) +Execute::Execute(std::vector> ops, MapTargetDevice deviceType) : ops_(std::move(ops)), device_type_(deviceType) { - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -123,15 +128,21 @@ Execute::Execute(std::vector> ops, std::string #endif } -Execute::Execute(std::vector> ops, std::string deviceType) { +Execute::Execute(std::vector> ops, MapTargetDevice deviceType) { // Convert ops from TensorTransform to TensorOperation - (void)std::transform( - ops.begin(), ops.end(), std::back_inserter(ops_), - [](std::shared_ptr operation) -> std::shared_ptr { return operation->Parse(); }); + if (deviceType == MapTargetDevice::kCpu) { + (void)std::transform(ops.begin(), ops.end(), std::back_inserter(ops_), + [](std::shared_ptr operation) -> std::shared_ptr { + return operation->Parse(); + }); + } else { + for (auto &op : ops) { + ops_.emplace_back(op->Parse(deviceType)); + } + } device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -142,15 +153,20 @@ Execute::Execute(std::vector> ops, std::string #endif } -Execute::Execute(const std::vector> ops, std::string deviceType) { +Execute::Execute(const std::vector> ops, MapTargetDevice deviceType) { // Convert ops from TensorTransform to TensorOperation - (void)std::transform( - ops.begin(), ops.end(), std::back_inserter(ops_), - [](TensorTransform &operation) -> std::shared_ptr { return operation.Parse(); }); + if (deviceType == MapTargetDevice::kCpu) { + (void)std::transform( + ops.begin(), ops.end(), std::back_inserter(ops_), + [](TensorTransform &operation) -> std::shared_ptr { return operation.Parse(); }); + } else { + for (auto &op : ops) { + ops_.emplace_back(op.get().Parse(deviceType)); + } + } device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -162,15 +178,20 @@ Execute::Execute(const std::vector> ops, } // Execute function for the example vector case: auto decode(new vision::Decode()); -Execute::Execute(std::vector ops, std::string deviceType) { +Execute::Execute(std::vector ops, MapTargetDevice deviceType) { // Convert ops from TensorTransform to TensorOperation - (void)std::transform( - ops.begin(), ops.end(), std::back_inserter(ops_), - [](TensorTransform *operation) -> std::shared_ptr { return operation->Parse(); }); + if (deviceType == MapTargetDevice::kCpu) { + (void)std::transform( + ops.begin(), ops.end(), std::back_inserter(ops_), + [](TensorTransform *operation) -> std::shared_ptr { return operation->Parse(); }); + } else { + for (auto &op : ops) { + ops_.emplace_back(op->Parse(deviceType)); + } + } device_type_ = deviceType; - MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { device_resource_ = std::make_shared(); Status rc = device_resource_->InitResource(); if (!rc.IsOk()) { @@ -183,7 +204,7 @@ Execute::Execute(std::vector ops, std::string deviceType) { Execute::~Execute() { #ifdef ENABLE_ACL - if (device_type_ == "Ascend310") { + if (device_type_ == MapTargetDevice::kAscend310) { if (device_resource_) { device_resource_->FinalizeResource(); } else { @@ -205,7 +226,7 @@ Status Execute::operator()(const mindspore::MSTensor &input, mindspore::MSTensor RETURN_IF_NOT_OK(ops_[i]->ValidateParams()); transforms.emplace_back(ops_[i]->Build()); } - if (device_type_ == "CPU") { + if (device_type_ == MapTargetDevice::kCpu) { // Convert mindspore::Tensor to dataset::Tensor std::shared_ptr de_tensor; Status rc = dataset::Tensor::CreateFromMemory(dataset::TensorShape(input.Shape()), @@ -268,7 +289,7 @@ Status Execute::operator()(const std::vector &input_tensor_list, std:: RETURN_IF_NOT_OK(ops_[i]->ValidateParams()); transforms.emplace_back(ops_[i]->Build()); } - if (device_type_ == "CPU") { // Case CPU + if (device_type_ == MapTargetDevice::kCpu) { // Case CPU TensorRow de_tensor_list; for (auto &tensor : input_tensor_list) { std::shared_ptr de_tensor; @@ -325,8 +346,8 @@ Status Execute::operator()(const std::vector &input_tensor_list, std:: } Status Execute::validate_device_() { - if (device_type_ != "CPU" && device_type_ != "Ascend310") { - std::string err_msg = device_type_ + " is not supported. (Option: CPU or Ascend310)"; + if (device_type_ != MapTargetDevice::kCpu && device_type_ != MapTargetDevice::kAscend310) { + std::string err_msg = "Your input device is not supported. (Option: CPU or Ascend310)"; MS_LOG(ERROR) << err_msg; RETURN_STATUS_UNEXPECTED(err_msg); } diff --git a/mindspore/ccsrc/minddata/dataset/api/vision.cc b/mindspore/ccsrc/minddata/dataset/api/vision.cc index a3f40c24669..5e0243a0428 100644 --- a/mindspore/ccsrc/minddata/dataset/api/vision.cc +++ b/mindspore/ccsrc/minddata/dataset/api/vision.cc @@ -17,6 +17,7 @@ #include "minddata/dataset/include/vision.h" #ifdef ENABLE_ACL #include "minddata/dataset/include/vision_ascend.h" +#include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h" #endif #include "minddata/dataset/include/transforms.h" @@ -24,19 +25,13 @@ #ifndef ENABLE_ANDROID #include "minddata/dataset/kernels/image/image_utils.h" +#include "utils/log_adapter.h" +#else +#include "mindspore/lite/src/common/log_adapter.h" #endif #include "minddata/dataset/kernels/ir/validators.h" // Kernel image headers (in alphabetical order) -// FIXME - Delete these dvpp include header when dvpp TensorOperation moved to IR level -#ifdef ENABLE_ACL -#include "minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h" -#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h" -#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h" -#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h" -#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h" -#include "minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h" -#endif namespace mindspore { namespace dataset { @@ -71,6 +66,18 @@ CenterCrop::CenterCrop(std::vector size) : size_(size) {} std::shared_ptr CenterCrop::Parse() { return std::make_shared(size_); } +std::shared_ptr CenterCrop::Parse(const MapTargetDevice &env) { + if (env == MapTargetDevice::kAscend310) { +#ifdef ENABLE_ACL + std::vector usize_; + usize_.reserve(size_.size()); + std::transform(size_.begin(), size_.end(), std::back_inserter(usize_), [](int32_t i) { return (uint32_t)i; }); + return std::make_shared(usize_); +#endif + } + return std::make_shared(size_); +} + // Crop Transform Operation. Crop::Crop(std::vector coordinates, std::vector size) : coordinates_(coordinates), size_(size) {} @@ -91,117 +98,54 @@ CutOut::CutOut(int32_t length, int32_t num_patches) : length_(length), num_patch std::shared_ptr CutOut::Parse() { return std::make_shared(length_, num_patches_); } // Decode Transform Operation. -Decode::Decode(bool rgb) {} +Decode::Decode(bool rgb) : rgb_(rgb) {} std::shared_ptr Decode::Parse() { return std::make_shared(rgb_); } -#endif +std::shared_ptr Decode::Parse(const MapTargetDevice &env) { + if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL -// Function to create DvppResizeOperation. -std::shared_ptr DvppCropJpeg(std::vector crop) { - auto op = std::make_shared(crop); - // Input validation - return op->ValidateParams() ? op : nullptr; + return std::make_shared(); +#endif + } + return std::make_shared(rgb_); } -// Function to create DvppDecodeResizeOperation. -std::shared_ptr DvppDecodeResizeJpeg(std::vector resize) { - auto op = std::make_shared(resize); - // Input validation - return op->ValidateParams() ? op : nullptr; -} - -// Function to create DvppDecodeResizeCropOperation. -std::shared_ptr DvppDecodeResizeCropJpeg(std::vector crop, - std::vector resize) { - auto op = std::make_shared(crop, resize); - // Input validation - return op->ValidateParams() ? op : nullptr; -} - -// Function to create DvppDecodeJpegOperation. -std::shared_ptr DvppDecodeJpeg() { - auto op = std::make_shared(); - // Input validation - return op->ValidateParams() ? op : nullptr; -} - -// Function to create DvppDecodePngOperation. -std::shared_ptr DvppDecodePng() { - auto op = std::make_shared(); - // Input validation - return op->ValidateParams() ? op : nullptr; -} - -// Function to create DvppResizeOperation. -std::shared_ptr DvppResizeJpeg(std::vector resize) { - auto op = std::make_shared(resize); - // Input validation - return op->ValidateParams() ? op : nullptr; -} #endif -/* -// DvppResize Transform Operation. -DvppCropJpeg::DvppCropJpeg(std::vector crop) : crop_(crop) {} - -std::shared_ptr DvppCropJpeg::Parse() { return std::make_shared(crop); } - +#ifdef ENABLE_ACL // DvppDecodeResize Transform Operation. -DvppDecodeResize::DvppDecodeResizeJpeg(std::vector resize) : resize_(resize) {} +DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(std::vector resize) : resize_(resize) {} std::shared_ptr DvppDecodeResizeJpeg::Parse() { - return std::make_shared(resize); + return std::make_shared(resize_); +} + +std::shared_ptr DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) { + return std::make_shared(resize_); } // DvppDecodeResizeCrop Transform Operation. -DvppDecodeResizeCrop::DvppDecodeResizeCropJpeg(std::vector crop, std::vector resize) +DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(std::vector crop, std::vector resize) : crop_(crop), resize_(resize) {} std::shared_ptr DvppDecodeResizeCropJpeg::Parse() { - return std::make_shared(crop, resize); + return std::make_shared(crop_, resize_); } -// DvppCropOperation -DvppCropJpeg::DvppCropJpeg(const std::vector &crop) : crop_(crop) {} - -std::shared_ptr DvppCropJpeg::Parse() { return std::make_shared(); } - -// DvppDecodeResizeOperation -DvppDecodeResize::DvppDecodeResize(const std::vector &resize) : resize_(resize) {} - -std::shared_ptr DvppDecodeResize::Parse() { return std::make_shared(); } - -// DvppDecodeJpeg Transform Operation. -DvppDecodeJpeg::DvppDecodeJpeg() {} - -std::shared_ptr DvppDecodeJpeg::Parse() { return std::make_shared(); } +std::shared_ptr DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) { + return std::make_shared(crop_, resize_); +} // DvppDecodePng Transform Operation. DvppDecodePng::DvppDecodePng() {} std::shared_ptr DvppDecodePng::Parse() { return std::make_shared(); } -// DvppResizeOperation -DvppDecodeResize::DvppResizeJpeg(const std::vector &resize) : resize_(resize) {} - -std::shared_ptr DvppDecodeResize::Parse() { return std::make_shared(); } - -// DvppDecodeResizeCropOperation -DvppDecodeResizeCrop::DvppDecodeResizeCrop(const std::vector &crop, const std::vector &resize) - : crop_(crop), resize_(resize) {} - -std::shared_ptr DvppDecodeResizeCrop::Parse() { - return std::make_shared(); +std::shared_ptr DvppDecodePng::Parse(const MapTargetDevice &env) { + return std::make_shared(); } - -// DvppResize Transform Operation. -DvppResizeJpeg::DvppResizeJpeg(std::vector resize) {} - -std::shared_ptr DvppResizeJpeg::Parse() { return std::make_shared(resize); } #endif -*/ - #ifndef ENABLE_ANDROID // Equalize Transform Operation. Equalize::Equalize() {} @@ -425,6 +369,18 @@ Resize::Resize(std::vector size, InterpolationMode interpolation) std::shared_ptr Resize::Parse() { return std::make_shared(size_, interpolation_); } +std::shared_ptr Resize::Parse(const MapTargetDevice &env) { + if (env == MapTargetDevice::kAscend310) { +#ifdef ENABLE_ACL + std::vector usize_; + usize_.reserve(size_.size()); + std::transform(size_.begin(), size_.end(), std::back_inserter(usize_), [](int32_t i) { return (uint32_t)i; }); + return std::make_shared(usize_); +#endif + } + return std::make_shared(size_, interpolation_); +} + #ifdef ENABLE_ANDROID // Rotate Transform Operation. Rotate::Rotate() {} @@ -486,235 +442,6 @@ std::shared_ptr UniformAugment::Parse() { } #endif -// FIXME - Move these DVPP Derived TensorOperation classes to IR level -/* ####################################### Derived TensorOperation classes ################################# */ - -#ifdef ENABLE_ACL -// DvppCropOperation -DvppCropJpegOperation::DvppCropJpegOperation(const std::vector &crop) : crop_(crop) {} - -Status DvppCropJpegOperation::ValidateParams() { - // size - if (crop_.empty() || crop_.size() > 2) { - std::string err_msg = - "DvppCropJpeg: Crop resolution 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 (*min_element(crop_.begin(), crop_.end()) < 32 || *max_element(crop_.begin(), crop_.end()) > 2048) { - std::string err_msg = "Dvpp module supports crop image with resolution in range [32, 2048], got crop Parameters: "; - if (crop_.size() == 2) { - MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[1] << "]"; - } else { - MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[0] << "]"; - } - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - return Status::OK(); -} - -std::shared_ptr DvppCropJpegOperation::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; - // User specified the width value. - if (crop_.size() == 1) { - cropHeight = crop_[0]; - cropWidth = crop_[0]; - } else { - cropHeight = crop_[0]; - cropWidth = crop_[1]; - } - std::shared_ptr tensor_op = std::make_shared(cropHeight, cropWidth); - return tensor_op; -} - -// DvppDecodeResizeOperation -DvppDecodeResizeOperation::DvppDecodeResizeOperation(const std::vector &resize) : resize_(resize) {} - -Status DvppDecodeResizeOperation::ValidateParams() { - // size - if (resize_.empty() || resize_.size() > 2) { - std::string err_msg = "DvppDecodeResizeJpeg: resize resolution 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 (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) { - std::string err_msg = - "Dvpp module supports resize image with resolution in range [32, 2048], got resize Parameters: "; - if (resize_.size() == 2) { - MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]"; - } else { - MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[0] << "]"; - } - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - return Status::OK(); -} - -std::shared_ptr DvppDecodeResizeOperation::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 resizeHeight, resizeWidth; - // User specified the width value. - if (resize_.size() == 1) { - resizeHeight = resize_[0]; - resizeWidth = 0; - } else { - resizeHeight = resize_[0]; - resizeWidth = resize_[1]; - } - std::shared_ptr tensor_op = - std::make_shared(resizeHeight, resizeWidth); - return tensor_op; -} - -// DvppDecodeResizeCropOperation -DvppDecodeResizeCropOperation::DvppDecodeResizeCropOperation(const std::vector &crop, - const std::vector &resize) - : crop_(crop), resize_(resize) {} - -Status DvppDecodeResizeCropOperation::ValidateParams() { - // size - if (crop_.empty() || crop_.size() > 2) { - std::string err_msg = "DvppDecodeResizeCropJpeg: crop resolution 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 resolution 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 (*min_element(crop_.begin(), crop_.end()) < 32 || *max_element(crop_.begin(), crop_.end()) > 2048) { - std::string err_msg = "Dvpp module supports crop image with resolution in range [32, 2048], got Crop Parameters: "; - if (crop_.size() == 2) { - MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[1] << "]"; - } else { - MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[0] << "]"; - } - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) { - std::string err_msg = - "Dvpp module supports resize image with resolution in range [32, 2048], got Crop Parameters: "; - if (resize_.size() == 2) { - MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]"; - } else { - MS_LOG(ERROR) << err_msg << "[" << resize_[0] << "]"; - } - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (crop_.size() < resize_.size()) { - if (crop_[0] > MIN(resize_[0], resize_[1])) { - std::string err_msg = - "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= " - "y[0], and x[1] <= y[1], please verify your input parameters."; - 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 = - "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= " - "y[0], and x[1] <= y[1], please verify your input parameters."; - 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 = - "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= " - "y[0], and x[1] <= y[1], please verify your input parameters."; - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } - } - return Status::OK(); -} - -std::shared_ptr 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 = 0; - } else { - resizeHeight = resize_[0]; - resizeWidth = resize_[1]; - } - std::shared_ptr tensor_op = - std::make_shared(cropHeight, cropWidth, resizeHeight, resizeWidth); - return tensor_op; -} - -// DvppDecodeJPEG -Status DvppDecodeJpegOperation::ValidateParams() { return Status::OK(); } - -std::shared_ptr DvppDecodeJpegOperation::Build() { return std::make_shared(); } - -// DvppDecodePNG -Status DvppDecodePngOperation::ValidateParams() { return Status::OK(); } - -std::shared_ptr DvppDecodePngOperation::Build() { return std::make_shared(); } - -// DvppResizeOperation -DvppResizeJpegOperation::DvppResizeJpegOperation(const std::vector &resize) : resize_(resize) {} - -Status DvppResizeJpegOperation::ValidateParams() { - // size - if (resize_.empty() || resize_.size() > 2) { - std::string err_msg = "DvppResizeJpeg: resize resolution 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 (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) { - std::string err_msg = - "Dvpp module supports resize image with resolution in range [32, 2048], got resize Parameters: "; - if (resize_.size() == 2) { - MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]"; - } else { - MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[0] << "]"; - } - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - return Status::OK(); -} - -std::shared_ptr DvppResizeJpegOperation::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 resizeHeight, resizeWidth; - // User specified the width value. - if (resize_.size() == 1) { - resizeHeight = resize_[0]; - resizeWidth = 0; - } else { - resizeHeight = resize_[0]; - resizeWidth = resize_[1]; - } - std::shared_ptr tensor_op = std::make_shared(resizeHeight, resizeWidth); - return tensor_op; -} -#endif - } // namespace vision } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/include/constants.h b/mindspore/ccsrc/minddata/dataset/include/constants.h index 2f34ea573dc..4f1607046d5 100644 --- a/mindspore/ccsrc/minddata/dataset/include/constants.h +++ b/mindspore/ccsrc/minddata/dataset/include/constants.h @@ -27,7 +27,7 @@ using uchar = unsigned char; using dsize_t = int64_t; // Target devices to perform map operation -enum class MapTargetDevice { kCpu, kGpu, kDvpp }; +enum class MapTargetDevice { kCpu, kGpu, kAscend310 }; // Possible dataset types for holding the data and client type enum class DatasetType { kUnknown, kArrow, kTf }; diff --git a/mindspore/ccsrc/minddata/dataset/include/execute.h b/mindspore/ccsrc/minddata/dataset/include/execute.h index fa6e7d3665e..4b8a85c8693 100644 --- a/mindspore/ccsrc/minddata/dataset/include/execute.h +++ b/mindspore/ccsrc/minddata/dataset/include/execute.h @@ -33,15 +33,18 @@ class Execute { public: /// \brief Constructor // FIXME - Temporarily overload Execute to support both TensorOperation and TensorTransform - explicit Execute(std::shared_ptr op, std::string deviceType = "CPU"); - explicit Execute(std::shared_ptr op, std::string deviceType = "CPU"); - // explicit Execute(TensorTransform op, std::string deviceType = "CPU"); - explicit Execute(TensorTransform *op, std::string deviceType = "CPU"); + explicit Execute(std::shared_ptr op, MapTargetDevice deviceType = MapTargetDevice::kCpu); + explicit Execute(std::shared_ptr op, MapTargetDevice deviceType = MapTargetDevice::kCpu); + // explicit Execute(TensorTransform op, MapTargetDevice deviceType = MapTargetDevice::KCpu); + explicit Execute(TensorTransform *op, MapTargetDevice deviceType = MapTargetDevice::kCpu); - explicit Execute(std::vector> ops, std::string deviceType = "CPU"); - explicit Execute(std::vector> ops, std::string deviceType = "CPU"); - explicit Execute(const std::vector> ops, std::string deviceType = "CPU"); - explicit Execute(std::vector ops, std::string deviceType = "CPU"); + explicit Execute(std::vector> ops, + MapTargetDevice deviceType = MapTargetDevice::kCpu); + explicit Execute(std::vector> ops, + MapTargetDevice deviceType = MapTargetDevice::kCpu); + explicit Execute(const std::vector> ops, + MapTargetDevice deviceType = MapTargetDevice::kCpu); + explicit Execute(std::vector ops, MapTargetDevice deviceType = MapTargetDevice::kCpu); /// \brief Destructor ~Execute(); @@ -65,7 +68,7 @@ class Execute { std::vector> ops_; - std::string device_type_; + MapTargetDevice device_type_; std::shared_ptr device_resource_; }; diff --git a/mindspore/ccsrc/minddata/dataset/include/transforms.h b/mindspore/ccsrc/minddata/dataset/include/transforms.h index 84b016dfb9d..0c58c0086ce 100644 --- a/mindspore/ccsrc/minddata/dataset/include/transforms.h +++ b/mindspore/ccsrc/minddata/dataset/include/transforms.h @@ -44,6 +44,11 @@ class TensorTransform : public std::enable_shared_from_this { /// \brief Pure virtual function to convert a TensorTransform class into a IR TensorOperation object. /// \return shared pointer to the newly created TensorOperation. virtual std::shared_ptr Parse() = 0; + + /// \brief Virtual function to convert a TensorTransform class into a IR TensorOperation object. + /// \param[in] env A string to determine the running environment + /// \return shared pointer to the newly created TensorOperation. + virtual std::shared_ptr Parse(const MapTargetDevice &env) { return nullptr; } }; // Transform operations for performing data transformation. diff --git a/mindspore/ccsrc/minddata/dataset/include/vision_ascend.h b/mindspore/ccsrc/minddata/dataset/include/vision_ascend.h index b168d66b766..5fcf8841e1c 100644 --- a/mindspore/ccsrc/minddata/dataset/include/vision_ascend.h +++ b/mindspore/ccsrc/minddata/dataset/include/vision_ascend.h @@ -32,186 +32,67 @@ namespace dataset { // Transform operations for performing computer vision. namespace vision { -// Char arrays storing name of corresponding classes (in alphabetical order) -constexpr char kDvppCropJpegOperation[] = "DvppCropJpeg"; -constexpr char kDvppDecodeResizeOperation[] = "DvppDecodeResize"; -constexpr char kDvppDecodeResizeCropOperation[] = "DvppDecodeResizeCrop"; -constexpr char kDvppDecodeJpegOperation[] = "DvppDecodeJpeg"; -constexpr char kDvppDecodePngOperation[] = "DvppDecodePng"; -constexpr char kDvppResizeJpegOperation[] = "DvppResizeJpeg"; - -class DvppCropJpegOperation; class DvppDecodeResizeOperation; class DvppDecodeResizeCropOperation; -class DvppDecodeJpegOperation; class DvppDecodePngOperation; -class DvppResizeJpegOperation; -/// \brief Function to create a DvppCropJpeg TensorOperation. -/// \notes Tensor operation to crop 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 [32*32, 2048*2048]. -/// 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, the shape will be a square. If size has 2 values, it should be (height, width). -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr DvppCropJpeg(std::vector crop = {256, 256}); +/* ##################################### API class ###########################################*/ -/// \brief Function to create a DvppDecodeResizeJpeg 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 [32*32, 2048*2048]. -/// 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 DvppDecodeResizeJpeg(std::vector resize = {256, 256}); - -/// \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 [32*32, 2048*2048]. -/// 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] Resize 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 the 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 DvppDecodeResizeCropJpeg(std::vector crop = {224, 224}, - std::vector resize = {256, 256}); - -/// \brief Function to create a DvppDecodeJpeg TensorOperation. -/// \notes Tensor operation to decode 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 [32*32, 2048*2048]. -/// Only images with an even resolution can be output. The output of odd resolution is not supported. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr DvppDecodeJpeg(); - -/// \brief Function to create a DvppDecodePng TensorOperation. -/// \notes Tensor operation to decode PNG 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 [32*32, 2048*2048]. -/// Only images with an even resolution can be output. The output of odd resolution is not supported. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr DvppDecodePng(); - -/// \brief Function to create a DvppResizeJpeg TensorOperation. -/// \notes Tensor operation to resize JPEG image using 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 [32*32, 2048*2048]. -/// Only images with an even resolution can be output. The output of odd resolution is not supported. -/// \param[in] resize vector represents the shape of image after resize. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr DvppResizeJpeg(std::vector resize = {256, 256}); - -class DvppCropJpegOperation : public TensorOperation { +class DvppDecodeResizeJpeg : public TensorTransform { public: - explicit DvppCropJpegOperation(const std::vector &resize); + /// \brief Constructor. + /// \param[in] resize A vector of int value for each dimension, w.r.t H,W order. + explicit DvppDecodeResizeJpeg(std::vector resize); - ~DvppCropJpegOperation() = default; + /// \brief Destructor. + ~DvppDecodeResizeJpeg() = default; - std::shared_ptr Build() override; + /// \brief Function to convert TensorTransform object into a TensorOperation object. + /// \return Shared pointer to TensorOperation object. + std::shared_ptr Parse() override; - Status ValidateParams() override; - - std::string Name() const override { return kDvppCropJpegOperation; } - - private: - std::vector crop_; -}; - -class DvppDecodeResizeOperation : public TensorOperation { - public: - explicit DvppDecodeResizeOperation(const std::vector &resize); - - ~DvppDecodeResizeOperation() = default; - - std::shared_ptr Build() override; - - Status ValidateParams() override; - - std::string Name() const override { return kDvppDecodeResizeOperation; } + std::shared_ptr Parse(const MapTargetDevice &env) override; private: std::vector resize_; }; -class DvppDecodeResizeCropOperation : public TensorOperation { +class DvppDecodeResizeCropJpeg : public TensorTransform { public: - explicit DvppDecodeResizeCropOperation(const std::vector &crop, const std::vector &resize); + /// \brief Constructor. + /// \param[in] crop A vector of int value for each dimension after final crop, w.r.t H,W order. + /// \param[in] resize A vector of int value for each dimension after resize, w.r.t H,W order. + explicit DvppDecodeResizeCropJpeg(std::vector crop, std::vector resize); - ~DvppDecodeResizeCropOperation() = default; + /// \brief Destructor. + ~DvppDecodeResizeCropJpeg() = default; - std::shared_ptr Build() override; + /// \brief Function to convert TensorTransform object into a TensorOperation object. + /// \return Shared pointer to TensorOperation object. + std::shared_ptr Parse() override; - Status ValidateParams() override; - - std::string Name() const override { return kDvppDecodeResizeCropOperation; } + std::shared_ptr Parse(const MapTargetDevice &env) override; private: std::vector crop_; std::vector resize_; }; -class DvppDecodeJpegOperation : public TensorOperation { +class DvppDecodePng : public TensorTransform { public: - ~DvppDecodeJpegOperation() = default; + /// \brief Constructor. + DvppDecodePng(); - std::shared_ptr Build() override; + /// \brief Destructor. + ~DvppDecodePng() = default; - Status ValidateParams() override; + /// \brief Function to convert TensorTransform object into a TensorOperation object. + /// \return Shared pointer to TensorOperation object. + std::shared_ptr Parse() override; - std::string Name() const override { return kDvppDecodeJpegOperation; } + std::shared_ptr Parse(const MapTargetDevice &env) override; }; -class DvppDecodePngOperation : public TensorOperation { - public: - ~DvppDecodePngOperation() = default; - - std::shared_ptr Build() override; - - Status ValidateParams() override; - - std::string Name() const override { return kDvppDecodePngOperation; } -}; - -class DvppResizeJpegOperation : public TensorOperation { - public: - explicit DvppResizeJpegOperation(const std::vector &resize); - - ~DvppResizeJpegOperation() = default; - - std::shared_ptr Build() override; - - Status ValidateParams() override; - - std::string Name() const override { return kDvppResizeJpegOperation; } - - private: - std::vector resize_; -}; } // namespace vision } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/include/vision_lite.h b/mindspore/ccsrc/minddata/dataset/include/vision_lite.h index e2cc616c432..32fa8fe9ea3 100644 --- a/mindspore/ccsrc/minddata/dataset/include/vision_lite.h +++ b/mindspore/ccsrc/minddata/dataset/include/vision_lite.h @@ -52,6 +52,8 @@ class CenterCrop : public TensorTransform { /// \return Shared pointer to TensorOperation object. std::shared_ptr Parse() override; + std::shared_ptr Parse(const MapTargetDevice &env) override; + private: std::vector size_; }; @@ -94,6 +96,8 @@ class Decode : public TensorTransform { /// \return Shared pointer to TensorOperation object. std::shared_ptr Parse() override; + std::shared_ptr Parse(const MapTargetDevice &env) override; + private: bool rgb_; }; @@ -139,6 +143,8 @@ class Resize : public TensorTransform { /// \return Shared pointer to TensorOperation object. std::shared_ptr Parse() override; + std::shared_ptr Parse(const MapTargetDevice &env) override; + private: std::vector size_; InterpolationMode interpolation_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/CMakeLists.txt b/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/CMakeLists.txt index 261f51e4e56..58973ac38fc 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/CMakeLists.txt +++ b/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/CMakeLists.txt @@ -5,4 +5,10 @@ set(DATASET_KERNELS_IR_VISION_SRC_FILES vision_ir.cc ) +if(ENABLE_ACL) + set(DATASET_KERNELS_IR_VISION_SRC_FILES + ${DATASET_KERNELS_IR_VISION_SRC_FILES} + ascend_vision_ir.cc) +endif() + add_library(kernels-ir-vision OBJECT ${DATASET_KERNELS_IR_VISION_SRC_FILES}) diff --git a/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/ascend_vision_ir.cc b/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/ascend_vision_ir.cc new file mode 100644 index 00000000000..cd858a4f8fa --- /dev/null +++ b/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/ascend_vision_ir.cc @@ -0,0 +1,293 @@ +/** + * Copyright 2020-2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 + +#include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h" +#ifndef ENABLE_ANDROID +#include "minddata/dataset/kernels/image/image_utils.h" +#endif + +#include "minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h" +#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h" +#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h" +#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h" +#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h" +#include "minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h" + +namespace mindspore { +namespace dataset { + +// Transform operations for computer vision +namespace vision { +/* ####################################### Derived TensorOperation classes ################################# */ + +// DvppCropOperation +DvppCropJpegOperation::DvppCropJpegOperation(const std::vector &crop) : crop_(crop) {} + +Status DvppCropJpegOperation::ValidateParams() { + // size + if (crop_.empty() || crop_.size() > 2) { + std::string err_msg = + "DvppCropJpeg: Crop resolution 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 (*min_element(crop_.begin(), crop_.end()) < 32 || *max_element(crop_.begin(), crop_.end()) > 2048) { + std::string err_msg = "Dvpp module supports crop image with resolution in range [32, 2048], got crop Parameters: "; + if (crop_.size() == 2) { + MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[1] << "]"; + } else { + MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[0] << "]"; + } + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + return Status::OK(); +} + +std::shared_ptr DvppCropJpegOperation::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; + // User specified the width value. + if (crop_.size() == 1) { + cropHeight = crop_[0]; + cropWidth = crop_[0]; + } else { + cropHeight = crop_[0]; + cropWidth = crop_[1]; + } + std::shared_ptr tensor_op = std::make_shared(cropHeight, cropWidth); + return tensor_op; +} + +Status DvppCropJpegOperation::to_json(nlohmann::json *out_json) { + nlohmann::json args; + args["size"] = crop_; + *out_json = args; + return Status::OK(); +} + +// DvppDecodeResizeOperation +DvppDecodeResizeOperation::DvppDecodeResizeOperation(const std::vector &resize) : resize_(resize) {} + +Status DvppDecodeResizeOperation::ValidateParams() { + // size + if (resize_.empty() || resize_.size() > 2) { + std::string err_msg = "DvppDecodeResizeJpeg: resize resolution 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 (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) { + std::string err_msg = + "Dvpp module supports resize image with resolution in range [32, 2048], got resize Parameters: "; + if (resize_.size() == 2) { + MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]"; + } else { + MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[0] << "]"; + } + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + return Status::OK(); +} + +std::shared_ptr DvppDecodeResizeOperation::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 resizeHeight, resizeWidth; + // User specified the width value. + if (resize_.size() == 1) { + resizeHeight = resize_[0]; + resizeWidth = 0; + } else { + resizeHeight = resize_[0]; + resizeWidth = resize_[1]; + } + std::shared_ptr tensor_op = + std::make_shared(resizeHeight, resizeWidth); + return tensor_op; +} + +Status DvppDecodeResizeOperation::to_json(nlohmann::json *out_json) { + nlohmann::json args; + args["size"] = resize_; + *out_json = args; + return Status::OK(); +} + +// DvppDecodeResizeCropOperation +DvppDecodeResizeCropOperation::DvppDecodeResizeCropOperation(const std::vector &crop, + const std::vector &resize) + : crop_(crop), resize_(resize) {} + +Status DvppDecodeResizeCropOperation::ValidateParams() { + // size + if (crop_.empty() || crop_.size() > 2) { + std::string err_msg = "DvppDecodeResizeCropJpeg: crop resolution 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 resolution 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 (*min_element(crop_.begin(), crop_.end()) < 32 || *max_element(crop_.begin(), crop_.end()) > 2048) { + std::string err_msg = "Dvpp module supports crop image with resolution in range [32, 2048], got Crop Parameters: "; + if (crop_.size() == 2) { + MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[1] << "]"; + } else { + MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[0] << "]"; + } + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + if (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) { + std::string err_msg = + "Dvpp module supports resize image with resolution in range [32, 2048], got Crop Parameters: "; + if (resize_.size() == 2) { + MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]"; + } else { + MS_LOG(ERROR) << err_msg << "[" << resize_[0] << "]"; + } + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + if (crop_.size() < resize_.size()) { + if (crop_[0] > MIN(resize_[0], resize_[1])) { + std::string err_msg = + "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= " + "y[0], and x[1] <= y[1], please verify your input parameters."; + 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 = + "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= " + "y[0], and x[1] <= y[1], please verify your input parameters."; + 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 = + "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= " + "y[0], and x[1] <= y[1], please verify your input parameters."; + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + } + } + return Status::OK(); +} + +std::shared_ptr 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 = 0; + } else { + resizeHeight = resize_[0]; + resizeWidth = resize_[1]; + } + std::shared_ptr tensor_op = + std::make_shared(cropHeight, cropWidth, resizeHeight, resizeWidth); + return tensor_op; +} + +Status DvppDecodeResizeCropOperation::to_json(nlohmann::json *out_json) { + nlohmann::json args; + args["crop_size"] = crop_; + args["resize_size"] = resize_; + *out_json = args; + return Status::OK(); +} + +// DvppDecodeJPEG +Status DvppDecodeJpegOperation::ValidateParams() { return Status::OK(); } + +std::shared_ptr DvppDecodeJpegOperation::Build() { return std::make_shared(); } + +// DvppDecodePNG +Status DvppDecodePngOperation::ValidateParams() { return Status::OK(); } + +std::shared_ptr DvppDecodePngOperation::Build() { return std::make_shared(); } + +// DvppResizeOperation +DvppResizeJpegOperation::DvppResizeJpegOperation(const std::vector &resize) : resize_(resize) {} + +Status DvppResizeJpegOperation::ValidateParams() { + // size + if (resize_.empty() || resize_.size() > 2) { + std::string err_msg = "DvppResizeJpeg: resize resolution 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 (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) { + std::string err_msg = + "Dvpp module supports resize image with resolution in range [32, 2048], got resize Parameters: "; + if (resize_.size() == 2) { + MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]"; + } else { + MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[0] << "]"; + } + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + return Status::OK(); +} + +std::shared_ptr DvppResizeJpegOperation::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 resizeHeight, resizeWidth; + // User specified the width value. + if (resize_.size() == 1) { + resizeHeight = resize_[0]; + resizeWidth = 0; + } else { + resizeHeight = resize_[0]; + resizeWidth = resize_[1]; + } + std::shared_ptr tensor_op = std::make_shared(resizeHeight, resizeWidth); + return tensor_op; +} + +Status DvppResizeJpegOperation::to_json(nlohmann::json *out_json) { + nlohmann::json args; + args["size"] = resize_; + *out_json = args; + return Status::OK(); +} + +} // namespace vision +} // namespace dataset +} // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/ascend_vision_ir.h b/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/ascend_vision_ir.h new file mode 100644 index 00000000000..57ae9fd4009 --- /dev/null +++ b/mindspore/ccsrc/minddata/dataset/kernels/ir/vision/ascend_vision_ir.h @@ -0,0 +1,146 @@ +/** + * Copyright 2020-2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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_CCSRC_MINDDATA_DATASET_KERNELS_IR_VISION_ASCEND_VISION_IR_H_ +#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IR_VISION_ASCEND_VISION_IR_H_ + +#include +#include +#include +#include +#include + +#include "include/api/status.h" +#include "minddata/dataset/include/constants.h" +#include "minddata/dataset/include/transforms.h" +#include "minddata/dataset/kernels/ir/tensor_operation.h" + +namespace mindspore { +namespace dataset { + +// Transform operations for computer vision +namespace vision { + +// Char arrays storing name of corresponding classes (in alphabetical order) +constexpr char kDvppCropJpegOperation[] = "DvppCropJpeg"; +constexpr char kDvppDecodeResizeOperation[] = "DvppDecodeResize"; +constexpr char kDvppDecodeResizeCropOperation[] = "DvppDecodeResizeCrop"; +constexpr char kDvppDecodeJpegOperation[] = "DvppDecodeJpeg"; +constexpr char kDvppDecodePngOperation[] = "DvppDecodePng"; +constexpr char kDvppResizeJpegOperation[] = "DvppResizeJpeg"; + +/* ####################################### Derived TensorOperation classes ################################# */ + +class DvppCropJpegOperation : public TensorOperation { + public: + explicit DvppCropJpegOperation(const std::vector &resize); + + ~DvppCropJpegOperation() = default; + + std::shared_ptr Build() override; + + Status ValidateParams() override; + + std::string Name() const override { return kDvppCropJpegOperation; } + + Status to_json(nlohmann::json *out_json) override; + + private: + std::vector crop_; +}; + +class DvppDecodeResizeOperation : public TensorOperation { + public: + explicit DvppDecodeResizeOperation(const std::vector &resize); + + ~DvppDecodeResizeOperation() = default; + + std::shared_ptr Build() override; + + Status ValidateParams() override; + + std::string Name() const override { return kDvppDecodeResizeOperation; } + + Status to_json(nlohmann::json *out_json) override; + + private: + std::vector resize_; +}; + +class DvppDecodeResizeCropOperation : public TensorOperation { + public: + explicit DvppDecodeResizeCropOperation(const std::vector &crop, const std::vector &resize); + + ~DvppDecodeResizeCropOperation() = default; + + std::shared_ptr Build() override; + + Status ValidateParams() override; + + std::string Name() const override { return kDvppDecodeResizeCropOperation; } + + Status to_json(nlohmann::json *out_json) override; + + private: + std::vector crop_; + std::vector resize_; +}; + +class DvppDecodeJpegOperation : public TensorOperation { + public: + ~DvppDecodeJpegOperation() = default; + + std::shared_ptr Build() override; + + Status ValidateParams() override; + + std::string Name() const override { return kDvppDecodeJpegOperation; } +}; + +class DvppDecodePngOperation : public TensorOperation { + public: + ~DvppDecodePngOperation() = default; + + std::shared_ptr Build() override; + + Status ValidateParams() override; + + std::string Name() const override { return kDvppDecodePngOperation; } +}; + +class DvppResizeJpegOperation : public TensorOperation { + public: + explicit DvppResizeJpegOperation(const std::vector &resize); + + ~DvppResizeJpegOperation() = default; + + std::shared_ptr Build() override; + + Status ValidateParams() override; + + std::string Name() const override { return kDvppResizeJpegOperation; } + + Status to_json(nlohmann::json *out_json) override; + + private: + std::vector resize_; +}; + +} // namespace vision +} // namespace dataset +} // namespace mindspore + +#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IR_VISION_ASCEND_VISION_IR_H_ diff --git a/tests/st/cpp/dataset/test_de.cc b/tests/st/cpp/dataset/test_de.cc index a324edba1ec..32143ac83b7 100644 --- a/tests/st/cpp/dataset/test_de.cc +++ b/tests/st/cpp/dataset/test_de.cc @@ -73,14 +73,15 @@ TEST_F(TestDE, TestDvpp) { // Define dvpp transform std::vector crop_paras = {224, 224}; std::vector resize_paras = {256, 256}; - mindspore::dataset::Execute Transform(DvppDecodeResizeCropJpeg(crop_paras, resize_paras)); + auto decode_resize_crop(new vision::DvppDecodeResizeCropJpeg(crop_paras, resize_paras)); + mindspore::dataset::Execute Transform(decode_resize_crop, MapTargetDevice::kAscend310); // Apply transform on images Status rc = Transform(image, &image); // Check image info ASSERT_TRUE(rc.IsOk()); - ASSERT_EQ(image.Shape().size(), 3); + ASSERT_EQ(image.Shape().size(), 2); int32_t real_h = 0; int32_t real_w = 0; int32_t remainder = crop_paras[crop_paras.size() - 1] % 16; @@ -91,9 +92,9 @@ TEST_F(TestDE, TestDvpp) { real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1; real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder; } - ASSERT_EQ(image.Shape()[0], real_h * real_w * 1.5); // For image in YUV format, each pixel takes 1.5 byte - ASSERT_EQ(image.Shape()[1], 1); - ASSERT_EQ(image.Shape()[2], 1); + ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte + ASSERT_EQ(image.Shape()[1], real_w); + ASSERT_EQ(image.DataSize(), real_h * real_w * 1.5); #endif } @@ -105,10 +106,13 @@ TEST_F(TestDE, TestDvppSinkMode) { auto image = MSTensor(std::make_shared(de_tensor)); // Define dvpp transform - std::vector crop_paras = {224, 224}; - std::vector resize_paras = {256}; - mindspore::dataset::Execute Transform({DvppDecodeJpeg(), DvppResizeJpeg(resize_paras), DvppCropJpeg(crop_paras)}, - "Ascend310"); + std::vector crop_paras = {224, 224}; + std::vector resize_paras = {256}; + std::shared_ptr decode(new vision::Decode()); + std::shared_ptr resize(new vision::Resize(resize_paras)); + std::shared_ptr centercrop(new vision::CenterCrop(crop_paras)); + std::vector> transforms = {decode, resize, centercrop}; + mindspore::dataset::Execute Transform(transforms, MapTargetDevice::kAscend310); // Apply transform on images Status rc = Transform(image, &image); @@ -140,9 +144,13 @@ TEST_F(TestDE, TestDvppDecodeResizeCrop) { auto image = MSTensor(std::make_shared(de_tensor)); // Define dvpp transform - std::vector crop_paras = {416}; - std::vector resize_paras = {512}; - mindspore::dataset::Execute Transform(DvppDecodeResizeCropJpeg(crop_paras, resize_paras), "Ascend310"); + std::vector crop_paras = {416}; + std::vector resize_paras = {512}; + auto decode(new vision::Decode()); + auto resize(new vision::Resize(resize_paras)); + auto centercrop(new vision::CenterCrop(crop_paras)); + std::vector transforms = {decode, resize, centercrop}; + mindspore::dataset::Execute Transform(transforms, MapTargetDevice::kAscend310); // Apply transform on images Status rc = Transform(image, &image); diff --git a/tests/ut/cpp/dataset/execute_test.cc b/tests/ut/cpp/dataset/execute_test.cc index 81470214706..474cd3beac2 100644 --- a/tests/ut/cpp/dataset/execute_test.cc +++ b/tests/ut/cpp/dataset/execute_test.cc @@ -200,7 +200,7 @@ TEST_F(MindDataTestExecute, TestTransformDecodeResizeCenterCrop1) { auto hwc2chw(new vision::HWC2CHW()); std::vector op_list = {decode, resize, centercrop, hwc2chw}; - mindspore::dataset::Execute Transform(op_list, "CPU"); + mindspore::dataset::Execute Transform(op_list, MapTargetDevice::kCpu); // Apply transform on image Status rc = Transform(image, &image);