diff options
Diffstat (limited to 'debian/ti-timl/usr/src/timl/src/app/cnn/scene/sbd/appCNNSceneSBDTesting.c')
-rw-r--r-- | debian/ti-timl/usr/src/timl/src/app/cnn/scene/sbd/appCNNSceneSBDTesting.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/debian/ti-timl/usr/src/timl/src/app/cnn/scene/sbd/appCNNSceneSBDTesting.c b/debian/ti-timl/usr/src/timl/src/app/cnn/scene/sbd/appCNNSceneSBDTesting.c new file mode 100644 index 0000000..646e6c2 --- /dev/null +++ b/debian/ti-timl/usr/src/timl/src/app/cnn/scene/sbd/appCNNSceneSBDTesting.c | |||
@@ -0,0 +1,175 @@ | |||
1 | /******************************************************************************/ | ||
2 | /*! | ||
3 | * \file appCNNSceneSBDTesting.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 | ******************************************************************************/ | ||
36 | |||
37 | |||
38 | /******************************************************************************* | ||
39 | * | ||
40 | * INCLUDES | ||
41 | * | ||
42 | ******************************************************************************/ | ||
43 | |||
44 | #include "../appCNNScene.h" | ||
45 | |||
46 | |||
47 | /******************************************************************************* | ||
48 | * | ||
49 | * DEFINES | ||
50 | * | ||
51 | ******************************************************************************/ | ||
52 | |||
53 | #define SCALE 4 | ||
54 | #define IMAGE_NUM 10 | ||
55 | #define IMAGE_ROW 240 | ||
56 | #define IMAGE_COL 320 | ||
57 | #define IMAGE_CHANNEL 3 | ||
58 | #define PATCH_SIZE 133 | ||
59 | #define MODEL_PATH "../../../../database/model/sbd/databaseModelSBD.m" | ||
60 | #define IMAGE_PATH "../../../../database/sbd/test/%03d.jpg" | ||
61 | #define LABEL_PATH "../../../../database/sbd/test/%03d.txt" | ||
62 | |||
63 | |||
64 | /******************************************************************************* | ||
65 | * | ||
66 | * main() | ||
67 | * | ||
68 | ******************************************************************************/ | ||
69 | |||
70 | int main() | ||
71 | { | ||
72 | return appCNNSceneSBDTesting(); | ||
73 | } | ||
74 | |||
75 | |||
76 | /******************************************************************************/ | ||
77 | /*! | ||
78 | * \ingroup appCNNScene | ||
79 | * \brief Standford Backgournd Database Scene labeling testing example | ||
80 | */ | ||
81 | /******************************************************************************/ | ||
82 | |||
83 | int appCNNSceneSBDTesting() | ||
84 | { | ||
85 | int i; | ||
86 | int n; | ||
87 | int m; | ||
88 | int read; | ||
89 | struct timespec startTime; | ||
90 | struct timespec endTime; | ||
91 | long testingTime; | ||
92 | int err; | ||
93 | long mem; | ||
94 | int scale; | ||
95 | int *labelMatrix; | ||
96 | float *image; | ||
97 | int *trueLabelMatrix; | ||
98 | float labelAccuracy; | ||
99 | int thread; | ||
100 | timlConvNeuralNetwork **cnnTeam; | ||
101 | char str[TIML_UTIL_MAX_STR]; | ||
102 | FILE *fp; | ||
103 | appCNNSceneDataSet slTesting; | ||
104 | |||
105 | // init | ||
106 | err = 0; | ||
107 | scale = SCALE; | ||
108 | thread = omp_get_max_threads(); | ||
109 | slTesting.num = IMAGE_NUM; | ||
110 | slTesting.row = IMAGE_ROW; | ||
111 | slTesting.col = IMAGE_COL; | ||
112 | slTesting.channel = IMAGE_CHANNEL; | ||
113 | slTesting.patchSize = PATCH_SIZE; | ||
114 | slTesting.imageFileNameStr = IMAGE_PATH; | ||
115 | slTesting.labelFileNameStr = LABEL_PATH; | ||
116 | labelMatrix = malloc(sizeof(int)*slTesting.row*slTesting.col); | ||
117 | trueLabelMatrix = malloc(sizeof(int)*slTesting.row*slTesting.col); | ||
118 | image = malloc(sizeof(float)*slTesting.row*slTesting.col*slTesting.channel); | ||
119 | cnnTeam = malloc(sizeof(timlConvNeuralNetwork*)*thread); | ||
120 | setbuf(stdout, NULL); // do not buffer the console output | ||
121 | |||
122 | // read CNN config | ||
123 | printf("1. Read cnn config\n"); | ||
124 | timlConvNeuralNetwork *cnn = timlCNNReadFromFile(MODEL_PATH, 0); | ||
125 | mem = timlCNNMemory(cnn); | ||
126 | printf("CNN memory = %.10f MB.\n", (float)mem/1024.0/1024.0); | ||
127 | |||
128 | // create cnnTeam | ||
129 | cnnTeam[0] = cnn; | ||
130 | for (i = 1; i < thread; i++) { | ||
131 | cnnTeam[i] = timlCNNShareParams(cnn, 0); | ||
132 | } | ||
133 | |||
134 | // testing | ||
135 | printf("2. Start testing\n"); | ||
136 | for (i = 0; i < slTesting.num; i++) { | ||
137 | printf("Read image %03d.jpg\n", i); | ||
138 | sprintf(str, slTesting.imageFileNameStr, i); | ||
139 | timlUtilReadFixedSizeJPEG(str, image, slTesting.row, slTesting.col, slTesting.channel); | ||
140 | clock_gettime(CLOCK_REALTIME, &startTime); | ||
141 | appCNNSceneClassifyOpenMP(cnnTeam, thread, image, slTesting.row, slTesting.col, slTesting.channel, labelMatrix, scale); | ||
142 | clock_gettime(CLOCK_REALTIME, &endTime); | ||
143 | testingTime = timlUtilDiffTime(startTime, endTime); | ||
144 | |||
145 | // read true label | ||
146 | sprintf(str, slTesting.labelFileNameStr, i); | ||
147 | fp = fopen(str, "rt"); | ||
148 | for (n = 0; n < slTesting.row; n++) { | ||
149 | for (m = 0; m < slTesting.col; m++) { | ||
150 | read = fscanf(fp, "%d", trueLabelMatrix + n*slTesting.col + m); | ||
151 | } | ||
152 | read = fscanf(fp, "\n"); | ||
153 | } | ||
154 | fclose(fp); | ||
155 | |||
156 | // calculate accuracy | ||
157 | labelAccuracy = appCNNSceneAccuracy(labelMatrix, trueLabelMatrix, slTesting.row*slTesting.col); | ||
158 | printf("Test image %03d label accuracy = %.2f %%\n", i, 100.0*labelAccuracy); | ||
159 | printf("Test image %03d time = %.3f s\n", i, testingTime/1000000.0); | ||
160 | } | ||
161 | |||
162 | // clean up | ||
163 | printf("4. Clean up"); | ||
164 | for (i = 0; i < thread; i++) { | ||
165 | timlCNNDelete(cnnTeam[i]); | ||
166 | } | ||
167 | free(cnnTeam); | ||
168 | free(labelMatrix); | ||
169 | free(trueLabelMatrix); | ||
170 | free(image); | ||
171 | |||
172 | return err; | ||
173 | |||
174 | } | ||
175 | |||