You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cutmix_batch_op_test.cc 5.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "common/cvop_common.h"
  18. #include "minddata/dataset/kernels/image/cutmix_batch_op.h"
  19. #include "utils/log_adapter.h"
  20. using namespace mindspore::dataset;
  21. using mindspore::LogStream;
  22. using mindspore::ExceptionType::NoExceptionType;
  23. using mindspore::MsLogLevel::INFO;
  24. class MindDataTestCutMixBatchOp : public UT::CVOP::CVOpCommon {
  25. protected:
  26. MindDataTestCutMixBatchOp() : CVOpCommon() {}
  27. };
  28. TEST_F(MindDataTestCutMixBatchOp, TestSuccess1) {
  29. MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp success1 case";
  30. std::shared_ptr<Tensor> input_tensor_resized;
  31. std::shared_ptr<Tensor> batched_tensor;
  32. std::shared_ptr<Tensor> batched_labels;
  33. Resize(input_tensor_, &input_tensor_resized, 227, 403);
  34. Tensor::CreateEmpty(TensorShape({2, input_tensor_resized->shape()[0], input_tensor_resized->shape()[1],
  35. input_tensor_resized->shape()[2]}), input_tensor_resized->type(), &batched_tensor);
  36. for (int i = 0; i < 2; i++) {
  37. batched_tensor->InsertTensor({i}, input_tensor_resized);
  38. }
  39. Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels);
  40. std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNHWC, 1.0, 1.0);
  41. TensorRow in;
  42. in.push_back(batched_tensor);
  43. in.push_back(batched_labels);
  44. TensorRow out;
  45. ASSERT_TRUE(op->Compute(in, &out).IsOk());
  46. EXPECT_EQ(in.at(0)->shape()[0], out.at(0)->shape()[0]);
  47. EXPECT_EQ(in.at(0)->shape()[1], out.at(0)->shape()[1]);
  48. EXPECT_EQ(in.at(0)->shape()[2], out.at(0)->shape()[2]);
  49. EXPECT_EQ(in.at(0)->shape()[3], out.at(0)->shape()[3]);
  50. EXPECT_EQ(in.at(1)->shape()[0], out.at(1)->shape()[0]);
  51. EXPECT_EQ(in.at(1)->shape()[1], out.at(1)->shape()[1]);
  52. }
  53. TEST_F(MindDataTestCutMixBatchOp, TestSuccess2) {
  54. MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp success2 case";
  55. std::shared_ptr<Tensor> input_tensor_resized;
  56. std::shared_ptr<Tensor> batched_tensor;
  57. std::shared_ptr<Tensor> batched_labels;
  58. std::shared_ptr<Tensor> chw_tensor;
  59. Resize(input_tensor_, &input_tensor_resized, 227, 403);
  60. ASSERT_TRUE(HwcToChw(input_tensor_resized, &chw_tensor).IsOk());
  61. Tensor::CreateEmpty(TensorShape({2, chw_tensor->shape()[0], chw_tensor->shape()[1], chw_tensor->shape()[2]}),
  62. chw_tensor->type(), &batched_tensor);
  63. for (int i = 0; i < 2; i++) {
  64. batched_tensor->InsertTensor({i}, chw_tensor);
  65. }
  66. Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels);
  67. std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNCHW, 1.0, 0.5);
  68. TensorRow in;
  69. in.push_back(batched_tensor);
  70. in.push_back(batched_labels);
  71. TensorRow out;
  72. ASSERT_TRUE(op->Compute(in, &out).IsOk());
  73. EXPECT_EQ(in.at(0)->shape()[0], out.at(0)->shape()[0]);
  74. EXPECT_EQ(in.at(0)->shape()[1], out.at(0)->shape()[1]);
  75. EXPECT_EQ(in.at(0)->shape()[2], out.at(0)->shape()[2]);
  76. EXPECT_EQ(in.at(0)->shape()[3], out.at(0)->shape()[3]);
  77. EXPECT_EQ(in.at(1)->shape()[0], out.at(1)->shape()[0]);
  78. EXPECT_EQ(in.at(1)->shape()[1], out.at(1)->shape()[1]);
  79. }
  80. TEST_F(MindDataTestCutMixBatchOp, TestFail1) {
  81. // This is a fail case because our labels are not batched and are 1-dimensional
  82. MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp fail1 case";
  83. std::shared_ptr<Tensor> labels;
  84. Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({4}), &labels);
  85. std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNHWC, 1.0, 1.0);
  86. TensorRow in;
  87. in.push_back(input_tensor_);
  88. in.push_back(labels);
  89. TensorRow out;
  90. ASSERT_FALSE(op->Compute(in, &out).IsOk());
  91. }
  92. TEST_F(MindDataTestCutMixBatchOp, TestFail2) {
  93. // This should fail because the image_batch_format provided is not the same as the actual format of the images
  94. MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp fail2 case";
  95. std::shared_ptr<Tensor> batched_tensor;
  96. std::shared_ptr<Tensor> batched_labels;
  97. Tensor::CreateEmpty(TensorShape({2, input_tensor_->shape()[0], input_tensor_->shape()[1], input_tensor_->shape()[2]}),
  98. input_tensor_->type(), &batched_tensor);
  99. for (int i = 0; i < 2; i++) {
  100. batched_tensor->InsertTensor({i}, input_tensor_);
  101. }
  102. Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels);
  103. std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNCHW, 1.0, 1.0);
  104. TensorRow in;
  105. in.push_back(batched_tensor);
  106. in.push_back(batched_labels);
  107. TensorRow out;
  108. ASSERT_FALSE(op->Compute(in, &out).IsOk());
  109. }