-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.cpp
174 lines (135 loc) · 4.22 KB
/
test.cpp
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
#include <vector>
#include <map>
#include <sstream>
#include <string>
#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/types/map.hpp>
#include <cereal/types/polymorphic.hpp>
#include <cereal/types/vector.hpp>
class layer {
public:
virtual std::string name() const = 0;
virtual std::shared_ptr<layer> clone() const = 0;
};
#define IMPLEMENT_CLONE_AND_SERIALIZATION(type, layer_name) \
virtual std::string name() const { \
return layer_name; \
} \
virtual std::shared_ptr<layer> clone() const { \
return std::make_shared<type>(p); \
} \
template <class Archive> \
void serialize(Archive & archive) { \
p.serialize(archive); \
}
//////////////////////////
// conv-layer
struct conv_param {
size_t window_w;
size_t window_h;
size_t out_channels;
template<class Archive>
void serialize(Archive & archive)
{
archive(CEREAL_NVP(window_w), CEREAL_NVP(window_h), CEREAL_NVP(out_channels));
}
};
class conv_layer : public layer {
public:
conv_layer() {}
explicit conv_layer(const conv_param& p) : p(p) {}
conv_layer(size_t window_size, size_t out_channels) : p({ window_size, window_size, out_channels }) {}
IMPLEMENT_CLONE_AND_SERIALIZATION(conv_layer, "conv")
private:
conv_param p;
};
//////////////////////////
// fc-layer
struct fc_param {
size_t out_size;
bool has_bias;
template<class Archive>
void serialize(Archive & archive)
{
archive(CEREAL_NVP(out_size), CEREAL_NVP(has_bias));
}
};
class fc_layer : public layer {
public:
fc_layer() {}
explicit fc_layer(const fc_param& p) : p(p) {}
explicit fc_layer(size_t out_size) : p({out_size, true}) {}
IMPLEMENT_CLONE_AND_SERIALIZATION(fc_layer, "fc")
private:
fc_param p;
};
/////////////////////////////
// nodes(graph)
typedef layer* layerptr_t;
class nodes {
public:
void add(std::shared_ptr<layer> layer) {
own_nodes_.push_back(layer);
nodes_.push_back(&*layer);
}
void add(layerptr_t layer) {
nodes_.push_back(layer);
}
void save(std::ostream& os) const {
std::vector<std::shared_ptr<layer>> tmp(own_nodes_);
// clone all layers which nodes don't have its ownership
for (auto n : nodes_) {
if (!own(n)) {
tmp.push_back(n->clone());
}
}
cereal::JSONOutputArchive o_archive(os);
o_archive(tmp);
// TODO: save graph-connection structure
}
void load(std::istream& is) {
cereal::JSONInputArchive i_archive(is);
i_archive(own_nodes_);
for (auto& n : own_nodes_) {
nodes_.push_back(&*n);
}
}
private:
bool own(layerptr_t l) const {
for (auto& n : own_nodes_) {
if (&*n == l) return true;
}
return false;
}
std::vector<std::shared_ptr<layer> > own_nodes_;
std::vector<layerptr_t> nodes_;
};
// Register DerivedClassOne
CEREAL_REGISTER_TYPE(conv_layer);
// Register EmbarassingDerivedClass with a less embarrasing name
CEREAL_REGISTER_TYPE(fc_layer);
// Note that there is no need to register the base class, only derived classes
// However, since we did not use cereal::base_class, we need to clarify
// the relationship (more on this later)
CEREAL_REGISTER_POLYMORPHIC_RELATION(layer, conv_layer);
CEREAL_REGISTER_POLYMORPHIC_RELATION(layer, fc_layer);
int main(void) {
fc_layer l1(3);
conv_layer l2(5, 2);
nodes n;
n.add(std::make_shared<fc_layer>(l1));
n.add(&l2);
std::stringstream ss;
n.save(ss);
std::cout << ss.str();
nodes n2;
n2.load(ss);
/*{
cereal::JSONOutputArchive oarchive(std::cout);
// Create instances of the derived classes, but only keep base class pointers
std::shared_ptr<layer> ptr1 = std::make_shared<conv_layer>(cv);
std::shared_ptr<layer> ptr2 = std::make_shared<fc_layer>(fc);
oarchive(ptr1, ptr2);
}*/
}