1 /******************************************************************************
2 * Copyright (c) 2017 Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 #include <string>
30 #include <fstream>
32 #include "configuration.h"
33 #include "parameters.h"
35 using namespace tinn;
37 Configuration::Configuration(): numFrames(0), inHeight(0), inWidth(0),
38 inNumChannels(0),
39 noZeroCoeffsPercentage(100),
40 preProcType(0),
41 layersGroupId(tinn::internal::CURR_LAYERS_GROUP_ID),
42 runFullNet(0),
43 enableInternalInput(0),
44 EXTMEM_HEAP_SIZE(64 << 20), // 64MB for inceptionNetv1
45 PARAM_HEAP_SIZE(9 << 20) // 9MB for mobileNet1
46 {
47 }
49 void Configuration::Print(std::ostream &os) const
50 {
51 os << "Configuration"
52 << "\nFrame= " << numFrames << " " << inWidth << "x"
53 << inHeight << "x" << inNumChannels
54 << "\nPreProcType " << preProcType
55 << "\nLayersGroupId " << layersGroupId
56 << "\nRunFullNet " << runFullNet
57 << "\nEnableInternalInput " << enableInternalInput
58 << "\nInputFile " << inData
59 << "\nOutputFile " << outData
60 << "\nNetwork " << netBinFile
61 << "\nParameters " << paramsBinFile
62 << "\nEO Heap Size (MB) " << (EXTMEM_HEAP_SIZE >> 20)
63 << "\nParameter heap size (MB) " << (PARAM_HEAP_SIZE >> 20)
64 << "\n";
65 }
67 #include <sys/stat.h>
68 bool Configuration::Validate() const
69 {
70 int errors = 0;
72 if (inHeight == 0 || inWidth == 0)
73 {
74 std::cerr << "inHeight, inWidth must be > 0" << std::endl;
75 errors++;
76 }
78 if (inNumChannels < 1)
79 {
80 std::cerr << "inNumChannels must be > 1" << std::endl;
81 errors++;
82 }
84 struct stat buffer;
85 if (stat(netBinFile.c_str(), &buffer) != 0)
86 {
87 std::cerr << "netBinFile not found: " << netBinFile << std::endl;
88 errors++;
89 }
91 size_t paramsBinFileSize = 0;
92 if (stat(paramsBinFile.c_str(), &buffer) != 0)
93 {
94 std::cerr << "paramsBinFile not found: " << paramsBinFile << std::endl;
95 errors++;
96 }
97 else
98 paramsBinFileSize = buffer.st_size;
100 if (!inData.empty() && stat(inData.c_str(), &buffer) != 0)
101 {
102 std::cerr << "inData not found: " << inData << std::endl;
103 errors++;
104 }
106 // Due to alignment, the parameter heap must be larger than the
107 // parameter binary. Using 1.1 as a conservative factor.
108 if (paramsBinFileSize > 0 &&
109 (paramsBinFileSize * 1.1) > PARAM_HEAP_SIZE)
110 {
111 std::cerr << "Parameter binary file larger than paramter heap. "
112 "Increase Configuration::PARAM_HEAP_SIZE" << std::endl;
113 errors++;
114 }
116 if (errors > 0)
117 return false;
119 return true;
120 }