28 #include <google/protobuf/io/coded_stream.h>
29 #include <google/protobuf/io/zero_copy_stream_impl.h>
30 #include <google/protobuf/text_format.h>
33 #include "tiny_dnn/network.h"
34 #include "tiny_dnn/lossfunctions/loss_function.h"
35 #include "tiny_dnn/optimizers/optimizer.h"
36 #include "tiny_dnn/util/util.h"
37 #include "tiny_dnn/io/caffe/layer_factory_impl.h"
47 inline std::shared_ptr<network<sequential>>
48 create_net_from_caffe_net(
const caffe::NetParameter& layer,
const shape3d& data_shape)
50 detail::caffe_layer_vector src_net(layer);
53 if (data_shape.size() > 0) {
56 if (layer.input_shape_size() > 0) {
59 int depth =
static_cast<int>(layer.input_shape(0).dim(1));
60 int height =
static_cast<int>(layer.input_shape(0).dim(2));
61 int width =
static_cast<int>(layer.input_shape(0).dim(3));
62 shape = shape3d(width, height, depth);
64 else if (src_net[0].has_input_param()) {
66 int depth =
static_cast<int>(src_net[0].input_param().shape(0).dim(1));
67 int height =
static_cast<int>(src_net[0].input_param().shape(0).dim(2));
68 int width =
static_cast<int>(src_net[0].input_param().shape(0).dim(3));
69 shape = shape3d(width, height, depth);
72 throw nn_error(
"input_shape not found in caffemodel. must specify input shape explicitly");
76 auto dst_net = std::make_shared<network<sequential>>(layer.name());
78 for (
size_t i = 0; i < src_net.size(); i++) {
79 auto type = src_net[i].type();
81 if (detail::layer_skipped(type)) {
85 if (!detail::layer_supported(type)) {
86 throw nn_error(
"error: tiny-dnn does not support this layer type:" + type);
90 auto layer = detail::create(src_net[i], shape, &shape_next);
92 nn_info(
"convert " + type +
" => " +
typeid(*layer).name());
93 nn_info(
"shape:" + to_string(shape_next));
109 inline std::shared_ptr<network<sequential>>
110 create_net_from_caffe_protobinary(
const std::string& caffebinarymodel,
const shape3d& data_shape)
112 caffe::NetParameter np;
114 detail::read_proto_from_binary(caffebinarymodel, &np);
115 return create_net_from_caffe_net(np, data_shape);
123 inline std::shared_ptr<network<sequential>>
124 create_net_from_caffe_prototxt(
const std::string& caffeprototxt,
const shape3d& shape =shape3d())
126 caffe::NetParameter np;
128 detail::read_proto_from_text(caffeprototxt, &np);
129 return create_net_from_caffe_net(np, shape);
139 template <
typename N>
140 inline void reload_weight_from_caffe_net(
const caffe::NetParameter& layer, network<N> *net)
142 detail::caffe_layer_vector src_net(layer);
144 size_t tiny_layer_idx = 0;
146 for (
size_t caffe_layer_idx = 0; caffe_layer_idx < src_net.size(); caffe_layer_idx++) {
147 auto type = src_net[caffe_layer_idx].type();
149 size_t next_idx = tiny_layer_idx + 1;
151 while (next_idx < net->depth() && !detail::layer_match(type, (*net)[next_idx]->layer_type())) {
154 if (next_idx >= net->depth())
break;
156 tiny_layer_idx = next_idx;
159 detail::load(src_net[caffe_layer_idx], (*net)[tiny_layer_idx++]);
170 template <
typename N>
171 inline void reload_weight_from_caffe_protobinary(
const std::string& caffebinary, network<N> *net)
173 caffe::NetParameter np;
175 detail::read_proto_from_binary(caffebinary, &np);
176 reload_weight_from_caffe_net(np, net);