addressing comments

pull/2532/head
avakh 5 years ago
parent d7a312d078
commit 587e260236

@ -63,12 +63,14 @@
#include "dataset/kernels/image/random_horizontal_flip_bbox_op.h"
#include "dataset/kernels/image/random_horizontal_flip_op.h"
#include "dataset/kernels/image/random_resize_op.h"
#include "dataset/kernels/image/random_resize_with_bbox_op.h"
#include "dataset/kernels/image/random_rotation_op.h"
#include "dataset/kernels/image/random_vertical_flip_op.h"
#include "dataset/kernels/image/random_vertical_flip_with_bbox_op.h"
#include "dataset/kernels/image/rescale_op.h"
#include "dataset/kernels/image/resize_bilinear_op.h"
#include "dataset/kernels/image/resize_op.h"
#include "dataset/kernels/image/resize_with_bbox_op.h"
#include "dataset/kernels/image/uniform_aug_op.h"
#include "dataset/kernels/no_op.h"
#include "dataset/text/kernels/jieba_tokenizer_op.h"
@ -348,6 +350,18 @@ void bindTensorOps1(py::module *m) {
.def(py::init<int32_t, int32_t, InterpolationMode>(), py::arg("targetHeight"),
py::arg("targetWidth") = ResizeOp::kDefWidth, py::arg("interpolation") = ResizeOp::kDefInterpolation);
(void)py::class_<ResizeWithBBoxOp, TensorOp, std::shared_ptr<ResizeWithBBoxOp>>(
*m, "ResizeWithBBoxOp", "Tensor operation to resize an image. Takes height, width and mode.")
.def(py::init<int32_t, int32_t, InterpolationMode>(), py::arg("targetHeight"),
py::arg("targetWidth") = ResizeWithBBoxOp::kDefWidth,
py::arg("interpolation") = ResizeWithBBoxOp::kDefInterpolation);
(void)py::class_<RandomResizeWithBBoxOp, TensorOp, std::shared_ptr<RandomResizeWithBBoxOp>>(
*m, "RandomResizeWithBBoxOp",
"Tensor operation to resize an image using a randomly selected interpolation. Takes height and width.")
.def(py::init<int32_t, int32_t>(), py::arg("targetHeight"),
py::arg("targetWidth") = RandomResizeWithBBoxOp::kDefTargetWidth);
(void)py::class_<UniformAugOp, TensorOp, std::shared_ptr<UniformAugOp>>(
*m, "UniformAugOp", "Tensor operation to apply random augmentation(s).")
.def(py::init<std::vector<std::shared_ptr<TensorOp>>, int32_t>(), py::arg("operations"),

@ -25,4 +25,6 @@ add_library(kernels-image OBJECT
resize_bilinear_op.cc
resize_op.cc
uniform_aug_op.cc
resize_with_bbox_op.cc
random_resize_with_bbox_op.cc
)

@ -0,0 +1,33 @@
/**
* Copyright 2020 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 "dataset/kernels/image/random_resize_with_bbox_op.h"
#include "dataset/kernels/image/resize_with_bbox_op.h"
#include "dataset/util/status.h"
namespace mindspore {
namespace dataset {
const int32_t RandomResizeWithBBoxOp::kDefTargetWidth = 0;
Status RandomResizeWithBBoxOp::Compute(const TensorRow &input, TensorRow *output) {
// Randomly selects from the following four interpolation methods
// 0-bilinear, 1-nearest_neighbor, 2-bicubic, 3-area
interpolation_ = static_cast<InterpolationMode>(distribution_(random_generator_));
RETURN_IF_NOT_OK(ResizeWithBBoxOp::Compute(input, output));
return Status::OK();
}
} // namespace dataset
} // namespace mindspore

@ -0,0 +1,56 @@
/**
* Copyright 2020 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 DATASET_KERNELS_IMAGE_RANDOM_RESIZE_WITH_BBOX_OP_H
#define DATASET_KERNELS_IMAGE_RANDOM_RESIZE_WITH_BBOX_OP_H
#include <memory>
#include <random>
#include "dataset/core/tensor.h"
#include "dataset/kernels/image/resize_op.h"
#include "dataset/kernels/image/resize_with_bbox_op.h"
#include "dataset/kernels/tensor_op.h"
#include "dataset/util/random.h"
#include "dataset/util/status.h"
namespace mindspore {
namespace dataset {
class RandomResizeWithBBoxOp : public ResizeWithBBoxOp {
public:
// Default values, also used by python_bindings.cc
static const int32_t kDefTargetWidth;
explicit RandomResizeWithBBoxOp(int32_t size_1, int32_t size_2 = kDefTargetWidth) : ResizeWithBBoxOp(size_1, size_2) {
random_generator_.seed(GetSeed());
}
~RandomResizeWithBBoxOp() = default;
// Description: A function that prints info about the node
void Print(std::ostream &out) const override {
out << "RandomResizeWithBBoxOp: " << ResizeWithBBoxOp::size1_ << " " << ResizeWithBBoxOp::size2_;
}
Status Compute(const TensorRow &input, TensorRow *output) override;
private:
std::mt19937 random_generator_;
std::uniform_int_distribution<int> distribution_{0, 3};
};
} // namespace dataset
} // namespace mindspore
#endif // DATASET_KERNELS_IMAGE_RANDOM_RESIZE_WITH_BBOX_OP_H

@ -0,0 +1,53 @@
/**
* Copyright 2020 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 "dataset/kernels/image/resize_with_bbox_op.h"
#include <utility>
#include <memory>
#include "dataset/kernels/image/resize_op.h"
#include "dataset/kernels/image/image_utils.h"
#include "dataset/core/cv_tensor.h"
#include "dataset/core/pybind_support.h"
#include "dataset/core/tensor.h"
#include "dataset/kernels/tensor_op.h"
#include "dataset/util/status.h"
namespace mindspore {
namespace dataset {
Status ResizeWithBBoxOp::Compute(const TensorRow &input, TensorRow *output) {
IO_CHECK_VECTOR(input, output);
BOUNDING_BOX_CHECK(input);
int32_t input_h = input[0]->shape()[0];
int32_t input_w = input[0]->shape()[1];
output->resize(2);
(*output)[1] = std::move(input[1]); // move boxes over to output
std::shared_ptr<CVTensor> input_cv = CVTensor::AsCVTensor(std::move(input[0]));
RETURN_IF_NOT_OK(ResizeOp::Compute(std::static_pointer_cast<Tensor>(input_cv), &(*output)[0]));
int32_t output_h = (*output)[0]->shape()[0]; // output height if ResizeWithBBox
int32_t output_w = (*output)[0]->shape()[1]; // output width if ResizeWithBBox
size_t bboxCount = input[1]->shape()[0]; // number of rows in bbox tensor
RETURN_IF_NOT_OK(UpdateBBoxesForResize((*output)[1], bboxCount, output_w, output_h, input_w, input_h));
return Status::OK();
}
} // namespace dataset
} // namespace mindspore

@ -0,0 +1,43 @@
/**
* Copyright 2020 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 DATASET_KERNELS_IMAGE_RESIZE_WITH_BBOX_OP_H
#define DATASET_KERNELS_IMAGE_RESIZE_WITH_BBOX_OP_H
#include "dataset/core/tensor.h"
#include "dataset/kernels/image/image_utils.h"
#include "dataset/kernels/tensor_op.h"
#include "dataset/util/status.h"
#include "dataset/kernels/image/resize_op.h"
namespace mindspore {
namespace dataset {
class ResizeWithBBoxOp : public ResizeOp {
public:
// Constructor for ResizeWithBBoxOp, with default value and passing to base class constructor
explicit ResizeWithBBoxOp(int32_t size_1, int32_t size_2 = kDefWidth,
InterpolationMode mInterpolation = kDefInterpolation)
: ResizeOp(size_1, size_2, mInterpolation) {}
~ResizeWithBBoxOp() override = default;
void Print(std::ostream &out) const override { out << "ResizeWithBBoxOp: " << size1_ << " " << size2_; }
Status Compute(const TensorRow &input, TensorRow *output) override;
};
} // namespace dataset
} // namespace mindspore
#endif // DATASET_KERNELS_IMAGE_RESIZE_WITH_BBOX_OP_H

@ -265,6 +265,7 @@ class BoundingBoxAugment(cde.BoundingBoxAugmentOp):
ratio (float, optional): Ratio of bounding boxes to apply augmentation on.
Range: [0,1] (default=0.3).
"""
@check_bounding_box_augment_cpp
def __init__(self, transform, ratio=0.3):
self.ratio = ratio
@ -302,6 +303,36 @@ class Resize(cde.ResizeOp):
super().__init__(*size, interpoltn)
class ResizeWithBBox(cde.ResizeWithBBoxOp):
"""
Resize the input image to the given size and adjust the bounding boxes accordingly.
Args:
size (int or sequence): The output size of the resized image.
If size is an int, smaller edge of the image will be resized to this value with
the same image aspect ratio.
If size is a sequence of length 2, it should be (height, width).
interpolation (Inter mode, optional): Image interpolation mode (default=Inter.LINEAR).
It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC].
- Inter.LINEAR, means interpolation method is bilinear interpolation.
- Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.
- Inter.BICUBIC, means interpolation method is bicubic interpolation.
"""
@check_resize_interpolation
def __init__(self, size, interpolation=Inter.LINEAR):
self.size = size
self.interpolation = interpolation
interpoltn = DE_C_INTER_MODE[interpolation]
if isinstance(size, int):
super().__init__(size, interpolation=interpoltn)
else:
super().__init__(*size, interpoltn)
class RandomResizedCropWithBBox(cde.RandomCropAndResizeWithBBoxOp):
"""
Crop the input image to a random size and aspect ratio and adjust the Bounding Boxes accordingly
@ -326,6 +357,7 @@ class RandomResizedCropWithBBox(cde.RandomCropAndResizeWithBBoxOp):
max_attempts (int, optional): The maximum number of attempts to propose a valid
crop_area (default=10). If exceeded, fall back to use center_crop instead.
"""
@check_random_resize_crop
def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.),
interpolation=Inter.BILINEAR, max_attempts=10):
@ -499,6 +531,27 @@ class RandomResize(cde.RandomResizeOp):
super().__init__(*size)
class RandomResizeWithBBox(cde.RandomResizeWithBBoxOp):
"""
Tensor operation to resize the input image using a randomly selected interpolation mode and adjust
the bounding boxes accordingly.
Args:
size (int or sequence): The output size of the resized image.
If size is an int, smaller edge of the image will be resized to this value with
the same image aspect ratio.
If size is a sequence of length 2, it should be (height, width).
"""
@check_resize
def __init__(self, size):
self.size = size
if isinstance(size, int):
super().__init__(size)
else:
super().__init__(*size)
class HWC2CHW(cde.ChannelSwapOp):
"""
Transpose the input image; shape (H, W, C) to shape (C, H, W).

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save