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.h 3.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_TRANSFORMS_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_TRANSFORMS_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "minddata/dataset/core/constants.h"
  22. #include "minddata/dataset/util/status.h"
  23. namespace mindspore {
  24. namespace dataset {
  25. class TensorOp;
  26. namespace api {
  27. // Abstract class to represent a dataset in the data pipeline.
  28. class TensorOperation : public std::enable_shared_from_this<TensorOperation> {
  29. public:
  30. /// \brief Constructor
  31. TensorOperation();
  32. /// \brief Destructor
  33. ~TensorOperation() = default;
  34. /// \brief Pure virtual function to convert a TensorOperation class into a runtime TensorOp object.
  35. /// \return shared pointer to the newly created TensorOp.
  36. virtual std::shared_ptr<TensorOp> Build() = 0;
  37. virtual Status ValidateParams() = 0;
  38. };
  39. // Transform operations for performing data transformation.
  40. namespace transforms {
  41. // Transform Op classes (in alphabetical order)
  42. class OneHotOperation;
  43. class TypeCastOperation;
  44. /// \brief Function to create a OneHot TensorOperation.
  45. /// \notes Convert the labels into OneHot format.
  46. /// \param[in] num_classes number of classes.
  47. /// \return Shared pointer to the current TensorOperation.
  48. std::shared_ptr<OneHotOperation> OneHot(int32_t num_classes);
  49. /// \brief Function to create a TypeCast TensorOperation.
  50. /// \notes Tensor operation to cast to a given MindSpore data type.
  51. /// \param[in] data_type mindspore.dtype to be cast to.
  52. /// \return Shared pointer to the current TensorOperation.
  53. std::shared_ptr<TypeCastOperation> TypeCast(std::string data_type);
  54. /* ####################################### Derived TensorOperation classes ################################# */
  55. class OneHotOperation : public TensorOperation {
  56. public:
  57. explicit OneHotOperation(int32_t num_classes_);
  58. ~OneHotOperation() = default;
  59. std::shared_ptr<TensorOp> Build() override;
  60. Status ValidateParams() override;
  61. private:
  62. float num_classes_;
  63. };
  64. class TypeCastOperation : public TensorOperation {
  65. public:
  66. explicit TypeCastOperation(std::string data_type);
  67. ~TypeCastOperation() = default;
  68. std::shared_ptr<TensorOp> Build() override;
  69. Status ValidateParams() override;
  70. private:
  71. std::string data_type_;
  72. };
  73. } // namespace transforms
  74. } // namespace api
  75. } // namespace dataset
  76. } // namespace mindspore
  77. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_TRANSFORMS_H_