summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'debian/ti-timl/usr/src/timl/src/app/cnn/class/imagenet/appCNNClassImageNetAlexNetTesting.c')
-rw-r--r--debian/ti-timl/usr/src/timl/src/app/cnn/class/imagenet/appCNNClassImageNetAlexNetTesting.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/debian/ti-timl/usr/src/timl/src/app/cnn/class/imagenet/appCNNClassImageNetAlexNetTesting.c b/debian/ti-timl/usr/src/timl/src/app/cnn/class/imagenet/appCNNClassImageNetAlexNetTesting.c
new file mode 100644
index 0000000..778c731
--- /dev/null
+++ b/debian/ti-timl/usr/src/timl/src/app/cnn/class/imagenet/appCNNClassImageNetAlexNetTesting.c
@@ -0,0 +1,168 @@
1/******************************************************************************/
2/*!
3 * \file appCNNClassImageNetAlexNetTesting.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 "../appCNNClass.h"
45
46
47/*******************************************************************************
48 *
49 * DEFINES
50 *
51 ******************************************************************************/
52
53#define MODEL_PATH "../../../../database/model/alexnet/databaseModelAlexNet.m"
54#define LABEL_PATH "../../../../database/imagenet/test/label.txt"
55#define IMAGE_PATH "../../../../database/imagenet/test/%010d.jpg"
56#define TOP_N 5
57#define IMAGE_NUM 100
58#define IMAGE_ROW 256
59#define IMAGE_COL 256
60#define IMAGE_CHANNEL 3
61
62
63/*******************************************************************************
64 *
65 * main()
66 *
67 ******************************************************************************/
68
69int main()
70{
71 return appCNNClassImageNetAlexNetTesting();
72}
73
74
75/******************************************************************************/
76/*!
77 * \ingroup appCNNClass
78 * \brief AlexNet classification testing example
79 */
80/******************************************************************************/
81
82int appCNNClassImageNetAlexNetTesting()
83{
84 int i;
85 int j;
86 int read;
87 int err;
88 int success;
89 float classifyPercent;
90 long mem;
91 FILE *fp;
92 struct timespec startTime;
93 struct timespec endTime;
94 long testingTime;
95 timlUtilImage image;
96 int testNum;
97 int dim;
98 int *testLabel;
99 float *testImage;
100 char str[TIML_UTIL_MAX_STR];
101 int topN;
102 int *label;
103
104 // init
105 err = 0;
106 testNum = IMAGE_NUM;
107 dim = IMAGE_ROW*IMAGE_COL*IMAGE_CHANNEL;
108 topN = TOP_N;
109
110 setbuf(stdout, NULL); // do not buffer the console output
111
112 // read CNN config
113 printf("1. Read CNN config\n");
114 timlConvNeuralNetwork *cnn = timlCNNReadFromFile(MODEL_PATH, 0);
115 mem = timlCNNMemory(cnn);
116 timlCNNPrint(cnn);
117 printf("CNN memory allocation = %.10f MB.\n", (float)mem/1024.0/1024.0);
118 printf("CNN parameter # = %lu.\n", timlCNNGetParamsNum(cnn));
119 timlCNNSetMode(cnn, Util_Test);
120
121 // read test images
122 printf("2. Read test images\n");
123 testLabel = malloc(sizeof(int)*testNum);
124 testImage = malloc(sizeof(float)*IMAGE_ROW*IMAGE_COL*IMAGE_CHANNEL*testNum);
125
126 // read labels
127 fp = fopen(LABEL_PATH, "rt");
128 for (i = 0; i < testNum; i++) {
129 read = fscanf(fp, "%d", testLabel + i);
130 }
131 fclose(fp);
132
133 // read images
134 for (i = 0; i < testNum; i++) {
135 sprintf(str, IMAGE_PATH, i);
136 image = timlUtilReadJPEG(str);
137 if (image.channel == 1) { // duplicate channels
138 for (j = 0; j < IMAGE_CHANNEL; j++) {
139 cblas_scopy(IMAGE_ROW * IMAGE_COL, image.data, 1, testImage + i*dim + j*IMAGE_ROW*IMAGE_COL, 1);
140 }
141 }
142 else {
143 cblas_scopy(dim, image.data, 1, testImage + i*dim, 1);
144 }
145 free(image.data);
146 }
147 label = malloc(sizeof(int)*testNum*topN);
148
149 // testing
150 printf("3. Start testing\n");
151 clock_gettime(CLOCK_REALTIME, &startTime);
152 timlCNNClassifyTopNBatchModeOpenMP(cnn, testImage, dim, testNum, label, NULL, topN);
153 clock_gettime(CLOCK_REALTIME, &endTime);
154 testingTime = timlUtilDiffTime(startTime, endTime);
155 success = timlUtilClassifyAccuracy(label, topN, testNum, testLabel);
156 classifyPercent = (float)success/(float)testNum;
157 printf("Testing time = %.2f s.\n", testingTime/1000000.0);
158 printf("Top%d success percent = %.3f %%\n", topN, classifyPercent*100.00);
159
160 // cleaning up
161 printf("4. Clean up\n");
162 timlCNNDelete(cnn);
163 free(testLabel);
164 free(testImage);
165 free(label);
166
167 return err;
168}