]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ti-machine-learning/ti-machine-learning.git/blob - doc/latex/index.tex
Remove the compiled binary files
[ti-machine-learning/ti-machine-learning.git] / doc / latex / index.tex
1 \hypertarget{index_sectionOverview}{}\section{Overview}\label{index_sectionOverview}
2 The Texas Instruments Machine Learning (T\-I\-M\-L) library is C implementation of common machine learning functions optimized for T\-I embedded devices. Initially, the library supports convolutional neural networks (C\-N\-Ns), but will grow with time in scope. The library has a minimal set of dependencies upon other libraries to simplify the installation and use.\hypertarget{index_sectionInstallation}{}\section{Installation}\label{index_sectionInstallation}
3 \hypertarget{index_subsectionDependencies}{}\subsection{Dependencies}\label{index_subsectionDependencies}
4 {\bfseries  T\-I\-M\-L library dependencies }\par
5  The T\-I\-M\-L library requires a high performance C basic linear algebra subprogram (C\-B\-L\-A\-S) library and a library for reading Joint Photographic Experts Group (J\-P\-E\-G) images. An optimized C\-B\-L\-A\-S implementation is included with T\-I embedded devices. For pre development work on a desktop, a reference (unoptimized) C\-B\-L\-A\-S implementation can be downloaded from \href{http://www.netlib.org/blas/blast-forum/cblas.tgz}{\tt http\-://www.\-netlib.\-org/blas/blast-\/forum/cblas.\-tgz} or in Ubuntu using the command
7 {\ttfamily  sudo apt-\/get install libatlas-\/base-\/dev }
9 A J\-P\-E\-G library, libjpeg, can be downloaded from \par
10 \href{http://sourceforge.net/projects/libjpeg/files/latest/download?source=files}{\tt http\-://sourceforge.\-net/projects/libjpeg/files/latest/download?source=files} \par
11 or in Ubuntu using the command
13 {\ttfamily  sudo apt-\/get install libjpeg-\/dev }
15 {\bfseries  Application dependencies }\par
16  The Image\-Net database conversion application resizes all the images to size 256$\ast$256. The resizing is done using open\-C\-V. In Ubuntu, use the following command to install open\-C\-V
18 {\ttfamily  sudo apt-\/get install libopencv-\/dev }
20 The interop application converts a C\-N\-N model in Caffe format to T\-I\-M\-L C\-N\-N format. The Caffe format depends on google's protocol buffer library. To install the library, use command
22 {\ttfamily  sudo apt-\/get install libprotobuf-\/dev }\hypertarget{index_subsectionDirectoryStructure}{}\subsection{Directory Structure}\label{index_subsectionDirectoryStructure}
23 Install the T\-I\-M\-L library by unzipping the zip file to a directory referred to as $<$install\-\_\-directory$>$. At a terminal prompt, change the working directory to {\ttfamily $<$install\-\_\-directory$>$/build} and use the command {\ttfamily make all} to compile the library. The library file will be placed inside the {\ttfamily $<$install\-\_\-directory$>$/bin} folder.
25 The directory structure of the compiled library is shown as follows\-:
26 \begin{DoxyItemize}
27 \item {\ttfamily $<$install\-\_\-directory$>$/bin}\-: library binary file directory
28 \item {\ttfamily $<$install\-\_\-directory$>$/build}\-: makefile directory
29 \item {\ttfamily $<$install\-\_\-directory$>$/doc}\-: doxygen file directory
30 \item {\ttfamily $<$install\-\_\-directory$>$/src/common}\-: common library directory
31 \begin{DoxyItemize}
32 \item {\ttfamily $<$install\-\_\-directory$>$/src/common/api}\-: library A\-P\-I source file
33 \item {\ttfamily $<$install\-\_\-directory$>$/src/common/cnn}\-: cnn moudule source file
34 \item {\ttfamily $<$install\-\_\-directory$>$/src/common/util}\-: util moudule source file
35 \end{DoxyItemize}
36 \item {\ttfamily $<$install\-\_\-directory$>$/src/test}\-: test module directory
37 \begin{DoxyItemize}
38 \item {\ttfamily $<$install\-\_\-directory$>$/src/test/cnn}\-: cnn test source file
39 \item {\ttfamily $<$install\-\_\-directory$>$/src/test/util}\-: util test source file
40 \end{DoxyItemize}
41 \item {\ttfamily $<$install\-\_\-directory$>$/src/benchmark}\-: benchmark module directory
42 \begin{DoxyItemize}
43 \item {\ttfamily $<$install\-\_\-directory$>$/src/benchmark/cnn}\-: cnn benchmark source file
44 \begin{DoxyItemize}
45 \item {\ttfamily $<$install\-\_\-directory$>$/src/benchmark/cnn/class}\-: cnn classification benchmark source file
46 \end{DoxyItemize}
47 \end{DoxyItemize}
48 \item {\ttfamily $<$install\-\_\-directory$>$/src/app}\-: application module directory
49 \begin{DoxyItemize}
50 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn}\-: cnn application source file
51 \begin{DoxyItemize}
52 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/class}\-: cnn classification application source file
53 \begin{DoxyItemize}
54 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/class/cifar10}\-: cnn C\-I\-F\-A\-R10 database classification application source file
55 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/class/imagenet}\-: cnn Image\-Net database classification application source file
56 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/class/mnist}\-: cnn M\-N\-I\-S\-T database classification application source file
57 \end{DoxyItemize}
58 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/scene}\-: cnn scene labeling application source file
59 \begin{DoxyItemize}
60 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/scene/sbd}\-: cnn stanford background dataset scene labeling application source file
61 \end{DoxyItemize}
62 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/interop}\-: cnn interoperation application source file
63 \begin{DoxyItemize}
64 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/interop/caffe}\-: cnn-\/caffe interoperation application source file
65 \end{DoxyItemize}
66 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/convert}\-: cnn database conversion application source file
67 \begin{DoxyItemize}
68 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/convert/imagenet}\-: cnn Image\-Net database conversion application source file
69 \item {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/convert/sbd}\-: cnn S\-B\-D database conversion application source file
70 \end{DoxyItemize}
71 \end{DoxyItemize}
72 \end{DoxyItemize}
73 \item {\ttfamily $<$install\-\_\-directory$>$/src/database}\-: database directory
74 \begin{DoxyItemize}
75 \item cifar10\-: Canadian Institute for Advanced Research-\/10 Class
76 \item imagenet\-: Image\-Net 2012
77 \item mnist\-: Mixed National Institute of Standards and Technology
78 \item sbd\-: Stanford Background Dataset
79 \item model\-: pretrained models
80 \begin{DoxyItemize}
81 \item cifar10
82 \item alexnet
83 \item caffenet
84 \item vggnet
85 \item mnist
86 \item sbd
87 \end{DoxyItemize}
88 \end{DoxyItemize}
89 \end{DoxyItemize}\hypertarget{index_subsectionDoc}{}\subsection{Document Genearation}\label{index_subsectionDoc}
90 Documents of html and pdf formats can be generated using Doxygen. Make sure you have both latex and Doxygen installed. In Ubuntu, the installation commands are
93 \begin{DoxyItemize}
94 \item {\ttfamily sudo apt-\/get install doxygen}
95 \item {\ttfamily sudo apt-\/get install texlive-\/full}
96 \end{DoxyItemize}
98 Use the command {\ttfamily doxygen $<$install-\/ {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn/scene}\-: cnn scene labeling application source file\-\_\-directory$>$/doc/timl.Doxyfile} to generate the documents. The html version is located at {\ttfamily doxygen $<$install\-\_\-directory$>$/doc/html}. To generate the pdf version, change the directory to {\ttfamily doxygen $<$install\-\_\-directory$>$/doc/latex} and run the command {\ttfamily make}. A filed named refman.\-pdf will be generated in the currnet folder.\hypertarget{index_subsectionImageDatabases}{}\subsection{Image Databases}\label{index_subsectionImageDatabases}
99 In order to run the examples provided by the library, it is required to download additional databases of test images.
101 {\bfseries M\-N\-I\-S\-T}
103 The M\-N\-I\-S\-T database of handwritten digits, available from this page, has a training set of 60,000 examples, and a test set of 10,000 examples. It is a subset of a larger set available from N\-I\-S\-T. The digits have been size-\/normalized and centered in a fixed-\/size image.
105 You can download the M\-N\-I\-S\-T database from \href{http://yann.lecun.com/exdb/mnist/}{\tt http\-://yann.\-lecun.\-com/exdb/mnist/} or simpliy change the directory to {\ttfamily $<$install\-\_\-directory$>$/src/database/mnist} and run the script {\ttfamily ./database\-M\-N\-I\-S\-T\-Download.sh}. After the download, there should be 4 files in the folder\-:
106 \begin{DoxyItemize}
107 \item {\ttfamily t10k-\/images.\-idx3-\/ubyte}
108 \item {\ttfamily t10k-\/labels.\-idx1-\/ubyte}
109 \item {\ttfamily train-\/images.\-idx3-\/ubyte}
110 \item {\ttfamily train-\/labels.\-idx1-\/ubyte}.
111 \end{DoxyItemize}
113 {\bfseries C\-I\-F\-A\-R10}
115 The C\-I\-F\-A\-R-\/10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. The dataset is divided into five training batches and one test batch, each with 10000 images. The test batch contains exactly 1000 randomly-\/selected images from each class. The training batches contain the remaining images in random order, but some training batches may contain more images from one class than another. Between them, the training batches contain exactly 5000 images from each class.
117 You can download the C\-I\-F\-A\-R10 database from \href{http://www.cs.toronto.edu/~kriz/cifar.html}{\tt http\-://www.\-cs.\-toronto.\-edu/$\sim$kriz/cifar.\-html} or simpliy change the directory to {\ttfamily $<$install\-\_\-directory$>$/src/database/cifar10} and run the script {\ttfamily ./database\-C\-I\-F\-A\-R10\-Download.sh}. After the download, there should be 6 files in the folder\-:
118 \begin{DoxyItemize}
119 \item {\ttfamily data\-\_\-batch\-\_\-1.\-bin}
120 \item {\ttfamily data\-\_\-batch\-\_\-2.\-bin}
121 \item {\ttfamily data\-\_\-batch\-\_\-3.\-bin}
122 \item {\ttfamily data\-\_\-batch\-\_\-4.\-bin}
123 \item {\ttfamily data\-\_\-batch\-\_\-5.\-bin}
124 \item {\ttfamily test\-\_\-batch\-\_\-6.\-bin}
125 \end{DoxyItemize}
127 {\bfseries Image\-Net}
129 Image\-Net is an image dataset organized according to the Word\-Net hierarchy.\-Each meaningful concept in Word\-Net, possibly described by multiple words or word phrases, is called a \char`\"{}synonym set\char`\"{} or \char`\"{}synset\char`\"{}. There are more than 100,000 synsets in Word\-Net, majority of them are nouns (80,000+). Image\-Net aims to provide on average 1000 images to illustrate each synset. Images of each concept are quality-\/controlled and human-\/annotated.
131 Download the database from \href{http://www.image-net.org/challenges/LSVRC/2012/nonpub-downloads}{\tt http\-://www.\-image-\/net.\-org/challenges/\-L\-S\-V\-R\-C/2012/nonpub-\/downloads} to $<$install\-\_\-directory$>$/database/imagnet. You need to register before you can download the database.\-http\-://dags.stanford.\-edu/projects/scenedataset.html
132 \begin{DoxyItemize}
133 \item Download the training images of size 138\-G\-B and M\-D5\-: 1d675b47d978889d74fa0da5fadfb00e
134 \item Download the validation images of size 6.\-3\-G\-B and M\-D5\-: 29b22e2961454d5413ddabcf34fc5622
135 \item Download the auxiliary files by changing the working directory to {\ttfamily $<$install\-\_\-directory$>$/src/database/imagnet} and runing the script {\ttfamily  ./database\-Image\-Net\-Download.sh }
136 \item Extract all the files to the $<$install\-\_\-directory$>$/src/database/imagnet
137 \item Change run the script ./database\-Image\-Net\-Convert.sh to convert the raw database to a format that is ready to be processed by T\-I\-M\-L. You may need to change the path variables in this script if you choose to store the database in other directories. You can also specify the number of images to be converted in the script.
138 \end{DoxyItemize}
140 After the conversion, there should be two folders, test and train, in $<$install\-\_\-directory$>$/src/database/imagnet. There will be a single labels.\-txt file in each of the folder. All the images will be resized to 256$\ast$256.
142 {\bfseries Stanford Background Dataset}
144 The Stanford Background Dataset is a new dataset introduced in Gould et al. (I\-C\-C\-V 2009) for evaluating methods for geometric and semantic scene understanding. The dataset contains 715 images chosen from existing public datasets\-: Label\-Me, M\-S\-R\-C, P\-A\-S\-C\-A\-L V\-O\-C and Geometric Context. The selection criteria were for the images to be of outdoor scenes, have approximately 320-\/by-\/240 pixels, contain at least one foreground object, and have the horizon position within the image.
146 You can download the database from \href{http://dags.stanford.edu/data/iccv09Data.tar.gz}{\tt http\-://dags.\-stanford.\-edu/data/iccv09\-Data.\-tar.\-gz} or simply change the working directory to {\ttfamily $<$install\-\_\-directory$>$/src/database/sbd} and run the script {\ttfamily database\-S\-B\-D\-Download.\-sh} To convert the database to T\-I\-M\-L compatible format, run the script ./database\-S\-B\-D\-Convert.sh. You can specify the number of images used for training and testing by changing the corresponding variables in the script. After running the script, there should be two folders named \char`\"{}train\char`\"{} and \char`\"{}test\char`\"{} in the current folder.\hypertarget{index_subsectionCNNPretrainedModels}{}\subsection{C\-N\-N Pretrained Models}\label{index_subsectionCNNPretrainedModels}
147 T\-I\-M\-L can convert models pretrained by Caffe\cite{Caffe} to a format that is compatible with T\-I\-M\-L. Caffe is a deep learning framework developed by the Berkeley Vision and Learning Center (B\-V\-L\-C). You can train your C\-N\-N on Caffe on C\-P\-U or G\-P\-U, save the parameters and then convert it to a format supported by T\-I\-M\-L.
149 {\bfseries  Alex\-Net }\cite{AlexNet}
151 Alex\-Net is a deep convolutional neural network to classify the 1.\-3 million high-\/resolution images in the L\-S\-V\-R\-C-\/2010 Image\-Net training set into the 1000 different classes. On the test data, Alex\-Net achieved top-\/1 and top-\/5 error rates of 39.\-7\% and 18.\-9\% which is considerably better than the previous state-\/of-\/the-\/art results. The neural network, which has 60 million parameters and 500,000 neurons, consists of five convolutional layers, some of which are followed by max-\/pooling layers, and two globally connected layers with a final 1000-\/way softmax.
152 \begin{DoxyItemize}
153 \item Download the Caffe Alex\-Net binary file from \href{http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel}{\tt http\-://dl.\-caffe.\-berkeleyvision.\-org/bvlc\-\_\-alexnet.\-caffemodel} to $<$install\-\_\-directory$>$/src/database/model/alexnet
154 \item Download the Caffe Alex\-Net depoly file from \href{https://github.com/BVLC/caffe/blob/master/models/bvlc_alexnet/deploy.prototxt}{\tt https\-://github.\-com/\-B\-V\-L\-C/caffe/blob/master/models/bvlc\-\_\-alexnet/deploy.\-prototxt} to $<$install\-\_\-directory$>$/src/database/model/alexnet
155 \item Run the script ./database\-Model\-Alex\-Net\-Interop.sh to perform the conversion
156 \end{DoxyItemize}
158 After the conversion, there should two files in $<$install\-\_\-directory$>$/database/model/alexnet\-:
159 \begin{DoxyItemize}
160 \item database\-Model\-Alex\-Net.\-m {\itshape A text file that defines the structure of the C\-N\-N}
161 \item database\-Model\-Alex\-Net.\-m.\-params {\itshape A binary file that stores the parameters of the C\-N\-N}
162 \end{DoxyItemize}
164 {\bfseries  Caffe\-Net }
166 The Caffe\-Net is a replication of the model described in the Alex\-Net publication with some differences\-:
167 \begin{DoxyItemize}
168 \item not training with the relighting data-\/augmentation;
169 \item the order of pooling and normalization layers is switched (in Caffe\-Net, pooling is done before normalization).
170 \end{DoxyItemize}
172 This model obtains a top-\/1 accuracy 57.\-4\% and a top-\/5 accuracy 80.\-4\% on the validation set, using just the center crop.
175 \begin{DoxyItemize}
176 \item Download the Caffe Caffe\-Net binary file from \href{http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel}{\tt http\-://dl.\-caffe.\-berkeleyvision.\-org/bvlc\-\_\-reference\-\_\-caffenet.\-caffemodel} to $<$install\-\_\-directory$>$/database/model/caffenet
177 \item Download the Caffe Caffe\-Net depoly file from \href{https://github.com/BVLC/caffe/blob/master/models/bvlc_reference_caffenet/deploy.prototxt}{\tt https\-://github.\-com/\-B\-V\-L\-C/caffe/blob/master/models/bvlc\-\_\-reference\-\_\-caffenet/deploy.\-prototxt} to $<$install\-\_\-directory$>$/src/database/model/caffenet
178 \item Run the script ./database\-Model\-Caffe\-Net\-Interop.sh to perform the conversion
179 \end{DoxyItemize}
181 After the conversion, there should two files in $<$install\-\_\-directory$>$/src/database/model/caffenet\-:
182 \begin{DoxyItemize}
183 \item database\-Model\-Caffe\-Net.\-m
184 \item database\-Model\-Caffe\-Net.\-m.\-params
185 \end{DoxyItemize}
187 {\bfseries  V\-G\-G\-Net } \cite{VGG}
189 V\-G\-G\-Net shows that a significant improvement on the prior-\/art configurations can be achieved by increasing the depth to 16-\/19 weight layers, which is substantially deeper than what has been used in the prior art. To reduce the number of parameters in such very deep networks, the researchers use very small 3×3 filters in all convolutional layers.
192 \begin{DoxyItemize}
193 \item Download the V\-G\-G\-Net binary file from \href{http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel}{\tt http\-://www.\-robots.\-ox.\-ac.\-uk/$\sim$vgg/software/very\-\_\-deep/caffe/\-V\-G\-G\-\_\-\-I\-L\-S\-V\-R\-C\-\_\-16\-\_\-layers.\-caffemodel} to $<$install\-\_\-directory$>$/src/database/model/vggnet
194 \item Download the V\-G\-G\-Net depoly file from \href{https://gist.githubusercontent.com/ksimonyan/211839e770f7b538e2d8/raw/0067c9b32f60362c74f4c445a080beed06b07eb3/VGG_ILSVRC_16_layers_deploy.prototxt}{\tt https\-://gist.\-githubusercontent.\-com/ksimonyan/211839e770f7b538e2d8/raw/0067c9b32f60362c74f4c445a080beed06b07eb3/\-V\-G\-G\-\_\-\-I\-L\-S\-V\-R\-C\-\_\-16\-\_\-layers\-\_\-deploy.\-prototxt} to $<$install\-\_\-directory$>$/src/database/model/vggnet
195 \item Run the script ./database\-Model\-V\-G\-G\-Net\-Interop.sh to perform the conversion
196 \end{DoxyItemize}
198 After the conversion, there should two files in $<$install\-\_\-directory$>$/src/database/model/caffenet\-:
199 \begin{DoxyItemize}
200 \item database\-Model\-V\-G\-G\-Net.\-m
201 \item database\-Model\-V\-G\-G\-Net.\-m.\-params
202 \end{DoxyItemize}
204 {\bfseries  M\-N\-I\-S\-T } A pretrained C\-N\-N for M\-N\-I\-S\-T database is located at $<$install\-\_\-directory$>$/src/database/model/mnist\-:
205 \begin{DoxyItemize}
206 \item database\-Model\-M\-N\-I\-S\-T.\-m
207 \item database\-Model\-M\-N\-I\-S\-T.\-m.\-params
208 \end{DoxyItemize}
210 {\bfseries  C\-I\-F\-A\-R10 } A pretrained C\-N\-N for C\-I\-F\-A\-R10 database is located at $<$install\-\_\-directory$>$/src/database/model/cifar10\-:
211 \begin{DoxyItemize}
212 \item database\-Model\-C\-I\-F\-A\-R10.\-m
213 \item database\-Model\-C\-I\-F\-A\-R10.\-m.\-params
214 \end{DoxyItemize}\hypertarget{index_sectionCNNs}{}\section{Convolutional Neural Networks (\-C\-N\-Ns)}\label{index_sectionCNNs}
215 The T\-I\-M\-L library provides a set of A\-P\-Is that allow a user to implement a C\-N\-N architecture and perform training and testing. Common C\-N\-N layer types are supported in the current version and more will be added in future versions. Both image classification and scene labeling examples are provided.
217 This section briefly describes the use of C\-N\-N library A\-P\-Is. For complete examples, refer to the folder {\ttfamily $<$install\-\_\-directory$>$/src/app/cnn}\hypertarget{index_subsectionTrainingParameters}{}\subsection{Training Parameters}\label{index_subsectionTrainingParameters}
218 The default training parameters structure is obtained by calling {\ttfamily \hyperlink{group__cnn_gaaf7a5b272ae2ba5d0453f214afaab747}{timl\-C\-N\-N\-Training\-Params\-Default()}}. Default parameters can be overridden by setting specific field values (refer to {\ttfamily \hyperlink{structtimlCNNTrainingParams}{timl\-C\-N\-N\-Training\-Params}} for details).\hypertarget{index_subsectionLayers}{}\subsection{Layers}\label{index_subsectionLayers}
219 This section describes the different C\-N\-N layers supported by the T\-I\-M\-L library. To create a C\-N\-N, call {\ttfamily \hyperlink{group__cnn_gaf75b4687f4b493b8a85bc7fcc37bdf18}{timl\-C\-N\-N\-Create\-Conv\-Neural\-Network()}}. Here is an example code block in {\ttfamily timl\-Test\-C\-N\-N\-Simple\-Training()} that generates a simple C\-N\-N with 8 layer types\-:
222 \begin{DoxyCode}
223 trainingParams              = \hyperlink{group__cnn_gaaf7a5b272ae2ba5d0453f214afaab747}{timlCNNTrainingParamsDefault}();
224 trainingParams.batchSize    = BATCH\_SIZE;
225 trainingParams.learningRate = 0.1;
226 cnn = \hyperlink{group__cnn_gaf75b4687f4b493b8a85bc7fcc37bdf18}{timlCNNCreateConvNeuralNetwork}(trainingParams, 0);
227 inputParams       = \hyperlink{group__cnn_ga71b91e772f1e63528e53a6284ba7b879}{timlCNNInputParamsDefault}();
228 inputParams.scale = 1.0/256.0;
229 \hyperlink{group__cnn_ga253033ab06b21f2bf28301bc1e53c419}{timlCNNAddInputLayer}(cnn, IMAGE\_ROW, IMAGE\_COL, IMAGE\_CHANNEL, inputParams);           
230        \textcolor{comment}{// input layer}
231 \hyperlink{group__cnn_ga772bc4b21c2f800ee8ac5248a732f116}{timlCNNAddConvLayer}(cnn, 5, 5, 1, 1, 6, 
232       \hyperlink{group__cnn_ga33ddb7145ae4e19ea52e12576f4871aa}{timlCNNConvParamsDefault}());                    \textcolor{comment}{// conv layer}
233 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Relu);                                          
234            \textcolor{comment}{// relu layer}
235 \hyperlink{group__cnn_gab5dcc5ac470e15bea85f0a5570eec37d}{timlCNNAddPoolingLayer}(cnn, 4, 4, 4, 4, CNN\_MaxPooling, 
236       \hyperlink{group__cnn_gacd163e268b3ccc9805bcca59689a36f5}{timlCNNPoolingParamsDefault}()); \textcolor{comment}{// max pooling layer}
237 \hyperlink{group__cnn_ga7627fe7a5294d7c8cac75d974b78a27a}{timlCNNAddNormLayer}(cnn, \hyperlink{group__cnn_ga03ee34f7d893af02536bcbf2d071f721}{timlCNNNormParamsDefault}());           
238                               \textcolor{comment}{// norm layer}
239 \hyperlink{group__cnn_gae988ef172bde701db59b1cfc33dd50ab}{timlCNNAddDropoutLayer}(cnn, 0.5);                                                    
240          \textcolor{comment}{// dropout layer}
241 \hyperlink{group__cnn_ga0219ba397301c5fb3eedfb64e62e33bc}{timlCNNAddLinearLayer}(cnn, 10, \hyperlink{group__cnn_gad5c8fd10f11ccaa8142a665fc8ee93b8}{timlCNNLinearParamsDefault}())
242       ;                           \textcolor{comment}{// linear layer}
243 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Softmax);                                       
244            \textcolor{comment}{// softmax layer}
245 \hyperlink{group__cnn_ga666fa0a68b725aa1e35dba659ba7c5f9}{timlCNNInitialize}(cnn);
246 \hyperlink{group__cnn_ga21bc0c855cb658f51fe5d6911acddb54}{timlCNNReset}(cnn);
247 \end{DoxyCode}
248 \hypertarget{index_subsubsectionInputLayer}{}\subsubsection{Input}\label{index_subsubsectionInputLayer}
249 The input layer is responsible for preprocessing the raw input data. The user can choose to crop, scale, mirror, or subtract the mean form the raw image. The required parameters are the dimensions (row, column, channel) of the feature maps in the input layer. Refer to {\ttfamily \hyperlink{group__cnn_ga253033ab06b21f2bf28301bc1e53c419}{timl\-C\-N\-N\-Add\-Input\-Layer()}} for more details.\hypertarget{index_subsubsectionConvolutionalLayer}{}\subsubsection{Convolutional}\label{index_subsubsectionConvolutionalLayer}
250 The convolutional layer performs 2\-D convolution or correlation between feature maps and kernels. The required parameters are the 2\-D kernel dimension (row, column), kernel strides along row and column and the output feature map channel. For each feature map in the previous layer, there will be a corresponding 2\-D kernel applied to it and links to each feature map in the next layer. Refer to {\ttfamily \hyperlink{group__cnn_ga772bc4b21c2f800ee8ac5248a732f116}{timl\-C\-N\-N\-Add\-Conv\-Layer()}} for more detail.\hypertarget{index_subsubsectionNonlinearLayer}{}\subsubsection{Nonlinear}\label{index_subsubsectionNonlinearLayer}
251 The nonlinear layer implements 1 nonlinearity, supported types are\-:
254 \begin{DoxyItemize}
255 \item rectified linear unit\par
256  $ f(z) = \max(0, z) $
257 \item sigmoid\par
258  $ f(z) = \frac{1}{1+e^{-z}} $
259 \item softmax\par
260  $ f(z)_j = \frac{e^{-z_j}}{\sum_{i=0}^{K}e^{-z_i}} $
261 \item tanh\par
262  $ f(z) = \frac{e^{z}-e^{-z}}{e^{z}+e^{-z}} $
263 \end{DoxyItemize}To add a nonlinear layer to the cnn structure, call the function {\ttfamily \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timl\-C\-N\-N\-Add\-Nonlinear\-Layer()}}. Note that for image classification applications, the last layer is most commonly chosen to be a softmax nonlinear layer that output an array of class probabilities that sum up to 1.\hypertarget{index_subsubsectionPoolingLayer}{}\subsubsection{Pooling}\label{index_subsubsectionPoolingLayer}
264 The pooling layer performs a local maxing or averaging operation on the feature maps. The required parameters are the pooling kernel dimension (row, col), kernel strides along column and row and the pooling method. Refer to {\ttfamily \hyperlink{group__cnn_gab5dcc5ac470e15bea85f0a5570eec37d}{timl\-C\-N\-N\-Add\-Pooling\-Layer()}} for more details.\hypertarget{index_subsubsectionNormalizationLayer}{}\subsubsection{Normalization}\label{index_subsubsectionNormalizationLayer}
265 The normalization layer performs local contrast normalization across channels\-:\par
266  $ y^i = \frac{x^i}{(1+\frac{\alpha}{N}\sum^{\min(N-1, i+N/2)}_{j=\max(0, i-N/2)}x^j)^\beta} $ ,where $x^i$ stands for the feature map in the i-\/th channel. The channel span N defaults to 5. $\alpha$ and $\beta$ default to 0.\-001 and 0.\-75, respectively. Refer to {\ttfamily \hyperlink{group__cnn_ga7627fe7a5294d7c8cac75d974b78a27a}{timl\-C\-N\-N\-Add\-Norm\-Layer()}} for more details.\hypertarget{index_subsubsectionDropoutLayer}{}\subsubsection{Dropout}\label{index_subsubsectionDropoutLayer}
267 The dropout layer is used during training to help prevent overfitting. Each element in the feature map is set to 0 (deactivated) according to a preset probability in the training phase. Note the dropout layer is simply a pass through in the testing phase. Refer to {\ttfamily \hyperlink{group__cnn_gae988ef172bde701db59b1cfc33dd50ab}{timl\-C\-N\-N\-Add\-Dropout\-Layer()}} for more details.\hypertarget{index_subsubsectionLinearLayer}{}\subsubsection{Linear}\label{index_subsubsectionLinearLayer}
268 The linear layer is also referred to as the inner product layer or fully connected layer. A traditional neural network layer, the feature map in the previous layer is first vectorized and then multiplied by a weight matrix to obtain the feature map of next layer. The required parameter is the dimension of the output feature map. Refer to {\ttfamily \hyperlink{group__cnn_ga0219ba397301c5fb3eedfb64e62e33bc}{timl\-C\-N\-N\-Add\-Linear\-Layer()}} for more details.\hypertarget{index_subsectionMemory}{}\subsection{Memory}\label{index_subsectionMemory}
269 Once the setup of the cnn structure is complete, {\ttfamily \hyperlink{group__cnn_ga666fa0a68b725aa1e35dba659ba7c5f9}{timl\-C\-N\-N\-Initialize()}} is called to allocate the memory. There are 3 levels of memory allocation specified inside {\ttfamily \hyperlink{structtimlCNNTrainingParams}{timl\-C\-N\-N\-Training\-Params}}.
270 \begin{DoxyItemize}
271 \item Level 1 can be used both for training and testing and requires the most memory.
272 \item Level 2 requires less memory but can only be used for testing.
273 \item Level 3 uses even less memory by operating on a memory pool and can also only be used for testing.
274 \end{DoxyItemize}
276 Note that this function does not initialize the kernels or weights in the convolutional and linear layers. To perform that initialization the function {\ttfamily \hyperlink{group__cnn_ga21bc0c855cb658f51fe5d6911acddb54}{timl\-C\-N\-N\-Reset()}} is used.
278 The exact memory allocated in bytes can be obtained by calling {\ttfamily \hyperlink{group__cnn_ga2e8a7e765f885a236e40dfaed59913c6}{timl\-C\-N\-N\-Memory()}}.
280 {\ttfamily \hyperlink{group__cnn_ga78addba765ea7dd8bf958d19d42be931}{timl\-C\-N\-N\-Resize()}} is used to resize the dimension of a C\-N\-N. Once the input layer dimension is changed, the dimension of the following layers will be re-\/calcualted and re-\/allocated.
282 {\ttfamily \hyperlink{group__cnn_ga409c28e6bb3633f1de65647b790aa320}{timl\-C\-N\-N\-Clone()}} creates a independent copy of a C\-N\-N. The function {\ttfamily \hyperlink{group__cnn_ga52826c14f9f1913947f5ca6937b5afd5}{timl\-C\-N\-N\-Share\-Params()}} differs from {\ttfamily \hyperlink{group__cnn_ga409c28e6bb3633f1de65647b790aa320}{timl\-C\-N\-N\-Clone()}} in that the C\-N\-N structure returned by this function allocates its own feature map memory but points to the parameter memory of its target. Therefore, {\ttfamily \hyperlink{group__cnn_ga52826c14f9f1913947f5ca6937b5afd5}{timl\-C\-N\-N\-Share\-Params()}} returns a structure that takes less memory compared with the structure returned by {\ttfamily \hyperlink{group__cnn_ga409c28e6bb3633f1de65647b790aa320}{timl\-C\-N\-N\-Clone()}}, which allocates its own parameter memory. The user should be careful when manipulating the shared copy of a C\-N\-N as it may write to the parameters of its target. {\ttfamily \hyperlink{group__cnn_ga52826c14f9f1913947f5ca6937b5afd5}{timl\-C\-N\-N\-Share\-Params()}} is used primarily to accelerate the training and testing of a C\-N\-N. Multiple shared copies of a C\-N\-N can work in parallel by using Open\-M\-P.
284 To free the space allocated by the C\-N\-N, call the function {\ttfamily \hyperlink{group__cnn_ga5af48b599237f48f016ce2060c6053e1}{timl\-C\-N\-N\-Delete()}}.\hypertarget{index_subsectionUtilityFunctions}{}\subsection{Utility Functions}\label{index_subsectionUtilityFunctions}
285 Utility functions aim to perform miscellaneous functions that related to I\-O.
287 The phase of a C\-N\-N can be set to training or testing by calling {\ttfamily \hyperlink{group__cnn_ga04d9a7e3725ce777402e1e5c4d5d4ce0}{timl\-C\-N\-N\-Set\-Mode()}}. A C\-N\-N allocated with level 2 or 3 is not allowed to be set to the training mode as no memory is allocated to run the back propagation algorithm. In the testing mode, only forward propagation will be performed.
289 C\-N\-N structure information can be printed to the console by using {\ttfamily \hyperlink{group__cnn_ga123557b8025497fe266d58d80ee9983b}{timl\-C\-N\-N\-Print()}}. {\ttfamily \hyperlink{group__cnn_gaf65f8d45c892f70eeee34b47791bdcb5}{timl\-C\-N\-N\-Get\-Layer\-Num()}} returns the number of layers of the C\-N\-N. {\ttfamily \hyperlink{group__cnn_ga72df82405eb43be8bb75373aaf8ec101}{timl\-C\-N\-N\-Get\-Params\-Num()}} returns the total number of parameters in the C\-N\-N.
291 {\ttfamily \hyperlink{group__cnn_gad7f3f988c763fe5a80826439c9fb08bd}{timl\-C\-N\-N\-Write\-To\-File()}} writes the cnn structure into a combination of text and binary files. The key parameter of this function is the {\ttfamily timl\-Util\-Params\-Level}, which specifies the level of details of the writing.
292 \begin{DoxyItemize}
293 \item Level 1 only writes the network structure to a text file without specifying the parameters or feature maps of the cnn. The text file is formatted in a syntax that is compatible with Matlab script.
294 \item Level 2 writes both the network structure text file and the binary parameter file.
295 \item Level 3 writes one more state binary files. Level 3 is only used for debugging purpose. Note that the path of the binary files to read is written into the text file.
296 \end{DoxyItemize}
298 Similarly, {\ttfamily \hyperlink{group__cnn_gabab7c3c18a2fa4a3a9985da42662682c}{timl\-C\-N\-N\-Read\-From\-File()}} can be used to read a C\-N\-N from text or binary files.
300 One example of the generated text file is shown below\-: 
301 \begin{DoxyCode}
302 paramsBinaryFileName = \textcolor{stringliteral}{'./database/test/cnn/timl\_cnn\_simple\_config.m.params'};
303 stateBinaryFileName  = \textcolor{stringliteral}{'./database/test/cnn/timl\_cnn\_simple\_config.m.state'};
305 cnn.params.\hyperlink{structtimlCNNTrainingParams_ae54e9bf53e695070047e6fe4711d42e2}{count}          = 0;
306 cnn.params.\hyperlink{structtimlCNNTrainingParams_a92c1f02fac7360a8aa865736f27b684a}{batchSize}      = 100;
307 cnn.params.\hyperlink{structtimlCNNTrainingParams_a8c02933b3f59a4b7086c9446e0d67297}{epoch}          = 1;
308 cnn.params.learningRate   = 0.1000;
309 cnn.params.momentum       = 0.0000;
310 cnn.params.phase          = 0;
311 cnn.params.allocatorLevel = 0;
312 cnn.params.\hyperlink{structtimlCNNTrainingParams_a7037ed9b5aa77cb921f21fa338abf26e}{costType}       = 0;
314 layerNum                                    = 8;
315 cnn.layer(1).id                             = 1;
316 cnn.layer(1).type                           = 0;
317 cnn.layer(1).row                            = 28;
318 cnn.layer(1).col                            = 28;
319 cnn.layer(1).channel                        = 1;
320 cnn.layer(1).inputParams.row                = 28;
321 cnn.layer(1).inputParams.col                = 28;
322 cnn.layer(1).inputParams.channel            = 1;
323 cnn.layer(1).inputParams.scale              = 1.0000;
324 cnn.layer(1).inputParams.trainingCropType   = 0;
325 cnn.layer(1).inputParams.trainingMirrorType = 1;
326 cnn.layer(1).inputParams.testingCropType    = 0;
327 cnn.layer(1).inputParams.testingMirrorType  = 1;
329 cnn.layer(2).id                                 = 2;
330 cnn.layer(2).type                               = 1;
331 cnn.layer(2).row                                = 24;
332 cnn.layer(2).col                                = 24;
333 cnn.layer(2).channel                            = 6;
334 cnn.layer(2).convParams.kernelRow               = 5;
335 cnn.layer(2).convParams.kernelCol               = 5;
336 cnn.layer(2).convParams.padUp                   = 0;
337 cnn.layer(2).convParams.padDown                 = 0;
338 cnn.layer(2).convParams.padLeft                 = 0;
339 cnn.layer(2).convParams.padRight                = 0;
340 cnn.layer(2).convParams.strideX                 = 1;
341 cnn.layer(2).convParams.strideY                 = 1;
342 cnn.layer(2).convParams.inputFeatureMapChannel  = 1;
343 cnn.layer(2).convParams.outputFeatureMapChannel = 6;
344 cnn.layer(2).convParams.type                    = 0;
345 cnn.layer(2).convParams.kernelDecayFactor       = 1.0000;
346 cnn.layer(2).convParams.kernelInit.type         = 3;
347 cnn.layer(2).convParams.kernelLearningFactor    = 1.0000;
348 cnn.layer(2).convParams.biasInit.type           = 0;
349 cnn.layer(2).convParams.biasLearningFactor      = 1.0000;
351 cnn.layer(3).id                   = 3;
352 cnn.layer(3).type                 = 3;
353 cnn.layer(3).row                  = 24;
354 cnn.layer(3).col                  = 24;
355 cnn.layer(3).channel              = 6;
356 cnn.layer(3).nonlinearParams.type = 0;
358 cnn.layer(4).id                     = 4;
359 cnn.layer(4).type                   = 2;
360 cnn.layer(4).row                    = 12;
361 cnn.layer(4).col                    = 12;
362 cnn.layer(4).channel                = 6;
363 cnn.layer(4).poolingParams.type     = 0;
364 cnn.layer(4).poolingParams.scaleRow = 2;
365 cnn.layer(4).poolingParams.scaleCol = 2;
366 cnn.layer(4).poolingParams.padUp    = 0;
367 cnn.layer(4).poolingParams.padDown  = 0;
368 cnn.layer(4).poolingParams.padLeft  = 0;
369 cnn.layer(4).poolingParams.padRight = 0;
370 cnn.layer(4).poolingParams.strideX  = 2;
371 cnn.layer(4).poolingParams.strideY  = 2;
373 cnn.layer(5).id                 = 5;
374 cnn.layer(5).type               = 6;
375 cnn.layer(5).row                = 12;
376 cnn.layer(5).col                = 12;
377 cnn.layer(5).channel            = 6;
378 cnn.layer(5).dropoutParams.prob = 0.5000
380 cnn.layer(6).id               = 6;
381 cnn.layer(6).type             = 5;
382 cnn.layer(6).row              = 12;
383 cnn.layer(6).col              = 12;
384 cnn.layer(6).channel          = 6;
385 cnn.layer(6).normParams.type  = 0;
386 cnn.layer(6).normParams.N     = 5;
387 cnn.layer(6).normParams.alpha = 0.0010;
388 cnn.layer(6).normParams.beta  = 0.7500;
390 cnn.layer(7).id                                = 7;
391 cnn.layer(7).type                              = 4;
392 cnn.layer(7).row                               = 1;
393 cnn.layer(7).col                               = 1;
394 cnn.layer(7).channel                           = 10;
395 cnn.layer(7).linearParams.dim                  = 10;
396 cnn.layer(7).linearParams.prevDim              = 864;
397 cnn.layer(7).linearParams.weightDecayFactor    = 1.0000;
398 cnn.layer(7).linearParams.weightInit.type      = 3;
399 cnn.layer(7).linearParams.weightLearningFactor = 1.0000;
400 cnn.layer(7).linearParams.biasInit.type        = 0;
401 cnn.layer(7).linearParams.biasLearningFactor   = 1.0000;
403 cnn.layer(8).id                   = 8;
404 cnn.layer(8).type                 = 3;
405 cnn.layer(8).row                  = 1;
406 cnn.layer(8).col                  = 1;
407 cnn.layer(8).channel              = 10;
408 cnn.layer(8).nonlinearParams.type = 1;
409 \end{DoxyCode}
412 Note that the text file is formatted using Matlab syntax such that the text file can be used as a script to obtain a cnn structure object.\hypertarget{index_cnnTraining}{}\subsection{Training}\label{index_cnnTraining}
413 The following code block in {\ttfamily \hyperlink{group__testCNN_gaae822c926bf3623fccbb8196504df2a1}{test\-C\-N\-N\-Simple\-Training()}} performs training\-:
416 \begin{DoxyCode}
417 \textcolor{comment}{// read MNIST database}
418  printf(\textcolor{stringliteral}{"2. Read the MNIST database\(\backslash\)n"});
419  \hyperlink{group__util_ga218c4e1b952545e65e70b46fc087c0fa}{timlUtilReadMNIST}(DATABASE\_PATH, &training, &testing);
421  \textcolor{comment}{// training}
422  printf(\textcolor{stringliteral}{"3. Start training\(\backslash\)n"});
423  clock\_gettime(CLOCK\_REALTIME, &startTime);
424  \textcolor{keywordflow}{for} (i = 0; i < batchNum; i++) \{
425     \hyperlink{group__cnn_ga9198f6379fad0e9e45dd8f64cc030ec3}{timlCNNSupervisedTrainingWithLabelBatchMode}(cnn, training.
426       data + i*batchSize*dim, training.label + i*batchSize, dim, batchSize);
427  \}
428  clock\_gettime(CLOCK\_REALTIME, &endTime);
429  trainingTime = \hyperlink{group__util_gad332b3fc9cca905cb073bb950c27d9a3}{timlUtilDiffTime}(startTime, endTime);
430  printf(\textcolor{stringliteral}{"Training time = %.2f s.\(\backslash\)n"}, trainingTime/1000000.0);
431 \end{DoxyCode}
434 In this example, the training and testing image data set structures are read from the M\-N\-I\-S\-T database and then passed into {\ttfamily \hyperlink{group__cnn_ga9198f6379fad0e9e45dd8f64cc030ec3}{timl\-C\-N\-N\-Supervised\-Training\-With\-Label\-Batch\-Mode()}}. Note that there is also a multi-\/thread version of this function that called {\ttfamily \hyperlink{group__cnn_ga9198f6379fad0e9e45dd8f64cc030ec3}{timl\-C\-N\-N\-Supervised\-Training\-With\-Label\-Batch\-Mode()}}. The training time is returned by {\ttfamily \hyperlink{group__util_gad332b3fc9cca905cb073bb950c27d9a3}{timl\-Util\-Diff\-Time()}} in microsecond precision.\hypertarget{index_subsectionTesting}{}\subsection{Testing}\label{index_subsectionTesting}
435 The following code block in {\ttfamily test\-C\-N\-N\-Simple\-Testing()} performs testing\-:
438 \begin{DoxyCode}
439 \textcolor{keywordflow}{for}(i = 0; i < testing.num; i++)
440 \{
441     label = \hyperlink{group__cnn_gafda5077b0d39c278469278b37f845aed}{timlCNNClassifyTop1SingleMode}(cnn, testing.data + i*dim, dim);
442     \textcolor{keywordflow}{if} (label != testing.label[i]) misClassifyNum++;
443 \}
444 \end{DoxyCode}
447 {\ttfamily \hyperlink{group__cnn_gafda5077b0d39c278469278b37f845aed}{timl\-C\-N\-N\-Classify\-Top1\-Single\-Mode()}} returns the top 1 label generated by the C\-N\-N. A similar function {\ttfamily \hyperlink{group__cnn_ga8b59ecbca36e54cfa1df0766ece686b5}{timl\-C\-N\-N\-Classify\-Top\-N\-Batch\-Mode()}} that returns the top N labels together with their corresponding probabilities for a batch of data. Note that there is also a multi-\/thread version of this function called {\ttfamily \hyperlink{group__cnn_ga4bb1537483995068ac4d250e9e8a7615}{timl\-C\-N\-N\-Classify\-Top\-N\-Batch\-Mode\-Open\-M\-P()}} that classifies a batch of images using Open\-M\-P.\hypertarget{index_sectionApplications}{}\section{Applications}\label{index_sectionApplications}
448 Application source code is located at {\ttfamily $<$install\-\_\-directory$>$/src/app}. To run the applications, change the directory to the corresponding binary files and run the applications.\par
449  There are 1 application example for Caffe to T\-I\-M\-L C\-N\-N model interoperation\-:
450 \begin{DoxyItemize}
451 \item {\ttfamily app\-C\-N\-N\-Interop\-Caffe}\-: Caffe interoperation
452 \end{DoxyItemize}
454 There are 2 application example for Caffe to T\-I\-M\-L C\-N\-N database conversion\-:
455 \begin{DoxyItemize}
456 \item {\ttfamily app\-C\-N\-N\-Convert\-Image\-Net}\-: Imag\-Net conversion
457 \item {\ttfamily app\-C\-N\-N\-Convert\-S\-B\-D}\-: S\-B\-D conversion
458 \end{DoxyItemize}
460 There are 8 application examples for C\-N\-N classification\-:
461 \begin{DoxyItemize}
462 \item {\ttfamily \hyperlink{group__appCNNClass_ga74ae1f2ec0bb9e1cfc344cdfd204f4da}{app\-C\-N\-N\-Class\-M\-N\-I\-S\-T\-Training()}}\-: M\-N\-I\-S\-T database training
463 \item {\ttfamily \hyperlink{group__appCNNClass_gafa188db78d9c7794ae1bac7a8c93cb3a}{app\-C\-N\-N\-Class\-M\-N\-I\-S\-T\-Testing()}}\-: M\-N\-I\-S\-T database testing
464 \item {\ttfamily \hyperlink{group__appCNNClass_gac0aaedb9badf585311124c33751ee778}{app\-C\-N\-N\-Class\-C\-I\-F\-A\-R10\-Training()}}\-: C\-I\-F\-A\-R10 database training
465 \item {\ttfamily \hyperlink{group__appCNNClass_gaff0f3476d60a46eeb68eae09d6df964a}{app\-C\-N\-N\-Class\-C\-I\-F\-A\-R10\-Testing()}}\-: C\-I\-F\-A\-R10 database testing
466 \item {\ttfamily app\-C\-N\-N\-Class\-Caffe\-Net\-Training()}\-: Caffe\-Net database training
467 \item {\ttfamily app\-C\-N\-N\-Class\-Caffe\-Net\-Testing()}\-: Caffe\-Net database testing
468 \item {\ttfamily app\-C\-N\-N\-Class\-Alex\-Net\-Testing()}\-: Alex\-Net database training
469 \item {\ttfamily app\-C\-N\-N\-Class\-V\-G\-G\-Net\-Testing()}\-: V\-G\-G\-Net database testing
470 \end{DoxyItemize}
472 There are 2 application examples for C\-N\-N scene labeling\-:
473 \begin{DoxyItemize}
474 \item {\ttfamily \hyperlink{group__appCNNScene_ga38cb0fe9d4fb06709a7e9330911c9431}{app\-C\-N\-N\-Scene\-S\-B\-D\-Training()}}\-: scene labeling database training
475 \item {\ttfamily \hyperlink{group__appCNNScene_gaa2f9ce79a0aaf4a15ef414ac09f0ca7d}{app\-C\-N\-N\-Scene\-S\-B\-D\-Testing()}}\-: scene labeling database testing
476 \end{DoxyItemize}\hypertarget{index_subsectionClassification}{}\subsection{Classification}\label{index_subsectionClassification}
477 Training and testing a C\-N\-N for image classification is very straightforward to implement using the T\-I\-M\-L library. Let's take the M\-N\-I\-S\-T database for example. Here is one code block in {\ttfamily \hyperlink{group__appCNNClass_ga74ae1f2ec0bb9e1cfc344cdfd204f4da}{app\-C\-N\-N\-Class\-M\-N\-I\-S\-T\-Training()}}. There are 3 major steps. First, build up the C\-N\-N structure using the library A\-P\-I. Next, read the database. Third, apply the training function on the database. After the training, you can write the trained network to file(s) using {\ttfamily \hyperlink{group__cnn_gad7f3f988c763fe5a80826439c9fb08bd}{timl\-C\-N\-N\-Write\-To\-File()}}. 
478 \begin{DoxyCode}
479 \textcolor{comment}{// setup CNN}
480 printf(\textcolor{stringliteral}{"1. Build up the CNN\(\backslash\)n"});
481 \hyperlink{struct__timlConvNeuralNetwork__}{timlConvNeuralNetwork} *cnn = \hyperlink{group__cnn_gaf75b4687f4b493b8a85bc7fcc37bdf18}{timlCNNCreateConvNeuralNetwork}
482       (\hyperlink{group__cnn_gaaf7a5b272ae2ba5d0453f214afaab747}{timlCNNTrainingParamsDefault}(), 0);
483 cnn->params.learningRate = LEARN\_RATE;
484 cnn->params.\hyperlink{structtimlCNNTrainingParams_a92c1f02fac7360a8aa865736f27b684a}{batchSize}    = BATCH\_SIZE;
485 inputParams = \hyperlink{group__cnn_ga71b91e772f1e63528e53a6284ba7b879}{timlCNNInputParamsDefault}();
486 inputParams.scale = 1.0/256.0;
487 \hyperlink{group__cnn_ga253033ab06b21f2bf28301bc1e53c419}{timlCNNAddInputLayer}(cnn, IMAGE\_ROW, IMAGE\_COL, IMAGE\_CHANNEL, inputParams);           
488         \textcolor{comment}{// input layer}
489 convParams = \hyperlink{group__cnn_ga33ddb7145ae4e19ea52e12576f4871aa}{timlCNNConvParamsDefault}();
490 convParams.kernelInit.type = Util\_Xavier;
491 \hyperlink{group__cnn_ga772bc4b21c2f800ee8ac5248a732f116}{timlCNNAddConvLayer}(cnn, 5, 5, 1, 1, 20, convParams);                                   
492        \textcolor{comment}{// conv layer}
493 poolingParams = \hyperlink{group__cnn_gacd163e268b3ccc9805bcca59689a36f5}{timlCNNPoolingParamsDefault}();
494 \hyperlink{group__cnn_gab5dcc5ac470e15bea85f0a5570eec37d}{timlCNNAddPoolingLayer}(cnn, 2, 2, 2, 2, CNN\_MaxPooling, poolingParams);              
495           \textcolor{comment}{// max pooling layer}
496 convParams = \hyperlink{group__cnn_ga33ddb7145ae4e19ea52e12576f4871aa}{timlCNNConvParamsDefault}();
497 convParams.kernelInit.type = Util\_Xavier;
498 \hyperlink{group__cnn_ga772bc4b21c2f800ee8ac5248a732f116}{timlCNNAddConvLayer}(cnn, 5, 5, 1, 1, 50, convParams);                                   
499        \textcolor{comment}{// conv layer}
500 \hyperlink{group__cnn_gab5dcc5ac470e15bea85f0a5570eec37d}{timlCNNAddPoolingLayer}(cnn, 2, 2, 2, 2, CNN\_MaxPooling, 
501       \hyperlink{group__cnn_gacd163e268b3ccc9805bcca59689a36f5}{timlCNNPoolingParamsDefault}());  \textcolor{comment}{// max pooling layer}
502 \hyperlink{group__cnn_ga0219ba397301c5fb3eedfb64e62e33bc}{timlCNNAddLinearLayer}(cnn, 500, \hyperlink{group__cnn_gad5c8fd10f11ccaa8142a665fc8ee93b8}{timlCNNLinearParamsDefault}()
503       );                           \textcolor{comment}{// linear layer}
504 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Relu);                                          
505             \textcolor{comment}{// relu layer}
506 \hyperlink{group__cnn_ga0219ba397301c5fb3eedfb64e62e33bc}{timlCNNAddLinearLayer}(cnn, 10, \hyperlink{group__cnn_gad5c8fd10f11ccaa8142a665fc8ee93b8}{timlCNNLinearParamsDefault}())
507       ;                            \textcolor{comment}{// linear layer}
508 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Softmax);                                       
509             \textcolor{comment}{// softmax layer}
510 \hyperlink{group__cnn_ga666fa0a68b725aa1e35dba659ba7c5f9}{timlCNNInitialize}(cnn);
511 \hyperlink{group__cnn_ga21bc0c855cb658f51fe5d6911acddb54}{timlCNNReset}(cnn);
512 mem = \hyperlink{group__cnn_ga2e8a7e765f885a236e40dfaed59913c6}{timlCNNMemory}(cnn);
513 \hyperlink{group__cnn_ga123557b8025497fe266d58d80ee9983b}{timlCNNPrint}(cnn);
514 printf(\textcolor{stringliteral}{"CNN memory allocation = %.10f MB.\(\backslash\)n"}, (\textcolor{keywordtype}{float}) mem/1024.0/1024.0);
515 printf(\textcolor{stringliteral}{"CNN parameter #       = %ld.\(\backslash\)n"}, \hyperlink{group__cnn_ga72df82405eb43be8bb75373aaf8ec101}{timlCNNGetParamsNum}(cnn));
517 \textcolor{comment}{// read MNIST database}
518 printf(\textcolor{stringliteral}{"2. Read the MNIST database\(\backslash\)n"});
519 \hyperlink{group__util_ga218c4e1b952545e65e70b46fc087c0fa}{timlUtilReadMNIST}(DATABASE\_PATH, &training, &testing);
521 \textcolor{comment}{// training}
522 printf(\textcolor{stringliteral}{"3. Start training\(\backslash\)n"});
523 clock\_gettime(CLOCK\_REALTIME, &startTime);
524 \textcolor{keywordflow}{for} (i = 0; i < batchNum; i++) \{
525    \hyperlink{group__cnn_ga9198f6379fad0e9e45dd8f64cc030ec3}{timlCNNSupervisedTrainingWithLabelBatchMode}(cnn, training.
526       data + i*batchSize*dim, training.label + i*batchSize, dim, batchSize);
527 \}
528 clock\_gettime(CLOCK\_REALTIME, &endTime);
529 trainingTime = \hyperlink{group__util_gad332b3fc9cca905cb073bb950c27d9a3}{timlUtilDiffTime}(startTime, endTime);
530 printf(\textcolor{stringliteral}{"Training time = %.2f s.\(\backslash\)n"}, trainingTime/1000000.0);
531 \end{DoxyCode}
532  Deploying or testing the C\-N\-N is even simpler. Here is one code block in {\ttfamily \hyperlink{group__appCNNClass_gafa188db78d9c7794ae1bac7a8c93cb3a}{app\-C\-N\-N\-Class\-M\-N\-I\-S\-T\-Testing()}}. Again, there are 3 major steps. First, read the C\-N\-N structure from file(s). Next, read the testing database. Third, apply the testing function to produced the labels generated by the C\-N\-N. 
533 \begin{DoxyCode}
534 \textcolor{comment}{// read CNN config}
535 printf(\textcolor{stringliteral}{"1. Read CNN config\(\backslash\)n"});
536 \hyperlink{struct__timlConvNeuralNetwork__}{timlConvNeuralNetwork} *cnn = \hyperlink{group__cnn_gabab7c3c18a2fa4a3a9985da42662682c}{timlCNNReadFromFile}(MODEL\_PATH, 0);
537 \hyperlink{group__cnn_ga04d9a7e3725ce777402e1e5c4d5d4ce0}{timlCNNSetMode}(cnn, Util\_Tes@subsection subsectionSceneLabeling Scene Labelingt);
538 mem = \hyperlink{group__cnn_ga2e8a7e765f885a236e40dfaed59913c6}{timlCNNMemory}(cnn);
539 \hyperlink{group__cnn_ga123557b8025497fe266d58d80ee9983b}{timlCNNPrint}(cnn);
540 printf(\textcolor{stringliteral}{"CNN memory allocation = %.10f MB.\(\backslash\)n"}, (\textcolor{keywordtype}{float})mem/1024.0/1024.0);
541 printf(\textcolor{stringliteral}{"CNN parameter #       = %lu.\(\backslash\)n"}, \hyperlink{group__cnn_ga72df82405eb43be8bb75373aaf8ec101}{timlCNNGetParamsNum}(cnn));
543 \textcolor{comment}{// read MNIST database}
544 printf(\textcolor{stringliteral}{"2. Read MNIST database\(\backslash\)n"});
545 \hyperlink{group__util_ga218c4e1b952545e65e70b46fc087c0fa}{timlUtilReadMNIST}(DATABASE\_PATH, &training, &testing);
547 \textcolor{comment}{// testing}
548 printf(\textcolor{stringliteral}{"3. Start testing\(\backslash\)n"});
549 clock\_gettime(CLOCK\_REALTIME, &startTime);
550 \hyperlink{group__cnn_ga8b59ecbca36e54cfa1df0766ece686b5}{timlCNNClassifyTopNBatchMode}(cnn, testing.data, dim, testing.num, label, NULL, 
551       topN);
552 clock\_gettime(CLOCK\_REALTIME, &endTime);
553 testingTime = \hyperlink{group__util_gad332b3fc9cca905cb073bb950c27d9a3}{timlUtilDiffTime}(startTime, endTime);
554 classifyNum = \hyperlink{group__util_ga64e5197e9d131870e97cd510b3f499ac}{timlUtilClassifyAccuracy}(label, topN, testing.num, testing.label);
555 classifyPercent = (float)classifyNum/(\textcolor{keywordtype}{float})testing.num;
556 printf(\textcolor{stringliteral}{"Testing time      = %.3f s\(\backslash\)n"}, testingTime/1000000.0);
557 printf(\textcolor{stringliteral}{"Classify accuracy = %.3f %%\(\backslash\)n"}, classifyPercent*100.00);
558 \end{DoxyCode}
559 \hypertarget{index_subsectionSceneLabeling}{}\subsection{Scene Labeling}\label{index_subsectionSceneLabeling}
560 Scene labeling consists in labeling each pixel in an image with the category of the object it belongs to. Instread of producing one lable for the entire image, scene labeling produces one label for each pixel in the image. Therefore, training a C\-N\-N for scene labeling is slightly different from training a C\-N\-N for classification purpose. The setup of the C\-N\-N follows the same procedure\-:
563 \begin{DoxyCode}
564 \textcolor{comment}{// build up the CNN}
565 printf(\textcolor{stringliteral}{"1. Build up the CNN\(\backslash\)n"});
566 cnn = \hyperlink{group__cnn_gaf75b4687f4b493b8a85bc7fcc37bdf18}{timlCNNCreateConvNeuralNetwork}(
567       \hyperlink{group__cnn_gaaf7a5b272ae2ba5d0453f214afaab747}{timlCNNTrainingParamsDefault}(), 0);
568 \hyperlink{group__cnn_ga253033ab06b21f2bf28301bc1e53c419}{timlCNNAddInputLayer}(cnn, PATCH\_SIZE, PATCH\_SIZE, IMAGE\_CHANNEL, 
569       \hyperlink{group__cnn_ga71b91e772f1e63528e53a6284ba7b879}{timlCNNInputParamsDefault}());
570 \hyperlink{group__cnn_ga772bc4b21c2f800ee8ac5248a732f116}{timlCNNAddConvLayer}(cnn, 6, 6, 1, 1, 25, 
571       \hyperlink{group__cnn_ga33ddb7145ae4e19ea52e12576f4871aa}{timlCNNConvParamsDefault}());                    \textcolor{comment}{// conv layer}
572 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Tanh);                                          
573             \textcolor{comment}{// tanh layer}
574 \hyperlink{group__cnn_gab5dcc5ac470e15bea85f0a5570eec37d}{timlCNNAddPoolingLayer}(cnn, 8, 8, 8, 8, CNN\_MaxPooling, 
575       \hyperlink{group__cnn_gacd163e268b3ccc9805bcca59689a36f5}{timlCNNPoolingParamsDefault}());  \textcolor{comment}{// max pooling layer}
576 \hyperlink{group__cnn_ga772bc4b21c2f800ee8ac5248a732f116}{timlCNNAddConvLayer}(cnn, 3, 3, 1, 1, 50, 
577       \hyperlink{group__cnn_ga33ddb7145ae4e19ea52e12576f4871aa}{timlCNNConvParamsDefault}());                    \textcolor{comment}{// conv layer}
578 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Tanh);                                          
579             \textcolor{comment}{// tanh layer}
580 \hyperlink{group__cnn_gab5dcc5ac470e15bea85f0a5570eec37d}{timlCNNAddPoolingLayer}(cnn, 2, 2, 2, 2, CNN\_MaxPooling, 
581       \hyperlink{group__cnn_gacd163e268b3ccc9805bcca59689a36f5}{timlCNNPoolingParamsDefault}());  \textcolor{comment}{// max pooling layer}
582 \hyperlink{group__cnn_ga0219ba397301c5fb3eedfb64e62e33bc}{timlCNNAddLinearLayer}(cnn, 8, \hyperlink{group__cnn_gad5c8fd10f11ccaa8142a665fc8ee93b8}{timlCNNLinearParamsDefault}());
583                                    \textcolor{comment}{// linear layer}
584 \hyperlink{group__cnn_gaddfd98d23613318bdc368eac98fddf0b}{timlCNNAddNonlinearLayer}(cnn, Util\_Softmax);                                       
585             \textcolor{comment}{// softmax layer}
586 \end{DoxyCode}
589 The second step is to setup the training database\-: 
590 \begin{DoxyCode}
591 slTraining.num              = TRAIN\_IMAGE\_NUM;
592 slTraining.row              = IMAGE\_ROW;
593 slTraining.col              = IMAGE\_COL;
594 slTraining.channel          = IMAGE\_CHANNEL;
595 slTraining.patchSize        = PATCH\_SIZE;
596 slTraining.imageFileNameStr = TRAIN\_IMAGE\_PATH;
597 slTraining.labelFileNameStr = TRAIN\_LABEL\_PATH;
598 \end{DoxyCode}
601 In this example, 450 images are used for training and 143 images are used for testing. The dimension of the images are 240$\ast$320$\ast$3. There are 450 text file labels in the training database, each with a 240$\ast$320 label matrix. The label integer ranges from -\/1 to 7 (8 classes) with -\/1 indicating an unlabeled pixel. The image and label text file name must follow the format as indicated in the image\-File\-Name\-Str and label\-File\-Name\-Str. For each pixel in the image, a 133$\ast$133 patch centered at the pixel is passed into the C\-N\-N to output a label.
603 To train the database, the C\-N\-N first randomly selects an image and then randomly selects a pixel. Next, a patch centered at that pixel is passed to the C\-N\-N for training. The pixels are randomly selected without replacement, with 240$\ast$320$\ast$450 iterations needed to sweep through the entire database 1x. There is a specialized training function\-: 
604 \begin{DoxyCode}
605 \hyperlink{group__appCNNScene_gaf308ce265a25eed146051684e2da2671}{appCNNSceneSupervisedTraining}(cnn, &slTraining);
606 \end{DoxyCode}
609 To test one image, a natural way is to generate a patch for each pixel which requires 240$\ast$320 forward passes through the C\-N\-N. However, there is more efficient way to do this which exploits the convolutional structure. First, pad zeros on the test image and shift the image in all four directions. The number of shifted and zero padded images is proportional to the number of pooling layers in the C\-N\-N. Next, pass the shifted and zero padded image to the trained C\-N\-N. Finally, merge the output feature maps into a label matrix for the image. Using this approach, the input feature map size is no longer the patch size which is 133$\ast$133 in the above example. We need to re-\/calculate the input feature map dimensions and use function {\ttfamily \hyperlink{group__cnn_ga78addba765ea7dd8bf958d19d42be931}{timl\-C\-N\-N\-Resize()}} to resize the C\-N\-N. Note that the above cnn has gone through 2 max pooling layers which scales down the feature map by a factor of 2$\ast$8 in the row and column dimensions. The formula to calculate the resized row and col for the input layer is\-: 
610 \begin{DoxyCode}
611 printf(\textcolor{stringliteral}{"3. Resize the feature maps\(\backslash\)n"});
612 resolutionLossRow = 8*2; \textcolor{comment}{// resolution loss due to max pooling}
613 resolutionLossCol = 8*2; \textcolor{comment}{// resolution loss due to max pooling}
614 resizeRow = slTraining.row + (slTraining.patchSize/2)*2 - (resolutionLossRow - 1);
615 resizeCol = slTraining.col + (slTraining.patchSize/2)*2 - (resolutionLossCol - 1);
616 \hyperlink{group__cnn_ga78addba765ea7dd8bf958d19d42be931}{timlCNNResize}(cnn, resizeRow, resizeCol, slTraining.channel);
617 \end{DoxyCode}
620 Multiple forward passes through the same C\-N\-N can be performed in parallel. First, create multilple shared copies of the C\-N\-N to form a team called cnn\-Team. Then we can call the function {\ttfamily \hyperlink{group__appCNNScene_gad77aeeb63649011f260d9c475bef945d}{app\-C\-N\-N\-Scene\-Classify\-Open\-M\-P()}} to perform the labeling operation. A single-\/thread version of the function also exists as {\ttfamily \hyperlink{group__appCNNScene_ga4924a13edce94afaba52667d78ef7256}{app\-C\-N\-N\-Scene\-Classify()}}; The parameter scale in {\ttfamily \hyperlink{group__appCNNScene_gad77aeeb63649011f260d9c475bef945d}{app\-C\-N\-N\-Scene\-Classify\-Open\-M\-P()}} stands for the scale down factor of the generated label matrix. When scale == 1, no downscaling is performed. When scale == 4, the generated label matrix would be of size 60$\ast$80 and then upscaled to 240$\ast$320 to match the size of the image. Downscaling can effectively reduce the labeling time.
623 \begin{DoxyCode}
624 \textcolor{comment}{// create cnnTeam}
625 cnnTeam[0] = cnn;
626 \textcolor{keywordflow}{for} (i = 1; i < thread; i++) \{
627    cnnTeam[i] = \hyperlink{group__cnn_ga52826c14f9f1913947f5ca6937b5afd5}{timlCNNShareParams}(cnn, 0);
628 \}
630 \textcolor{comment}{// testing}
631 printf(\textcolor{stringliteral}{"2. Start testing\(\backslash\)n"});
632 \textcolor{keywordflow}{for} (i = 0; i < slTesting.num; i++) \{
633    printf(\textcolor{stringliteral}{"Read image %03d.jpg\(\backslash\)n"}, i);
634    sprintf(str, slTesting.imageFileNameStr, i);
635    \hyperlink{group__util_ga4e3a2beb9a301e6c71f5c1df09d6099e}{timlUtilReadFixedSizeJPEG}(str, image, slTesting.row, slTesting.col, slTesting.
636       channel);
637    clock\_gettime(CLOCK\_REALTIME, &startTime);
638    \hyperlink{group__appCNNScene_gad77aeeb63649011f260d9c475bef945d}{appCNNSceneClassifyOpenMP}(cnnTeam, thread, image, slTesting.row, slTesting.col,
639        slTesting.channel, labelMatrix, scale);
640    clock\_gettime(CLOCK\_REALTIME, &endTime);
641    testingTime = \hyperlink{group__util_gad332b3fc9cca905cb073bb950c27d9a3}{timlUtilDiffTime}(startTime, endTime);
643    \textcolor{comment}{// read true label}
644    sprintf(str, slTesting.labelFileNameStr, i);
645    fp = fopen(str, \textcolor{stringliteral}{"rt"});
646    \textcolor{keywordflow}{for} (n = 0; n < slTesting.row; n++) \{
647       \textcolor{keywordflow}{for} (m = 0; m < slTesting.col; m++) \{
648          fscanf(fp, \textcolor{stringliteral}{"%d"}, trueLabelMatrix + n*slTesting.col + m);
649       \}
650       fscanf(fp, \textcolor{stringliteral}{"\(\backslash\)n"});
651    \}
652    fclose(fp);
654    \textcolor{comment}{// calculate accuracy}
655    labelAccuracy = \hyperlink{group__appCNNScene_ga656def3c972943463bee38f746074b79}{appCNNSceneAccuracy}(labelMatrix, trueLabelMatrix, slTesting.row*
656       slTesting.col);
657    printf(\textcolor{stringliteral}{"Test image %03d label accuracy = %.2f %%\(\backslash\)n"}, i, 100.0*labelAccuracy);
658    printf(\textcolor{stringliteral}{"Test image %03d time           = %.3f s\(\backslash\)n"}, i, testingTime/1000000.0);
659 \}
660 \end{DoxyCode}
661 \hypertarget{index_sectionBenchmarks}{}\section{Benchmarks}\label{index_sectionBenchmarks}
662 The benchmark source code is located at {\ttfamily $<$install\-\_\-directory$>$/src/benchmark}.
664 There are 2 examples for C\-N\-N classification benchmark\-:
665 \begin{DoxyItemize}
666 \item {\ttfamily \hyperlink{group__benchmarkCNNClass_ga9417402a234aae9eb184bda7d9c293a7}{benchmark\-C\-N\-N\-Class\-Caffe\-Net\-Testing()}} \-: Caffe\-Net benchmark
667 \item {\ttfamily \hyperlink{group__benchmarkCNNClass_ga83c37557afa0f2361fd24c3f4f01603c}{benchmark\-C\-N\-N\-Class\-V\-G\-G\-Net\-Testing()}} \-: V\-G\-G\-Net benchmark
668 \end{DoxyItemize}
670 These two functions will benchmark the memory usage and processing time for each individual layers in the network.