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 
25 #ifndef MXNET_CPP_IO_H_
26 #define MXNET_CPP_IO_H_
27 
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <sstream>
32 #include "mxnet-cpp/base.h"
33 #include "mxnet-cpp/ndarray.h"
34 #include "dmlc/logging.h"
35 
36 namespace mxnet {
37 namespace cpp {
42 class DataBatch {
43  public:
46  int pad_num;
47  std::vector<int> index;
48 };
49 class DataIter {
50  public:
51  virtual void BeforeFirst(void) = 0;
52  virtual bool Next(void) = 0;
53  virtual NDArray GetData(void) = 0;
54  virtual NDArray GetLabel(void) = 0;
55  virtual int GetPadNum(void) = 0;
56  virtual std::vector<int> GetIndex(void) = 0;
57 
59  return DataBatch{GetData(), GetLabel(), GetPadNum(), GetIndex()};
60  }
61  void Reset() { BeforeFirst(); }
62 };
63 
65  public:
66  inline MXDataIterMap() {
67  mx_uint num_data_iter_creators = 0;
68  DataIterCreator *data_iter_creators = nullptr;
69  int r = MXListDataIters(&num_data_iter_creators, &data_iter_creators);
70  CHECK_EQ(r, 0);
71  for (mx_uint i = 0; i < num_data_iter_creators; i++) {
72  const char *name;
73  const char *description;
74  mx_uint num_args;
75  const char **arg_names;
76  const char **arg_type_infos;
77  const char **arg_descriptions;
78  r = MXDataIterGetIterInfo(data_iter_creators[i], &name, &description,
79  &num_args, &arg_names, &arg_type_infos,
80  &arg_descriptions);
81  CHECK_EQ(r, 0);
82  mxdataiter_creators_[name] = data_iter_creators[i];
83  }
84  }
85  inline DataIterCreator GetMXDataIterCreator(const std::string &name) {
86  return mxdataiter_creators_[name];
87  }
88 
89  private:
90  std::map<std::string, DataIterCreator> mxdataiter_creators_;
91 };
92 
94  public:
95  MXDataIterBlob() : handle_(nullptr) {}
96  explicit MXDataIterBlob(DataIterHandle handle) : handle_(handle) {}
99 
100  private:
101  MXDataIterBlob &operator=(const MXDataIterBlob &);
102 };
103 
104 class MXDataIter : public DataIter {
105  public:
106  explicit MXDataIter(const std::string &mxdataiter_type);
107  MXDataIter(const MXDataIter &other) {
108  creator_ = other.creator_;
109  params_ = other.params_;
110  blob_ptr_ = other.blob_ptr_;
111  }
112  void BeforeFirst();
113  bool Next();
114  NDArray GetData();
115  NDArray GetLabel();
116  int GetPadNum();
117  std::vector<int> GetIndex();
118  MXDataIter CreateDataIter();
125  template <typename T>
126  MXDataIter &SetParam(const std::string &name, const T &value) {
127  std::string value_str;
128  std::stringstream ss;
129  ss << value;
130  ss >> value_str;
131 
132  params_[name] = value_str;
133  return *this;
134  }
135 
136  private:
137  DataIterCreator creator_;
138  std::map<std::string, std::string> params_;
139  std::shared_ptr<MXDataIterBlob> blob_ptr_;
140  static MXDataIterMap*& mxdataiter_map();
141 };
142 } // namespace cpp
143 } // namespace mxnet
144 
145 #endif // MXNET_CPP_IO_H_
146 
MXDataIterBlob(DataIterHandle handle)
Definition: io.h:96
MXDataIter & SetParam(const std::string &name, const T &value)
set config parameters
Definition: io.h:126
void * DataIterHandle
handle to a DataIterator
Definition: c_api.h:80
void Reset()
Definition: io.h:61
Definition: io.h:64
MXNET_DLL int MXDataIterFree(DataIterHandle handle)
Free the handle to the IO module.
namespace of mxnet
Definition: base.h:126
DataIterCreator GetMXDataIterCreator(const std::string &name)
Definition: io.h:85
~MXDataIterBlob()
Definition: io.h:97
MXNET_DLL int MXListDataIters(mx_uint *out_size, DataIterCreator **out_array)
List all the available iterator entries.
NDArray interface.
Definition: ndarray.h:120
Definition: io.h:104
MXDataIter(const MXDataIter &other)
Definition: io.h:107
MXDataIterMap()
Definition: io.h:66
unsigned int mx_uint
manually define unsigned int
Definition: c_api.h:57
std::vector< int > index
Definition: io.h:47
NDArray label
Definition: io.h:45
DataBatch GetDataBatch()
Definition: io.h:58
Definition: io.h:49
NDArray data
Definition: io.h:44
MXDataIterBlob()
Definition: io.h:95
int pad_num
Definition: io.h:46
void * DataIterCreator
handle a dataiter creator
Definition: c_api.h:78
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:98
Default object for holding a mini-batch of data and related information.
Definition: io.h:42
Definition: io.h:93