提交 509d29ec 编写于 作者: 刘琦

Merge branch 'resize' into 'master'

Change resize bilinear output size from tensor to attributes

See merge request !150
......@@ -13,7 +13,7 @@ namespace kernels {
template <typename T>
void ResizeBilinearFunctor<DeviceType::OPENCL, T>::operator()(
const Tensor *input, const Tensor *resize_dims, Tensor *output) {
const Tensor *input, Tensor *output) {
const index_t batch = input->dim(0);
const index_t in_height = input->dim(1);
const index_t in_width = input->dim(2);
......@@ -21,9 +21,8 @@ void ResizeBilinearFunctor<DeviceType::OPENCL, T>::operator()(
const index_t channel_blocks = RoundUpDiv4(channels);
index_t out_height;
index_t out_width;
GetOutputSize(resize_dims, &out_height, &out_width);
index_t out_height = out_height_;
index_t out_width = out_width_;
MACE_CHECK(out_height > 0 && out_width > 0);
std::vector<index_t> output_shape {batch, out_height, out_width, channels};
if (input->is_image()) {
......
......@@ -105,26 +105,16 @@ void ResizeImage(const T *images,
struct ResizeBilinearFunctorBase {
ResizeBilinearFunctorBase(const std::vector<index_t> &size,
bool align_corners)
: align_corners_(align_corners), size_(size) {}
: align_corners_(align_corners) {
MACE_CHECK(size.size() == 2);
out_height_ = size[0];
out_width_ = size[1];
}
protected:
void GetOutputSize(const Tensor *resize_dims,
index_t *out_height,
index_t *out_width) {
if (size_[0] < 0 || size_[1] < 0) {
MACE_CHECK(resize_dims != nullptr && resize_dims->dim_size() == 1);
Tensor::MappingGuard resize_dims_mapper(resize_dims);
auto dims_data = resize_dims->data<int32_t>();
*out_height = dims_data[0];
*out_width = dims_data[1];
} else {
*out_height = size_[0];
*out_width = size_[1];
}
}
bool align_corners_;
std::vector<index_t> size_;
index_t out_height_;
index_t out_width_;
};
template <DeviceType D, typename T>
......@@ -132,17 +122,14 @@ struct ResizeBilinearFunctor : ResizeBilinearFunctorBase {
ResizeBilinearFunctor(const std::vector<index_t> &size, bool align_corners)
: ResizeBilinearFunctorBase(size, align_corners) {}
void operator()(const Tensor *input,
const Tensor *resize_dims,
Tensor *output) {
void operator()(const Tensor *input, Tensor *output) {
const index_t batch = input->dim(0);
const index_t in_height = input->dim(1);
const index_t in_width = input->dim(2);
const index_t channels = input->dim(3);
index_t out_height;
index_t out_width;
GetOutputSize(resize_dims, &out_height, &out_width);
index_t out_height = out_height_;
index_t out_width = out_width_;
MACE_CHECK(out_height > 0 && out_width > 0);
std::vector<index_t> out_shape{batch, out_height, out_width, channels};
output->Resize(out_shape);
......@@ -180,9 +167,7 @@ struct ResizeBilinearFunctor<DeviceType::OPENCL, T> : ResizeBilinearFunctorBase
ResizeBilinearFunctor(const std::vector<index_t> &size, bool align_corners)
: ResizeBilinearFunctorBase(size, align_corners) {}
void operator()(const Tensor *input,
const Tensor *resize_dims,
Tensor *output);
void operator()(const Tensor *input, Tensor *output);
};
} // namespace kernels
......
......@@ -21,15 +21,12 @@ class ResizeBilinearOp : public Operator<D, T> {
bool Run() override {
const Tensor *input = this->Input(0);
const Tensor *resize_dims = this->Input(1);
Tensor *output = this->Output(0);
MACE_CHECK(input->dim_size() == 4, "input must be 4-dimensional.",
input->dim_size());
MACE_CHECK(resize_dims->dim_size() == 1,
"resize dim must be 2-dimensional.", resize_dims->dim_size());
functor_(input, resize_dims, output);
functor_(input, output);
return true;
}
......
......@@ -16,15 +16,14 @@ TEST_F(ResizeBilinearTest, CPUResizeBilinearWOAlignCorners) {
auto &net = test_net();
OpDefBuilder("ResizeBilinear", "ResizeBilinearTest")
.Input("Input")
.Input("OutSize")
.Output("Output")
.AddIntsArg("size", {1, 2})
.Finalize(net.NewOperatorDef());
// Add input data
vector<float> input(24);
std::iota(begin(input), end(input), 0);
net.AddInputFromArray<DeviceType::CPU, float>("Input", {1, 2, 4, 3}, input);
net.AddInputFromArray<DeviceType::CPU, int>("OutSize", {2}, {1, 2});
// Run
net.RunOp();
......@@ -41,16 +40,15 @@ TEST_F(ResizeBilinearTest, ResizeBilinearWAlignCorners) {
auto &net = test_net();
OpDefBuilder("ResizeBilinear", "ResizeBilinearTest")
.Input("Input")
.Input("OutSize")
.Output("Output")
.AddIntArg("align_corners", 1)
.AddIntsArg("size", {1, 2})
.Finalize(net.NewOperatorDef());
// Add input data
vector<float> input(24);
std::iota(begin(input), end(input), 0);
net.AddInputFromArray<DeviceType::CPU, float>("Input", {1, 2, 4, 3}, input);
net.AddInputFromArray<DeviceType::CPU, int>("OutSize", {2}, {1, 2});
// Run
net.RunOp();
......@@ -80,11 +78,9 @@ void TestRandomResizeBilinear() {
// Add input data
net.AddRandomInput<D, float>("Input",
{batch, in_height, in_width, channels});
net.AddInputFromArray<D, int>("OutSize", {2}, {height, width});
OpDefBuilder("ResizeBilinear", "ResizeBilinearTest")
.Input("Input")
.Input("OutSize")
.Output("Output")
.AddIntArg("align_corners", align_corners)
.AddIntsArg("size", {height, width})
......@@ -99,7 +95,6 @@ void TestRandomResizeBilinear() {
OpDefBuilder("ResizeBilinear", "ResizeBilinearTest")
.Input("InputImage")
.Input("OutSize")
.Output("OutputImage")
.AddIntArg("align_corners", align_corners)
.AddIntsArg("size", {height, width})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册