[ti-machine-learning/ti-machine-learning.git] / debian / ti-timl / usr / src / timl / src / app / cnn / scene / appCNNSceneClassify.c
1 /******************************************************************************/
2 /*!
3 * \file appCNNSceneClassify.c
4 */
5 /* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution.
18 *
19 * Neither the name of Texas Instruments Incorporated nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
38 /*******************************************************************************
39 *
40 * INCLUDES
41 *
42 ******************************************************************************/
44 #include "appCNNScene.h"
47 /******************************************************************************/
48 /*!
49 * \ingroup appCNNScene
50 * \brief Pixel label classification
51 * \param[in,out] cnn CNN
52 * \param[in] image Image
53 * \param[in,out] labelMatrix Generated label matrix
54 * \param[in] scale Down scaling factor of the label matrix
55 * \return Error code
56 */
57 /******************************************************************************/
59 int appCNNSceneClassify(timlConvNeuralNetwork *cnn, timlUtilImage image, int *labelMatrix, int scale)
60 {
61 int i;
62 int j;
63 int p;
64 int m;
65 int k;
66 int err;
67 int numRow;
68 int numCol;
69 int numChannel;
70 float imageMean;
71 float imageDeviation;
72 int paddedRow;
73 int paddedCol;
74 int paddedDim;
75 float *paddedImage;
76 int resolutionLossRow;
77 int resolutionLossCol;
78 int rowStart;
79 int rowEnd;
80 int colStart;
81 int colEnd;
82 int rowDown;
83 int colDown;
85 // init
86 err = 0;
87 numRow = image.row;
88 numCol = image.col;
89 numChannel = image.channel;
90 paddedRow = cnn->head->row;
91 paddedCol = cnn->head->col;
92 paddedDim = paddedRow*paddedCol*numChannel;
93 paddedImage = malloc(sizeof(float)*paddedDim);
94 resolutionLossRow = numRow/cnn->tail->row;
95 resolutionLossCol = numCol/cnn->tail->col;
97 // image normalization (per image)
98 for (k = 0; k < numChannel; k++) {
99 imageMean = 0.0;
100 imageDeviation = 0.0;
101 for (i = 0; i < numRow * numCol; i++) {
102 imageMean += image.data[i + k*numRow*numCol];
103 }
104 imageMean /= numRow*numCol;
105 for (i = 0; i < numRow*numCol; i++) {
106 image.data[i + k*numRow*numCol] -= imageMean;
107 }
108 for (i = 0; i < numRow * numCol; i++) {
109 imageDeviation += image.data[i + k*numRow*numCol] * image.data[i + k*numRow*numCol];
110 }
111 imageDeviation /= numRow*numCol;
112 imageDeviation = sqrtf(imageDeviation);
113 for (i = 0; i < numRow*numCol; i++) {
114 image.data[i + k*numRow*numCol] /= imageDeviation;
115 }
116 }
118 // main loop over each pixel on the image
119 for (m = -resolutionLossRow/2; m < resolutionLossRow/2; m += scale) {
120 for (k = -resolutionLossCol/2; k < resolutionLossCol/2; k += scale) {
121 rowStart = (paddedRow - numRow)/2 - m;
122 rowEnd = rowStart + numRow - 1;
123 colStart = (paddedCol - numCol)/2 - k;
124 colEnd = colStart + numCol - 1;
126 // zero padding
127 for (i = 0; i < paddedRow; i++) {
128 for (j = 0; j < paddedCol; j++) {
129 if (i < rowStart || i > rowEnd || j < colStart || j > colEnd) {
130 for (p = 0; p < numChannel; p++)
131 paddedImage[j + i*paddedCol + p*paddedRow*paddedCol] = 0.0;
132 }
133 else {
134 for (p = 0; p < numChannel; p++)
135 paddedImage[j + i*paddedCol + p*paddedRow*paddedCol] = image.data[j - colStart + (i - rowStart)*numCol + p*numRow*numCol];
136 }
137 }
138 }
140 // cnn Forward Propagation
141 err = timlCNNForwardPropagation(cnn, paddedImage, paddedDim);
143 // labeling
144 appCNNSceneLabelMatrix(cnn->tail->featureMap, cnn->tail->row, cnn->tail->col, cnn->tail->channel, m, k, labelMatrix, numRow, numCol);
145 }
146 }
148 // up-sample the label matrix
149 for (i = 0; i < image.row; i++) {
150 for (j = 0; j < image.col; j++) {
151 rowDown = i/scale;
152 colDown = j/scale;
153 labelMatrix[j + i*image.col] = labelMatrix[colDown*scale + rowDown*scale*image.col];
154 }
155 }
157 free(paddedImage);
159 return err;
160 }