25
25
#include < deque>
26
26
#include < map>
27
27
#include < set>
28
+ #include < tuple>
28
29
#include < vector>
29
30
30
31
#include < algorithm>
@@ -187,7 +188,7 @@ void RecodeBeamSearch::ExtractBestPathAsWords(const TBOX& line_box,
187
188
GenericVector<int > xcoords;
188
189
GenericVector<const RecodeNode*> best_nodes;
189
190
GenericVector<const RecodeNode*> second_nodes;
190
- std::deque<std::pair <int ,int >> best_choices;
191
+ std::deque<std::tuple <int , int , double >> best_choices;
191
192
ExtractBestPaths (&best_nodes, &second_nodes);
192
193
if (debug) {
193
194
DebugPath (unicharset, best_nodes);
@@ -201,13 +202,14 @@ void RecodeBeamSearch::ExtractBestPathAsWords(const TBOX& line_box,
201
202
int timestepEnd = 0 ;
202
203
// if lstm choice mode is required in granularity level 2 it stores the x
203
204
// Coordinates of every chosen character to match the alternative choices to it
204
- if (lstm_choice_mode == 2 ) {
205
+ if (lstm_choice_mode == 2 || lstm_choice_mode == 3 ) {
205
206
ExtractPathAsUnicharIds (best_nodes, &unichar_ids, &certs, &ratings,
206
207
&xcoords, &best_choices);
207
208
if (best_choices.size () > 0 ) {
208
- current_char = best_choices.front ().first ;
209
- timestepEnd = best_choices.front ().second ;
210
- best_choices.pop_front ();
209
+ current_char = std::get<0 >(best_choices.front ());
210
+ timestepEnd = std::get<1 >(best_choices.front ());
211
+ if (lstm_choice_mode == 2 )
212
+ best_choices.pop_front ();
211
213
}
212
214
} else {
213
215
ExtractPathAsUnicharIds (best_nodes, &unichar_ids, &certs, &ratings,
@@ -258,7 +260,7 @@ void RecodeBeamSearch::ExtractBestPathAsWords(const TBOX& line_box,
258
260
choice_pairs.push_back (choice);
259
261
}
260
262
}
261
- if ((best_choices.size () > 0 && i == best_choices.front (). second - 1 )
263
+ if ((best_choices.size () > 0 && i == std::get< 1 >( best_choices.front ()) - 1 )
262
264
|| i == xcoords[word_end]-1 ) {
263
265
std::map<const char *, float > summed_propabilities;
264
266
for (auto it = choice_pairs.begin (); it != choice_pairs.end (); ++it) {
@@ -283,7 +285,7 @@ void RecodeBeamSearch::ExtractBestPathAsWords(const TBOX& line_box,
283
285
it->second ));
284
286
}
285
287
if (best_choices.size () > 0 ) {
286
- current_char = best_choices.front (). first ;
288
+ current_char = std::get< 0 >( best_choices.front ()) ;
287
289
best_choices.pop_front ();
288
290
}
289
291
choice_pairs.clear ();
@@ -292,6 +294,25 @@ void RecodeBeamSearch::ExtractBestPathAsWords(const TBOX& line_box,
292
294
}
293
295
}
294
296
timestepEnd = xcoords[word_end];
297
+ } else if (lstm_choice_mode == 3 ) {
298
+ std::vector<std::vector<std::pair<const char *, float >>> currentSymbol;
299
+ for (size_t i = timestepEnd; i < xcoords[word_end]; i++) {
300
+ if (i == std::get<1 >(best_choices.front ())) {
301
+ if (currentSymbol.size () > 0 ) {
302
+ word_res->symbol_steps .push_back (currentSymbol);
303
+ currentSymbol.clear ();
304
+ }
305
+ std::vector<std::pair<const char *, float >> choice_Header;
306
+ choice_Header.push_back (std::pair<const char *, float >(
307
+ unicharset->id_to_unichar_ext (std::get<0 >(best_choices.front ())),
308
+ 2.0 ));
309
+ currentSymbol.push_back (choice_Header);
310
+ if (best_choices.size ()>1 ) best_choices.pop_front ();
311
+ }
312
+ currentSymbol.push_back (timesteps[i]);
313
+ }
314
+ word_res->symbol_steps .push_back (currentSymbol);
315
+ timestepEnd = xcoords[word_end];
295
316
}
296
317
for (int i = word_start; i < word_end; ++i) {
297
318
BLOB_CHOICE_LIST* choices = new BLOB_CHOICE_LIST;
@@ -366,7 +387,7 @@ void RecodeBeamSearch::ExtractPathAsUnicharIds(
366
387
const GenericVector<const RecodeNode*>& best_nodes,
367
388
GenericVector<int >* unichar_ids, GenericVector<float >* certs,
368
389
GenericVector<float >* ratings, GenericVector<int >* xcoords,
369
- std::deque<std::pair <int , int >>* best_choices) {
390
+ std::deque<std::tuple <int , int , double >>* best_choices) {
370
391
unichar_ids->truncate (0 );
371
392
certs->truncate (0 );
372
393
ratings->truncate (0 );
@@ -375,6 +396,8 @@ void RecodeBeamSearch::ExtractPathAsUnicharIds(
375
396
int t = 0 ;
376
397
int width = best_nodes.size ();
377
398
while (t < width) {
399
+ int id;
400
+ int tposition;
378
401
double certainty = 0.0 ;
379
402
double rating = 0.0 ;
380
403
while (t < width && best_nodes[t]->unichar_id == INVALID_UNICHAR_ID) {
@@ -396,7 +419,8 @@ void RecodeBeamSearch::ExtractPathAsUnicharIds(
396
419
unichar_ids->push_back (unichar_id);
397
420
xcoords->push_back (t);
398
421
if (best_choices != nullptr ) {
399
- best_choices->push_back (std::pair<int , int >(unichar_id, t));
422
+ tposition = t;
423
+ id = unichar_id;
400
424
}
401
425
do {
402
426
double cert = best_nodes[t++]->certainty ;
@@ -414,6 +438,10 @@ void RecodeBeamSearch::ExtractPathAsUnicharIds(
414
438
if (certainty < certs->back ()) certs->back () = certainty;
415
439
ratings->back () += rating;
416
440
}
441
+ if (best_choices != nullptr ) {
442
+ best_choices->push_back (
443
+ std::tuple<int , int , double >(id, tposition, rating));
444
+ }
417
445
}
418
446
xcoords->push_back (width);
419
447
}
0 commit comments