22 #include "tiny-dnn/tiny_dnn/tiny_dnn.h"
26 jevois::Component(instance)
34 template <
typename NetType>
40 template <
typename NetType>
47 #ifdef JEVOIS_PLATFORM
48 std::string
const wpath = JEVOIS_SHARE_PATH
"/tiny-dnn/" + instanceName() +
"/weights.tnn.platform";
50 std::string
const wpath = JEVOIS_SHARE_PATH
"/tiny-dnn/" + instanceName() +
"/weights.tnn.host";
52 std::string
const jpath = JEVOIS_SHARE_PATH
"/tiny-dnn/" + instanceName() +
".json";
57 LINFO(
"Loaded pre-trained weights and model from " << wpath);
63 net->load(jpath, tiny_dnn::content_type::weights_and_model, tiny_dnn::file_format::json);
65 LINFO(
"Converted and loaded pre-trained weights and model from " << wpath);
70 LINFO(
"Could not load pre-trained weights and model from " << wpath <<
" -- start training...");
76 this->train(JEVOIS_SHARE_PATH
"/tiny-dnn/" + instanceName());
79 LINFO(
"Saving trained weights to " << wpath);
83 net->save(jpath, tiny_dnn::content_type::weights_and_model, tiny_dnn::file_format::json);
84 LINFO(
"Weights saved -- Network ready to work.");
91 LINFO(
"Saving weights to " << wpath <<
" failed -- trying /tmp/weights.tnn");
94 net->save(
"/tmp/weights.tnn");
95 net->save(
"/tmp/weights.tnn.json", tiny_dnn::content_type::weights_and_model,
96 tiny_dnn::file_format::json);
97 LINFO(
"Weights saved to /tmp/weights.tnn -- Network ready to work.");
106 template <
typename NetType>
111 template <
typename NetType>
113 {
return (*
net)[0]->in_shape()[0]; }
116 template <
typename NetType>
120 auto inshape = (*net)[0]->in_shape()[0];
122 if (img.cols !=
int(inshape.width_) ||
123 img.rows !=
int(inshape.height_) ||
124 img.channels() !=
int(inshape.depth_))
LFATAL(
"Incorrect input image size or format");
127 size_t const sz = inshape.size();
128 tiny_dnn::vec_t data(sz);
129 unsigned char const * in = img.data; tiny_dnn::float_t *
out = &data[0];
130 for (
size_t i = 0; i < sz; ++i) *
out++ = (*in++) * (2.0F / 255.0F) - 1.0
F;
136 auto scores =
net->predict(data);
139 tiny_dnn::layer * lastlayer = (*net)[
net->depth() - 1];
140 std::pair<tiny_dnn::float_t, tiny_dnn::float_t> outrange = lastlayer->out_value_range();
141 tiny_dnn::float_t
const mi = outrange.first;
142 tiny_dnn::float_t
const ma = outrange.second;
144 for (tiny_dnn::float_t & s : scores) s = tiny_dnn::float_t(100) * (s - mi) / (ma - mi);
149 return net->predict(data);