parent
5f72693b4b
commit
e2d3495925
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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 "minddata/dataset/core/global_context.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#include "minddata/dataset/core/device_tensor.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
Status DeviceTensor::SetYuvStrideShape_(const uint32_t &width, const uint32_t &widthStride, const uint32_t &height,
|
||||
const uint32_t &heightStride) {
|
||||
YUV_shape_ = {width, widthStride, height, heightStride};
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
std::vector<uint32_t> DeviceTensor::GetYuvStrideShape() { return YUV_shape_; }
|
||||
|
||||
#ifdef ENABLE_ACL
|
||||
Status DeviceTensor::SetAttributes(const std::shared_ptr<DvppDataInfo> &data_ptr) {
|
||||
device_data_ = data_ptr->data;
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(device_data_ != nullptr, "Fail to get the device data.");
|
||||
SetSize_(data_ptr->dataSize);
|
||||
SetYuvStrideShape_(data_ptr->width, data_ptr->widthStride, data_ptr->height, data_ptr->heightStride);
|
||||
return Status::OK();
|
||||
}
|
||||
#endif
|
||||
|
||||
DeviceTensor::DeviceTensor(const TensorShape &shape, const DataType &type) : Tensor(shape, type) {
|
||||
// grab the mem pool from global context and create the allocator for char data area
|
||||
std::shared_ptr<MemoryPool> global_pool = GlobalContext::Instance()->mem_pool();
|
||||
data_allocator_ = std::make_unique<Allocator<unsigned char>>(global_pool);
|
||||
}
|
||||
|
||||
Status DeviceTensor::CreateEmpty(const TensorShape &shape, const DataType &type, std::shared_ptr<DeviceTensor> *out) {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(shape.known(), "Invalid shape.");
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(type != DataType::DE_UNKNOWN, "Invalid data type.");
|
||||
const DeviceTensorAlloc *alloc = GlobalContext::Instance()->device_tensor_allocator();
|
||||
*out = std::allocate_shared<DeviceTensor>(*alloc, shape, type);
|
||||
// if it's a string tensor and it has no elements, Just initialize the shape and type.
|
||||
if (!type.IsNumeric() && shape.NumOfElements() == 0) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(type.IsNumeric(), "Number of elements is not 0. The type should be numeric.");
|
||||
|
||||
int64_t byte_size = (*out)->SizeInBytes();
|
||||
|
||||
// Don't allocate if we have a tensor with no elements.
|
||||
if (byte_size != 0) {
|
||||
RETURN_IF_NOT_OK((*out)->AllocateBuffer(byte_size));
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
uint8_t *DeviceTensor::GetDeviceBuffer() { return device_data_; }
|
||||
|
||||
uint32_t DeviceTensor::DeviceDataSize() { return size_; }
|
||||
|
||||
Status DeviceTensor::SetSize_(const uint32_t &new_size) {
|
||||
size_ = new_size;
|
||||
return Status::OK();
|
||||
}
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 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_DEVICE_TENSOR_H
|
||||
#define MINDSPORE_DEVICE_TENSOR_H
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "include/api/status.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#ifdef ENABLE_ACL
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h"
|
||||
#endif
|
||||
#include "minddata/dataset/core/constants.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/util/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
class Tensor;
|
||||
class DeviceTensor : public Tensor {
|
||||
public:
|
||||
DeviceTensor(const TensorShape &shape, const DataType &type);
|
||||
|
||||
~DeviceTensor() {}
|
||||
#ifdef ENABLE_ACL
|
||||
Status SetAttributes(const std::shared_ptr<DvppDataInfo> &data);
|
||||
#endif
|
||||
static Status CreateEmpty(const TensorShape &shape, const DataType &type, std::shared_ptr<DeviceTensor> *out);
|
||||
|
||||
uint8_t *GetDeviceBuffer();
|
||||
|
||||
std::vector<uint32_t> GetYuvStrideShape();
|
||||
|
||||
uint32_t DeviceDataSize();
|
||||
|
||||
bool HasDeviceData() { return device_data_ != nullptr; }
|
||||
|
||||
private:
|
||||
Status SetSize_(const uint32_t &new_size);
|
||||
|
||||
Status SetYuvStrideShape_(const uint32_t &width, const uint32_t &widthStride, const uint32_t &height,
|
||||
const uint32_t &heightStride);
|
||||
|
||||
std::vector<uint32_t> YUV_shape_;
|
||||
uint8_t *device_data_;
|
||||
uint32_t size_;
|
||||
};
|
||||
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
#endif // MINDSPORE_DEVICE_TENSOR_H
|
@ -0,0 +1,219 @@
|
||||
/**
|
||||
* 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_INCLUDE_VISION_ASCEND_H_
|
||||
#define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_VISION_ASCEND_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "include/api/status.h"
|
||||
#include "minddata/dataset/include/constants.h"
|
||||
#include "minddata/dataset/include/transforms.h"
|
||||
|
||||
namespace mindspore {
|
||||
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<DvppCropJpegOperation> DvppCropJpeg(std::vector<uint32_t> crop = {256, 256});
|
||||
|
||||
/// \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<DvppDecodeResizeOperation> DvppDecodeResizeJpeg(std::vector<uint32_t> 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<DvppDecodeResizeCropOperation> DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop = {224, 224},
|
||||
std::vector<uint32_t> 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<DvppDecodeJpegOperation> 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<DvppDecodePngOperation> 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<DvppResizeJpegOperation> DvppResizeJpeg(std::vector<uint32_t> resize = {256, 256});
|
||||
|
||||
class DvppCropJpegOperation : public TensorOperation {
|
||||
public:
|
||||
explicit DvppCropJpegOperation(const std::vector<uint32_t> &resize);
|
||||
|
||||
~DvppCropJpegOperation() = default;
|
||||
|
||||
std::shared_ptr<TensorOp> Build() override;
|
||||
|
||||
Status ValidateParams() override;
|
||||
|
||||
std::string Name() const override { return kDvppCropJpegOperation; }
|
||||
|
||||
private:
|
||||
std::vector<uint32_t> crop_;
|
||||
};
|
||||
|
||||
class DvppDecodeResizeOperation : public TensorOperation {
|
||||
public:
|
||||
explicit DvppDecodeResizeOperation(const std::vector<uint32_t> &resize);
|
||||
|
||||
~DvppDecodeResizeOperation() = default;
|
||||
|
||||
std::shared_ptr<TensorOp> Build() override;
|
||||
|
||||
Status ValidateParams() override;
|
||||
|
||||
std::string Name() const override { return kDvppDecodeResizeOperation; }
|
||||
|
||||
private:
|
||||
std::vector<uint32_t> resize_;
|
||||
};
|
||||
|
||||
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 DvppDecodeJpegOperation : public TensorOperation {
|
||||
public:
|
||||
~DvppDecodeJpegOperation() = default;
|
||||
|
||||
std::shared_ptr<TensorOp> Build() override;
|
||||
|
||||
Status ValidateParams() override;
|
||||
|
||||
std::string Name() const override { return kDvppDecodeJpegOperation; }
|
||||
};
|
||||
|
||||
class DvppDecodePngOperation : public TensorOperation {
|
||||
public:
|
||||
~DvppDecodePngOperation() = default;
|
||||
|
||||
std::shared_ptr<TensorOp> Build() override;
|
||||
|
||||
Status ValidateParams() override;
|
||||
|
||||
std::string Name() const override { return kDvppDecodePngOperation; }
|
||||
};
|
||||
|
||||
class DvppResizeJpegOperation : public TensorOperation {
|
||||
public:
|
||||
explicit DvppResizeJpegOperation(const std::vector<uint32_t> &resize);
|
||||
|
||||
~DvppResizeJpegOperation() = default;
|
||||
|
||||
std::shared_ptr<TensorOp> Build() override;
|
||||
|
||||
Status ValidateParams() override;
|
||||
|
||||
std::string Name() const override { return kDvppResizeJpegOperation; }
|
||||
|
||||
private:
|
||||
std::vector<uint32_t> resize_;
|
||||
};
|
||||
} // namespace vision
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_VISION_ASCEND_H_
|
@ -0,0 +1,145 @@
|
||||
/**
|
||||
* 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 <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "include/api/context.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/device_tensor.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h"
|
||||
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
Status DvppCropJpegOp::Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetDeviceBuffer() != nullptr, "The input image buffer is empty.");
|
||||
std::string last_step = "Resize";
|
||||
std::shared_ptr<DvppDataInfo> imageinfo(processor_->Get_Resized_DeviceData());
|
||||
if (!imageinfo->data) {
|
||||
last_step = "Decode";
|
||||
}
|
||||
APP_ERROR ret = processor_->JPEG_C(last_step);
|
||||
if (ret != APP_ERR_OK) {
|
||||
processor_->Release();
|
||||
std::string error = "Error in dvpp crop processing:" + std::to_string(ret);
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
std::shared_ptr<DvppDataInfo> CropOut(processor_->Get_Croped_DeviceData());
|
||||
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);
|
||||
(*output)->SetAttributes(CropOut);
|
||||
if (!((*output)->HasDeviceData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from device memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
} catch (const cv::Exception &e) {
|
||||
std::string error = "[ERROR] Fail in DvppCropJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppCropJpegOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetBuffer() != nullptr, "The input image buffer is empty.");
|
||||
unsigned char *buffer = const_cast<unsigned char *>(input->GetBuffer());
|
||||
DvppDataInfo imageinfo;
|
||||
imageinfo.dataSize = input->SizeInBytes();
|
||||
imageinfo.data = static_cast<uint8_t *>(buffer);
|
||||
std::vector<uint32_t> yuv_shape_ = input->GetYuvShape();
|
||||
imageinfo.width = yuv_shape_[0];
|
||||
imageinfo.widthStride = yuv_shape_[1];
|
||||
imageinfo.height = yuv_shape_[2];
|
||||
imageinfo.heightStride = yuv_shape_[3];
|
||||
imageinfo.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
|
||||
ResourceInfo resource;
|
||||
resource.aclConfigPath = "";
|
||||
resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID());
|
||||
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
|
||||
MDAclProcess process(crop_width_, crop_height_, context, 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.JPEG_C(imageinfo);
|
||||
if (ret != APP_ERR_OK) {
|
||||
instance->Release();
|
||||
std::string error = "Error in dvpp crop 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_Croped_DeviceData());
|
||||
dsize_t dvpp_length = CropOut->dataSize;
|
||||
const TensorShape dvpp_shape({dvpp_length, 1, 1});
|
||||
uint32_t crop_height = CropOut->height;
|
||||
uint32_t crop_heightStride = CropOut->heightStride;
|
||||
uint32_t crop_width = CropOut->width;
|
||||
uint32_t crop_widthStride = CropOut->widthStride;
|
||||
const DataType dvpp_data_type(DataType::DE_UINT8);
|
||||
mindspore::dataset::Tensor::CreateFromMemory(dvpp_shape, dvpp_data_type, ret_ptr, output);
|
||||
(*output)->SetYuvShape(crop_width, crop_widthStride, crop_height, crop_heightStride);
|
||||
if (!((*output)->HasData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
process.device_memory_release();
|
||||
process.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 DvppCropJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppCropJpegOp::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 1 channels
|
||||
if (inputs[0].Rank() == 1) outputs.emplace_back(out);
|
||||
if (!outputs.empty()) return Status::OK();
|
||||
return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape");
|
||||
}
|
||||
|
||||
Status DvppCropJpegOp::SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) {
|
||||
processor_ = processor;
|
||||
processor_->SetCropParas(crop_width_, crop_height_);
|
||||
return Status::OK();
|
||||
}
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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_CROP_JPEG_OP_H
|
||||
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_CROP_JPEG_OP_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "acl/acl.h"
|
||||
#include "mindspore/core/utils/log_adapter.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/device_tensor.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
|
||||
#include "minddata/dataset/kernels/tensor_op.h"
|
||||
#include "minddata/dataset/util/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
class DvppCropJpegOp : public TensorOp {
|
||||
public:
|
||||
DvppCropJpegOp(int32_t crop_height, int32_t crop_width) : crop_height_(crop_height), crop_width_(crop_width) {}
|
||||
|
||||
/// \brief Destructor
|
||||
~DvppCropJpegOp() = default;
|
||||
|
||||
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||
|
||||
Status Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) override;
|
||||
|
||||
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
|
||||
|
||||
std::string Name() const override { return kDvppCropJpegOp; }
|
||||
|
||||
Status SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) override;
|
||||
|
||||
private:
|
||||
uint32_t crop_height_;
|
||||
uint32_t crop_width_;
|
||||
|
||||
std::shared_ptr<MDAclProcess> processor_;
|
||||
};
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_CROP_JPEG_OP_H
|
@ -0,0 +1,139 @@
|
||||
/**
|
||||
* 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 <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "include/api/context.h"
|
||||
#include "minddata/dataset/core/cv_tensor.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/device_tensor.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/utils/CommonDataType.h"
|
||||
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
// Compute() will be called when context=="Ascend310"
|
||||
Status DvppDecodeJpegOp::Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetDeviceBuffer() != nullptr, "The input image buffer on device is empty");
|
||||
APP_ERROR ret = processor_->JPEG_D();
|
||||
if (ret != APP_ERR_OK) {
|
||||
processor_->Release();
|
||||
std::string error = "Error in dvpp processing:" + std::to_string(ret);
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
std::shared_ptr<DvppDataInfo> DecodeOut(processor_->Get_Decode_DeviceData());
|
||||
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);
|
||||
(*output)->SetAttributes(DecodeOut);
|
||||
if (!((*output)->HasDeviceData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
} catch (const cv::Exception &e) {
|
||||
std::string error = "[ERROR] Fail in DvppDecodeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
// Compute() will be called when context=="CPU"
|
||||
Status DvppDecodeJpegOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
if (!IsNonEmptyJPEG(input)) {
|
||||
RETURN_STATUS_UNEXPECTED("DvppDecodeJpegOp 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 = static_cast<void *>(buffer);
|
||||
ResourceInfo resource;
|
||||
resource.aclConfigPath = "";
|
||||
resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID());
|
||||
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
|
||||
MDAclProcess process(context, false);
|
||||
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.JPEG_D(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> DecodeOut(process.Get_Decode_DeviceData());
|
||||
dsize_t dvpp_length = DecodeOut->dataSize;
|
||||
uint32_t decoded_height = DecodeOut->height;
|
||||
uint32_t decoded_heightStride = DecodeOut->heightStride;
|
||||
uint32_t decoded_width = DecodeOut->width;
|
||||
uint32_t decoded_widthStride = DecodeOut->widthStride;
|
||||
// std::cout << "Decoded size: " << decoded_width << ", " << decoded_height << std::endl;
|
||||
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);
|
||||
(*output)->SetYuvShape(decoded_width, decoded_widthStride, decoded_height, decoded_heightStride);
|
||||
if (!((*output)->HasData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from device memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
process.device_memory_release();
|
||||
process.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 DvppDecodeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppDecodeJpegOp::SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) {
|
||||
processor_ = processor;
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppDecodeJpegOp::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::kMDUnexpectedError, "Input has a wrong shape");
|
||||
}
|
||||
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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_DECODE_JPEG_OP_H
|
||||
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_DECODE_JPEG_OP_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "acl/acl.h"
|
||||
#include "mindspore/core/utils/log_adapter.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
|
||||
#include "minddata/dataset/kernels/tensor_op.h"
|
||||
#include "minddata/dataset/util/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
class DvppDecodeJpegOp : public TensorOp {
|
||||
public:
|
||||
DvppDecodeJpegOp() { processor_ = nullptr; }
|
||||
|
||||
/// \brief Destructor
|
||||
~DvppDecodeJpegOp() = default;
|
||||
|
||||
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||
|
||||
Status Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) override;
|
||||
|
||||
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
|
||||
|
||||
std::string Name() const override { return kDvppDecodeJpegOp; }
|
||||
|
||||
Status SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<MDAclProcess> processor_;
|
||||
};
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_DECODE_JPEG_OP_H
|
@ -0,0 +1,138 @@
|
||||
/**
|
||||
* 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 <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "include/api/context.h"
|
||||
#include "minddata/dataset/core/cv_tensor.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
|
||||
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
Status DvppDecodePngOp::Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetDeviceBuffer() != nullptr, "The input image buffer on device is empty");
|
||||
APP_ERROR ret = processor_->PNG_D();
|
||||
if (ret != APP_ERR_OK) {
|
||||
processor_->Release();
|
||||
std::string error = "Error in dvpp processing:" + std::to_string(ret);
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
std::shared_ptr<DvppDataInfo> DecodeOut(processor_->Get_Decode_DeviceData());
|
||||
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);
|
||||
(*output)->SetAttributes(DecodeOut);
|
||||
if (!((*output)->HasDeviceData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
} catch (const cv::Exception &e) {
|
||||
std::string error = "[ERROR] Fail in DvppDecodeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppDecodePngOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
if (!IsNonEmptyPNG(input)) {
|
||||
RETURN_STATUS_UNEXPECTED("DvppDecodePngOp only support process PNG 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 = static_cast<void *>(buffer);
|
||||
ResourceInfo resource;
|
||||
resource.aclConfigPath = "";
|
||||
resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID());
|
||||
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
|
||||
MDAclProcess process(context, false);
|
||||
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.PNG_D(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
|
||||
/* 测试Device内存
|
||||
*/
|
||||
auto data = std::static_pointer_cast<unsigned char>(process.Get_Memory_Data());
|
||||
unsigned char *ret_ptr = data.get();
|
||||
std::shared_ptr<DvppDataInfo> DecodeOut(process.Get_Decode_DeviceData());
|
||||
dsize_t dvpp_length = DecodeOut->dataSize;
|
||||
// dsize_t decode_height = DecodeOut->height;
|
||||
// dsize_t decode_width = DecodeOut->width;
|
||||
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();
|
||||
process.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 DvppDecodePngOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppDecodePngOp::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::kMDUnexpectedError, "Input has a wrong shape");
|
||||
}
|
||||
|
||||
Status DvppDecodePngOp::SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) {
|
||||
processor_ = processor;
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 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_DECODE_PNG_OP_H
|
||||
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_DECODE_PNG_OP_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "acl/acl.h"
|
||||
#include "mindspore/core/utils/log_adapter.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
|
||||
#include "minddata/dataset/kernels/tensor_op.h"
|
||||
#include "minddata/dataset/util/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
class DvppDecodePngOp : public TensorOp {
|
||||
public:
|
||||
DvppDecodePngOp() {}
|
||||
|
||||
/// \brief Destructor
|
||||
~DvppDecodePngOp() = default;
|
||||
|
||||
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||
|
||||
Status Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) override;
|
||||
|
||||
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
|
||||
|
||||
std::string Name() const override { return kDvppDecodePngOp; }
|
||||
|
||||
Status SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<MDAclProcess> processor_;
|
||||
};
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_DECODE_PNG_OP_H
|
@ -0,0 +1,134 @@
|
||||
/**
|
||||
* 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 <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "include/api/context.h"
|
||||
#include "minddata/dataset/core/cv_tensor.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
|
||||
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
Status DvppDecodeResizeJpegOp::Compute(const std::shared_ptr<DeviceTensor> &input,
|
||||
std::shared_ptr<DeviceTensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetDeviceBuffer() != nullptr, "The input image buffer on device is empty");
|
||||
APP_ERROR ret = processor_->JPEG_DR();
|
||||
if (ret != APP_ERR_OK) {
|
||||
processor_->Release();
|
||||
std::string error = "Error in dvpp processing:" + std::to_string(ret);
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
std::shared_ptr<DvppDataInfo> ResizeOut(processor_->Get_Resized_DeviceData());
|
||||
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);
|
||||
(*output)->SetAttributes(ResizeOut);
|
||||
if (!((*output)->HasDeviceData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
} catch (const cv::Exception &e) {
|
||||
std::string error = "[ERROR] Fail in DvppDecodeResizeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppDecodeResizeJpegOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
if (!IsNonEmptyJPEG(input)) {
|
||||
RETURN_STATUS_UNEXPECTED("DvppDecodeReiszeJpegOp 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 = static_cast<void *>(buffer);
|
||||
ResourceInfo resource;
|
||||
resource.aclConfigPath = "";
|
||||
resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID());
|
||||
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
|
||||
MDAclProcess process(resized_width_, resized_height_, context, false);
|
||||
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.JPEG_DR(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> ResizeOut(process.Get_Resized_DeviceData());
|
||||
dsize_t dvpp_length = ResizeOut->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();
|
||||
process.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 DvppDecodeResizeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppDecodeResizeJpegOp::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 1 channels
|
||||
if (inputs[0].Rank() == 1) outputs.emplace_back(out);
|
||||
if (!outputs.empty()) return Status::OK();
|
||||
return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape");
|
||||
}
|
||||
|
||||
Status DvppDecodeResizeJpegOp::SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) {
|
||||
processor_ = processor;
|
||||
processor_->SetResizeParas(resized_width_, resized_height_);
|
||||
return Status::OK();
|
||||
}
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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_DECODE_RESIZE_JPEG_OP_H
|
||||
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_DECODE_RESIZE_JPEG_OP_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "acl/acl.h"
|
||||
#include "mindspore/core/utils/log_adapter.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
|
||||
#include "minddata/dataset/kernels/tensor_op.h"
|
||||
#include "minddata/dataset/util/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
class DvppDecodeResizeJpegOp : public TensorOp {
|
||||
public:
|
||||
DvppDecodeResizeJpegOp(int32_t resized_height, int32_t resized_width)
|
||||
: resized_height_(resized_height), resized_width_(resized_width) {}
|
||||
|
||||
/// \brief Destructor
|
||||
~DvppDecodeResizeJpegOp() = default;
|
||||
|
||||
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||
|
||||
Status Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) override;
|
||||
|
||||
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
|
||||
|
||||
std::string Name() const override { return kDvppDecodeResizeJpegOp; }
|
||||
|
||||
Status SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) override;
|
||||
|
||||
private:
|
||||
int32_t resized_height_;
|
||||
int32_t resized_width_;
|
||||
|
||||
std::shared_ptr<MDAclProcess> processor_;
|
||||
};
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_DVPP_DECODE_RESIZE_JPEG_OP_H
|
@ -0,0 +1,147 @@
|
||||
/**
|
||||
* 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 <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "include/api/context.h"
|
||||
#include "minddata/dataset/core/cv_tensor.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "minddata/dataset/core/device_tensor.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
|
||||
#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
|
||||
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
Status DvppResizeJpegOp::Compute(const std::shared_ptr<DeviceTensor> &input, std::shared_ptr<DeviceTensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetDeviceBuffer() != nullptr, "The input image buffer is empty.");
|
||||
std::string last_step = "Decode";
|
||||
std::shared_ptr<DvppDataInfo> imageinfo(processor_->Get_Decode_DeviceData());
|
||||
if (!imageinfo->data) {
|
||||
last_step = "Crop";
|
||||
}
|
||||
APP_ERROR ret = processor_->JPEG_R(last_step);
|
||||
if (ret != APP_ERR_OK) {
|
||||
processor_->Release();
|
||||
std::string error = "Error in dvpp processing:" + std::to_string(ret);
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
std::shared_ptr<DvppDataInfo> ResizeOut(processor_->Get_Resized_DeviceData());
|
||||
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);
|
||||
(*output)->SetAttributes(ResizeOut); // Set attributes for output DeviceTensor
|
||||
if (!((*output)->HasDeviceData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from device memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
} catch (const cv::Exception &e) {
|
||||
std::string error = "[ERROR] Fail in DvppResizeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppResizeJpegOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||
IO_CHECK(input, output);
|
||||
try {
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(input->GetBuffer() != nullptr, "The input image buffer is empty.");
|
||||
unsigned char *buffer = const_cast<unsigned char *>(input->GetBuffer());
|
||||
DvppDataInfo imageinfo;
|
||||
imageinfo.dataSize = input->SizeInBytes();
|
||||
imageinfo.data = static_cast<uint8_t *>(buffer);
|
||||
std::vector<uint32_t> yuv_shape_ = input->GetYuvShape();
|
||||
imageinfo.width = yuv_shape_[0];
|
||||
imageinfo.widthStride = yuv_shape_[1];
|
||||
imageinfo.height = yuv_shape_[2];
|
||||
imageinfo.heightStride = yuv_shape_[3];
|
||||
imageinfo.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
|
||||
ResourceInfo resource;
|
||||
resource.aclConfigPath = "";
|
||||
resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID());
|
||||
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
|
||||
MDAclProcess process(resized_width_, resized_height_, context, false);
|
||||
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.JPEG_R(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> ResizeOut(process.Get_Resized_DeviceData());
|
||||
dsize_t dvpp_length = ResizeOut->dataSize;
|
||||
const TensorShape dvpp_shape({dvpp_length, 1, 1});
|
||||
uint32_t resized_height = ResizeOut->height;
|
||||
uint32_t resized_heightStride = ResizeOut->heightStride;
|
||||
uint32_t resized_width = ResizeOut->width;
|
||||
uint32_t resized_widthStride = ResizeOut->widthStride;
|
||||
const DataType dvpp_data_type(DataType::DE_UINT8);
|
||||
mindspore::dataset::Tensor::CreateFromMemory(dvpp_shape, dvpp_data_type, ret_ptr, output);
|
||||
(*output)->SetYuvShape(resized_width, resized_widthStride, resized_height, resized_heightStride);
|
||||
if (!((*output)->HasData())) {
|
||||
std::string error = "[ERROR] Fail to get the Output result from memory!";
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
process.device_memory_release();
|
||||
process.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 DvppResizeJpegOp:" + std::string(e.what());
|
||||
RETURN_STATUS_UNEXPECTED(error);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppResizeJpegOp::SetAscendResource(const std::shared_ptr<MDAclProcess> &processor) {
|
||||
processor_ = processor;
|
||||
processor_->SetResizeParas(resized_width_, resized_height_);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DvppResizeJpegOp::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 1 channels
|
||||
if (inputs[0].Rank() == 1) outputs.emplace_back(out);
|
||||
if (!outputs.empty()) return Status::OK();
|
||||
return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape");
|
||||
}
|
||||
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue