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