22#include "tiny-dnn/tiny_dnn/tiny_dnn.h"
26 jevois::Component(instance)
34template <
typename NetType>
40template <
typename NetType>
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...");
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.");
106template <
typename NetType>
111template <
typename NetType>
113{
return (*net)[0]->in_shape()[0]; }
116template <
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.0F;
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);
#define JEVOIS_SHARE_PATH
Abstract base class for an object recognition component.
ObjectRecognitionBase(std::string const &instance)
Constructor.
virtual ~ObjectRecognitionBase()
Virtual destructor for safe inheritance.
std::vector< tiny_dnn::float_t, tiny_dnn::aligned_allocator< tiny_dnn::float_t, 64 > > vec_t
Type used by tiny-dnn for the results:
Wrapper around a neural network implemented by with the tiny-dnn framework by Taiga Nomi.
ObjectRecognition(std::string const &instance)
Constructor allocates the (empty) network.
virtual tiny_dnn::index3d< size_t > insize() const override
Get the input size for the current network, useful to prepare inputs to process()
vec_t process(cv::Mat const &img, bool normalize=true) override
Process an image, results are confidence for each category.
virtual ~ObjectRecognition()
Destructor.
virtual void postInit() override
Initialize the network, required before one starts using it.
std::string warnAndIgnoreException(std::string const &prefix="")