[ti-machine-learning/ti-machine-learning.git] / src / common / cnn / timlCNNLinearReadFromTextFile.c
1 /******************************************************************************/
2 /*!
3 * \file timlCNNLinearReadFromTextFile.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 "../api/timl.h"
47 /******************************************************************************/
48 /*!
49 * \ingroup cnn
50 * \brief Read the linear layer from a text file
51 * \param[in] fp1 FILE ptr to the level 1 text file
52 * \param[in,out] cnn CNN
53 * \return Error code
54 */
55 /******************************************************************************/
57 int timlCNNLinearReadFromTextFile(FILE *fp1, timlConvNeuralNetwork *cnn)
58 {
59 int err;
60 int intBuffer;
61 int row;
62 int col;
63 int channel;
64 int read;
65 timlCNNLinearParams params;
66 char str[TIML_UTIL_MAX_STR];
68 err = 0;
69 params = timlCNNLinearParamsDefault();
71 read = fscanf(fp1, "%[^.].layer(%d).row = %d;\n", (char*)&str, &intBuffer, &row);
72 if (read != 3) {
73 timlCNNDelete(cnn);
74 return ERROR_CNN_READ_FILE;
75 }
76 read = fscanf(fp1, "%[^.].layer(%d).col = %d;\n", (char*)&str, &intBuffer, &col);
77 if (read != 3) {
78 timlCNNDelete(cnn);
79 return ERROR_CNN_READ_FILE;
80 }
81 read = fscanf(fp1, "%[^.].layer(%d).channel = %d;\n", (char*)&str, &intBuffer, &channel);
82 if (read != 3) {
83 timlCNNDelete(cnn);
84 return ERROR_CNN_READ_FILE;
85 }
86 read = fscanf(fp1, "%[^.].layer(%d).linearParams.dim = %d;\n", (char*)&str, &intBuffer, ¶ms.dim);
87 if (read != 3) {
88 timlCNNDelete(cnn);
89 return ERROR_CNN_READ_FILE;
90 }
91 read = fscanf(fp1, "%[^.].layer(%d).linearParams.prevDim = %d;\n", (char*)&str, &intBuffer, ¶ms.prevDim);
92 if (read != 3) {
93 timlCNNDelete(cnn);
94 return ERROR_CNN_READ_FILE;
95 }
96 read = fscanf(fp1, "%[^.].layer(%d).linearParams.weightDecayFactor = %f;\n", (char*)&str, &intBuffer, ¶ms.weightDecayFactor);
97 if (read != 3) {
98 timlCNNDelete(cnn);
99 return ERROR_CNN_READ_FILE;
100 }
101 read = fscanf(fp1, "%[^.].layer(%d).linearParams.weightInit.type = %d;\n", (char*)&str, &intBuffer, (int*)¶ms.weightInit.type);
102 if (read != 3) {
103 timlCNNDelete(cnn);
104 return ERROR_CNN_READ_FILE;
105 }
106 read = fscanf(fp1, "%[^.].layer(%d).linearParams.weightLearningFactor = %f;\n", (char*)&str, &intBuffer, ¶ms.weightLearningFactor);
107 if (read != 3) {
108 timlCNNDelete(cnn);
109 return ERROR_CNN_READ_FILE;
110 }
111 read = fscanf(fp1, "%[^.].layer(%d).linearParams.biasInit.type = %d;\n", (char*)&str, &intBuffer, (int*)¶ms.biasInit.type);
112 if (read != 3) {
113 timlCNNDelete(cnn);
114 return ERROR_CNN_READ_FILE;
115 }
116 read = fscanf(fp1, "%[^.].layer(%d).linearParams.biasLearningFactor = %f;\n", (char*)&str, &intBuffer, ¶ms.biasLearningFactor);
117 if (read != 3) {
118 timlCNNDelete(cnn);
119 return ERROR_CNN_READ_FILE;
120 }
122 // create structure
123 err = timlCNNAddLinearLayer(cnn, params.dim, params);
124 if (err) {
125 timlCNNDelete(cnn);
126 return err;
127 }
129 return 0;
130 }