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 // Constructor 54 // @param TaskType task_type - task type of VOC 55 // @param std::string task_mode - task mode of VOC 56 // @param std::string folder_path - dir directory of VOC 57 // @param std::map<std::string, int32_t> class_index - input class-to-index of annotation 58 // @param int32_t num_workers - number of workers reading images in parallel 59 // @param int32_t queue_size - connector queue size 60 // @param bool decode - whether to decode images 61 // @param std::unique_ptr<DataSchema> data_schema - the schema of the VOC dataset 62 // @param std::shared_ptr<Sampler> sampler - sampler tells VOCOp what to read 63 // @param extra_metadata - flag to add extra meta-data to row 64 VOCOp(const TaskType &task_type, const std::string &task_mode, const std::string &folder_path, 65 const std::map<std::string, int32_t> &class_index, int32_t num_workers, int32_t queue_size, bool decode, 66 std::unique_ptr<DataSchema> data_schema, std::shared_ptr<SamplerRT> sampler, bool extra_metadata); 67 68 // Destructor 69 ~VOCOp() = default; 70 71 // A print method typically used for debugging 72 // @param out 73 // @param show_all 74 void Print(std::ostream &out, bool show_all) const override; 75 76 // @param int64_t *count - output rows number of VOCDataset 77 Status CountTotalRows(int64_t *count); 78 79 // Op name getter 80 // @return Name of the current Op Name()81 std::string Name() const override { return "VOCOp"; } 82 83 // /// \brief Gets the class indexing 84 // /// \return Status - The status code return 85 Status GetClassIndexing(std::vector<std::pair<std::string, std::vector<int32_t>>> *output_class_indexing) override; 86 87 private: 88 // Load a tensor row according to image id 89 // @param row_id_type row_id - id for this tensor row 90 // @param std::string image_id - image id 91 // @param TensorRow row - image & target read into this tensor row 92 // @return Status The status code returned 93 Status LoadTensorRow(row_id_type row_id, TensorRow *row) override; 94 95 // @param const std::string &path - path to the image file 96 // @param const ColDescriptor &col - contains tensor implementation and datatype 97 // @param std::shared_ptr<Tensor> tensor - return 98 // @return Status The status code returned 99 Status ReadImageToTensor(const std::string &path, const ColDescriptor &col, std::shared_ptr<Tensor> *tensor); 100 101 // @param const std::string &path - path to the image file 102 // @param TensorRow *row - return 103 // @return Status The status code returned 104 Status ReadAnnotationToTensor(const std::string &path, TensorRow *row); 105 106 // Read image list from ImageSets 107 // @return Status The status code returned 108 Status ParseImageIds(); 109 110 // Read annotation from Annotation folder 111 // @return Status The status code returned 112 Status ParseAnnotationIds(); 113 114 // @param const std::string &path - path to annotation xml 115 // @return Status The status code returned 116 Status ParseAnnotationBbox(const std::string &path); 117 118 // @param xmin - the left coordinate of bndbox 119 // @param ymin - the top coordinate of bndbox 120 // @param xmax - the right coordinate of bndbox 121 // @param ymax - the bottom coordinate of bndbox 122 // @param path - the file path of bndbox xml 123 // @return Status The status code returned 124 Status CheckIfBboxValid(const float &xmin, const float &ymin, const float &xmax, const float &ymax, 125 const std::string &path); 126 127 // @param XMLElement *bbox_node - bbox node info found in json object 128 // @param const char *name - sub node name in object 129 // @param float *value - value of certain sub node 130 // @return Status The status code returned 131 void ParseNodeValue(XMLElement *bbox_node, const char *name, float *value); 132 133 // Called first when function is called 134 // @return Status The status code returned 135 Status LaunchThreadsAndInitOp() override; 136 137 // Private function for computing the assignment of the column name map. 138 // @return - Status 139 Status ComputeColMap() override; 140 141 bool decode_; 142 int64_t row_cnt_; 143 std::string folder_path_; 144 TaskType task_type_; 145 std::string usage_; 146 std::unique_ptr<DataSchema> data_schema_; 147 bool extra_metadata_; 148 149 std::vector<std::string> image_ids_; 150 std::map<std::string, int32_t> class_index_; 151 std::map<std::string, int32_t> label_index_; 152 std::map<std::string, Annotation> annotation_map_; 153 }; 154 } // namespace dataset 155 } // namespace mindspore 156 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_VOC_OP_H_ 157