From 9a6b7a59370ab881a38f7e324cf20c60f99e2164 Mon Sep 17 00:00:00 2001 From: Cathy Wong Date: Fri, 9 Jul 2021 15:13:03 -0400 Subject: [PATCH] MD CI code warning fixes --- .../ccsrc/minddata/dataset/api/execute.cc | 1 - mindspore/ccsrc/minddata/dataset/api/text.cc | 16 +++++--- .../dataset/kernels/image/cutmix_batch_op.cc | 40 +++++++++++-------- mindspore/dataset/vision/c_transforms.py | 2 + 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/mindspore/ccsrc/minddata/dataset/api/execute.cc b/mindspore/ccsrc/minddata/dataset/api/execute.cc index 35d2ea2e8ba..aed2e5f90a3 100644 --- a/mindspore/ccsrc/minddata/dataset/api/execute.cc +++ b/mindspore/ccsrc/minddata/dataset/api/execute.cc @@ -54,7 +54,6 @@ struct Execute::ExtraInfo { #endif }; -// FIXME - Temporarily overload Execute to support both TensorOperation and TensorTransform Execute::Execute(std::shared_ptr op, MapTargetDevice device_type, uint32_t device_id) { ops_.emplace_back(std::move(op)); device_type_ = device_type; diff --git a/mindspore/ccsrc/minddata/dataset/api/text.cc b/mindspore/ccsrc/minddata/dataset/api/text.cc index f1cb9007a74..91c438dd7ef 100644 --- a/mindspore/ccsrc/minddata/dataset/api/text.cc +++ b/mindspore/ccsrc/minddata/dataset/api/text.cc @@ -30,6 +30,12 @@ namespace dataset { // Transform operations for text. namespace text { +constexpr size_t size_two = 2; +constexpr size_t size_three = 3; +constexpr int64_t value_one = 1; +constexpr int64_t value_two = 2; +constexpr size_t kMaxLoggedRows = 10; + // FUNCTIONS TO CREATE TEXT OPERATIONS // (In alphabetical order) @@ -188,10 +194,10 @@ Status JiebaTokenizer::ParserFile(const std::string &file_path, std::smatch tokens; std::regex_match(line, tokens, regex); if (std::regex_match(line, tokens, regex)) { - if (tokens.size() == 2) { - user_dict->emplace_back(tokens.str(1), 0); - } else if (tokens.size() == 3) { - user_dict->emplace_back(tokens.str(1), strtoll(tokens.str(2).c_str(), NULL, 0)); + if (tokens.size() == size_two) { + user_dict->emplace_back(tokens.str(value_one), 0); + } else if (tokens.size() == size_three) { + user_dict->emplace_back(tokens.str(value_one), strtoll(tokens.str(value_two).c_str(), NULL, 0)); } else { continue; } @@ -202,7 +208,7 @@ Status JiebaTokenizer::ParserFile(const std::string &file_path, MS_LOG(INFO) << "JiebaTokenizer::AddDict: The size of user input dictionary is: " << user_dict->size(); MS_LOG(INFO) << "Valid rows in input dictionary (Maximum of first 10 rows are shown.):"; for (std::size_t i = 0; i != user_dict->size(); ++i) { - if (i >= 10) break; + if (i >= kMaxLoggedRows) break; MS_LOG(INFO) << user_dict->at(i).first << " " << user_dict->at(i).second; } return Status::OK(); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc index b934996ba7e..269b52b4459 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc @@ -59,14 +59,14 @@ void CutMixBatchOp::GetCropBox(int height, int width, float lam, int *x, int *y, } Status CutMixBatchOp::ValidateCutMixBatch(const TensorRow &input) { - if (input.size() < 2) { + if (input.size() < kMinLabelShapeSize) { RETURN_STATUS_UNEXPECTED("CutMixBatch: invalid input, both image and label columns are required."); } std::vector image_shape = input.at(0)->shape().AsVector(); std::vector label_shape = input.at(1)->shape().AsVector(); // Check inputs - if (image_shape.size() != 4 || image_shape[0] != label_shape[0]) { + if (image_shape.size() != kExpectedImageShapeSize || image_shape[0] != label_shape[0]) { RETURN_STATUS_UNEXPECTED( "CutMixBatch: please make sure images are HWC or CHW " "and batched before calling CutMixBatch."); @@ -74,17 +74,19 @@ Status CutMixBatchOp::ValidateCutMixBatch(const TensorRow &input) { if (!input.at(1)->type().IsInt()) { RETURN_STATUS_UNEXPECTED("CutMixBatch: Wrong labels type. The second column (labels) must only include int types."); } - if (label_shape.size() != 2 && label_shape.size() != 3) { + if (label_shape.size() != kMinLabelShapeSize && label_shape.size() != kMaxLabelShapeSize) { RETURN_STATUS_UNEXPECTED( "CutMixBatch: wrong labels shape. " "The second column (labels) must have a shape of NC or NLC where N is the batch size, " "L is the number of labels in each row, and C is the number of classes. " "labels must be in one-hot format and in a batch."); } - if ((image_shape[1] != 1 && image_shape[1] != 3) && image_batch_format_ == ImageBatchFormat::kNCHW) { + if ((image_shape[dimension_one] != value_one && image_shape[dimension_one] != value_three) && + image_batch_format_ == ImageBatchFormat::kNCHW) { RETURN_STATUS_UNEXPECTED("CutMixBatch: image doesn't match the NCHW format."); } - if ((image_shape[3] != 1 && image_shape[3] != 3) && image_batch_format_ == ImageBatchFormat::kNHWC) { + if ((image_shape[dimension_three] != value_one && image_shape[dimension_three] != value_three) && + image_batch_format_ == ImageBatchFormat::kNHWC) { RETURN_STATUS_UNEXPECTED("CutMixBatch: image doesn't match the NHWC format."); } @@ -101,22 +103,24 @@ Status CutMixBatchOp::ComputeImage(const TensorRow &input, const int64_t rand_in std::shared_ptr rand_image; RETURN_IF_NOT_OK(input.at(0)->StartAddrOfIndex({rand_indx_i, 0, 0, 0}, &start_addr_of_index, &remaining)); - RETURN_IF_NOT_OK(Tensor::CreateFromMemory(TensorShape({image_shape[1], image_shape[2], image_shape[3]}), - input.at(0)->type(), start_addr_of_index, &rand_image)); + RETURN_IF_NOT_OK(Tensor::CreateFromMemory( + TensorShape({image_shape[dimension_one], image_shape[dimension_two], image_shape[dimension_three]}), + input.at(0)->type(), start_addr_of_index, &rand_image)); // Compute image if (image_batch_format_ == ImageBatchFormat::kNHWC) { // NHWC Format - GetCropBox(static_cast(image_shape[1]), static_cast(image_shape[2]), lam, &x, &y, &crop_width, - &crop_height); + GetCropBox(static_cast(image_shape[dimension_one]), static_cast(image_shape[dimension_two]), lam, + &x, &y, &crop_width, &crop_height); std::shared_ptr cropped; RETURN_IF_NOT_OK(Crop(rand_image, &cropped, x, y, crop_width, crop_height)); RETURN_IF_NOT_OK(MaskWithTensor(cropped, image_i, x, y, crop_width, crop_height, ImageFormat::HWC)); - *label_lam = 1 - (crop_width * crop_height / static_cast(image_shape[1] * image_shape[2])); + *label_lam = value_one - (crop_width * crop_height / + static_cast(image_shape[dimension_one] * image_shape[dimension_two])); } else { // NCHW Format - GetCropBox(static_cast(image_shape[2]), static_cast(image_shape[3]), lam, &x, &y, &crop_width, - &crop_height); + GetCropBox(static_cast(image_shape[dimension_two]), static_cast(image_shape[dimension_three]), + lam, &x, &y, &crop_width, &crop_height); std::vector> channels; // A vector holding channels of the CHW image std::vector> cropped_channels; // A vector holding the channels of the cropped CHW RETURN_IF_NOT_OK(BatchTensorToTensorVector(rand_image, &channels)); @@ -131,7 +135,8 @@ Status CutMixBatchOp::ComputeImage(const TensorRow &input, const int64_t rand_in RETURN_IF_NOT_OK(TensorVectorToBatchTensor(cropped_channels, &cropped)); RETURN_IF_NOT_OK(MaskWithTensor(cropped, image_i, x, y, crop_width, crop_height, ImageFormat::CHW)); - *label_lam = 1 - (crop_width * crop_height / static_cast(image_shape[2] * image_shape[3])); + *label_lam = value_one - (crop_width * crop_height / + static_cast(image_shape[dimension_two] * image_shape[dimension_three])); } return Status::OK(); @@ -144,9 +149,10 @@ Status CutMixBatchOp::ComputeLabel(const TensorRow &input, const int64_t rand_in // Compute labels for (int64_t j = 0; j < row_labels; j++) { for (int64_t k = 0; k < num_classes; k++) { - std::vector first_index = label_shape_size == 3 ? std::vector{index_i, j, k} : std::vector{index_i, k}; + std::vector first_index = + label_shape_size == kMaxLabelShapeSize ? std::vector{index_i, j, k} : std::vector{index_i, k}; std::vector second_index = - label_shape_size == 3 ? std::vector{rand_indx_i, j, k} : std::vector{rand_indx_i, k}; + label_shape_size == kMaxLabelShapeSize ? std::vector{rand_indx_i, j, k} : std::vector{rand_indx_i, k}; if (input.at(1)->type().IsSignedInt()) { int64_t first_value, second_value; RETURN_IF_NOT_OK(input.at(1)->GetItemAt(&first_value, first_index)); @@ -188,8 +194,8 @@ Status CutMixBatchOp::Compute(const TensorRow &input, TensorRow *output) { // Tensor holding the output labels std::shared_ptr out_labels; RETURN_IF_NOT_OK(TypeCast(std::move(input.at(1)), &out_labels, DataType(DataType::DE_FLOAT32))); - int64_t row_labels = label_shape.size() == value_three ? label_shape[1] : 1; - int64_t num_classes = label_shape.size() == value_three ? label_shape[dimension_two] : label_shape[1]; + int64_t row_labels = label_shape.size() == value_three ? label_shape[dimension_one] : value_one; + int64_t num_classes = label_shape.size() == value_three ? label_shape[dimension_two] : label_shape[dimension_one]; // Compute labels and images for (size_t i = 0; i < static_cast(image_shape[0]); i++) { diff --git a/mindspore/dataset/vision/c_transforms.py b/mindspore/dataset/vision/c_transforms.py index 586b590d0e3..e47f7604b9d 100644 --- a/mindspore/dataset/vision/c_transforms.py +++ b/mindspore/dataset/vision/c_transforms.py @@ -720,6 +720,7 @@ class RandomColorAdjust(ImageTensorOperation): self.hue = hue def expand_values(self, value, center=1, bound=(0, FLOAT_MAX_INTEGER), non_negative=True): + """Expand input value for vision adjustment factor.""" if isinstance(value, numbers.Number): value = [center - value, center + value] if non_negative: @@ -1576,6 +1577,7 @@ class SlicePatches(ImageTensorOperation): return cde.SlicePatchesOperation(self.num_height, self.num_width, DE_C_SLICE_MODE[self.slice_mode], self.fill_value) + class SoftDvppDecodeRandomCropResizeJpeg(ImageTensorOperation): """ A combination of `Crop`, `Decode` and `Resize` using the simulation algorithm of Ascend series chip DVPP module.