JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
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// ####################################################################################################
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 using conv = tiny_dnn::convolutional_layer;
37 using pool = tiny_dnn::max_pooling_layer;
38 using fc = tiny_dnn::fully_connected_layer;
39 using relu = tiny_dnn::relu_layer;
40 using softmax = tiny_dnn::softmax_layer;
41
42 const size_t n_fmaps = 32; ///< number of feature maps for upper layer
43 const size_t n_fmaps2 = 64; ///< number of feature maps for lower layer
44 const size_t n_fc = 64; ///< number of hidden units in fully-connected layer
45
46 (*net) << conv(32, 32, 5, 3, n_fmaps, tiny_dnn::padding::same) // C1
47 << pool(32, 32, n_fmaps, 2) // P2
48 << relu(16, 16, n_fmaps) // activation
49 << conv(16, 16, 5, n_fmaps, n_fmaps, tiny_dnn::padding::same) // C3
50 << pool(16, 16, n_fmaps, 2) // P4
51 << relu(8, 8, n_fmaps) // activation
52 << conv(8, 8, 5, n_fmaps, n_fmaps2, tiny_dnn::padding::same) // C5
53 << pool(8, 8, n_fmaps2, 2) // P6
54 << relu(4, 4, n_fmaps2) // activation
55 << fc(4 * 4 * n_fmaps2, n_fc) // FC7
56 << fc(n_fc, 10) << softmax(10); // FC10
57}
58
59// ####################################################################################################
60void ObjectRecognitionCIFAR::train(std::string const & path)
61{
62 LINFO("Load training data from directory " << path);
63
64 float learning_rate = 0.01F;
65
66 // Load CIFAR dataset:
67 std::vector<tiny_dnn::label_t> train_labels, test_labels;
68 std::vector<tiny_dnn::vec_t> train_images, test_images;
69 for (int i = 1; i <= 5; ++i)
70 tiny_dnn::parse_cifar10(path + "/data_batch_" + std::to_string(i) + ".bin",
71 &train_images, &train_labels, -1.0, 1.0, 0, 0);
72
73 tiny_dnn::parse_cifar10(path + "/test_batch.bin", &test_images, &test_labels, -1.0, 1.0, 0, 0);
74
75 LINFO("Start training...");
76 int const n_minibatch = 10;
77 int const n_train_epochs = 30;
78
79 tiny_dnn::timer t;
80
81 // Create callbacks:
82 auto on_enumerate_epoch = [&](){
83 LINFO(t.elapsed() << "s elapsed.");
84 tiny_dnn::result res = net->test(test_images, test_labels);
85 LINFO(res.num_success << "/" << res.num_total << " success/total validation score so far");
86
87 //disp.restart(train_images.size());
88 t.restart();
89 };
90
91 auto on_enumerate_minibatch = [&](){
92 //disp += n_minibatch;
93 };
94
95 // Training:
96 tiny_dnn::adam optimizer;
97 optimizer.alpha *= static_cast<tiny_dnn::float_t>(sqrt(n_minibatch) * learning_rate);
98 net->train<tiny_dnn::cross_entropy>(optimizer, train_images, train_labels, n_minibatch, n_train_epochs,
99 on_enumerate_minibatch, on_enumerate_epoch);
100
101 LINFO("Training complete");
102
103 // test and show results
104 net->test(test_images, test_labels).print_detail(std::cout);
105}
106
107// ####################################################################################################
108std::string const & ObjectRecognitionCIFAR::category(size_t idx) const
109{
110 static std::vector<std::string> const names =
111 { "plane" /*"airplane"*/, "car" /*"automobile"*/, "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" };
112
113 if (idx >= names.size()) LFATAL("Category index out of bounds");
114
115 return names[idx];
116}
virtual void define() override
Define the network structure.
virtual ~ObjectRecognitionCIFAR()
Destructor.
virtual void train(std::string const &path) override
Train the network.
ObjectRecognitionCIFAR(std::string const &instance)
Constructor, loads the given CNN, its sizes must match our (fixed) internal network structure.
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.
tiny_dnn::network< tiny_dnn::sequential > * net
#define LFATAL(msg)
#define LINFO(msg)