summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'debian/ti-timl/usr/src/timl/src/common/cnn/timlCNNWriteToFile.c')
-rw-r--r--debian/ti-timl/usr/src/timl/src/common/cnn/timlCNNWriteToFile.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/debian/ti-timl/usr/src/timl/src/common/cnn/timlCNNWriteToFile.c b/debian/ti-timl/usr/src/timl/src/common/cnn/timlCNNWriteToFile.c
new file mode 100644
index 0000000..1867461
--- /dev/null
+++ b/debian/ti-timl/usr/src/timl/src/common/cnn/timlCNNWriteToFile.c
@@ -0,0 +1,156 @@
1/******************************************************************************/
2/*!
3 * \file timlCNNWriteToFile.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 "../api/timl.h"
45
46
47/******************************************************************************/
48/*!
49 * \ingroup cnn
50 * \brief Write the cnn to file(s)
51 * \param[in] fileName File name
52 * \param[in] cnn CNN
53 * \param[in] level Parameter write level
54 * \param[in] name Name of the cnn
55 * \param[in] floatFormat Format string for float
56 * \param[in] intFormat Format string for int
57 * \return Error code
58 */
59/******************************************************************************/
60
61int timlCNNWriteToFile(const char *fileName, timlConvNeuralNetwork *cnn, timlUtilParamsLevel level, const char* name, const char *floatFormat, const char *intFormat)
62{
63 int err;
64 char str[TIML_UTIL_MAX_STR];
65 FILE *fp1; // ParamsLevel1
66 FILE *fp2; // ParamsLevel2
67 FILE *fp3; // ParamsLevel3
68 timlCNNLayer *layer;
69 char *baseName;
70
71
72 err = 0;
73 fp1 = NULL;
74 fp2 = NULL;
75 fp3 = NULL;
76 layer = cnn->head;
77 baseName = basename(strdup(fileName));
78
79 if (cnn == NULL) {
80 return ERROR_CNN_NULL_PTR;
81 }
82
83 cnn->params.allocatorLevel = level;
84
85 fp1 = fopen(fileName, "wt"); // level 1, 2, 3
86 if (level == Util_ParamsLevel2 || level == Util_ParamsLevel3) { // level 2, 3
87 sprintf(str, "%s.params", fileName);
88 fp2 = fopen(str, "wb");
89 sprintf(str, "%s.params", baseName);
90 fprintf(fp1, "paramsBinaryFileName = '%s';\n", str);
91 }
92 else {
93 fprintf(fp1, "paramsBinaryFileName = '';\n");
94 }
95
96 if (level == Util_ParamsLevel3 && cnn->params.allocatorLevel == Util_AllocatorLevel1) { // paramsLevel 3 + allocatorLevel1
97 sprintf(str, "%s.states", fileName);
98 fp3 = fopen(str, "wb");
99 sprintf(str, "%s.states", baseName);
100 fprintf(fp1, "stateBinaryFileName = '%s';\n", str);
101 }
102 else {
103 fprintf(fp1, "stateBinaryFileName = '';\n");
104 }
105 fprintf(fp1, "\n");
106
107 // write training params
108 timlCNNTrainingParamsWriteToFile(fp1, cnn, name, floatFormat, intFormat);
109 fprintf(fp1, "\n");
110 fprintf(fp1, "layerNum = %d;\n", timlCNNGetLayerNum(cnn));
111 while (layer != NULL) {
112 // layer type
113 fprintf(fp1, "%s.layer(%d).id = %d;\n", name, layer->id + 1, layer->id + 1);
114 fprintf(fp1, "%s.layer(%d).type = %d;\n", name, layer->id + 1, layer->type);
115 fprintf(fp1, "%s.layer(%d).row = %d;\n", name, layer->id + 1, layer->row);
116 fprintf(fp1, "%s.layer(%d).col = %d;\n", name, layer->id + 1, layer->col);
117 fprintf(fp1, "%s.layer(%d).channel = %d;\n", name, layer->id + 1, layer->channel);
118 switch (layer->type) {
119 case CNN_Dropout:
120 timlCNNDropoutWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
121 break;
122 case CNN_Input:
123 timlCNNInputWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
124 break;
125 case CNN_Conv:
126 timlCNNConvWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
127 break;
128 case CNN_Linear:
129 timlCNNLinearWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
130 break;
131 case CNN_Nonlinear:
132 timlCNNNonlinearWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
133 break;
134 case CNN_Norm:
135 timlCNNNormWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
136 break;
137 case CNN_Pooling:
138 timlCNNPoolingWriteToFile(fp1, fp2, fp3, layer, level, name, floatFormat, intFormat);
139 break;
140 }
141 fprintf(fp1, "\n");
142 layer = layer->next;
143 }
144
145 if (fp1 != NULL) {
146 fclose(fp1);
147 }
148 if (fp2 != NULL) {
149 fclose(fp2);
150 }
151 if (fp3 != NULL) {
152 fclose(fp3);
153 }
154
155 return err;
156}