!12672 Aipp config file generation

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

File diff suppressed because it is too large Load Diff

@ -191,6 +191,15 @@ Normalize::Normalize(std::vector<float> mean, std::vector<float> std) : mean_(me
std::shared_ptr<TensorOperation> Normalize::Parse() { return std::make_shared<NormalizeOperation>(mean_, std_); }
std::shared_ptr<TensorOperation> Normalize::Parse(const MapTargetDevice &env) {
if (env == MapTargetDevice::kAscend310) {
#ifdef ENABLE_ACL
return std::make_shared<DvppNormalizeOperation>(mean_, std_);
#endif
}
return std::make_shared<NormalizeOperation>(mean_, std_);
}
#ifndef ENABLE_ANDROID
// NormalizePad Transform Operation.
NormalizePad::NormalizePad(const std::vector<float> &mean, const std::vector<float> &std, const std::string &dtype)

@ -53,8 +53,10 @@ class DeviceTensor : public Tensor {
Status SetYuvStrideShape_(const uint32_t &width, const uint32_t &widthStride, const uint32_t &height,
const uint32_t &heightStride);
std::vector<uint32_t> YUV_shape_;
std::vector<uint32_t> YUV_shape_; // YUV_shape_ = {width, widthStride, height, heightStride}
uint8_t *device_data_;
uint32_t size_;
};

@ -19,6 +19,7 @@
#include <string>
#include <vector>
#include <map>
#include <memory>
#include "include/api/context.h"
#include "include/api/types.h"
@ -63,6 +64,8 @@ class Execute {
Status DeviceMemoryRelease();
std::string AippCfgGenerator();
private:
Status validate_device_();
@ -71,6 +74,9 @@ class Execute {
MapTargetDevice device_type_;
std::shared_ptr<DeviceResource> device_resource_;
struct ExtraInfo;
std::shared_ptr<ExtraInfo> info_;
};
} // namespace dataset

@ -155,6 +155,8 @@ class Normalize : public TensorTransform {
/// \return Shared pointer to TensorOperation object.
std::shared_ptr<TensorOperation> Parse() override;
std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
private:
std::vector<float> mean_;
std::vector<float> std_;

@ -6,6 +6,7 @@ add_library(kernels-dvpp-image OBJECT
dvpp_decode_resize_crop_jpeg_op.cc
dvpp_decode_resize_jpeg_op.cc
dvpp_decode_jpeg_op.cc
dvpp_resize_jpeg_op.cc
dvpp_decode_png_op.cc)
dvpp_decode_png_op.cc
dvpp_normalize_op.cc
dvpp_resize_jpeg_op.cc)
add_dependencies(kernels-dvpp-image dvpp-utils)

@ -145,5 +145,6 @@ Status DvppCropJpegOp::SetAscendResource(const std::shared_ptr<DeviceResource> &
processor_->SetCropParas(crop_width_, crop_height_);
return Status::OK();
}
} // namespace dataset
} // namespace mindspore

@ -134,5 +134,6 @@ Status DvppDecodeResizeJpegOp::SetAscendResource(const std::shared_ptr<DeviceRes
processor_->SetResizeParas(resized_width_, resized_height_);
return Status::OK();
}
} // namespace dataset
} // namespace mindspore

@ -0,0 +1,39 @@
/**
* Copyright 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 <algorithm>
#include "minddata/dataset/kernels/image/dvpp/dvpp_normalize_op.h"
namespace mindspore {
namespace dataset {
Status DvppNormalizeOp::Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) {
const TensorShape dvpp_shape({1, 1, 1});
const DataType dvpp_data_type(DataType::DE_UINT8);
mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output);
std::vector<uint32_t> yuv_shape = input->GetYuvStrideShape();
(*output)->SetAttributes(input->GetDeviceBuffer(), input->DeviceDataSize(), yuv_shape[0], yuv_shape[1], yuv_shape[2],
yuv_shape[3]);
if (!((*output)->HasDeviceData())) {
std::string error = "[ERROR] Fail to get the output result from device memory!";
RETURN_STATUS_UNEXPECTED(error);
}
return Status::OK();
}
Status DvppNormalizeOp::SetAscendResource(const std::shared_ptr<DeviceResource> &resource) { return Status::OK(); }
} // namespace dataset
} // namespace mindspore

@ -0,0 +1,51 @@
/**
* Copyright 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_IMAGE_DVPP_DVPP_NORMALIZE_JPEG_OP_H
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_NORMALIZE_JPEG_OP_H
#include <memory>
#include <string>
#include <vector>
#include "minddata/dataset/core/device_tensor.h"
#include "minddata/dataset/core/device_resource.h"
#include "minddata/dataset/kernels/tensor_op.h"
#include "minddata/dataset/util/status.h"
#include "mindspore/core/utils/log_adapter.h"
namespace mindspore {
namespace dataset {
class DvppNormalizeOp : public TensorOp {
public:
explicit DvppNormalizeOp(std::vector<float> mean, std::vector<float> std) : mean_(mean), std_(std) {}
~DvppNormalizeOp() = default;
Status Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) override;
std::string Name() const override { return kDvppNormalizeOp; }
Status SetAscendResource(const std::shared_ptr<DeviceResource> &resource) override;
private:
std::vector<float> mean_;
std::vector<float> std_;
};
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_NORMALIZE_JPEG_OP_H

@ -26,6 +26,7 @@
#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_normalize_op.h"
#include "minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h"
namespace mindspore {
@ -241,6 +242,53 @@ Status DvppDecodePngOperation::ValidateParams() { return Status::OK(); }
std::shared_ptr<TensorOp> DvppDecodePngOperation::Build() { return std::make_shared<DvppDecodePngOp>(); }
// DvppNormalize
DvppNormalizeOperation::DvppNormalizeOperation(const std::vector<float> &mean, const std::vector<float> &std)
: mean_(mean), std_(std) {}
Status DvppNormalizeOperation::ValidateParams() {
if (mean_.size() != 3) {
std::string err_msg = "DvppNormalization:: mean expecting size 3, got size: " + std::to_string(mean_.size());
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
if (std_.size() != 3) {
std::string err_msg = "DvppNormalization: std expecting size 3, got size: " + std::to_string(std_.size());
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
if (*min_element(mean_.begin(), mean_.end()) < 0 || *max_element(mean_.begin(), mean_.end()) > 256) {
std::string err_msg =
"Normalization can take parameters in range [0, 256] according to math theory of mean and sigma, got mean "
"vector" +
std::to_string(std_.size());
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
if (*min_element(std_.begin(), std_.end()) < 0 || *max_element(std_.begin(), std_.end()) > 256) {
std::string err_msg =
"Normalization can take parameters in range [0, 256] according to math theory of mean and sigma, got mean "
"vector" +
std::to_string(std_.size());
MS_LOG(ERROR) << err_msg;
RETURN_STATUS_SYNTAX_ERROR(err_msg);
}
return Status::OK();
}
std::shared_ptr<TensorOp> DvppNormalizeOperation::Build() {
std::shared_ptr<DvppNormalizeOp> tensor_op = std::make_shared<DvppNormalizeOp>(mean_, std_);
return tensor_op;
}
Status DvppNormalizeOperation::to_json(nlohmann::json *out_json) {
nlohmann::json args;
args["mean"] = mean_;
args["std"] = std_;
*out_json = args;
return Status::OK();
}
// DvppResizeOperation
DvppResizeJpegOperation::DvppResizeJpegOperation(const std::vector<uint32_t> &resize) : resize_(resize) {}

@ -40,6 +40,7 @@ constexpr char kDvppDecodeResizeOperation[] = "DvppDecodeResize";
constexpr char kDvppDecodeResizeCropOperation[] = "DvppDecodeResizeCrop";
constexpr char kDvppDecodeJpegOperation[] = "DvppDecodeJpeg";
constexpr char kDvppDecodePngOperation[] = "DvppDecodePng";
constexpr char kDvppNormalizeOperation[] = "DvppNormalize";
constexpr char kDvppResizeJpegOperation[] = "DvppResizeJpeg";
/* ####################################### Derived TensorOperation classes ################################# */
@ -121,6 +122,25 @@ class DvppDecodePngOperation : public TensorOperation {
std::string Name() const override { return kDvppDecodePngOperation; }
};
class DvppNormalizeOperation : public TensorOperation {
public:
explicit DvppNormalizeOperation(const std::vector<float> &mean, const std::vector<float> &std);
~DvppNormalizeOperation() = default;
std::shared_ptr<TensorOp> Build() override;
Status ValidateParams() override;
std::string Name() const override { return kDvppNormalizeOperation; }
Status to_json(nlohmann::json *out_json) override;
private:
std::vector<float> mean_;
std::vector<float> std_;
};
class DvppResizeJpegOperation : public TensorOperation {
public:
explicit DvppResizeJpegOperation(const std::vector<uint32_t> &resize);

@ -75,5 +75,6 @@ Status TensorOp::SetAscendResource(const std::shared_ptr<DeviceResource> &resour
return Status(StatusCode::kMDUnexpectedError,
"This is a CPU operator which doesn't have Ascend Resource. Please verify your context");
}
} // namespace dataset
} // namespace mindspore

@ -66,6 +66,7 @@ constexpr char kDvppDecodeResizeCropJpegOp[] = "DvppDecodeResizeCropJpegOp";
constexpr char kDvppDecodeResizeJpegOp[] = "DvppDecodeResizeJpegOp";
constexpr char kDvppDecodeJpegOp[] = "DvppDecodeJpegOp";
constexpr char kDvppDecodePngOp[] = "DvppDecodePngOp";
constexpr char kDvppNormalizeOp[] = "DvppNormalizeOp";
constexpr char kDvppResizeJpegOp[] = "DvppResizeJpegOp";
constexpr char kEqualizeOp[] = "EqualizeOp";
constexpr char kHwcToChwOp[] = "HWC2CHWOp";

@ -81,7 +81,7 @@ TEST_F(TestDE, TestDvpp) {
// Check image info
ASSERT_TRUE(rc.IsOk());
ASSERT_EQ(image.Shape().size(), 2);
ASSERT_EQ(image.Shape().size(), 3);
int32_t real_h = 0;
int32_t real_w = 0;
int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
@ -92,9 +92,15 @@ 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;
}
/* Use in the future
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);
*/
ASSERT_EQ(image.Shape()[0], 1.5 * real_h * real_w); // 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.DataSize(), real_h * real_w * 1.5);
#endif
}
@ -119,7 +125,7 @@ TEST_F(TestDE, TestDvppSinkMode) {
// Check image info
ASSERT_TRUE(rc.IsOk());
ASSERT_EQ(image.Shape().size(), 2);
ASSERT_EQ(image.Shape().size(), 3);
int32_t real_h = 0;
int32_t real_w = 0;
int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
@ -130,14 +136,15 @@ TEST_F(TestDE, TestDvppSinkMode) {
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); // For image in YUV format, each pixel takes 1.5 byte
ASSERT_EQ(image.Shape()[1], real_w);
ASSERT_EQ(image.DataSize(), 1.5 * real_w * real_h);
ASSERT_EQ(image.Shape()[0], 1.5 * real_h * real_w); // 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.DataSize(), real_h * real_w * 1.5);
Transform.DeviceMemoryRelease();
#endif
}
TEST_F(TestDE, TestDvppDecodeResizeCrop) {
TEST_F(TestDE, TestDvppDecodeResizeCropNormalize) {
#ifdef ENABLE_ACL
std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
@ -146,18 +153,24 @@ TEST_F(TestDE, TestDvppDecodeResizeCrop) {
// Define dvpp transform
std::vector<int32_t> crop_paras = {416};
std::vector<int32_t> resize_paras = {512};
std::vector<float> mean = {0.485 * 255, 0.456 * 255, 0.406 * 255};
std::vector<float> std = {0.229 * 255, 0.224 * 255, 0.225 * 255};
auto decode(new vision::Decode());
auto resize(new vision::Resize(resize_paras));
auto centercrop(new vision::CenterCrop(crop_paras));
std::vector<TensorTransform *> transforms = {decode, resize, centercrop};
mindspore::dataset::Execute Transform(transforms, MapTargetDevice::kAscend310);
auto normalize(new vision::Normalize(mean, std));
std::vector<TensorTransform *> trans_lists = {decode, resize, centercrop, normalize};
mindspore::dataset::Execute Transform(trans_lists, MapTargetDevice::kAscend310);
std::string aipp_cfg = Transform.AippCfgGenerator();
ASSERT_EQ(aipp_cfg, "./aipp.cfg");
// Apply transform on images
Status rc = Transform(image, &image);
// Check image info
ASSERT_TRUE(rc.IsOk());
ASSERT_EQ(image.Shape().size(), 2);
ASSERT_EQ(image.Shape().size(), 3);
int32_t real_h = 0;
int32_t real_w = 0;
int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
@ -168,9 +181,10 @@ TEST_F(TestDE, TestDvppDecodeResizeCrop) {
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); // For image in YUV format, each pixel takes 1.5 byte
ASSERT_EQ(image.Shape()[1], real_w);
ASSERT_EQ(image.DataSize(), 1.5 * real_w * real_h);
ASSERT_EQ(image.Shape()[0], 1.5 * real_h * real_w); // 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.DataSize(), real_h * real_w * 1.5);
Transform.DeviceMemoryRelease();
#endif
}

Loading…
Cancel
Save