!19890 MD CI code warning fixes [r1.3]

Merge pull request !19890 from cathwong/ckw_r1.3_ci_q3_codedex1
This commit is contained in:
i-robot 2021-07-12 12:56:25 +00:00 committed by Gitee
commit e56fb99d67
4 changed files with 35 additions and 23 deletions

View File

@ -54,7 +54,6 @@ struct Execute::ExtraInfo {
#endif
};
// FIXME - Temporarily overload Execute to support both TensorOperation and TensorTransform
Execute::Execute(std::shared_ptr<TensorOperation> op, MapTargetDevice device_type, uint32_t device_id) {
ops_.emplace_back(std::move(op));
device_type_ = device_type;

View File

@ -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();

View File

@ -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<int64_t> image_shape = input.at(0)->shape().AsVector();
std::vector<int64_t> 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<Tensor> 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<int32_t>(image_shape[1]), static_cast<int32_t>(image_shape[2]), lam, &x, &y, &crop_width,
&crop_height);
GetCropBox(static_cast<int32_t>(image_shape[dimension_one]), static_cast<int32_t>(image_shape[dimension_two]), lam,
&x, &y, &crop_width, &crop_height);
std::shared_ptr<Tensor> 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<float>(image_shape[1] * image_shape[2]));
*label_lam = value_one - (crop_width * crop_height /
static_cast<float>(image_shape[dimension_one] * image_shape[dimension_two]));
} else {
// NCHW Format
GetCropBox(static_cast<int32_t>(image_shape[2]), static_cast<int32_t>(image_shape[3]), lam, &x, &y, &crop_width,
&crop_height);
GetCropBox(static_cast<int32_t>(image_shape[dimension_two]), static_cast<int32_t>(image_shape[dimension_three]),
lam, &x, &y, &crop_width, &crop_height);
std::vector<std::shared_ptr<Tensor>> channels; // A vector holding channels of the CHW image
std::vector<std::shared_ptr<Tensor>> 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<float>(image_shape[2] * image_shape[3]));
*label_lam = value_one - (crop_width * crop_height /
static_cast<float>(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<int64_t> first_index = label_shape_size == 3 ? std::vector{index_i, j, k} : std::vector{index_i, k};
std::vector<int64_t> first_index =
label_shape_size == kMaxLabelShapeSize ? std::vector{index_i, j, k} : std::vector{index_i, k};
std::vector<int64_t> 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<Tensor> 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<size_t>(image_shape[0]); i++) {

View File

@ -721,6 +721,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: