/
ctc.cc
190 lines (176 loc) · 5.14 KB
/
ctc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "clstm.h"
#include <assert.h>
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <math.h>
#include <stdarg.h>
#include "clstm_compute.h"
#ifndef MAXEXP
#define MAXEXP 30
#endif
namespace ocropus {
using namespace std;
using Eigen::Ref;
static void forward_algorithm(Tensor2 &lr, Tensor2 &lmatch, double skip = -5) {
int n = rows(lmatch), m = cols(lmatch);
lr.resize(n, m);
Tensor1 v(m), w(m);
for (int j = 0; j < m; j++) v(j) = skip * j;
for (int i = 0; i < n; i++) {
w(0) = skip * i;
for (int j = 1; j < m; j++) w(j) = v(j - 1);
for (int j = 0; j < m; j++) {
Float same = log_mul(v(j), lmatch(i, j));
Float next = log_mul(w(j), lmatch(i, j));
v(j) = log_add(same, next);
}
for (int j = 0; j < m; j++) lr(i, j) = v(j);
}
}
static void forwardbackward(Tensor2 &both, Tensor2 &lmatch) {
int n = rows(lmatch), m = cols(lmatch);
Tensor2 lr;
forward_algorithm(lr, lmatch);
Tensor2 rlmatch(n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) rlmatch(i, j) = lmatch(n - i - 1, m - j - 1);
Tensor2 rrl;
forward_algorithm(rrl, rlmatch);
Tensor2 rl(n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) rl(i, j) = rrl(n - i - 1, m - j - 1);
both = lr + rl;
}
void ctc_align_targets(Tensor2 &posteriors, Tensor2 &outputs,
Tensor2 &targets) {
double lo = 1e-5;
int n1 = rows(outputs);
int n2 = rows(targets);
int nc = cols(targets);
assert(nc == cols(outputs));
// compute log probability of state matches
Tensor2 lmatch;
lmatch.resize(n1, n2);
for (int t1 = 0; t1 < n1; t1++) {
Tensor1 out(nc);
for (int i = 0; i < nc; i++) out(i) = fmax(lo, outputs(t1, i));
out = out / sum(out);
for (int t2 = 0; t2 < n2; t2++) {
double total = 0.0;
for (int k = 0; k < nc; k++) total += out(k) * targets(t2, k);
lmatch(t1, t2) = log(total);
}
}
// compute unnormalized forward backward algorithm
Tensor2 both;
forwardbackward(both, lmatch);
// compute normalized state probabilities
Tensor2 epath = (both - maximum(both)).unaryExpr(ptr_fun(limexp));
for (int j = 0; j < n2; j++) {
double total = 0.0;
for (int i = 0; i < rows(epath); i++) total += epath(i, j);
total = fmax(1e-9, total);
for (int i = 0; i < rows(epath); i++) epath(i, j) /= total;
}
// compute posterior probabilities for each class and normalize
Tensor2 aligned;
aligned.resize(n1, nc);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < nc; j++) {
double total = 0.0;
for (int k = 0; k < n2; k++) {
double value = epath(i, k) * targets(k, j);
total += value;
}
aligned(i, j) = total;
}
}
for (int i = 0; i < n1; i++) {
double total = 0.0;
for (int j = 0; j < nc; j++) total += aligned(i, j);
total = fmax(total, 1e-9);
for (int j = 0; j < nc; j++) aligned(i, j) /= total;
}
posteriors = aligned;
}
void ctc_align_targets(Sequence &posteriors, Sequence &outputs,
Sequence &targets) {
assert(outputs.cols() == 1);
assert(targets.cols() == 1);
assert(outputs.rows() == targets.rows());
int n1 = outputs.size();
int n2 = targets.size();
int nc = targets[0].rows();
Tensor2 moutputs(n1, nc);
Tensor2 mtargets(n2, nc);
for (int i = 0; i < n1; i++)
for (int j = 0; j < nc; j++) moutputs(i, j) = outputs[i].v(j, 0);
for (int i = 0; i < n2; i++)
for (int j = 0; j < nc; j++) mtargets(i, j) = targets[i].v(j, 0);
Tensor2 aligned;
ctc_align_targets(aligned, moutputs, mtargets);
posteriors.resize(n1, nc, 1);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < nc; j++) posteriors[i].v(j, 0) = aligned(i, j);
}
}
void ctc_align_targets(Sequence &posteriors, Sequence &outputs,
Classes &targets) {
int nclasses = outputs.rows();
Sequence stargets;
stargets.resize(targets.size());
for (int t = 0; t < stargets.size(); t++) {
stargets[t].resize(nclasses, 1);
stargets[t].V().setConstant(0);
stargets[t].v(targets[t], 0) = 1.0;
}
ctc_align_targets(posteriors, outputs, stargets);
}
void mktargets(Sequence &seq, Classes &transcript, int ndim) {
seq.resize(2 * transcript.size() + 1);
for (int t = 0; t < seq.size(); t++) {
seq[t].setZero(ndim, 1);
if (t % 2 == 1)
seq[t].v(transcript[(t - 1) / 2],0) = 1;
else
seq[t].v(0,0) = 1;
}
}
void trivial_decode(Classes &cs, Sequence &outputs, int batch,
vector<int> *locs) {
cs.clear();
if (locs) locs->clear();
int N = outputs.size();
int t = 0;
float mv = 0;
int mc = -1;
int mt = -1;
while (t < N) {
int index = argmax(outputs[t].V().chip(batch,1));
float v = outputs[t].v(index, batch);
if (index == 0) {
// NB: there should be a 0 at the end anyway
if (mc != -1 && mc != 0) {
cs.push_back(mc);
if (locs) locs->push_back(mt);
}
mv = 0;
mc = -1;
mt = -1;
t++;
continue;
}
if (v > mv) {
mv = v;
mc = index;
mt = t;
}
t++;
}
}
void trivial_decode(Classes &cs, Sequence &outputs, int batch) {
trivial_decode(cs, outputs, batch, nullptr);
}
} // ocropus