]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ti-machine-learning/ti-machine-learning.git/blob - src/app/cnn/class/imagenet/appCNNClassImageNetAlexNetTesting.c
1. Enable network state write/read
[ti-machine-learning/ti-machine-learning.git] / src / app / cnn / class / imagenet / appCNNClassImageNetAlexNetTesting.c
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  ******************************************************************************/
38 /*******************************************************************************
39  *
40  * INCLUDES
41  *
42  ******************************************************************************/
44 #include "../appCNNClass.h"
47 /*******************************************************************************
48  *
49  * DEFINES
50  *
51  ******************************************************************************/
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        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 appCNNClassImageNetAlexNetTesting();
72 }
75 /******************************************************************************/
76 /*!
77  * \ingroup appCNNClass
78  * \brief   AlexNet classification testing example
79  */
80 /******************************************************************************/
82 int appCNNClassImageNetAlexNetTesting()
83 {
84    int             i;
85    int             j;
86    int             read;
87    int             err;
88    int             success;
89    float           classifyPercent;
90    long            mem1, mem2, mem3;
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;
103    // init
104    err     = 0;
105    testNum = IMAGE_NUM;
106    dim     = IMAGE_ROW*IMAGE_COL*IMAGE_CHANNEL;
107    topN    = TOP_N;
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);
118    timlCNNPrint(cnn);
119    mem1 = cnn->forwardMemory + cnn->backwardMemory + cnn->fixedMemory + cnn->paramsMemory;
120    mem2 = cnn->forwardMemory + cnn->fixedMemory + cnn->paramsMemory;
121    mem3 = cnn->memPoolSize + cnn->fixedMemory + cnn->paramsMemory;
122    printf("CNN level 1 memory size = %10.4f MB.\n", (float)mem1/1024.0/1024.0);
123    printf("CNN level 2 memory size = %10.4f MB.\n", (float)mem2/1024.0/1024.0);
124    printf("CNN level 3 memory size = %10.4f MB.\n", (float)mem3/1024.0/1024.0);
125    printf("CNN forward memory size = %10.4f MB.\n", (float)cnn->forwardMemory/1024.0/1024.0);
126    printf("CNN memory pool size    = %10.4f MB.\n", (float)cnn->memPoolSize/1024.0/1024.0);
127    printf("CNN params memory size  = %10.4f MB.\n", (float)cnn->paramsMemory/1024.0/1024.0);
128    timlCNNSetMode(cnn, Util_Test);
130    // read test images
131    printf("2. Read test images\n");
132    testLabel = malloc(sizeof(int)*testNum);
133    testImage = malloc(sizeof(float)*IMAGE_ROW*IMAGE_COL*IMAGE_CHANNEL*testNum);
135    // read labels
136    fp = fopen(LABEL_PATH, "rt");
137    for (i = 0; i < testNum; i++) {
138       read = fscanf(fp, "%d", testLabel + i);
139    }
140    fclose(fp);
142    // read images
143    for (i = 0; i < testNum; i++) {
144       sprintf(str, IMAGE_PATH, i);
145       image = timlUtilReadJPEG(str);
146       if (image.channel == 1) { // duplicate channels
147          for (j = 0; j < IMAGE_CHANNEL; j++) {
148             cblas_scopy(IMAGE_ROW*IMAGE_COL, image.data, 1, testImage + i*dim + j*IMAGE_ROW*IMAGE_COL, 1);
149          }
150       }
151       else {
152          cblas_scopy(dim, image.data, 1, testImage + i*dim, 1);
153       }
154       free(image.data);
155    }
157    // testing
158    printf("3. Start testing\n");
159    clock_gettime(CLOCK_REALTIME, &startTime);
160    timlCNNClassifyAccuracy(cnn, testImage, IMAGE_ROW, IMAGE_COL, IMAGE_CHANNEL, testLabel, 1, 1, testNum, &success);
161    clock_gettime(CLOCK_REALTIME, &endTime);
162    testingTime = timlUtilDiffTime(startTime, endTime);
163    classifyPercent = (float)success/(float)testNum;
164    printf("Testing time          = %.2f s.\n", testingTime/1000000.0);
165    printf("Top%d success percent  = %.3f %%\n", topN, classifyPercent*100.00);
167    // cleaning up
168    printf("4. Clean up\n");
169    timlCNNDelete(cnn);
170    free(testLabel);
171    free(testImage);
173    return err;