-
Notifications
You must be signed in to change notification settings - Fork 226
/
clstm_proto.cc
181 lines (168 loc) · 5.23 KB
/
clstm_proto.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
// Loading and saving networks using protobuf library.
// See clstm.proto for the protocol buffer definitions used here.
#include "clstm.h"
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <fstream>
#include <iostream>
#include <iostream>
#include <memory>
#include <string>
#include <typeinfo>
#include <vector>
#include "utils.h"
#ifdef GOOGLE
#include "third_party/clstm/tensor/clstm.pb.h"
#else
#include "clstm.pb.h"
#endif
namespace ocropus {
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
using std::ios;
using std::ofstream;
using std::ifstream;
using std::to_string;
bool proto_verbose =
getenv("clstm_proto_verbose") && atoi(getenv("clstm_proto_verbose"));
void proto_of_params(clstm::Array *array, Params ¶ms, bool weights = true) {
Tensor2 temp;
temp = params.v; // copy values in case they are on GPU
TensorMap2 a = temp();
int n = a.dimension(0), m = a.dimension(1);
array->add_dim(n);
array->add_dim(m);
if (!weights) return;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) array->add_value(a(i, j));
}
void params_of_proto(Params ¶ms, const clstm::Array *array) {
if (array->dim_size() != 2)
throwf("bad format (Mat, %s, %d)", array->name().c_str(),
array->dim_size());
params.setZero(array->dim(0), array->dim(1));
TensorMap2 a = params.v();
if (array->value_size() > 0) {
if (array->value_size() != a.size()) THROW("bad size (Mat)");
int k = 0;
for (int i = 0; i < a.dimension(0); i++)
for (int j = 0; j < a.dimension(1); j++) a(i, j) = array->value(k++);
}
}
void proto_of_net(clstm::NetworkProto *proto, INetwork *net,
bool weights = true) {
if (net->kind == "") {
cerr << typeid(*net).name() << endl;
assert(net->kind != "");
}
proto->set_kind(net->kind);
proto->set_ninput(net->ninput());
proto->set_noutput(net->noutput());
assert(proto->kind() != "");
assert(proto->ninput() >= 0);
assert(proto->ninput() < 1000000);
assert(proto->noutput() >= 0);
assert(proto->noutput() < 1000000);
for (int i = 0; i < net->icodec.size(); i++)
proto->add_icodec(net->icodec.codec[i]);
for (int i = 0; i < net->codec.size(); i++)
proto->add_codec(net->codec.codec[i]);
for (auto kv : net->attr) {
if (kv.first == "name") continue;
if (kv.first == "ninput") continue;
if (kv.first == "noutput") continue;
clstm::KeyValue *kvp = proto->add_attribute();
kvp->set_key(kv.first);
kvp->set_value(kv.second);
}
for (auto it : net->parameters) {
Params *a = it.second;
string name = it.first;
clstm::Array *array = proto->add_weights();
array->set_name(name);
proto_of_params(array, *a, weights);
}
for (int i = 0; i < net->sub.size(); i++) {
clstm::NetworkProto *subproto = proto->add_sub();
proto_of_net(subproto, net->sub[i].get(), weights);
}
}
Network net_of_proto(const clstm::NetworkProto *proto) {
Network net;
assert(proto->kind() != "");
assert(proto->ninput() >= 0);
assert(proto->ninput() < 1000000);
assert(proto->noutput() >= 0);
assert(proto->noutput() < 1000000);
net = make_layer(proto->kind());
net->attr.set("ninput", proto->ninput());
net->attr.set("noutput", proto->noutput());
for (int i = 0; i < proto->attribute_size(); i++) {
const clstm::KeyValue *attr = &proto->attribute(i);
net->attr.set(attr->key(), std::string(attr->value()));
}
vector<int> icodec;
for (int i = 0; i < proto->icodec_size(); i++)
icodec.push_back(proto->icodec(i));
net->icodec.set(icodec);
vector<int> codec;
for (int i = 0; i < proto->codec_size(); i++)
codec.push_back(proto->codec(i));
net->codec.set(codec);
map<string, Params *> weights;
for (auto it : net->parameters) {
weights[it.first] = it.second;
}
for (int i = 0; i < proto->weights_size(); i++) {
string key = proto->weights(i).name();
Params *a = weights[key];
params_of_proto(*a, &proto->weights(i));
}
for (int i = 0; i < proto->sub_size(); i++) {
net->add(net_of_proto(&proto->sub(i)));
net->sub[i]->attr.super = &net->attr;
}
net->postLoad();
return net;
}
Network proto_clone_net(INetwork *net) {
clstm::NetworkProto *proto = new clstm::NetworkProto();
proto_of_net(proto, net);
Network net2 = net_of_proto(proto);
return net2;
}
void debug_as_proto(INetwork *net, bool weights) {
clstm::NetworkProto *proto = new clstm::NetworkProto();
proto_of_net(proto, net, weights);
cout << proto->DebugString();
delete proto;
}
bool write_as_proto(ostream &output, INetwork *net) {
unique_ptr<clstm::NetworkProto> proto;
proto.reset(new clstm::NetworkProto());
proto_of_net(proto.get(), net);
return proto->SerializeToOstream(&output);
}
bool save_as_proto(const string &fname, INetwork *net) {
ofstream stream;
stream.open(fname, ios::binary);
return write_as_proto(stream, net);
}
Network read_as_proto(istream &stream) {
unique_ptr<clstm::NetworkProto> proto;
proto.reset(new clstm::NetworkProto());
if (proto->ParseFromIstream(&stream) == false) {
return Network();
}
return net_of_proto(proto.get());
}
Network load_as_proto(const string &fname) {
ifstream stream;
stream.open(fname, ios::binary);
if (!stream) throwf("cannot open: %s", fname.c_str());
return read_as_proto(stream);
}
}