]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ti-machine-learning/ti-machine-learning.git/blob - src/common/cnn/timlCNNWriteToFile.c
Remove deleted files
[ti-machine-learning/ti-machine-learning.git] / src / common / cnn / timlCNNWriteToFile.c
1 /******************************************************************************/\r
2 /*!\r
3  * \file timlCNNWriteToFile.c\r
4  */\r
5 /* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/\r
6  *\r
7  * Redistribution and use in source and binary forms, with or without\r
8  * modification, are permitted provided that the following conditions\r
9  * are met:\r
10  *\r
11  *    Redistributions of source code must retain the above copyright\r
12  *    notice, this list of conditions and the following disclaimer.\r
13  *\r
14  *    Redistributions in binary form must reproduce the above copyright\r
15  *    notice, this list of conditions and the following disclaimer in the\r
16  *    documentation and/or other materials provided with the\r
17  *    distribution.\r
18  *\r
19  *    Neither the name of Texas Instruments Incorporated nor the names of\r
20  *    its contributors may be used to endorse or promote products derived\r
21  *    from this software without specific prior written permission.\r
22  *\r
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
34  *\r
35  ******************************************************************************/\r
36 \r
37 \r
38 /*******************************************************************************\r
39  *\r
40  * INCLUDES\r
41  *\r
42  ******************************************************************************/\r
43 \r
44 #include "../api/timl.h"\r
45 \r
46 \r
47 /******************************************************************************/\r
48 /*!\r
49  * \ingroup cnn\r
50  * \brief     Write the cnn to file(s)\r
51  * \param[in] fileName    File name\r
52  * \param[in] cnn         CNN\r
53  * \param[in] level       Parameter write level\r
54  * \param[in] name        Name of the cnn\r
55  * \param[in] floatFormat Format string for float\r
56  * \param[in] intFormat   Format string for int\r
57  * \return    Error code\r
58  */\r
59 /******************************************************************************/\r
60 \r
61 int timlCNNWriteToFile(const char *fileName, timlConvNeuralNetwork *cnn, timlUtilParamsLevel level, const char* name, const char *floatFormat, const char *intFormat)\r
62 {\r
63    int          err;\r
64    char         str[TIML_UTIL_MAX_STR];\r
65    FILE         *fp1; // ParamsLevel1\r
66    FILE         *fp2; // ParamsLevel2\r
67    FILE         *fp3; // ParamsLevel3\r
68    timlCNNLayer *layer;\r
69    char         *baseName;\r
70 \r
71 \r
72    err   = 0;\r
73    fp1   = NULL;\r
74    fp2   = NULL;\r
75    fp3   = NULL;\r
76    layer = cnn->head;\r
77    baseName = basename(strdup(fileName));\r
78 \r
79    if (cnn == NULL) {\r
80       return ERROR_CNN_NULL_PTR;\r
81    }\r
82 \r
83    // paramsLevel 1, 2, 3\r
84    fp1 = fopen(fileName, "wt");\r
85    if (level == Util_ParamsLevel2 || level == Util_ParamsLevel3) { // level 2, 3\r
86       sprintf(str, "%s.params", fileName);\r
87       fp2 = fopen(str, "wb");\r
88       sprintf(str, "%s.params", baseName);\r
89       fprintf(fp1, "paramsBinaryFileName = '%s';\n", str);\r
90    }\r
91    else {\r
92       fprintf(fp1, "paramsBinaryFileName = '';\n");\r
93    }\r
94 \r
95    // paramsLevel 3\r
96    if (level == Util_ParamsLevel3) {\r
97       sprintf(str, "%s.states", fileName);\r
98       fp3 = fopen(str, "wb");\r
99       sprintf(str, "%s.states", baseName);\r
100       fprintf(fp1, "stateBinaryFileName = '%s';\n", str);\r
101    }\r
102    else {\r
103       fprintf(fp1, "stateBinaryFileName = '';\n");\r
104    }\r
105    fprintf(fp1, "\n");\r
106 \r
107    // allocatorLevel3\r
108    if (cnn->params.allocatorLevel == Util_AllocatorLevel3) {\r
109       timlUtilFwrite(cnn->memPool, sizeof(char), cnn->memPoolSize, fp3);\r
110       fclose(fp3);\r
111       fp3 = NULL;\r
112    }\r
113 \r
114    // write training params\r
115    timlCNNTrainingParamsWriteToFile(fp1, cnn, name, floatFormat, intFormat);\r
116    fprintf(fp1, "\n");\r
117    fprintf(fp1, "layerNum = %d;\n", timlCNNGetLayerNum(cnn));\r
118    while (layer != NULL) {\r
119       // layer type\r
120       fprintf(fp1, "%s.layer(%d).id = %d;\n", name, layer->id + 1, layer->id + 1);\r
121       fprintf(fp1, "%s.layer(%d).type = %d;\n", name, layer->id + 1, layer->type);\r
122       fprintf(fp1, "%s.layer(%d).row = %d;\n", name, layer->id + 1, layer->row);\r
123       fprintf(fp1, "%s.layer(%d).col = %d;\n", name, layer->id + 1, layer->col);\r
124       fprintf(fp1, "%s.layer(%d).channel = %d;\n", name, layer->id + 1, layer->channel);\r
125       switch (layer->type) {\r
126          case CNN_Accuracy:\r
127             timlCNNAccuracyWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
128             break;\r
129          case CNN_Softmax:\r
130           timlCNNSoftmaxWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
131           break;\r
132          case CNN_SoftmaxCost:\r
133             timlCNNSoftmaxCostWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
134             break;\r
135          case CNN_Dropout:\r
136             timlCNNDropoutWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
137             break;\r
138          case CNN_Input:\r
139             timlCNNInputWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
140             break;\r
141          case CNN_Conv:\r
142             timlCNNConvWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
143             break;\r
144          case CNN_Linear:\r
145             timlCNNLinearWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
146             break;\r
147          case CNN_Nonlinear:\r
148             timlCNNNonlinearWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
149             break;\r
150          case CNN_Norm:\r
151             timlCNNNormWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
152             break;\r
153          case CNN_Pooling:\r
154             timlCNNPoolingWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);\r
155             break;\r
156          default:\r
157                 break;\r
158       }\r
159       fprintf(fp1, "\n");\r
160       layer = layer->next;\r
161    }\r
162 \r
163    if (fp1 != NULL) {\r
164       fclose(fp1);\r
165    }\r
166    if (fp2 != NULL) {\r
167       fclose(fp2);\r
168    }\r
169    if (fp3 != NULL) {\r
170       fclose(fp3);\r
171    }\r
172 \r
173    return err;\r
174 }\r