21 #ifndef KALDI_NNET_NNET_MATRIX_BUFFER_H_ 22 #define KALDI_NNET_NNET_MATRIX_BUFFER_H_ 41 matrix_buffer_size(3 * 1024)
45 opts->
Register(
"matrix-buffer-size", &matrix_buffer_size,
46 "Capacity of buffer for feature matrices, in MB.");
82 return (reader_->Done() && NumPairs() <= 1);
88 preferred_length_ = 0;
92 return current_->first;
95 return current_->second;
99 size_t SizeInBytes()
const;
102 size_t SizeInMegaBytes()
const;
105 size_t NumPairs()
const;
114 typedef std::pair<std::string,Matrix<BaseFloat> >
PairType;
133 if (SizeInMegaBytes() < 0.5 * opts_.matrix_buffer_size) {
141 if (preferred_length_ == 0) {
142 int32 longest = (--buffer_.end())->first;
144 std::vector<int32> keys;
145 BufferType::iterator it;
146 for (it = buffer_.begin(); it != buffer_.end(); ++it) {
147 int32 key = it->first;
148 int32 frames_in_bin = it->second.size() * key;
149 for (
int32 i = 0;
i < frames_in_bin;
i += longest) {
154 std::vector<int32>::iterator it2 = keys.begin();
155 std::advance(it2, rand() % keys.size());
156 preferred_length_ = (*it2);
160 BufferType::iterator it = buffer_.lower_bound(preferred_length_);
161 if (it == buffer_.end()) { --it; }
164 current_ = &(it->second.front());
169 for (BufferType::const_iterator it = buffer_.begin(); it != buffer_.end(); ++it) {
170 for (ListType::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
171 ans += it2->second.SizeInBytes();
178 return (SizeInBytes() / (1024 * 1024));
183 for (BufferType::const_iterator it = buffer_.begin(); it != buffer_.end(); ++it) {
184 ans += it->second.size();
190 if (!reader_->Done())
191 KALDI_LOG <<
"Read() started... Buffer size in MB: " 192 << SizeInMegaBytes() <<
", max " << opts_.matrix_buffer_size
193 <<
", having " << NumPairs() <<
" utterances.";
194 for ( ; !reader_->Done(); reader_->Next()) {
196 if (SizeInMegaBytes() > opts_.matrix_buffer_size) {
197 KALDI_LOG <<
"Read() finished... Buffer size in MB: " 198 << SizeInMegaBytes() <<
", max " << opts_.matrix_buffer_size
199 <<
", having " << NumPairs() <<
" utterances.";
203 const std::string& key = reader_->Key();
205 size_t num_rows = mat.
NumRows();
207 if (buffer_.find(num_rows) == buffer_.end()) {
211 buffer_[num_rows].push_back(
PairType(key, mat));
217 if (current_ != NULL) {
218 size_t r = current_->second.NumRows();
221 buffer_[r].pop_front();
223 if (buffer_[r].empty()) { buffer_.erase(r); }
232 #endif // KALDI_NNET_NNET_MATRIX_BUFFER_H_ void Init(SequentialBaseFloatMatrixReader *reader, MatrixBufferOptions opts=MatrixBufferOptions())
PairType * current_
The currently active (key,value) pair,.
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Matrix< BaseFloat > Value()
std::list< PairType > ListType
BufferType buffer_
Buffer indexed by 'NumRows()',.
std::pair< std::string, Matrix< BaseFloat > > PairType
void DisposeValue()
removes 'current_' from data structure,
SequentialBaseFloatMatrixReader * reader_
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
void Read()
fills the buffer,
std::map< size_t, ListType > BufferType
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
size_t NumPairs() const
Total number of (key,matrix) pairs in the buffer,.
A buffer for caching (utterance-key, feature-matrix) pairs.
void Register(OptionsItf *opts)
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
size_t SizeInMegaBytes() const
Total amount of features in the buffer (Mega-bytes),.
size_t SizeInBytes() const
Total amount of features in the buffer (bytes),.
MatrixBufferOptions opts_