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.

transforms.cc 3.2 kB

5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "minddata/dataset/include/transforms.h"
  17. #include "minddata/dataset/kernels/image/image_utils.h"
  18. // Kernel data headers (in alphabetical order)
  19. #include "minddata/dataset/kernels/data/one_hot_op.h"
  20. #include "minddata/dataset/kernels/data/type_cast_op.h"
  21. namespace mindspore {
  22. namespace dataset {
  23. namespace api {
  24. TensorOperation::TensorOperation() {}
  25. // Transform operations for data.
  26. namespace transforms {
  27. // FUNCTIONS TO CREATE DATA TRANSFORM OPERATIONS
  28. // (In alphabetical order)
  29. // Function to create OneHotOperation.
  30. std::shared_ptr<OneHotOperation> OneHot(int32_t num_classes) {
  31. auto op = std::make_shared<OneHotOperation>(num_classes);
  32. // Input validation
  33. if (!op->ValidateParams()) {
  34. return nullptr;
  35. }
  36. return op;
  37. }
  38. // Function to create TypeCastOperation.
  39. std::shared_ptr<TypeCastOperation> TypeCast(std::string data_type) {
  40. auto op = std::make_shared<TypeCastOperation>(data_type);
  41. // Input validation
  42. if (!op->ValidateParams()) {
  43. return nullptr;
  44. }
  45. return op;
  46. }
  47. /* ####################################### Validator Functions ############################################ */
  48. /* ####################################### Derived TensorOperation classes ################################# */
  49. // (In alphabetical order)
  50. // OneHotOperation
  51. OneHotOperation::OneHotOperation(int32_t num_classes) : num_classes_(num_classes) {}
  52. bool OneHotOperation::ValidateParams() {
  53. if (num_classes_ < 0) {
  54. MS_LOG(ERROR) << "OneHot: Number of classes cannot be negative. Number of classes: " << num_classes_;
  55. return false;
  56. }
  57. return true;
  58. }
  59. std::shared_ptr<TensorOp> OneHotOperation::Build() { return std::make_shared<OneHotOp>(num_classes_); }
  60. // TypeCastOperation
  61. TypeCastOperation::TypeCastOperation(std::string data_type) : data_type_(data_type) {}
  62. bool TypeCastOperation::ValidateParams() {
  63. std::vector<std::string> predefine_type = {"bool", "int8", "uint8", "int16", "uint16", "int32", "uint32",
  64. "int64", "uint64", "float16", "float32", "float64", "string"};
  65. auto itr = std::find(predefine_type.begin(), predefine_type.end(), data_type_);
  66. if (itr == predefine_type.end()) {
  67. MS_LOG(ERROR) << "TypeCast: Only support type bool, int8, uint8, int16, uint16, int32, uint32, "
  68. << "int64, uint64, float16, float32, float64, string, but got " << data_type_;
  69. return false;
  70. }
  71. return true;
  72. }
  73. std::shared_ptr<TensorOp> TypeCastOperation::Build() { return std::make_shared<TypeCastOp>(data_type_); }
  74. } // namespace transforms
  75. } // namespace api
  76. } // namespace dataset
  77. } // namespace mindspore