diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/bounding_box_augment_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/bounding_box_augment_op.cc index 97c68a82343..81993d7c6ed 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/bounding_box_augment_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/bounding_box_augment_op.cc @@ -45,17 +45,21 @@ Status BoundingBoxAugmentOp::Compute(const TensorRow &input, TensorRow *output) RETURN_IF_NOT_OK(Crop(input_restore, &crop_out, static_cast(bbox->x()), static_cast(bbox->y()), static_cast(bbox->width()), static_cast(bbox->height()))); // transform the cropped bbox region - RETURN_IF_NOT_OK(transform_->Compute(crop_out, &res_out)); + TensorRow crop_out_row; + TensorRow res_out_row; + crop_out_row.push_back(crop_out); + res_out_row.push_back(res_out); + RETURN_IF_NOT_OK(transform_->Compute(crop_out_row, &res_out_row)); // place the transformed region back in the restored input - std::shared_ptr res_img = CVTensor::AsCVTensor(res_out); + std::shared_ptr res_img = CVTensor::AsCVTensor(res_out_row[0]); // check if transformed crop is out of bounds of the box if (res_img->mat().cols > bbox->width() || res_img->mat().rows > bbox->height() || res_img->mat().cols < bbox->width() || res_img->mat().rows < bbox->height()) { // if so, resize to fit in the box std::shared_ptr resize_op = std::make_shared(static_cast(bbox->height()), static_cast(bbox->width())); - RETURN_IF_NOT_OK(resize_op->Compute(std::static_pointer_cast(res_img), &res_out)); - res_img = CVTensor::AsCVTensor(res_out); + RETURN_IF_NOT_OK(resize_op->Compute(std::static_pointer_cast(res_img), &res_out_row[0])); + res_img = CVTensor::AsCVTensor(res_out_row[0]); } res_img->mat().copyTo( input_restore->mat()(cv::Rect(bbox->x(), bbox->y(), res_img->mat().cols, res_img->mat().rows))); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.cc index f705fefaaf6..177b9adb36d 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -14,6 +14,7 @@ * limitations under the License. */ #include "minddata/dataset/kernels/image/random_crop_and_resize_op.h" +#include #include #include "minddata/dataset/kernels/image/image_utils.h" @@ -44,19 +45,28 @@ RandomCropAndResizeOp::RandomCropAndResizeOp(int32_t target_height, int32_t targ is_deterministic_ = false; } -Status RandomCropAndResizeOp::Compute(const std::shared_ptr &input, std::shared_ptr *output) { - IO_CHECK(input, output); - CHECK_FAIL_RETURN_UNEXPECTED(input->shape().Size() >= 2, "RandomCropAndResize: the image is not or "); - - int h_in = input->shape()[0]; - int w_in = input->shape()[1]; +Status RandomCropAndResizeOp::Compute(const TensorRow &input, TensorRow *output) { + IO_CHECK_VECTOR(input, output); + const int output_count = input.size(); + output->resize(output_count); int x = 0; int y = 0; int crop_height = 0; int crop_width = 0; - (void)GetCropBox(h_in, w_in, &x, &y, &crop_height, &crop_width); - return CropAndResize(input, output, x, y, crop_height, crop_width, target_height_, target_width_, interpolation_); + for (size_t i = 0; i < input.size(); i++) { + CHECK_FAIL_RETURN_UNEXPECTED(input[i]->shape().Size() >= 2, + "RandomCropAndResize: the image is not or "); + int h_in = input[i]->shape()[0]; + int w_in = input[i]->shape()[1]; + if (i == 0) { + (void)GetCropBox(h_in, w_in, &x, &y, &crop_height, &crop_width); + } + RETURN_IF_NOT_OK(CropAndResize(input[i], &(*output)[i], x, y, crop_height, crop_width, target_height_, + target_width_, interpolation_)); + } + return Status::OK(); } + Status RandomCropAndResizeOp::OutputShape(const std::vector &inputs, std::vector &outputs) { RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs)); outputs.clear(); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.h index d8a5815c9d9..536aa5b9aa2 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_and_resize_op.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -54,13 +54,18 @@ class RandomCropAndResizeOp : public TensorOp { out << "RandomCropAndResize: " << target_height_ << " " << target_width_; } - Status Compute(const std::shared_ptr &input, std::shared_ptr *output) override; + Status Compute(const TensorRow &input, TensorRow *output) override; + Status OutputShape(const std::vector &inputs, std::vector &outputs) override; Status GetCropBox(int h_in, int w_in, int *x, int *y, int *crop_height, int *crop_width); std::string Name() const override { return kRandomCropAndResizeOp; } + uint32_t NumInput() override { return -1; } + + uint32_t NumOutput() override { return -1; } + protected: int32_t target_height_; int32_t target_width_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.cc index f76c795f547..e1714f03bea 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -27,30 +27,37 @@ RandomCropDecodeResizeOp::RandomCropDecodeResizeOp(int32_t target_height, int32_ : RandomCropAndResizeOp(target_height, target_width, scale_lb, scale_ub, aspect_lb, aspect_ub, interpolation, max_attempts) {} -Status RandomCropDecodeResizeOp::Compute(const std::shared_ptr &input, std::shared_ptr *output) { - if (input == nullptr) { - RETURN_STATUS_UNEXPECTED("RandomCropDecodeResize: input image is empty."); - } - if (!IsNonEmptyJPEG(input)) { - DecodeOp op(true); - std::shared_ptr decoded; - RETURN_IF_NOT_OK(op.Compute(input, &decoded)); - return RandomCropAndResizeOp::Compute(decoded, output); - } else { - int h_in = 0; - int w_in = 0; - RETURN_IF_NOT_OK(GetJpegImageInfo(input, &w_in, &h_in)); - - int x = 0; - int y = 0; - int crop_height = 0; - int crop_width = 0; - (void)GetCropBox(h_in, w_in, &x, &y, &crop_height, &crop_width); - - std::shared_ptr decoded; - RETURN_IF_NOT_OK(JpegCropAndDecode(input, &decoded, x, y, crop_width, crop_height)); - return Resize(decoded, output, target_height_, target_width_, 0.0, 0.0, interpolation_); +Status RandomCropDecodeResizeOp::Compute(const TensorRow &input, TensorRow *output) { + IO_CHECK_VECTOR(input, output); + const int output_count = input.size(); + output->resize(output_count); + int x = 0; + int y = 0; + int crop_height = 0; + int crop_width = 0; + TensorRow decoded; + decoded.resize(output_count); + for (size_t i = 0; i < input.size(); i++) { + if (input[i] == nullptr) { + RETURN_STATUS_UNEXPECTED("RandomCropDecodeResize: input image is empty."); + } + if (!IsNonEmptyJPEG(input[i])) { + DecodeOp op(true); + RETURN_IF_NOT_OK(op.Compute(input[i], &decoded[i])); + RETURN_IF_NOT_OK(RandomCropAndResizeOp::Compute(decoded, output)); + } else { + int h_in = 0; + int w_in = 0; + RETURN_IF_NOT_OK(GetJpegImageInfo(input[i], &w_in, &h_in)); + if (i == 0) { + (void)GetCropBox(h_in, w_in, &x, &y, &crop_height, &crop_width); + } + std::shared_ptr decoded_tensor = nullptr; + RETURN_IF_NOT_OK(JpegCropAndDecode(input[i], &decoded_tensor, x, y, crop_width, crop_height)); + RETURN_IF_NOT_OK(Resize(decoded_tensor, &(*output)[i], target_height_, target_width_, 0.0, 0.0, interpolation_)); + } } + return Status::OK(); } } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.h index 781ac2af193..8fcfcf7311e 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_decode_resize_op.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -43,7 +43,7 @@ class RandomCropDecodeResizeOp : public RandomCropAndResizeOp { out << Name() << ": " << RandomCropAndResizeOp::target_height_ << " " << RandomCropAndResizeOp::target_width_; } - Status Compute(const std::shared_ptr &input, std::shared_ptr *output) override; + Status Compute(const TensorRow &input, TensorRow *output) override; std::string Name() const override { return kRandomCropDecodeResizeOp; } }; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc index 61b39e7add6..ea49bc43623 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -112,35 +112,38 @@ void RandomCropOp::GenRandomXY(int *x, int *y, const int32_t &padded_image_w, co *y = std::uniform_int_distribution(0, padded_image_h - crop_height_)(rnd_); } -Status RandomCropOp::Compute(const std::shared_ptr &input, std::shared_ptr *output) { - IO_CHECK(input, output); - - if (input->Rank() != 3 && input->Rank() != 2) { - RETURN_STATUS_UNEXPECTED("RandomCrop: image shape is not or ."); - } - - // Apply padding first then crop - std::shared_ptr pad_image; - int32_t t_pad_top = 0; - int32_t t_pad_bottom = 0; - int32_t t_pad_left = 0; - int32_t t_pad_right = 0; - int32_t padded_image_w = 0; - int32_t padded_image_h = 0; - bool crop_further = true; // whether image needs further cropping based on new size & requirements - - RETURN_IF_NOT_OK( // error code sent back directly - ImagePadding(input, &pad_image, &t_pad_top, &t_pad_bottom, &t_pad_left, &t_pad_right, &padded_image_w, - &padded_image_h, &crop_further)); - if (!crop_further) { - *output = pad_image; - return Status::OK(); - } - +Status RandomCropOp::Compute(const TensorRow &input, TensorRow *output) { + IO_CHECK_VECTOR(input, output); int x = 0; int y = 0; - GenRandomXY(&x, &y, padded_image_w, padded_image_h); - return Crop(pad_image, output, x, y, crop_width_, crop_height_); + const int output_count = input.size(); + output->resize(output_count); + for (size_t i = 0; i < input.size(); i++) { + if (input[i]->Rank() != 3 && input[i]->Rank() != 2) { + RETURN_STATUS_UNEXPECTED("RandomCrop: image shape is not or ."); + } + std::shared_ptr pad_image = nullptr; + int32_t t_pad_top = 0; + int32_t t_pad_bottom = 0; + int32_t t_pad_left = 0; + int32_t t_pad_right = 0; + int32_t padded_image_w = 0; + int32_t padded_image_h = 0; + bool crop_further = true; // whether image needs further cropping based on new size & requirements + + RETURN_IF_NOT_OK( // error code sent back directly + ImagePadding(input[i], &pad_image, &t_pad_top, &t_pad_bottom, &t_pad_left, &t_pad_right, &padded_image_w, + &padded_image_h, &crop_further)); + if (!crop_further) { + (*output)[i] = pad_image; + return Status::OK(); + } + if (i == 0) { + GenRandomXY(&x, &y, padded_image_w, padded_image_h); + } + RETURN_IF_NOT_OK(Crop(pad_image, &(*output)[i], x, y, crop_width_, crop_height_)); + } + return Status::OK(); } Status RandomCropOp::OutputShape(const std::vector &inputs, std::vector &outputs) { diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.h index 4e832cd10e4..b815bc207e0 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -54,7 +54,7 @@ class RandomCropOp : public TensorOp { void Print(std::ostream &out) const override { out << Name() << ": " << crop_height_ << " " << crop_width_; } - Status Compute(const std::shared_ptr &input, std::shared_ptr *output) override; + Status Compute(const TensorRow &input, TensorRow *output) override; // Function breaks out the compute function's image padding functionality and makes available to other Ops // Using this class as a base - re-structured to allow for RandomCropWithBBox Augmentation Op @@ -79,6 +79,10 @@ class RandomCropOp : public TensorOp { std::string Name() const override { return kRandomCropOp; } + uint32_t NumInput() override { return -1; } + + uint32_t NumOutput() override { return -1; } + protected: int32_t crop_height_ = 0; int32_t crop_width_ = 0; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.cc index 5e8ab8a6347..d682cf15236 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -22,10 +22,15 @@ namespace mindspore { namespace dataset { const float RandomHorizontalFlipOp::kDefProbability = 0.5; -Status RandomHorizontalFlipOp::Compute(const std::shared_ptr &input, std::shared_ptr *output) { - IO_CHECK(input, output); +Status RandomHorizontalFlipOp::Compute(const TensorRow &input, TensorRow *output) { + IO_CHECK_VECTOR(input, output); + const int output_count = input.size(); + output->resize(output_count); if (distribution_(rnd_)) { - return HorizontalFlip(input, output); + for (size_t i = 0; i < input.size(); i++) { + RETURN_IF_NOT_OK(HorizontalFlip(input[i], &(*output)[i])); + } + return Status::OK(); } *output = input; return Status::OK(); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.h index b52c8b1fba3..cdbe82ae9da 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_horizontal_flip_op.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -45,10 +45,14 @@ class RandomHorizontalFlipOp : public TensorOp { return out; } - Status Compute(const std::shared_ptr &input, std::shared_ptr *output) override; + Status Compute(const TensorRow &input, TensorRow *output) override; std::string Name() const override { return kRandomHorizontalFlipOp; } + uint32_t NumInput() override { return -1; } + + uint32_t NumOutput() override { return -1; } + private: std::mt19937 rnd_; std::bernoulli_distribution distribution_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.cc index 989fe2605a4..f03b680cd6e 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -25,11 +25,16 @@ namespace mindspore { namespace dataset { const int32_t RandomResizeOp::kDefTargetWidth = 0; -Status RandomResizeOp::Compute(const std::shared_ptr &input, std::shared_ptr *output) { - // Randomly selects from the following four interpolation methods - // 0-bilinear, 1-nearest_neighbor, 2-bicubic, 3-area - interpolation_ = static_cast(distribution_(random_generator_)); - return ResizeOp::Compute(input, output); +Status RandomResizeOp::Compute(const TensorRow &input, TensorRow *output) { + IO_CHECK_VECTOR(input, output); + const int output_count = input.size(); + output->resize(output_count); + InterpolationMode interpolation_random_resize = static_cast(distribution_(random_generator_)); + std::shared_ptr resize_op = std::make_shared(size1_, size2_, interpolation_random_resize); + for (size_t i = 0; i < input.size(); i++) { + RETURN_IF_NOT_OK(resize_op->Compute(input[i], &(*output)[i])); + } + return Status::OK(); } } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.h index 4451eb820cd..90e5adb028f 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_resize_op.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -43,10 +43,14 @@ class RandomResizeOp : public ResizeOp { // Description: A function that prints info about the node void Print(std::ostream &out) const override { out << Name() << ": " << ResizeOp::size1_ << " " << ResizeOp::size2_; } - Status Compute(const std::shared_ptr &input, std::shared_ptr *output) override; + Status Compute(const TensorRow &input, TensorRow *output) override; std::string Name() const override { return kRandomResizeOp; } + uint32_t NumInput() override { return -1; } + + uint32_t NumOutput() override { return -1; } + private: std::mt19937 random_generator_; std::uniform_int_distribution distribution_{0, 3}; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.cc index 24d816ef1a0..36d4bb4ef67 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -23,10 +23,15 @@ namespace mindspore { namespace dataset { const float RandomVerticalFlipOp::kDefProbability = 0.5; -Status RandomVerticalFlipOp::Compute(const std::shared_ptr &input, std::shared_ptr *output) { - IO_CHECK(input, output); +Status RandomVerticalFlipOp::Compute(const TensorRow &input, TensorRow *output) { + IO_CHECK_VECTOR(input, output); + const int output_count = input.size(); + output->resize(output_count); if (distribution_(rnd_)) { - return VerticalFlip(input, output); + for (size_t i = 0; i < input.size(); i++) { + RETURN_IF_NOT_OK(VerticalFlip(input[i], &(*output)[i])); + } + return Status::OK(); } *output = input; return Status::OK(); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.h index 26a965a7cd4..c66d24a9b27 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_vertical_flip_op.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -39,10 +39,14 @@ class RandomVerticalFlipOp : public TensorOp { ~RandomVerticalFlipOp() override = default; - Status Compute(const std::shared_ptr &input, std::shared_ptr *output) override; + Status Compute(const TensorRow &input, TensorRow *output) override; std::string Name() const override { return kRandomVerticalFlipOp; } + uint32_t NumInput() override { return -1; } + + uint32_t NumOutput() override { return -1; } + private: std::mt19937 rnd_; std::bernoulli_distribution distribution_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.cc index 43ca7a43a5c..bcb9e527886 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.cc @@ -13,13 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h" #include #include "opencv2/opencv.hpp" #include "minddata/dataset/core/cv_tensor.h" #include "minddata/dataset/kernels/image/image_utils.h" +#include "minddata/dataset/kernels/image/random_crop_and_resize_op.h" +#include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h" #include "minddata/dataset/util/random.h" namespace mindspore { @@ -28,8 +29,13 @@ SoftDvppDecodeRandomCropResizeJpegOp::SoftDvppDecodeRandomCropResizeJpegOp(int32 float scale_lb, float scale_ub, float aspect_lb, float aspect_ub, int32_t max_attempts) - : RandomCropAndResizeOp(target_height, target_width, scale_lb, scale_ub, aspect_lb, aspect_ub, - InterpolationMode::kLinear, max_attempts) {} + : target_height_(target_height), + target_width_(target_width), + scale_lb_(scale_lb), + scale_ub_(scale_ub), + aspect_lb_(aspect_lb), + aspect_ub_(aspect_ub), + max_attempts_(max_attempts) {} Status SoftDvppDecodeRandomCropResizeJpegOp::GetCropInfo(const std::shared_ptr &input, SoftDpCropInfo *crop_info) { @@ -40,7 +46,10 @@ Status SoftDvppDecodeRandomCropResizeJpegOp::GetCropInfo(const std::shared_ptr random_crop_resize( + new RandomCropAndResizeOp(target_height_, target_width_, scale_lb_, scale_ub_, aspect_lb_, aspect_ub_, + InterpolationMode::kLinear, max_attempts_)); + RETURN_IF_NOT_OK(random_crop_resize->GetCropBox(img_height, img_width, &x, &y, &crop_heigh, &crop_widht)); crop_info->left = x; crop_info->up = y; crop_info->right = crop_info->left + crop_widht - 1; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h index 2672b32ec42..52aac69d787 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h @@ -28,8 +28,15 @@ namespace mindspore { namespace dataset { -class SoftDvppDecodeRandomCropResizeJpegOp : public RandomCropAndResizeOp { +class SoftDvppDecodeRandomCropResizeJpegOp : public TensorOp { public: + static const float kDefScaleLb; + static const float kDefScaleUb; + static const float kDefAspectLb; + static const float kDefAspectUb; + static const InterpolationMode kDefInterpolation; + static const int32_t kDefMaxIter; + SoftDvppDecodeRandomCropResizeJpegOp(int32_t target_height, int32_t target_width, float scale_lb = kDefScaleLb, float scale_ub = kDefScaleUb, float aspect_lb = kDefAspectLb, float aspect_ub = kDefAspectUb, int32_t max_attempts = kDefMaxIter); @@ -43,6 +50,14 @@ class SoftDvppDecodeRandomCropResizeJpegOp : public RandomCropAndResizeOp { protected: Status GetCropInfo(const std::shared_ptr &input, SoftDpCropInfo *crop_info); + + int32_t target_height_; + int32_t target_width_; + float scale_lb_; + float scale_ub_; + float aspect_lb_; + float aspect_ub_; + int32_t max_attempts_; }; } // namespace dataset } // namespace mindspore diff --git a/mindspore/dataset/text/__init__.py b/mindspore/dataset/text/__init__.py index 3f2ea87dcdc..b7e6b9e8d59 100644 --- a/mindspore/dataset/text/__init__.py +++ b/mindspore/dataset/text/__init__.py @@ -34,5 +34,5 @@ if platform.system().lower() != 'windows': from .transforms import UnicodeScriptTokenizer, WhitespaceTokenizer, CaseFold, NormalizeUTF8, \ RegexReplace, RegexTokenizer, BasicTokenizer, BertTokenizer - __all__.append(["UnicodeScriptTokenizer", "WhitespaceTokenizer", "CaseFold", "NormalizeUTF8", + __all__.extend(["UnicodeScriptTokenizer", "WhitespaceTokenizer", "CaseFold", "NormalizeUTF8", "RegexReplace", "RegexTokenizer", "BasicTokenizer", "BertTokenizer"]) diff --git a/mindspore/dataset/vision/c_transforms.py b/mindspore/dataset/vision/c_transforms.py index 70a72c87b85..2ccbaf28c2a 100644 --- a/mindspore/dataset/vision/c_transforms.py +++ b/mindspore/dataset/vision/c_transforms.py @@ -260,6 +260,46 @@ class ConvertColor(ImageTensorOperation): Args: convert_mode (ConvertMode): The mode of image channel conversion. + - ConvertMode.COLOR_BGR2BGRA, Add alpha channel to BGR image. + + - ConvertMode.COLOR_RGB2RGBA, Add alpha channel to RGB image. + + - ConvertMode.COLOR_BGRA2BGR, Remove alpha channel to BGR image. + + - ConvertMode.COLOR_RGBA2RGB, Remove alpha channel to RGB image. + + - ConvertMode.COLOR_BGR2RGBA, Convert BGR image to RGBA image. + + - ConvertMode.COLOR_RGB2BGRA, Convert RGB image to BGRA image. + + - ConvertMode.COLOR_RGBA2BGR, Convert RGBA image to BGR image. + + - ConvertMode.COLOR_BGRA2RGB, Convert BGRA image to RGB image. + + - ConvertMode.COLOR_BGR2RGB, Convert BGR image to RGB image. + + - ConvertMode.COLOR_RGB2BGR, Convert RGB image to BGR image. + + - ConvertMode.COLOR_BGRA2RGBA, Convert BGRA image to RGBA image. + + - ConvertMode.COLOR_RGBA2BGRA, Convert RGBA image to BGRA image. + + - ConvertMode.COLOR_BGR2GRAY, Convert BGR image to GRAY image. + + - ConvertMode.COLOR_RGB2GRAY, Convert RGB image to GRAY image. + + - ConvertMode.COLOR_GRAY2BGR, Convert GRAY image to BGR image. + + - ConvertMode.COLOR_GRAY2RGB, Convert GRAY image to RGB image. + + - ConvertMode.COLOR_GRAY2BGRA, Convert GRAY image to BGRA image. + + - ConvertMode.COLOR_GRAY2RGBA, Convert GRAY image to RGBA image. + + - ConvertMode.COLOR_BGRA2GRAY, Convert BGRA image to GRAY image. + + - ConvertMode.COLOR_RGBA2GRAY, Convert RGBA image to GRAY image. + Examples: >>> # Convert RGB images to GRAY images >>> convert_op = c_vision.ConvertColor(ConvertMode.COLOR_RGB2GRAY) diff --git a/tests/ut/cpp/dataset/c_api_vision_random_test.cc b/tests/ut/cpp/dataset/c_api_vision_random_test.cc index b7993072a20..65a5af3c426 100644 --- a/tests/ut/cpp/dataset/c_api_vision_random_test.cc +++ b/tests/ut/cpp/dataset/c_api_vision_random_test.cc @@ -336,6 +336,49 @@ TEST_F(MindDataTestPipeline, TestRandomCropSuccess) { iter->Stop(); } +TEST_F(MindDataTestPipeline, TestRandomCropWithMultiField) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropWithMultiField."; + // Create an VOC Dataset + std::string folder_path = datasets_root_path_ + "/testVOC2012_2"; + std::shared_ptr ds = + VOC(folder_path, "Segmentation", "train", {}, true, std::make_shared(0, 1)); + EXPECT_NE(ds, nullptr); + + // Create objects for the tensor ops + transforms::Duplicate duplicate = transforms::Duplicate(); + std::shared_ptr random_crop(new mindspore::dataset::vision::RandomCrop({500, 500})); + + // Create a Map operation on ds + ds = ds->Map({duplicate}, {"image"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + ds = ds->Map({random_crop}, {"image", "image_copy"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + // Create an iterator over the result of the above dataset + // This will trigger the creation of the Execution Tree and launch it. + std::shared_ptr iter = ds->CreateIterator(); + EXPECT_NE(iter, nullptr); + + // Iterate the dataset and get each row + std::unordered_map row; + ASSERT_OK(iter->GetNextRow(&row)); + + uint64_t i = 0; + while (row.size() != 0) { + i++; + auto image = row["image"]; + auto image_copy = row["image_copy"]; + MS_LOG(INFO) << "Tensor image shape: " << image.Shape(); + MS_LOG(INFO) << "Tensor image_copy shape: " << image_copy.Shape(); + ASSERT_OK(iter->GetNextRow(&row)); + } + + EXPECT_EQ(i, 1); + // Manually terminate the pipeline + iter->Stop(); +} + TEST_F(MindDataTestPipeline, TestRandomCropFail) { MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropFail with invalid parameters."; // Create an VOC Dataset @@ -661,6 +704,50 @@ TEST_F(MindDataTestPipeline, TestRandomHorizontalAndVerticalFlip) { iter->Stop(); } +TEST_F(MindDataTestPipeline, TestRandomResizeWithMultiField) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeWithMultiField with single integer input."; + + // Create an ImageFolder Dataset + std::string folder_path = datasets_root_path_ + "/testPK/data/"; + std::shared_ptr ds = ImageFolder(folder_path, true, std::make_shared(false, 1)); + EXPECT_NE(ds, nullptr); + + // Create objects for the tensor ops + transforms::Duplicate duplicate = transforms::Duplicate(); + std::shared_ptr random_resize(new vision::RandomResize({100})); + + // Create a Map operation on ds + ds = ds->Map({duplicate}, {"image"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + ds = ds->Map({random_resize}, {"image", "image_copy"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + // Create an iterator over the result of the above dataset + // This will trigger the creation of the Execution Tree and launch it. + std::shared_ptr iter = ds->CreateIterator(); + EXPECT_NE(iter, nullptr); + + // Iterate the dataset and get each row + std::unordered_map row; + ASSERT_OK(iter->GetNextRow(&row)); + + uint64_t i = 0; + while (row.size() != 0) { + i++; + auto image = row["image"]; + auto image_copy = row["image_copy"]; + MS_LOG(INFO) << "Tensor image shape: " << image.Shape(); + MS_LOG(INFO) << "Tensor image_copy shape: " << image_copy.Shape(); + ASSERT_OK(iter->GetNextRow(&row)); + } + + EXPECT_EQ(i, 1); + + // Manually terminate the pipeline + iter->Stop(); +} + TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess1) { MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess1 with non-default parameters."; @@ -878,7 +965,7 @@ TEST_F(MindDataTestPipeline, TestRandomResizeWithBBoxSuccess1) { } EXPECT_EQ(i, 3); - + // Manually terminate the pipeline iter->Stop(); config::set_seed(current_seed); @@ -1555,3 +1642,138 @@ TEST_F(MindDataTestPipeline, TestRandomVerticalFlipWithBBoxSuccess) { // Manually terminate the pipeline iter->Stop(); } + +TEST_F(MindDataTestPipeline, TestRandomHorizontalAndVerticalFlipWithMultiField) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalAndVerticalFlipWithMultiField for horizontal and " + "vertical flips."; + + // Create an ImageFolder Dataset + std::string folder_path = datasets_root_path_ + "/testPK/data/"; + std::shared_ptr ds = ImageFolder(folder_path, true, std::make_shared(false, 1)); + EXPECT_NE(ds, nullptr); + + // Create objects for the tensor ops + transforms::Duplicate duplicate = transforms::Duplicate(); + std::shared_ptr random_vertical_flip_op = std::make_shared(1); + std::shared_ptr random_horizontal_flip_op = std::make_shared(1); + + // Create a Map operation on ds + ds = ds->Map({duplicate}, {"image"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + ds = ds->Map({random_vertical_flip_op}, {"image", "image_copy"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + // Create an iterator over the result of the above dataset + // This will trigger the creation of the Execution Tree and launch it. + std::shared_ptr iter = ds->CreateIterator(); + EXPECT_NE(iter, nullptr); + + // Iterate the dataset and get each row + std::unordered_map row; + ASSERT_OK(iter->GetNextRow(&row)); + + uint64_t i = 0; + while (row.size() != 0) { + i++; + auto image = row["image"]; + auto image_copy = row["image_copy"]; + MS_LOG(INFO) << "Tensor image shape: " << image.Shape(); + MS_LOG(INFO) << "Tensor image_copy shape: " << image_copy.Shape(); + ASSERT_OK(iter->GetNextRow(&row)); + } + + EXPECT_EQ(i, 1); + + // Manually terminate the pipeline + iter->Stop(); +} + +TEST_F(MindDataTestPipeline, TestRandomCropDecodeResizeWithMultiField) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropDecodeResizeWithMultiField."; + + // Create an ImageFolder Dataset + std::string folder_path = datasets_root_path_ + "/testPK/data/"; + std::shared_ptr ds = ImageFolder(folder_path, false, std::make_shared(false, 1)); + EXPECT_NE(ds, nullptr); + + // Create objects for the tensor ops + transforms::Duplicate duplicate = transforms::Duplicate(); + std::shared_ptr random_crop_decode_resize(new vision::RandomCropDecodeResize({500, 500})); + + // Create a Map operation on ds + ds = ds->Map({duplicate}, {"image"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + ds = ds->Map({random_crop_decode_resize}, {"image", "image_copy"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + // Create an iterator over the result of the above dataset + // This will trigger the creation of the Execution Tree and launch it. + std::shared_ptr iter = ds->CreateIterator(); + EXPECT_NE(iter, nullptr); + + // Iterate the dataset and get each row + std::unordered_map row; + ASSERT_OK(iter->GetNextRow(&row)); + + uint64_t i = 0; + while (row.size() != 0) { + i++; + auto image = row["image"]; + auto image_copy = row["image_copy"]; + MS_LOG(INFO) << "Tensor image shape: " << image.Shape(); + MS_LOG(INFO) << "Tensor image_copy shape: " << image_copy.Shape(); + + ASSERT_OK(iter->GetNextRow(&row)); + } + + EXPECT_EQ(i, 1); + + // Manually terminate the pipeline + iter->Stop(); +} + +TEST_F(MindDataTestPipeline, TestRandomCropResizeWithMultiField) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropResizeWithMultiField."; + + // Create an ImageFolder Dataset + std::string folder_path = datasets_root_path_ + "/testPK/data/"; + std::shared_ptr ds = ImageFolder(folder_path, true, std::make_shared(false, 1)); + EXPECT_NE(ds, nullptr); + + // Create objects for the tensor ops + transforms::Duplicate duplicate = transforms::Duplicate(); + std::shared_ptr random_crop_decode_resize(new vision::RandomResizedCrop({500, 500})); + + // Create a Map operation on ds + ds = ds->Map({duplicate}, {"image"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + ds = ds->Map({random_crop_decode_resize}, {"image", "image_copy"}, {"image", "image_copy"}); + EXPECT_NE(ds, nullptr); + + // Create an iterator over the result of the above dataset + // This will trigger the creation of the Execution Tree and launch it. + std::shared_ptr iter = ds->CreateIterator(); + EXPECT_NE(iter, nullptr); + + // Iterate the dataset and get each row + std::unordered_map row; + ASSERT_OK(iter->GetNextRow(&row)); + + uint64_t i = 0; + while (row.size() != 0) { + i++; + auto image = row["image"]; + auto image_copy = row["image_copy"]; + MS_LOG(INFO) << "Tensor image shape: " << image.Shape(); + MS_LOG(INFO) << "Tensor image_copy shape: " << image_copy.Shape(); + ASSERT_OK(iter->GetNextRow(&row)); + } + + EXPECT_EQ(i, 1); + + // Manually terminate the pipeline + iter->Stop(); +} diff --git a/tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc b/tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc index fd59a901172..631d6975e5a 100644 --- a/tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc +++ b/tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -31,6 +31,10 @@ class MindDataTestRandomCropAndResizeOp : public UT::CVOP::CVOpCommon { TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest1) { MS_LOG(INFO) << " starting RandomCropAndResizeOp simple test"; TensorShape s_in = input_tensor_->shape(); + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + TensorRow output_tensor_row; std::shared_ptr output_tensor; int h_out = 1024; int w_out = 2048; @@ -44,7 +48,7 @@ TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest1) { auto op = std::make_unique(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub); Status s; for (auto i = 0; i < 100; i++) { - s = op->Compute(input_tensor_, &output_tensor); + s = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_TRUE(s.IsOk()); } @@ -53,6 +57,10 @@ TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest1) { TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest2) { MS_LOG(INFO) << " starting RandomCropAndResizeOp simple test"; TensorShape s_in = input_tensor_->shape(); + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + TensorRow output_tensor_row; std::shared_ptr output_tensor; int h_out = 1024; int w_out = 2048; @@ -66,7 +74,7 @@ TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest2) { auto op = std::make_unique(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub); Status s; for (auto i = 0; i < 100; i++) { - s = op->Compute(input_tensor_, &output_tensor); + s = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_TRUE(s.IsOk()); } @@ -75,6 +83,10 @@ TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest2) { TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest3) { MS_LOG(INFO) << " starting RandomCropAndResizeOp simple test"; TensorShape s_in = input_tensor_->shape(); + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + TensorRow output_tensor_row; std::shared_ptr output_tensor; int h_out = 1024; int w_out = 2048; @@ -88,7 +100,7 @@ TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest3) { auto op = std::make_unique(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub); Status s; for (auto i = 0; i < 100; i++) { - s = op->Compute(input_tensor_, &output_tensor); + s = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_TRUE(s.IsOk()); } diff --git a/tests/ut/cpp/dataset/random_crop_decode_resize_op_test.cc b/tests/ut/cpp/dataset/random_crop_decode_resize_op_test.cc index 170525b4e7d..9c0b5022b4f 100644 --- a/tests/ut/cpp/dataset/random_crop_decode_resize_op_test.cc +++ b/tests/ut/cpp/dataset/random_crop_decode_resize_op_test.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -36,7 +36,6 @@ class MindDataTestRandomCropDecodeResizeOp : public UT::CVOP::CVOpCommon { TEST_F(MindDataTestRandomCropDecodeResizeOp, TestOp2) { MS_LOG(INFO) << "starting RandomCropDecodeResizeOp test 1"; - std::shared_ptr decode_and_crop_output; std::shared_ptr crop_and_decode_output; constexpr int target_height = 884; @@ -52,13 +51,18 @@ TEST_F(MindDataTestRandomCropDecodeResizeOp, TestOp2) { interpolation, max_iter); auto crop_and_decode_copy = crop_and_decode; auto decode_and_crop = static_cast(crop_and_decode_copy); - EXPECT_TRUE(crop_and_decode.OneToOne()); GlobalContext::config_manager()->set_seed(42); + TensorRow input_tensor_row_decode; + TensorRow output_tensor_row_decode; + input_tensor_row_decode.push_back(raw_input_tensor_); + TensorRow input_tensor_row; + TensorRow output_tensor_row; + input_tensor_row.push_back(input_tensor_); for (int k = 0; k < 10; k++) { - (void)crop_and_decode.Compute(raw_input_tensor_, &crop_and_decode_output); - (void)decode_and_crop.Compute(input_tensor_, &decode_and_crop_output); - cv::Mat output1 = CVTensor::AsCVTensor(crop_and_decode_output)->mat().clone(); - cv::Mat output2 = CVTensor::AsCVTensor(decode_and_crop_output)->mat().clone(); + (void)crop_and_decode.Compute(input_tensor_row_decode, &output_tensor_row_decode); + (void)decode_and_crop.Compute(input_tensor_row, &output_tensor_row); + cv::Mat output1 = CVTensor::AsCVTensor(output_tensor_row_decode[0])->mat().clone(); + cv::Mat output2 = CVTensor::AsCVTensor(output_tensor_row[0])->mat().clone(); long int mse_sum = 0; long int count = 0; diff --git a/tests/ut/cpp/dataset/random_crop_op_test.cc b/tests/ut/cpp/dataset/random_crop_op_test.cc index 3c5f2c64808..838f69c3ed7 100644 --- a/tests/ut/cpp/dataset/random_crop_op_test.cc +++ b/tests/ut/cpp/dataset/random_crop_op_test.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -27,7 +27,7 @@ class MindDataTestRandomCropOp : public UT::CVOP::CVOpCommon { protected: MindDataTestRandomCropOp() : CVOpCommon() {} - std::shared_ptr output_tensor_; + TensorRow output_tensor_row; }; TEST_F(MindDataTestRandomCropOp, TestOp1) { @@ -36,14 +36,18 @@ TEST_F(MindDataTestRandomCropOp, TestOp1) { unsigned int crop_height = 128; unsigned int crop_width = 128; std::unique_ptr op(new RandomCropOp(crop_height, crop_width, 0, 0, 0, 0, false, BorderType::kConstant)); - EXPECT_TRUE(op->OneToOne()); - Status s = op->Compute(input_tensor_, &output_tensor_); - size_t actual = 0; - if (s == Status::OK()) { - actual = output_tensor_->shape()[0] * output_tensor_->shape()[1] * output_tensor_->shape()[2]; + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + Status s = op->Compute(input_tensor_row, &output_tensor_row); + for (size_t i = 0; i < input_tensor_row.size(); i++) { + size_t actual = 0; + if (s == Status::OK()) { + actual = output_tensor_row[i]->shape()[0] * output_tensor_row[i]->shape()[1] * output_tensor_row[i]->shape()[2]; + } + EXPECT_EQ(actual, crop_height * crop_width * 3); + EXPECT_EQ(s, Status::OK()); } - EXPECT_EQ(actual, crop_height * crop_width * 3); - EXPECT_EQ(s, Status::OK()); } TEST_F(MindDataTestRandomCropOp, TestOp2) { @@ -51,10 +55,12 @@ TEST_F(MindDataTestRandomCropOp, TestOp2) { // Crop params unsigned int crop_height = 1280; unsigned int crop_width = 1280; + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); std::unique_ptr op( new RandomCropOp(crop_height, crop_width, 513, 513, 513, 513, false, BorderType::kConstant)); - EXPECT_TRUE(op->OneToOne()); - Status s = op->Compute(input_tensor_, &output_tensor_); + Status s = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_EQ(true, s.IsOk()); MS_LOG(INFO) << "testRandomCrop end."; } diff --git a/tests/ut/cpp/dataset/random_horizontal_flip_op_test.cc b/tests/ut/cpp/dataset/random_horizontal_flip_op_test.cc index bb4ba7498dd..85db7c8a3d5 100644 --- a/tests/ut/cpp/dataset/random_horizontal_flip_op_test.cc +++ b/tests/ut/cpp/dataset/random_horizontal_flip_op_test.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -19,9 +19,9 @@ #include "utils/log_adapter.h" using namespace mindspore::dataset; -using mindspore::MsLogLevel::INFO; -using mindspore::ExceptionType::NoExceptionType; using mindspore::LogStream; +using mindspore::ExceptionType::NoExceptionType; +using mindspore::MsLogLevel::INFO; class MindDataTestRandomHorizontalFlipOp : public UT::CVOP::CVOpCommon { protected: @@ -31,9 +31,12 @@ class MindDataTestRandomHorizontalFlipOp : public UT::CVOP::CVOpCommon { TEST_F(MindDataTestRandomHorizontalFlipOp, TestOp) { MS_LOG(INFO) << "Doing testHorizontalFlip."; // flip + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + TensorRow output_tensor_row; std::unique_ptr op(new RandomHorizontalFlipOp(0.5)); - EXPECT_TRUE(op->OneToOne()); - Status s = op->Compute(input_tensor_, &input_tensor_); + Status s = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_TRUE(s.IsOk()); CheckImageShapeAndData(input_tensor_, kFlipHorizontal); MS_LOG(INFO) << "testHorizontalFlip end."; diff --git a/tests/ut/cpp/dataset/random_resize_op_test.cc b/tests/ut/cpp/dataset/random_resize_op_test.cc index d9e85de6e53..2eeac96c86c 100644 --- a/tests/ut/cpp/dataset/random_resize_op_test.cc +++ b/tests/ut/cpp/dataset/random_resize_op_test.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -19,9 +19,9 @@ #include "utils/log_adapter.h" using namespace mindspore::dataset; -using mindspore::MsLogLevel::INFO; -using mindspore::ExceptionType::NoExceptionType; using mindspore::LogStream; +using mindspore::ExceptionType::NoExceptionType; +using mindspore::MsLogLevel::INFO; class MindDataTestRandomResize : public UT::CVOP::CVOpCommon { public: @@ -34,11 +34,13 @@ TEST_F(MindDataTestRandomResize, TestOp) { TensorShape s = input_tensor_->shape(); int output_h = 0.5 * s[0]; int output_w = 0.5 * s[1]; - std::shared_ptr output_tensor; + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + TensorRow output_tensor_row; // Resizing std::unique_ptr op(new RandomResizeOp(output_h, output_w)); - EXPECT_TRUE(op->OneToOne()); - Status st = op->Compute(input_tensor_, &output_tensor); + Status st = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_TRUE(st.IsOk()); MS_LOG(INFO) << "testResize end."; } diff --git a/tests/ut/cpp/dataset/random_vertical_flip_op_test.cc b/tests/ut/cpp/dataset/random_vertical_flip_op_test.cc index db8cc89893e..178251a1a15 100644 --- a/tests/ut/cpp/dataset/random_vertical_flip_op_test.cc +++ b/tests/ut/cpp/dataset/random_vertical_flip_op_test.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * 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. @@ -31,9 +31,12 @@ class MindDataTestRandomVerticalFlipOp : public UT::CVOP::CVOpCommon { TEST_F(MindDataTestRandomVerticalFlipOp, TestOp) { MS_LOG(INFO) << "Doing testVerticalFlip."; // flip + TensorRow input_tensor_row; + input_tensor_row.push_back(input_tensor_); + input_tensor_row.push_back(input_tensor_); + TensorRow output_tensor_row; std::unique_ptr op(new RandomVerticalFlipOp(0.5)); - Status s = op->Compute(input_tensor_, &input_tensor_); - EXPECT_TRUE(op->OneToOne()); + Status s = op->Compute(input_tensor_row, &output_tensor_row); EXPECT_TRUE(s.IsOk()); CheckImageShapeAndData(input_tensor_, kFlipVertical); MS_LOG(INFO) << "testVerticalFlip end."; diff --git a/tests/ut/python/dataset/test_random_crop.py b/tests/ut/python/dataset/test_random_crop.py index f3137fe1f44..605e849bc5b 100644 --- a/tests/ut/python/dataset/test_random_crop.py +++ b/tests/ut/python/dataset/test_random_crop.py @@ -18,13 +18,14 @@ Testing RandomCrop op in DE import numpy as np import mindspore.dataset.transforms.py_transforms +import mindspore.dataset.transforms.c_transforms as ops import mindspore.dataset.vision.c_transforms as c_vision import mindspore.dataset.vision.py_transforms as py_vision import mindspore.dataset.vision.utils as mode import mindspore.dataset as ds from mindspore import log as logger from util import save_and_check_md5, visualize_list, config_get_set_seed, \ - config_get_set_num_parallel_workers + config_get_set_num_parallel_workers, diff_mse GENERATE_GOLDEN = False @@ -541,6 +542,30 @@ def test_random_crop_comp(plot=False): if plot: visualize_list(image_c_cropped, image_py_cropped, visualize_mode=2) +def test_random_crop_09_c(): + """ + Test RandomCrop with different fields. + """ + logger.info("Test RandomCrop with different fields.") + + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + data = data.map(operations=ops.Duplicate(), input_columns=["image"], + output_columns=["image", "image_copy"], column_order=["image", "image_copy"]) + random_crop_op = c_vision.RandomCrop([512, 512], [200, 200, 200, 200]) + decode_op = c_vision.Decode() + + data = data.map(operations=decode_op, input_columns=["image"]) + data = data.map(operations=decode_op, input_columns=["image_copy"]) + data = data.map(operations=random_crop_op, input_columns=["image", "image_copy"]) + + num_iter = 0 + for data1 in data.create_dict_iterator(num_epochs=1, output_numpy=True): + image = data1["image"] + image_copy = data1["image_copy"] + mse = diff_mse(image, image_copy) + assert mse == 0 + num_iter += 1 + if __name__ == "__main__": test_random_crop_01_c() @@ -563,3 +588,4 @@ if __name__ == "__main__": test_random_crop_op_c(True) test_random_crop_op_py(True) test_random_crop_comp(True) + test_random_crop_09_c() diff --git a/tests/ut/python/dataset/test_random_crop_and_resize.py b/tests/ut/python/dataset/test_random_crop_and_resize.py index 5fdaf6c6e77..50309165f31 100644 --- a/tests/ut/python/dataset/test_random_crop_and_resize.py +++ b/tests/ut/python/dataset/test_random_crop_and_resize.py @@ -19,6 +19,7 @@ import numpy as np import cv2 import mindspore.dataset.transforms.py_transforms +import mindspore.dataset.transforms.c_transforms as ops import mindspore.dataset.vision.c_transforms as c_vision import mindspore.dataset.vision.py_transforms as py_vision import mindspore.dataset.vision.utils as mode @@ -405,6 +406,31 @@ def test_random_crop_and_resize_06(): logger.info("Got an exception in DE: {}".format(str(e))) assert "Argument scale[1] with value 2 is not of type [, ]" in str(e) +def test_random_crop_and_resize_07(): + """ + Test RandomCropAndResize with different fields. + """ + logger.info("Test RandomCropAndResize with different fields.") + + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + data = data.map(operations=ops.Duplicate(), input_columns=["image"], + output_columns=["image", "image_copy"], column_order=["image", "image_copy"]) + random_crop_and_resize_op = c_vision.RandomResizedCrop((256, 512), (2, 2), (1, 3)) + decode_op = c_vision.Decode() + + data = data.map(operations=decode_op, input_columns=["image"]) + data = data.map(operations=decode_op, input_columns=["image_copy"]) + data = data.map(operations=random_crop_and_resize_op, input_columns=["image", "image_copy"]) + + num_iter = 0 + for data1 in data.create_dict_iterator(num_epochs=1, output_numpy=True): + image = data1["image"] + image_copy = data1["image_copy"] + mse = diff_mse(image, image_copy) + logger.info("image_{}, mse: {}".format(num_iter + 1, mse)) + assert mse == 0 + num_iter += 1 + if __name__ == "__main__": test_random_crop_and_resize_callable() @@ -420,3 +446,4 @@ if __name__ == "__main__": test_random_crop_and_resize_05_py() test_random_crop_and_resize_06() test_random_crop_and_resize_comp(True) + test_random_crop_and_resize_07() diff --git a/tests/ut/python/dataset/test_random_horizontal_flip.py b/tests/ut/python/dataset/test_random_horizontal_flip.py index 7c04acb1c14..9c2d6a37803 100644 --- a/tests/ut/python/dataset/test_random_horizontal_flip.py +++ b/tests/ut/python/dataset/test_random_horizontal_flip.py @@ -18,6 +18,7 @@ Testing the random horizontal flip op in DE import numpy as np import mindspore.dataset as ds import mindspore.dataset.transforms.py_transforms +import mindspore.dataset.transforms.c_transforms as ops import mindspore.dataset.vision.c_transforms as c_vision import mindspore.dataset.vision.py_transforms as py_vision from mindspore import log as logger @@ -208,6 +209,31 @@ def test_random_horizontal_comp(plot=False): if plot: visualize_list(images_list_c, images_list_py, visualize_mode=2) +def test_random_horizontal_op_1(): + """ + Test RandomHorizontalFlip with different fields. + """ + logger.info("Test RandomHorizontalFlip with different fields.") + + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + data = data.map(operations=ops.Duplicate(), input_columns=["image"], + output_columns=["image", "image_copy"], column_order=["image", "image_copy"]) + random_horizontal_op = c_vision.RandomHorizontalFlip(1.0) + decode_op = c_vision.Decode() + + data = data.map(operations=decode_op, input_columns=["image"]) + data = data.map(operations=decode_op, input_columns=["image_copy"]) + data = data.map(operations=random_horizontal_op, input_columns=["image", "image_copy"]) + + num_iter = 0 + for data1 in data.create_dict_iterator(num_epochs=1, output_numpy=True): + image = data1["image"] + image_copy = data1["image_copy"] + mse = diff_mse(image, image_copy) + logger.info("image_{}, mse: {}".format(num_iter + 1, mse)) + assert mse == 0 + num_iter += 1 + if __name__ == "__main__": test_random_horizontal_op(plot=True) @@ -216,3 +242,4 @@ if __name__ == "__main__": test_random_horizontal_invalid_prob_c() test_random_horizontal_invalid_prob_py() test_random_horizontal_comp(plot=True) + test_random_horizontal_op_1() diff --git a/tests/ut/python/dataset/test_random_resize.py b/tests/ut/python/dataset/test_random_resize.py index 25f34f67cf7..5b28d8632ed 100644 --- a/tests/ut/python/dataset/test_random_resize.py +++ b/tests/ut/python/dataset/test_random_resize.py @@ -16,9 +16,10 @@ Testing RandomResize op in DE """ import mindspore.dataset as ds +import mindspore.dataset.transforms.c_transforms as ops import mindspore.dataset.vision.c_transforms as vision from mindspore import log as logger -from util import visualize_list, save_and_check_md5, \ +from util import visualize_list, save_and_check_md5, diff_mse, \ config_get_set_seed, config_get_set_num_parallel_workers DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"] @@ -77,7 +78,33 @@ def test_random_resize_md5(): ds.config.set_seed(original_seed) ds.config.set_num_parallel_workers(original_num_parallel_workers) +def test_random_resize_op_1(): + """ + Test RandomResize with different fields. + """ + logger.info("Test RandomResize with different fields.") + + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + data = data.map(operations=ops.Duplicate(), input_columns=["image"], + output_columns=["image", "image_copy"], column_order=["image", "image_copy"]) + resize_op = vision.RandomResize(10) + decode_op = vision.Decode() + + data = data.map(operations=decode_op, input_columns=["image"]) + data = data.map(operations=decode_op, input_columns=["image_copy"]) + data = data.map(operations=resize_op, input_columns=["image", "image_copy"]) + + num_iter = 0 + for data1 in data.create_dict_iterator(num_epochs=1, output_numpy=True): + image = data1["image"] + image_copy = data1["image_copy"] + mse = diff_mse(image, image_copy) + logger.info("image_{}, mse: {}".format(num_iter + 1, mse)) + assert mse == 0 + num_iter += 1 + if __name__ == "__main__": test_random_resize_op(plot=True) test_random_resize_md5() + test_random_resize_op_1() diff --git a/tests/ut/python/dataset/test_random_vertical_flip.py b/tests/ut/python/dataset/test_random_vertical_flip.py index a56f69b7419..58290f72aed 100644 --- a/tests/ut/python/dataset/test_random_vertical_flip.py +++ b/tests/ut/python/dataset/test_random_vertical_flip.py @@ -18,6 +18,7 @@ Testing the random vertical flip op in DE import numpy as np import mindspore.dataset as ds import mindspore.dataset.transforms.py_transforms +import mindspore.dataset.transforms.c_transforms as ops import mindspore.dataset.vision.c_transforms as c_vision import mindspore.dataset.vision.py_transforms as py_vision from mindspore import log as logger @@ -208,6 +209,31 @@ def test_random_vertical_comp(plot=False): if plot: visualize_list(images_list_c, images_list_py, visualize_mode=2) +def test_random_vertical_op_1(): + """ + Test RandomVerticalFlip with different fields. + """ + logger.info("Test RandomVerticalFlip with different fields.") + + data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) + data = data.map(operations=ops.Duplicate(), input_columns=["image"], + output_columns=["image", "image_copy"], column_order=["image", "image_copy"]) + random_vertical_op = c_vision.RandomVerticalFlip(1.0) + decode_op = c_vision.Decode() + + data = data.map(operations=decode_op, input_columns=["image"]) + data = data.map(operations=decode_op, input_columns=["image_copy"]) + data = data.map(operations=random_vertical_op, input_columns=["image", "image_copy"]) + + num_iter = 0 + for data1 in data.create_dict_iterator(num_epochs=1, output_numpy=True): + image = data1["image"] + image_copy = data1["image_copy"] + mse = diff_mse(image, image_copy) + logger.info("image_{}, mse: {}".format(num_iter + 1, mse)) + assert mse == 0 + num_iter += 1 + if __name__ == "__main__": test_random_vertical_op(plot=True) @@ -216,3 +242,4 @@ if __name__ == "__main__": test_random_vertical_invalid_prob_c() test_random_vertical_invalid_prob_py() test_random_vertical_comp(plot=True) + test_random_vertical_op_1()