mxnet
io.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef MXNET_CPP_IO_H_
27 #define MXNET_CPP_IO_H_
28 
29 #include <map>
30 #include <string>
31 #include <vector>
32 #include <sstream>
33 #include "mxnet-cpp/base.h"
34 #include "mxnet-cpp/ndarray.h"
35 #include "dmlc/logging.h"
36 
37 namespace mxnet {
38 namespace cpp {
43 class DataBatch {
44  public:
47  int pad_num;
48  std::vector<int> index;
49 };
50 class DataIter {
51  public:
52  virtual void BeforeFirst(void) = 0;
53  virtual bool Next(void) = 0;
54  virtual NDArray GetData(void) = 0;
55  virtual NDArray GetLabel(void) = 0;
56  virtual int GetPadNum(void) = 0;
57  virtual std::vector<int> GetIndex(void) = 0;
58 
60  return DataBatch{GetData(), GetLabel(), GetPadNum(), GetIndex()};
61  }
62  void Reset() { BeforeFirst(); }
63 };
64 
66  public:
67  inline MXDataIterMap() {
68  mx_uint num_data_iter_creators = 0;
69  DataIterCreator *data_iter_creators = nullptr;
70  int r = MXListDataIters(&num_data_iter_creators, &data_iter_creators);
71  CHECK_EQ(r, 0);
72  for (mx_uint i = 0; i < num_data_iter_creators; i++) {
73  const char *name;
74  const char *description;
75  mx_uint num_args;
76  const char **arg_names;
77  const char **arg_type_infos;
78  const char **arg_descriptions;
79  r = MXDataIterGetIterInfo(data_iter_creators[i], &name, &description,
80  &num_args, &arg_names, &arg_type_infos,
81  &arg_descriptions);
82  CHECK_EQ(r, 0);
83  mxdataiter_creators_[name] = data_iter_creators[i];
84  }
85  }
86  inline DataIterCreator GetMXDataIterCreator(const std::string &name) {
87  return mxdataiter_creators_[name];
88  }
89 
90  private:
91  std::map<std::string, DataIterCreator> mxdataiter_creators_;
92 };
93 
95  public:
96  MXDataIterBlob() : handle_(nullptr) {}
97  explicit MXDataIterBlob(DataIterHandle handle) : handle_(handle) {}
100 
101  private:
102  MXDataIterBlob &operator=(const MXDataIterBlob &);
103 };
104 
105 class MXDataIter : public DataIter {
106  public:
107  explicit MXDataIter(const std::string &mxdataiter_type);
108  MXDataIter(const MXDataIter &other) {
109  creator_ = other.creator_;
110  params_ = other.params_;
111  blob_ptr_ = other.blob_ptr_;
112  }
113  void BeforeFirst();
114  bool Next();
115  NDArray GetData();
116  NDArray GetLabel();
117  int GetPadNum();
118  std::vector<int> GetIndex();
119  MXDataIter CreateDataIter();
126  template <typename T>
127  MXDataIter &SetParam(const std::string &name, const T &value) {
128  std::string value_str;
129  std::stringstream ss;
130  ss << value;
131  ss >> value_str;
132 
133  params_[name] = value_str;
134  return *this;
135  }
136 
137  private:
138  DataIterCreator creator_;
139  std::map<std::string, std::string> params_;
140  std::shared_ptr<MXDataIterBlob> blob_ptr_;
141  static MXDataIterMap*& mxdataiter_map();
142 };
143 } // namespace cpp
144 } // namespace mxnet
145 
146 #endif // MXNET_CPP_IO_H_
147 
MXDataIterBlob(DataIterHandle handle)
Definition: io.h:97
MXDataIter & SetParam(const std::string &name, const T &value)
set config parameters
Definition: io.h:127
void * DataIterHandle
handle to a DataIterator
Definition: c_api.h:83
void Reset()
Definition: io.h:62
Definition: io.h:65
MXNET_DLL int MXDataIterFree(DataIterHandle handle)
Free the handle to the IO module.
namespace of mxnet
Definition: base.h:89
DataIterCreator GetMXDataIterCreator(const std::string &name)
Definition: io.h:86
~MXDataIterBlob()
Definition: io.h:98
MXNET_DLL int MXListDataIters(mx_uint *out_size, DataIterCreator **out_array)
List all the available iterator entries.
NDArray interface.
Definition: ndarray.h:121
Definition: io.h:105
MXDataIter(const MXDataIter &other)
Definition: io.h:108
MXDataIterMap()
Definition: io.h:67
unsigned int mx_uint
manually define unsigned int
Definition: c_api.h:58
std::vector< int > index
Definition: io.h:48
NDArray label
Definition: io.h:46
DataBatch GetDataBatch()
Definition: io.h:59
Definition: io.h:50
NDArray data
Definition: io.h:45
MXDataIterBlob()
Definition: io.h:96
int pad_num
Definition: io.h:47
void * DataIterCreator
handle a dataiter creator
Definition: c_api.h:81
MXNET_DLL int MXDataIterGetIterInfo(DataIterCreator creator, const char **name, const char **description, mx_uint *num_args, const char ***arg_names, const char ***arg_type_infos, const char ***arg_descriptions)
Get the detailed information about data iterator.
DataIterHandle handle_
Definition: io.h:99
Default object for holding a mini-batch of data and related information.
Definition: io.h:43
Definition: io.h:94