parent
b3ecba94c2
commit
9d9d2e1ff4
@ -0,0 +1,111 @@
|
||||
/**
|
||||
* 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 "src/ops/crop_and_resize.h"
|
||||
|
||||
#ifndef PRIMITIVE_WRITEABLE
|
||||
#include "src/ops/ops_register.h"
|
||||
#endif
|
||||
|
||||
namespace mindspore {
|
||||
namespace lite {
|
||||
#ifdef PRIMITIVE_WRITEABLE
|
||||
int CropAndResize::GetMethod() const { return this->primitive_->value.AsCropAndResize()->method; }
|
||||
float CropAndResize::GetExtrapolationValue() const {
|
||||
return this->primitive_->value.AsCropAndResize()->extrapolation_value;
|
||||
}
|
||||
|
||||
void CropAndResize::SetMethod(int method) {
|
||||
this->primitive_->value.AsCropAndResize()->method = (schema::ResizeMethod)method;
|
||||
}
|
||||
void CropAndResize::SetExtrapolationValue(float value) {
|
||||
this->primitive_->value.AsCropAndResize()->extrapolation_value = value;
|
||||
}
|
||||
#else
|
||||
|
||||
int CropAndResize::GetMethod() const { return this->primitive_->value_as_CropAndResize()->method(); }
|
||||
float CropAndResize::GetExtrapolationValue() const {
|
||||
return this->primitive_->value_as_CropAndResize()->extrapolation_value();
|
||||
}
|
||||
int CropAndResize::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) {
|
||||
MS_ASSERT(nullptr != primitive);
|
||||
MS_ASSERT(nullptr != fbb);
|
||||
auto attr = primitive->value_as_CropAndResize();
|
||||
if (attr == nullptr) {
|
||||
MS_LOG(ERROR) << "value_as_CropAndResize return nullptr";
|
||||
return RET_ERROR;
|
||||
}
|
||||
auto val_offset = schema::CreateCropAndResize(*fbb, attr->method(), attr->extrapolation_value());
|
||||
auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_CropAndResize, val_offset.o);
|
||||
fbb->Finish(prim_offset);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
PrimitiveC *CropAndResizeCreator(const schema::Primitive *primitive) {
|
||||
return PrimitiveC::NewPrimitiveC<CropAndResize>(primitive);
|
||||
}
|
||||
Registry CropAndResizeRegistry(schema::PrimitiveType_CropAndResize, CropAndResizeCreator);
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
constexpr int kInputRank = 4;
|
||||
} // namespace
|
||||
int CropAndResize::InferShape(std::vector<lite::Tensor *> inputs_, std::vector<lite::Tensor *> outputs_) {
|
||||
MS_ASSERT(this->primitive_ != nullptr);
|
||||
if (inputs_.size() != 4) {
|
||||
MS_LOG(ERROR) << "Input tensor num should be 4 for crop_an_resize.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
auto input = inputs_.front();
|
||||
if (input == nullptr) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
if (!input->shape().empty() && input->shape().size() != kInputRank) {
|
||||
MS_LOG(ERROR) << "Size of input shape is wrong.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
if (input->format() != schema::Format_NHWC) {
|
||||
MS_LOG(ERROR) << "Crop_an_resize op only support NHWC format.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
auto output = outputs_.front();
|
||||
if (output == nullptr) {
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
output->set_data_type(input->data_type());
|
||||
output->set_format(input->format());
|
||||
if (!infer_flag()) {
|
||||
return RET_INFER_INVALID;
|
||||
}
|
||||
|
||||
std::vector<int> output_shape;
|
||||
auto boxes_tensor = inputs_[1];
|
||||
output_shape.push_back(boxes_tensor->shape()[0]);
|
||||
auto shape_tensor = inputs_[3];
|
||||
auto data = reinterpret_cast<int32_t *>(shape_tensor->data_c());
|
||||
if (data == nullptr) {
|
||||
MS_LOG(INFO) << "The data of 4th input tensor(shape tensor) for crop_an_resize op is nullptr.";
|
||||
return RET_INFER_INVALID;
|
||||
}
|
||||
output_shape.push_back(data[0]);
|
||||
output_shape.push_back(data[1]);
|
||||
output_shape.push_back(input->Channel());
|
||||
output->set_shape(output_shape);
|
||||
return RET_OK;
|
||||
}
|
||||
} // namespace lite
|
||||
} // namespace mindspore
|
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 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 LITE_MINDSPORE_LITE_C_OPS_CROP_AND_RESIZE_H_
|
||||
#define LITE_MINDSPORE_LITE_C_OPS_CROP_AND_RESIZE_H_
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <cmath>
|
||||
|
||||
#include "src/ops/primitive_c.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace lite {
|
||||
class CropAndResize : public PrimitiveC {
|
||||
public:
|
||||
CropAndResize() = default;
|
||||
~CropAndResize() = default;
|
||||
#ifdef PRIMITIVE_WRITEABLE
|
||||
MS_DECLARE_PARENT(CropAndResize, PrimitiveC);
|
||||
explicit CropAndResize(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {}
|
||||
void SetMethod(int method);
|
||||
void SetExtrapolationValue(float value);
|
||||
#else
|
||||
int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override;
|
||||
#endif
|
||||
int InferShape(std::vector<lite::Tensor *> inputs_, std::vector<lite::Tensor *> outputs_) override;
|
||||
int GetMethod() const;
|
||||
float GetExtrapolationValue() const;
|
||||
};
|
||||
} // namespace lite
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // LITE_MINDSPORE_LITE_C_OPS_CROP_AND_RESIZE_H_
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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 "src/ops/crop_and_resize.h"
|
||||
#include "src/ops/primitive_c.h"
|
||||
#include "src/ops/populate/populate_register.h"
|
||||
#include "nnacl/resize_parameter.h"
|
||||
namespace mindspore {
|
||||
namespace lite {
|
||||
OpParameter *PopulateCropAndResizeParameter(const mindspore::lite::PrimitiveC *primitive) {
|
||||
CropAndResizeParameter *crop_resize_param =
|
||||
reinterpret_cast<CropAndResizeParameter *>(malloc(sizeof(CropAndResizeParameter)));
|
||||
if (crop_resize_param == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc CropAndResizeParameter failed.";
|
||||
return nullptr;
|
||||
}
|
||||
memset(crop_resize_param, 0, sizeof(CropAndResizeParameter));
|
||||
crop_resize_param->op_parameter_.type_ = primitive->Type();
|
||||
auto param = reinterpret_cast<mindspore::lite::CropAndResize *>(const_cast<mindspore::lite::PrimitiveC *>(primitive));
|
||||
crop_resize_param->method_ = static_cast<int>(param->GetMethod());
|
||||
crop_resize_param->extrapolation_value_ = param->GetExtrapolationValue();
|
||||
return reinterpret_cast<OpParameter *>(crop_resize_param);
|
||||
}
|
||||
|
||||
Registry CropAndResizeParameterRegistry(schema::PrimitiveType_CropAndResize, PopulateCropAndResizeParameter);
|
||||
} // namespace lite
|
||||
} // namespace mindspore
|
@ -0,0 +1,178 @@
|
||||
/**
|
||||
* 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 "src/runtime/kernel/arm/fp32/crop_and_resize_fp32.h"
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "src/runtime/runtime_api.h"
|
||||
#include "nnacl/fp32/resize_fp32.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kCPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_INVALID_OP_ATTR;
|
||||
using mindspore::lite::RET_NULL_PTR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_CropAndResize;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int CropAndResizeCPUKernel::Init() {
|
||||
if (!InferShapeDone()) {
|
||||
return RET_OK;
|
||||
}
|
||||
return ReSize();
|
||||
}
|
||||
|
||||
int CropAndResizeCPUKernel::ReSize() {
|
||||
new_height_ = out_tensors_.at(0)->shape()[1];
|
||||
new_width_ = out_tensors_.at(0)->shape()[2];
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CropAndResizeCPUKernel::MallocTmpBuffer() {
|
||||
// Malloc buffer to save coordinate.
|
||||
// For mode CROP_AND_RESIZE, different output batches require different cache coordinates.
|
||||
int c = in_tensors_.at(0)->Channel();
|
||||
y_bottoms_ = reinterpret_cast<int *>(context_->allocator->Malloc(sizeof(int) * new_height_ * batch_));
|
||||
if (y_bottoms_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
y_tops_ = reinterpret_cast<int *>(context_->allocator->Malloc(sizeof(int) * new_height_ * batch_));
|
||||
if (y_tops_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
y_bottom_weights_ = reinterpret_cast<float *>(context_->allocator->Malloc(sizeof(float) * new_height_ * batch_));
|
||||
if (y_bottom_weights_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
|
||||
x_lefts_ = reinterpret_cast<int *>(context_->allocator->Malloc(sizeof(int) * new_width_ * batch_));
|
||||
if (x_lefts_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
x_rights_ = reinterpret_cast<int *>(context_->allocator->Malloc(sizeof(int) * new_width_ * batch_));
|
||||
if (x_rights_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
x_left_weights_ = reinterpret_cast<float *>(context_->allocator->Malloc(sizeof(float) * new_width_ * batch_));
|
||||
if (x_left_weights_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
line_buffer_ =
|
||||
reinterpret_cast<float *>(context_->allocator->Malloc(sizeof(float) * new_width_ * c * 2 * context_->thread_num_));
|
||||
if (line_buffer_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc data failed";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void CropAndResizeCPUKernel::FreeTmpBuffer() {
|
||||
context_->allocator->Free(y_bottoms_);
|
||||
context_->allocator->Free(y_tops_);
|
||||
context_->allocator->Free(y_bottom_weights_);
|
||||
context_->allocator->Free(x_lefts_);
|
||||
context_->allocator->Free(x_rights_);
|
||||
context_->allocator->Free(x_left_weights_);
|
||||
context_->allocator->Free(line_buffer_);
|
||||
}
|
||||
|
||||
int CropAndResizeImpl(void *cdata, int task_id) {
|
||||
auto resize = reinterpret_cast<CropAndResizeCPUKernel *>(cdata);
|
||||
auto error_code = resize->RunImpl(task_id);
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "CropAndResize Run error task_id[" << task_id << "] error_code[" << error_code << "]";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CropAndResizeCPUKernel::RunImpl(int task_id) {
|
||||
auto input = in_tensors_.at(0);
|
||||
auto input_data = reinterpret_cast<float *>(input->data_c());
|
||||
if (input_data == nullptr) {
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
auto output_data = reinterpret_cast<float *>(out_tensors_.at(0)->data_c());
|
||||
if (output_data == nullptr) {
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
auto input_shape = input->shape();
|
||||
int unit = UP_DIV(new_height_, context_->thread_num_);
|
||||
int h_begin = unit * task_id;
|
||||
int h_end = MSMIN(h_begin + unit, new_height_);
|
||||
int c = in_tensors_.at(0)->shape().at(3);
|
||||
float *line0 = line_buffer_ + new_width_ * c * 2 * task_id;
|
||||
float *line1 = line0 + new_width_ * c;
|
||||
int ret = 0;
|
||||
if (is_crop_) {
|
||||
ret = CropAndResizeBilinear(input_data, output_data, input_shape.data(), out_tensors_.at(0)->shape().data(),
|
||||
y_bottoms_, y_tops_, x_lefts_, x_rights_, y_bottom_weights_, x_left_weights_, line0,
|
||||
line1, h_begin, h_end);
|
||||
} else {
|
||||
ret =
|
||||
ResizeBilinear(input_data, output_data, input_shape.data(), out_tensors_.at(0)->shape().data(), y_bottoms_,
|
||||
y_tops_, x_lefts_, x_rights_, y_bottom_weights_, x_left_weights_, line0, line1, h_begin, h_end);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int CropAndResizeCPUKernel::Run() {
|
||||
auto ret = MallocTmpBuffer();
|
||||
if (ret != RET_OK) {
|
||||
FreeTmpBuffer();
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto input = in_tensors_.at(0);
|
||||
auto input_shape = input->shape();
|
||||
// if boxes tensor data is nullptr, crop_and_resize can be seen as resize with coordinate transformation mode
|
||||
// ALIGN_CORNERS
|
||||
if (in_tensors_.at(1)->ElementsNum() == 0 || in_tensors_.at(1)->data_c() == nullptr) {
|
||||
batch_ = 1;
|
||||
is_crop_ = false;
|
||||
ret = PrepareResizeBilinear(input_shape.data(), out_tensors_.at(0)->shape().data(), CalculateAlignCorners,
|
||||
y_bottoms_, y_tops_, x_lefts_, x_rights_, y_bottom_weights_, x_left_weights_);
|
||||
} else {
|
||||
batch_ = out_tensors_[0]->Batch();
|
||||
auto boxes = reinterpret_cast<float *>(in_tensors_.at(1)->data_c());
|
||||
auto box_idx = reinterpret_cast<int32_t *>(in_tensors_.at(2)->data_c());
|
||||
ret = PrepareCropAndResizeBilinear(input_shape.data(), boxes, box_idx, out_tensors_.at(0)->shape().data(),
|
||||
y_bottoms_, y_tops_, x_lefts_, x_rights_, y_bottom_weights_, x_left_weights_);
|
||||
}
|
||||
if (ret != RET_OK) {
|
||||
FreeTmpBuffer();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int error_code = ParallelLaunch(this->context_->thread_pool_, CropAndResizeImpl, this, context_->thread_num_);
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "CropAndResize run error, error_code[" << error_code << "]";
|
||||
FreeTmpBuffer();
|
||||
return RET_ERROR;
|
||||
}
|
||||
FreeTmpBuffer();
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_CropAndResize, LiteKernelCreator<CropAndResizeCPUKernel>)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 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_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_CROP_AND_RESIZE_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_CROP_AND_RESIZE_H_
|
||||
|
||||
#include <vector>
|
||||
#include "include/errorcode.h"
|
||||
#include "nnacl/resize_parameter.h"
|
||||
#include "src/lite_kernel.h"
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class CropAndResizeCPUKernel : public LiteKernel {
|
||||
public:
|
||||
CropAndResizeCPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx,
|
||||
const mindspore::lite::PrimitiveC *primitive)
|
||||
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {
|
||||
param_ = reinterpret_cast<CropAndResizeParameter *>(op_parameter_);
|
||||
}
|
||||
|
||||
~CropAndResizeCPUKernel() = default;
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
int RunImpl(int task_id);
|
||||
|
||||
protected:
|
||||
int MallocTmpBuffer();
|
||||
void FreeTmpBuffer();
|
||||
|
||||
CropAndResizeParameter *param_;
|
||||
int batch_;
|
||||
int new_height_;
|
||||
int new_width_;
|
||||
bool is_crop_ = true;
|
||||
int *y_tops_ = nullptr;
|
||||
int *y_bottoms_ = nullptr;
|
||||
int *x_lefts_ = nullptr;
|
||||
int *x_rights_ = nullptr;
|
||||
float *y_bottom_weights_ = nullptr;
|
||||
float *x_left_weights_ = nullptr;
|
||||
float *line_buffer_ = nullptr;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_CROP_AND_RESIZE_H_
|
Loading…
Reference in new issue