1 /** 2 * Copyright 2019-2021 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_ENGINE_DATASETOPS_SOURCE_VOC_OP_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_VOC_OP_H_ 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "tinyxml2.h" 26 #include "minddata/dataset/core/tensor.h" 27 28 #include "minddata/dataset/engine/data_schema.h" 29 #include "minddata/dataset/engine/datasetops/parallel_op.h" 30 #include "minddata/dataset/engine/datasetops/source/mappable_leaf_op.h" 31 #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h" 32 #include "minddata/dataset/kernels/image/image_utils.h" 33 #include "minddata/dataset/util/path.h" 34 #include "minddata/dataset/util/queue.h" 35 #include "minddata/dataset/util/status.h" 36 #include "minddata/dataset/util/wait_post.h" 37 38 using tinyxml2::XMLDocument; 39 using tinyxml2::XMLElement; 40 using tinyxml2::XMLError; 41 namespace mindspore { 42 namespace dataset { 43 // Forward declares 44 template <typename T> 45 class Queue; 46 47 using Annotation = std::vector<std::pair<std::string, std::vector<float>>>; 48 49 class VOCOp : public MappableLeafOp { 50 public: 51 enum class TaskType { Segmentation = 0, Detection = 1 }; 52 53 #ifdef ENABLE_PYTHON 54 // Constructor 55 // @param TaskType task_type - task type of VOC 56 // @param std::string task_mode - task mode of VOC 57 // @param std::string folder_path - dir directory of VOC 58 // @param std::map<std::string, int32_t> class_index - input class-to-index of annotation 59 // @param int32_t num_workers - number of workers reading images in parallel 60 // @param int32_t queue_size - connector queue size 61 // @param bool decode - whether to decode images 62 // @param std::unique_ptr<DataSchema> data_schema - the schema of the VOC dataset 63 // @param std::shared_ptr<Sampler> sampler - sampler tells VOCOp what to read 64 // @param extra_metadata - flag to add extra meta-data to row 65 // @param py::function decrypt - Image decryption function, which accepts the path of the encrypted image file 66 // and returns the decrypted bytes data. Default: None, no decryption. 67 VOCOp(const TaskType &task_type, const std::string &task_mode, const std::string &folder_path, 68 const std::map<std::string, int32_t> &class_index, int32_t num_workers, int32_t queue_size, bool decode, 69 std::unique_ptr<DataSchema> data_schema, std::shared_ptr<SamplerRT> sampler, bool extra_metadata, 70 py::function decrypt = py::none()); 71 #else 72 // Constructor 73 // @param TaskType task_type - task type of VOC 74 // @param std::string task_mode - task mode of VOC 75 // @param std::string folder_path - dir directory of VOC 76 // @param std::map<std::string, int32_t> class_index - input class-to-index of annotation 77 // @param int32_t num_workers - number of workers reading images in parallel 78 // @param int32_t queue_size - connector queue size 79 // @param bool decode - whether to decode images 80 // @param std::unique_ptr<DataSchema> data_schema - the schema of the VOC dataset 81 // @param std::shared_ptr<Sampler> sampler - sampler tells VOCOp what to read 82 // @param extra_metadata - flag to add extra meta-data to row 83 VOCOp(const TaskType &task_type, const std::string &task_mode, const std::string &folder_path, 84 const std::map<std::string, int32_t> &class_index, int32_t num_workers, int32_t queue_size, bool decode, 85 std::unique_ptr<DataSchema> data_schema, std::shared_ptr<SamplerRT> sampler, bool extra_metadata); 86 #endif 87 88 // Destructor 89 ~VOCOp() = default; 90 91 // A print method typically used for debugging 92 // @param out 93 // @param show_all 94 void Print(std::ostream &out, bool show_all) const override; 95 96 // @param int64_t *count - output rows number of VOCDataset 97 Status CountTotalRows(int64_t *count); 98 99 // Op name getter 100 // @return Name of the current Op Name()101 std::string Name() const override { return "VOCOp"; } 102 103 // /// \brief Gets the class indexing 104 // /// \return Status - The status code return 105 Status GetClassIndexing(std::vector<std::pair<std::string, std::vector<int32_t>>> *output_class_indexing) override; 106 107 private: 108 // Load a tensor row according to image id 109 // @param row_id_type row_id - id for this tensor row 110 // @param std::string image_id - image id 111 // @param TensorRow row - image & target read into this tensor row 112 // @return Status The status code returned 113 Status LoadTensorRow(row_id_type row_id, TensorRow *row) override; 114 115 // @param const std::string &path - path to the image file 116 // @param const ColDescriptor &col - contains tensor implementation and datatype 117 // @param std::shared_ptr<Tensor> tensor - return 118 // @return Status The status code returned 119 Status ReadImageToTensor(const std::string &path, const ColDescriptor &col, std::shared_ptr<Tensor> *tensor); 120 121 // @param const std::string &path - path to the image file 122 // @param TensorRow *row - return 123 // @return Status The status code returned 124 Status ReadAnnotationToTensor(const std::string &path, TensorRow *row); 125 126 // Read image list from ImageSets 127 // @return Status The status code returned 128 Status ParseImageIds(); 129 130 // Read annotation from Annotation folder 131 // @return Status The status code returned 132 Status ParseAnnotationIds(); 133 134 // @param const std::string &path - path to annotation xml 135 // @return Status The status code returned 136 Status ParseAnnotationBbox(const std::string &path); 137 138 // @param xmin - the left coordinate of bndbox 139 // @param ymin - the top coordinate of bndbox 140 // @param xmax - the right coordinate of bndbox 141 // @param ymax - the bottom coordinate of bndbox 142 // @param path - the file path of bndbox xml 143 // @return Status The status code returned 144 Status CheckIfBboxValid(const float &xmin, const float &ymin, const float &xmax, const float &ymax, 145 const std::string &path); 146 147 // @param XMLElement *bbox_node - bbox node info found in json object 148 // @param const char *name - sub node name in object 149 // @param float *value - value of certain sub node 150 // @return Status The status code returned 151 void ParseNodeValue(XMLElement *bbox_node, const char *name, float *value); 152 153 // Private function for computing the assignment of the column name map. 154 // @return - Status 155 Status ComputeColMap() override; 156 157 protected: 158 Status PrepareData() override; 159 160 private: 161 bool decode_; 162 int64_t row_cnt_; 163 std::string folder_path_; 164 TaskType task_type_; 165 std::string usage_; 166 std::unique_ptr<DataSchema> data_schema_; 167 bool extra_metadata_; 168 169 std::vector<std::string> image_ids_; 170 std::map<std::string, int32_t> class_index_; 171 std::map<std::string, int32_t> label_index_; 172 std::map<std::string, Annotation> annotation_map_; 173 #ifdef ENABLE_PYTHON 174 py::function decrypt_; 175 #endif 176 }; 177 } // namespace dataset 178 } // namespace mindspore 179 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_VOC_OP_H_ 180