tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
deserialization_helper.h
1 /*
2  Copyright (c) 2016, Taiga Nomi
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the tiny-dnn nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #pragma once
28 #include <typeindex>
29 #include <map>
30 #include <functional>
31 #include <memory>
32 #include <string>
33 #include <cereal/archives/json.hpp>
34 #include <cereal/types/memory.hpp>
35 #include "tiny_dnn/util/nn_error.h"
36 #include "tiny_dnn/util/macro.h"
37 #include "tiny_dnn/layers/layers.h"
38 
39 namespace tiny_dnn {
40 
41 template <typename InputArchive>
43 public:
44  void register_loader(const std::string& name, std::function<std::shared_ptr<layer>(InputArchive&)> func) {
45  loaders_[name] = [=](void* ar) {
46  return func(*reinterpret_cast<InputArchive*>(ar));
47  };
48  }
49 
50  template <typename T>
51  void register_type(const std::string& name) {
52  type_names_[typeid(T)] = name;
53  }
54 
55  std::shared_ptr<layer> load(const std::string& layer_name, InputArchive& ar) {
56  check_if_enabled();
57 
58  if (loaders_.find(layer_name) == loaders_.end()) {
59  throw nn_error("Failed to generate layer. Generator for " + layer_name + " is not found.\n"
60  "Please use CNN_REGISTER_LAYER_DESERIALIZER macro to register appropriate generator");
61  }
62 
63  return loaders_[layer_name](reinterpret_cast<void*>(&ar));
64  }
65 
66  const std::string& type_name(std::type_index index) const {
67  if (type_names_.find(index) == type_names_.end()) {
68  throw nn_error("Typename is not registered");
69  }
70  return type_names_.at(index);
71  }
72 
73  static deserialization_helper& get_instance() {
74  static deserialization_helper instance;
75  return instance;
76  }
77 
78 private:
79  void check_if_enabled() const {
80 #ifdef CNN_NO_SERIALIZATION
81  static_assert(sizeof(InputArchive)==0,
82  "You are using load functions, but deserialization function is disabled in current configuration.\n\n"
83  "You need to undef CNN_NO_SERIALIZATION to enable these functions.\n"
84  "If you are using cmake, you can use -DUSE_SERIALIZER=ON option.\n\n");
85 #endif
86  }
87 
89  std::map<std::string, std::function<std::shared_ptr<layer>(void*)>> loaders_;
90 
91  std::map<std::type_index, std::string> type_names_;
92 
93  template <typename T>
94  static std::shared_ptr<layer> load_layer_impl(InputArchive& ia);
95 
96 #define CNN_REGISTER_LAYER_BODY(layer_type, layer_name) \
97  register_loader(layer_name, load_layer_impl<layer_type>);\
98  register_type<layer_type>(layer_name);
99 
100 #define CNN_REGISTER_LAYER(layer_type, layer_name) CNN_REGISTER_LAYER_BODY(layer_type, #layer_name)
101 
102 #define CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, activation_type, layer_name) \
103 CNN_REGISTER_LAYER_BODY(layer_type<activation::activation_type>, #layer_name "<" #activation_type ">")
104 
105 #define CNN_REGISTER_LAYER_WITH_ACTIVATIONS(layer_type, layer_name) \
106 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, tan_h, layer_name); \
107 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, softmax, layer_name); \
108 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, identity, layer_name); \
109 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, sigmoid, layer_name); \
110 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, relu, layer_name); \
111 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, leaky_relu, layer_name); \
112 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, elu, layer_name); \
113 CNN_REGISTER_LAYER_WITH_ACTIVATION(layer_type, tan_hp1m2, layer_name)
114 
116 #include "serialization_layer_list.h"
117  }
118 
119 #undef CNN_REGISTER_LAYER_BODY
120 #undef CNN_REGISTER_LAYER
121 #undef CNN_REGISTER_LAYER_WITH_ACTIVATION
122 #undef CNN_REGISTER_LAYER_WITH_ACTIVATIONS
123 
124 }; // class deserialization_helper
125 
126 template <typename InputArchive>
127 template <typename T>
128 std::shared_ptr<layer> deserialization_helper<InputArchive>::load_layer_impl(InputArchive& ia) {
129 
130  using ST = typename std::aligned_storage<sizeof(T), CNN_ALIGNOF(T)>::type;
131 
132  std::unique_ptr<ST> bn(new ST());
133 
134  cereal::memory_detail::LoadAndConstructLoadWrapper<InputArchive, T> wrapper(reinterpret_cast<T*>(bn.get()));
135 
136  wrapper.CEREAL_SERIALIZE_FUNCTION_NAME(ia);
137 
138  std::shared_ptr<layer> t;
139  t.reset(reinterpret_cast<T*>(bn.get()));
140  bn.release();
141 
142  return t;
143 }
144 
145 template <typename T>
146 void start_loading_layer(T & ar) {}
147 
148 template <typename T>
149 void finish_loading_layer(T & ar) {}
150 
151 inline void start_loading_layer(cereal::JSONInputArchive & ia) { ia.startNode(); }
152 
153 inline void finish_loading_layer(cereal::JSONInputArchive & ia) { ia.finishNode(); }
154 
158 template <typename InputArchive>
159 std::shared_ptr<layer> layer::load_layer(InputArchive & ia) {
160  start_loading_layer(ia);
161 
162  std::string p;
163  ia(cereal::make_nvp("type", p));
165 
166  finish_loading_layer(ia);
167 
168  return l;
169 }
170 
171 } // namespace tiny_dnn
Definition: deserialization_helper.h:42
static std::shared_ptr< layer > load_layer(InputArchive &ia)
generate layer from cereal's Archive
Definition: deserialization_helper.h:159
error exception class for tiny-dnn
Definition: nn_error.h:37