JeVoisBase  1.3
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
ObjectRecognition.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
19 #include <jevois/Debug/Log.H>
20 #include <fstream>
21 
22 #include "tiny-dnn/tiny_dnn/tiny_dnn.h"
23 
24 // ####################################################################################################
25 ObjectRecognitionBase::ObjectRecognitionBase(std::string const & instance) :
26  jevois::Component(instance)
27 { }
28 
29 // ####################################################################################################
31 { }
32 
33 // ####################################################################################################
34 template <typename NetType>
35 ObjectRecognition<NetType>::ObjectRecognition(std::string const & instance) :
36  ObjectRecognitionBase(instance), net(new tiny_dnn::network<NetType>())
37 { }
38 
39 // ####################################################################################################
40 template <typename NetType>
42 {
43  // Load from file, if available, otherwise trigger training:
44  std::string const wpath = absolutePath("tiny-dnn/" + instanceName() + "/weights.tnn");
45 
46  try
47  {
48  net->load(wpath);
49  LINFO("Loaded pre-trained weights from " << wpath);
50  }
51  catch (...)
52  {
53  LINFO("Could not load pre-trained weights from " << wpath);
54 
55  // First define the network (implemented in derived classes):
56  this->define();
57 
58  // Then train it:
59  this->train(absolutePath("tiny-dnn/" + instanceName()));
60 
61  // Finally save:
62  LINFO("Saving trained weights to " << wpath);
63  net->save(wpath);
64  LINFO("Weights saved. Network ready to work.");
65  }
66 }
67 
68 // ####################################################################################################
69 template <typename NetType>
71 { delete net; }
72 
73 // ####################################################################################################
74 template <typename NetType>
75 typename tiny_dnn::index3d<tiny_dnn::serial_size_t>
77 { return (*net)[0]->in_shape()[0]; }
78 
79 // ####################################################################################################
80 template <typename NetType>
82 ObjectRecognition<NetType>::process(cv::Mat const & img, bool normalize)
83 {
84  auto inshape = (*net)[0]->in_shape()[0];
85 
86  if (img.cols != int(inshape.width_) ||
87  img.rows != int(inshape.height_) ||
88  img.channels() != int(inshape.depth_)) LFATAL("Incorrect input image size or format");
89 
90  // Convert input image to vec_t with values in [-1..1]:
91  size_t const sz = inshape.size();
92  tiny_dnn::vec_t data(sz);
93  unsigned char const * in = img.data; tiny_dnn::float_t * out = &data[0];
94  for (size_t i = 0; i < sz; ++i) *out++ = (*in++) * (2.0F / 255.0F) - 1.0F;
95 
96  // Recognize:
97  if (normalize)
98  {
99  // Get the raw scores:
100  auto scores = net->predict(data);
101 
102  // Normalize activation values between 0...100:
103  tiny_dnn::layer * lastlayer = (*net)[net->depth() - 1];
104  std::pair<tiny_dnn::float_t, tiny_dnn::float_t> outrange = lastlayer->out_value_range();
105  tiny_dnn::float_t const mi = outrange.first;
106  tiny_dnn::float_t const ma = outrange.second;
107 
108  for (tiny_dnn::float_t & s : scores) s = tiny_dnn::float_t(100) * (s - mi) / (ma - mi);
109 
110  return scores;
111  }
112  else
113  return net->predict(data);
114 }
115 
116 // ####################################################################################################
117 // Expplicit instantiations:
ObjectRecognitionBase(std::string const &instance)
Constructor.
virtual void postInit() override
Initialize the network, required before one starts using it.
vec_t process(cv::Mat const &img, bool normalize=true) override
Process an image, results are confidence for each category.
virtual ~ObjectRecognitionBase()
Virtual destructor for safe inheritance.
virtual tiny_dnn::index3d< tiny_dnn::serial_size_t > insize() const override
Get the input size for the current network, useful to prepare inputs to process() ...
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:
virtual ~ObjectRecognition()
Destructor.
#define LFATAL(msg)
ObjectRecognition(std::string const &instance)
Constructor allocates the (empty) network.
Abstract base class for an object recognition component.
#define LINFO(msg)