Repository navigation
Expand file tree
/
Copy pathclstmhl.h
More file actions
179 lines (174 loc) · 5.8 KB
/
Copy pathclstmhl.h
File metadata and controls
179 lines (174 loc) · 5.8 KB
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
// -*- C++ -*-
#ifndef ocropus_clstmhl_
#define ocropus_clstmhl_
#include "pstring.h"
#include "clstm.h"
#include "extras.h"
#include <memory>
#include <string>
#include <vector>
namespace ocropus {
struct CLSTMText {
Network net;
int nclasses = -1;
int iclasses = -1;
int neps = 3;
Sequence targets;
Sequence aligned;
void setLearningRate(float lr, float mom) {
net->setLearningRate(lr, mom);
}
void load(const std::string &fname) {
net = load_net(fname);
nclasses = net->codec.size();
iclasses = net->icodec.size();
if (net->attributes["neps"] == "") {
cerr << "WARNING: no neps";
} else {
neps = stoi(net->attributes["neps"]);
}
net->makeEncoders();
}
void save(const std::string &fname) {
save_net(fname, net);
}
void createBidi(const std::vector<int> &icodec, const std::vector<int> codec, int nhidden) {
// This is just the simplest case of creating a network. For more complex
// networks, create them outside and assign them to "net".
iclasses = icodec.size();
nclasses = codec.size();
net = make_net("bidi", {
{"ninput", (int)icodec.size()},
{"noutput", (int)codec.size()},
{"nhidden", nhidden}
});
net->attributes["neps"] = std::to_string(neps);
net->icodec = icodec;
net->codec = codec;
net->makeEncoders();
}
void setInputs(const std::wstring &s) {
Classes cs;
net->iencode(cs, s);
Sequence &seq = net->inputs;
int d = net->ninput();
seq.clear();
for (int i = 0; i < neps; i++) seq.push_back(Vec::Zero(d));
for (int pos = 0; pos < cs.size(); pos++) {
int c = cs[pos];
Vec v = Vec::Zero(d);
v[c] = 1.0;
seq.push_back(v);
for (int i = 0; i < neps; i++) seq.push_back(Vec::Zero(d));
}
}
std::wstring train(const std::wstring &in, const std::wstring &target) {
setInputs(in);
net->forward();
Classes transcript;
net->encode(transcript, target);
mktargets(targets, transcript, nclasses);
ctc_align_targets(aligned, net->outputs, targets);
net->d_outputs.resize(net->outputs.size());
for (int t = 0; t < aligned.size(); t++)
net->d_outputs[t] = aligned[t] - net->outputs[t];
net->backward();
net->update();
Classes output_classes;
trivial_decode(output_classes, net->outputs);
return net->decode(output_classes);
}
std::wstring predict(const std::wstring &in) {
setInputs(in);
net->forward();
Classes output_classes;
trivial_decode(output_classes, net->outputs);
return net->decode(output_classes);
}
void train_utf8(const std::string &in, const std::string &target) {
train(utf8_to_utf32(in), utf8_to_utf32(target));
}
std::string aligned_utf8() {
Classes outputs;
trivial_decode(outputs, aligned);
std::wstring temp = net->decode(outputs);
return utf32_to_utf8(temp);
}
std::string predict_utf8(const std::string &in) {
return utf32_to_utf8(predict(utf8_to_utf32(in)));
}
};
struct CLSTMOCR {
unique_ptr<INormalizer> normalizer;
Network net;
int target_height = 48;
int nclasses = -1;
Sequence aligned, targets;
mdarray<float> image;
void setLearningRate(float lr, float mom) {
net->setLearningRate(lr, mom);
}
void load(const std::string &fname) {
net = load_net(fname);
nclasses = net->codec.size();
normalizer.reset(make_CenterNormalizer());
normalizer->target_height = target_height;
}
void save(const std::string &fname) {
save_net(fname, net);
}
void createBidi(const std::vector<int> codec, int nhidden) {
nclasses = codec.size();
net = make_net("bidi", {
{"ninput", target_height},
{"noutput", nclasses},
{"nhidden", nhidden}
});
net->initialize();
net->codec = codec;
net->makeEncoders();
normalizer.reset(make_CenterNormalizer());
normalizer->target_height = target_height;
}
std::wstring train(mdarray<float> &raw, const std::wstring &target) {
normalizer->measure(raw);
normalizer->normalize(image, raw);
assign(net->inputs, image);
net->forward();
Classes transcript;
net->encode(transcript, target);
mktargets(targets, transcript, nclasses);
ctc_align_targets(aligned, net->outputs, targets);
net->d_outputs.resize(net->outputs.size());
for (int t = 0; t < aligned.size(); t++)
net->d_outputs[t] = aligned[t] - net->outputs[t];
net->backward();
net->update();
Classes outputs;
trivial_decode(outputs, net->outputs);
return net->decode(outputs);
}
std::string aligned_utf8() {
Classes outputs;
trivial_decode(outputs, aligned);
std::wstring temp = net->decode(outputs);
return utf32_to_utf8(temp);
}
std::string train_utf8(mdarray<float> &raw, const std::string &target) {
return utf32_to_utf8(train(raw, utf8_to_utf32(target)));
}
std::wstring predict(mdarray<float> &raw) {
normalizer->measure(raw);
normalizer->normalize(image, raw);
assign(net->inputs, image);
net->forward();
Classes outputs;
trivial_decode(outputs, net->outputs);
return net->decode(outputs);
}
std::string predict_utf8(mdarray<float> &raw) {
return utf32_to_utf8(predict(raw));
}
};
}
#endif