Apollo  6.0
Open source self driving car software
sequence_data_loader.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 #include <algorithm>
19 #include <fstream>
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include <boost/filesystem.hpp>
26 
28 
29 namespace apollo {
30 namespace perception {
31 namespace benchmark {
32 
33 // @brief General data loader base class, class DataType must implement
34 // "bool load(const std::vector<std::string>& filenames) and void release()"
35 // member function
36 template <class DataType>
38  public:
39  SequenceDataLoader() = default;
40  virtual ~SequenceDataLoader() = default;
41  bool init_loader_with_list(const std::vector<std::string>& file_lists);
42  bool init_loader_with_folder(const std::vector<std::string>& folders);
43  virtual bool query_next(std::shared_ptr<DataType>& data); // NOLINT
44  virtual bool query_last(std::shared_ptr<DataType>& data); // NOLINT
45  inline std::size_t size() { return _initialized ? _filenames[0].size() : 0; }
46  inline const std::vector<std::vector<std::string>>& get_filenames() const {
47  return _filenames;
48  }
49 
50  protected:
51  std::vector<std::vector<std::string>> _filenames;
52  int _idx = -1;
53  bool _initialized = false;
54 };
55 
56 template <class DataType>
58  const std::vector<std::string>& file_lists) {
59  _filenames.clear();
60  _filenames.resize(file_lists.size());
61 
62  std::ifstream fin;
63  std::string name = "";
64  std::vector<std::size_t> sorted_indices;
65  for (std::size_t i = 0; i < file_lists.size(); ++i) {
66  fin.open(file_lists[i].c_str());
67  if (!fin.is_open()) {
68  _initialized = false;
69  return false;
70  }
71  std::getline(fin, name);
72  while (!fin.eof()) {
73  _filenames[i].push_back(name);
74  std::getline(fin, name);
75  }
76  fin.close();
77  }
78  sort_strings_by_split_length(_filenames[0], &sorted_indices);
79  shuffle_by_indices(&_filenames[0], sorted_indices);
80 
81  for (std::size_t i = 1; i < file_lists.size(); ++i) {
82  if (_filenames[i].size() != _filenames[0].size()) {
83  _initialized = false;
84  return false;
85  }
86  shuffle_by_indices(&_filenames[i], sorted_indices);
87  }
88  _idx = -1;
89  _initialized = true;
90  return true;
91 }
92 
93 template <class DataType>
95  const std::vector<std::string>& folders) {
96  _filenames.clear();
97  _filenames.resize(folders.size());
98 
99  for (std::size_t i = 0; i < folders.size(); ++i) {
100  if (!boost::filesystem::exists(folders[i])) {
101  _initialized = false;
102  return false;
103  }
104  boost::filesystem::directory_iterator it(folders[i]);
105  boost::filesystem::directory_iterator eit;
106  while (it != eit) {
107  _filenames[i].push_back(it->path().string());
108  ++it;
109  }
110  std::sort(_filenames[i].begin(), _filenames[i].end(),
111  string_compare_by_length);
112  }
113  for (std::size_t i = 1; i < folders.size(); ++i) {
114  if (_filenames[i].size() != _filenames[0].size()) {
115  _initialized = false;
116  return false;
117  }
118  }
119  _idx = -1;
120  _initialized = true;
121  return true;
122 }
123 
124 template <class DataType>
126  std::shared_ptr<DataType>& data) { // NOLINT
127  if (!_initialized) {
128  return false;
129  }
130  if (data == nullptr) {
131  data.reset(new DataType);
132  }
133  ++_idx;
134  if (_idx >= static_cast<int>(_filenames[0].size())) {
135  return false;
136  } else if (_idx < 0) {
137  _idx = 0;
138  }
139  std::vector<std::string> files;
140  for (auto& names : _filenames) {
141  files.push_back(names[_idx]);
142  }
143  return data->load(files);
144 }
145 
146 template <class DataType>
148  std::shared_ptr<DataType>& data) { // NOLINT
149  if (!_initialized) {
150  return false;
151  }
152  if (data == nullptr) {
153  data.reset(new DataType);
154  }
155  --_idx;
156  if (_idx < 0) {
157  return false;
158  } else if (_idx >= static_cast<int>(_filenames[0].size())) {
159  _idx = static_cast<int>(_filenames[0].size() - 1);
160  }
161  std::vector<std::string> files;
162  for (auto& names : _filenames) {
163  files.push_back(names[_idx]);
164  }
165  return data->load(files);
166 }
167 
168 } // namespace benchmark
169 } // namespace perception
170 } // namespace apollo
bool _initialized
Definition: sequence_data_loader.h:53
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void shuffle_by_indices(std::vector< T > *src, const std::vector< std::size_t > &indices)
Definition: string_compare.h:95
bool init_loader_with_list(const std::vector< std::string > &file_lists)
Definition: sequence_data_loader.h:57
std::size_t size()
Definition: sequence_data_loader.h:45
const std::vector< std::vector< std::string > > & get_filenames() const
Definition: sequence_data_loader.h:46
bool init_loader_with_folder(const std::vector< std::string > &folders)
Definition: sequence_data_loader.h:94
Definition: sequence_data_loader.h:37
virtual bool query_last(std::shared_ptr< DataType > &data)
Definition: sequence_data_loader.h:147
std::vector< std::vector< std::string > > _filenames
Definition: sequence_data_loader.h:51
virtual bool query_next(std::shared_ptr< DataType > &data)
Definition: sequence_data_loader.h:125
int _idx
Definition: sequence_data_loader.h:52