JeVoisBase  1.3
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
ObjectRecognitionCIFAR.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 "tiny-dnn/tiny_dnn/tiny_dnn.h"
20 #include <jevois/Debug/Log.H>
21 
22 // ####################################################################################################
23 ObjectRecognitionCIFAR::ObjectRecognitionCIFAR(std::string const & instance) :
24  ObjectRecognition<tiny_dnn::sequential>(instance)
25 { }
26 
27 // ####################################################################################################
29 {
30  // Nothing to do, base class destructor will de-allocate the network
31 }
32 
33 // ####################################################################################################
35 {
36  typedef tiny_dnn::convolutional_layer<tiny_dnn::activation::identity> conv;
37  typedef tiny_dnn::max_pooling_layer<tiny_dnn::activation::relu> pool;
38 
39  int const n_fmaps = 32; // number of feature maps for upper layer
40  int const n_fmaps2 = 64; // number of feature maps for lower layer
41  int const n_fc = 64; // number of hidden units in fully-connected layer
42 
43  (*net) << conv(32, 32, 5, 3, n_fmaps, tiny_dnn::padding::same)
44  << pool(32, 32, n_fmaps, 2)
45  << conv(16, 16, 5, n_fmaps, n_fmaps, tiny_dnn::padding::same)
46  << pool(16, 16, n_fmaps, 2)
47  << conv(8, 8, 5, n_fmaps, n_fmaps2, tiny_dnn::padding::same)
48  << pool(8, 8, n_fmaps2, 2)
49  << tiny_dnn::fully_connected_layer<tiny_dnn::activation::identity>(4 * 4 * n_fmaps2, n_fc)
50  << tiny_dnn::fully_connected_layer<tiny_dnn::activation::softmax>(n_fc, 10);
51 }
52 
53 // ####################################################################################################
54 void ObjectRecognitionCIFAR::train(std::string const & path)
55 {
56  LINFO("Load training data from directory " << path);
57 
58  float learning_rate = 0.01F;
59 
60  // Load CIFAR dataset:
61  std::vector<tiny_dnn::label_t> train_labels, test_labels;
62  std::vector<tiny_dnn::vec_t> train_images, test_images;
63  for (int i = 1; i <= 5; ++i)
64  tiny_dnn::parse_cifar10(path + "/data_batch_" + std::to_string(i) + ".bin",
65  &train_images, &train_labels, -1.0, 1.0, 0, 0);
66 
67  tiny_dnn::parse_cifar10(path + "/test_batch.bin", &test_images, &test_labels, -1.0, 1.0, 0, 0);
68 
69  LINFO("Start training...");
70  int const n_minibatch = 10;
71  int const n_train_epochs = 30;
72 
73  tiny_dnn::timer t;
74 
75  // Create callbacks:
76  auto on_enumerate_epoch = [&](){
77  LINFO(t.elapsed() << "s elapsed.");
78  tiny_dnn::result res = net->test(test_images, test_labels);
79  LINFO(res.num_success << "/" << res.num_total << " success/total validation score so far");
80 
81  //disp.restart(train_images.size());
82  t.restart();
83  };
84 
85  auto on_enumerate_minibatch = [&](){
86  //disp += n_minibatch;
87  };
88 
89  // Training:
90  tiny_dnn::adam optimizer;
91  optimizer.alpha *= static_cast<tiny_dnn::float_t>(sqrt(n_minibatch) * learning_rate);
92  net->train<tiny_dnn::cross_entropy>(optimizer, train_images, train_labels, n_minibatch, n_train_epochs,
93  on_enumerate_minibatch, on_enumerate_epoch);
94 
95  LINFO("Training complete");
96 
97  // test and show results
98  net->test(test_images, test_labels).print_detail(std::cout);
99 }
100 
101 // ####################################################################################################
102 std::string const & ObjectRecognitionCIFAR::category(size_t idx) const
103 {
104  static std::vector<std::string> const names =
105  { "plane" /*"airplane"*/, "car" /*"automobile"*/, "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" };
106 
107  if (idx >= names.size()) LFATAL("Category index out of bounds");
108 
109  return names[idx];
110 }
ObjectRecognitionCIFAR(std::string const &instance)
Constructor, loads the given CNN, its sizes must match our (fixed) internal network structure...
virtual void train(std::string const &path) override
Train the network.
virtual ~ObjectRecognitionCIFAR()
Destructor.
#define LFATAL(msg)
virtual std::string const & category(size_t idx) const override
Return the name of a given category (0-based index in the vector of results)
Wrapper around a neural network implemented by with the tiny-dnn framework by Taiga Nomi...
std::string to_string(T const &val)
virtual void define() override
Define the network structure.
tiny_dnn::network< tiny_dnn::sequential > * net
#define LINFO(msg)