]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ti-machine-learning/ti-machine-learning.git/blob - src/app/cnn/class/imagenet/appCNNClassImageNetCaffeNetTesting.c
Add testignore
[ti-machine-learning/ti-machine-learning.git] / src / app / cnn / class / imagenet / appCNNClassImageNetCaffeNetTesting.c
1 /******************************************************************************/
2 /*!
3  * \file appCNNClassImageNetCaffeNetTesting.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 "../appCNNClass.h"
47 /*******************************************************************************
48  *
49  * DEFINES
50  *
51  ******************************************************************************/
53 #define MODEL_PATH       "../../../../database/model/caffenet/databaseModelCaffeNet.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        1000
58 #define IMAGE_ROW        256
59 #define IMAGE_COL        256
60 #define IMAGE_CHANNEL    3
63 /*******************************************************************************
64  *
65  * main()
66  *
67  ******************************************************************************/
69 int main()
70 {
71    return appCNNClassImageNetCaffeNetTesting();
72 }
75 /******************************************************************************/
76 /*!
77  * \ingroup appCNNClass
78  * \brief   CaffeNet classification testing example
79  */
80 /******************************************************************************/
82 int appCNNClassImageNetCaffeNetTesting() {
83    int             i;
84    int             j;
85    int             read;
86    int             err;
87    int             success;
88    float           classifyPercent;
89    long            mem1;
90    long            mem2;
91    long            mem3;
92    FILE            *fp;
93    struct timespec startTime;
94    struct timespec endTime;
95    long            testingTime;
96    timlUtilImage   image;
97    int             testNum;
98    int             dim;
99    int             *testLabel;
100    float           *testImage;
101    char            str[TIML_UTIL_MAX_STR];
102    int             *label;
104    // init
105    err     = 0;
106    testNum = IMAGE_NUM;
107    dim     = IMAGE_ROW*IMAGE_COL*IMAGE_CHANNEL;
109    setbuf(stdout, NULL); // do not buffer the console output
111    // read CNN config
112    printf("1. Read CNN config\n");
113    timlConvNeuralNetwork *cnn = timlCNNReadFromFile(MODEL_PATH);
114    timlCNNAddAccuracyLayer(cnn, TOP_N);
115    timlCNNInitialize(cnn);
116    timlCNNLoadParamsFromFile(cnn, cnn->paramsFileName);
117    timlCNNPrint(cnn);
118    mem1 = cnn->forwardMemory + cnn->backwardMemory + cnn->fixedMemory + cnn->paramsMemory;
119    mem2 = cnn->forwardMemory + cnn->fixedMemory + cnn->paramsMemory;
120    mem3 = cnn->memPoolSize + cnn->fixedMemory + cnn->paramsMemory;
121    printf("CNN level 1 memory size = %10.4f MB.\n", (float)mem1/1024.0/1024.0);
122    printf("CNN level 2 memory size = %10.4f MB.\n", (float)mem2/1024.0/1024.0);
123    printf("CNN level 3 memory size = %10.4f MB.\n", (float)mem3/1024.0/1024.0);
124    printf("CNN memory pool size    = %10.4f MB.\n", (float)cnn->memPoolSize/1024.0/1024.0);
125    printf("CNN params memory size  = %10.4f MB.\n", (float)timlCNNGetParamsNum(cnn)*sizeof(float)/1024.0/1024.0);
126    timlCNNSetMode(cnn, Util_Test);
128    // read test images
129    printf("2. Read test images\n");
130    testLabel = malloc(sizeof(int)*testNum);
131    testImage = malloc(sizeof(float)*dim*testNum);
133    // read labels
134    fp = fopen(LABEL_PATH, "rt");
135    for (i = 0; i < testNum; i++) {
136       read = fscanf(fp, "%d", testLabel + i);
137    }
138    fclose(fp);
140    // read images
141    for (i = 0; i < testNum; i++) {
142       sprintf(str, IMAGE_PATH, i);
143       image = timlUtilReadJPEG(str);
144       if (image.channel == 1) { // duplicate channels
145          for (j = 0; j < IMAGE_CHANNEL; j++) {
146             cblas_scopy(IMAGE_ROW*IMAGE_COL, image.data, 1, testImage + i*dim + j*IMAGE_ROW*IMAGE_COL, 1);
147          }
148       }
149       else {
150          cblas_scopy(dim, image.data, 1, testImage + i*dim, 1);
151       }
152       free(image.data);
153    }
155    // testing
156    printf("3. Start testing\n");
157    clock_gettime(CLOCK_REALTIME, &startTime);
158    timlCNNClassifyAccuracy(cnn, testImage, IMAGE_ROW, IMAGE_COL, IMAGE_CHANNEL, testLabel, 1, 1, testNum, &success);
159    clock_gettime(CLOCK_REALTIME, &endTime);
160    testingTime = timlUtilDiffTime(startTime, endTime);
161    classifyPercent = (float) success/(float) testNum;
162    printf("Testing time          = %.2f s.\n", testingTime/1000000.0);
163    printf("Top%d success percent = %.3f %%\n", TOP_N, classifyPercent*100.00);
165    // cleaning
166    printf("4. Clean up\n");
167    timlCNNDelete(cnn);
168    free(testLabel);
169    free(testImage);
170    free(label);
172    return err;