2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "QuantizationDataSet.hpp"
7 #include "CsvReader.hpp"
9 #define BOOST_FILESYSTEM_NO_DEPRECATED
11 #include <boost/filesystem/operations.hpp>
12 #include <boost/filesystem/path.hpp>
14 namespace armnnQuantizer
17 QuantizationDataSet::QuantizationDataSet()
21 QuantizationDataSet::QuantizationDataSet(const std::string csvFilePath):
22 m_QuantizationInputs(),
23 m_CsvFilePath(csvFilePath)
28 void AddInputData(unsigned int passId,
29 armnn::LayerBindingId bindingId,
30 const std::string& inputFilePath,
31 std::map<unsigned int, QuantizationInput>& passIdToQuantizationInput)
33 auto iterator = passIdToQuantizationInput.find(passId);
34 if (iterator == passIdToQuantizationInput.end())
36 QuantizationInput input(passId, bindingId, inputFilePath);
37 passIdToQuantizationInput.emplace(passId, input);
41 auto existingQuantizationInput = iterator->second;
42 existingQuantizationInput.AddEntry(bindingId, inputFilePath);
46 QuantizationDataSet::~QuantizationDataSet()
50 void InputLayerVisitor::VisitInputLayer(const armnn::IConnectableLayer* layer,
51 armnn::LayerBindingId id,
54 m_TensorInfos.emplace(id, layer->GetInputSlot(0).GetConnection()->GetTensorInfo());
57 armnn::TensorInfo InputLayerVisitor::GetTensorInfo(armnn::LayerBindingId layerBindingId)
59 auto iterator = m_TensorInfos.find(layerBindingId);
60 if (iterator != m_TensorInfos.end())
62 return m_TensorInfos.at(layerBindingId);
66 throw armnn::Exception("Could not retrieve tensor info for binding ID " + std::to_string(layerBindingId));
71 unsigned int GetPassIdFromCsvRow(std::vector<armnnUtils::CsvRow> csvRows, unsigned int rowIndex)
76 passId = static_cast<unsigned int>(std::stoi(csvRows[rowIndex].values[0]));
78 catch (std::invalid_argument)
80 throw armnn::ParseException("Pass ID [" + csvRows[rowIndex].values[0] + "]" +
81 " is not correct format on CSV row " + std::to_string(rowIndex));
86 armnn::LayerBindingId GetBindingIdFromCsvRow(std::vector<armnnUtils::CsvRow> csvRows, unsigned int rowIndex)
88 armnn::LayerBindingId bindingId;
91 bindingId = std::stoi(csvRows[rowIndex].values[1]);
93 catch (std::invalid_argument)
95 throw armnn::ParseException("Binding ID [" + csvRows[rowIndex].values[0] + "]" +
96 " is not correct format on CSV row " + std::to_string(rowIndex));
101 std::string GetFileNameFromCsvRow(std::vector<armnnUtils::CsvRow> csvRows, unsigned int rowIndex)
103 std::string fileName = csvRows[rowIndex].values[2];
105 if (!boost::filesystem::exists(fileName))
107 throw armnn::ParseException("File [ " + fileName + "] provided on CSV row " + std::to_string(rowIndex) +
111 if (fileName.empty())
113 throw armnn::ParseException("Filename cannot be empty on CSV row " + std::to_string(rowIndex));
119 void QuantizationDataSet::ParseCsvFile()
121 std::map<unsigned int, QuantizationInput> passIdToQuantizationInput;
122 armnnUtils::CsvReader reader;
124 if (m_CsvFilePath == "")
126 throw armnn::Exception("CSV file not specified.");
129 // Parse CSV file and extract data
130 std::vector<armnnUtils::CsvRow> csvRows = reader.ParseFile(m_CsvFilePath);
133 throw armnn::Exception("CSV file [" + m_CsvFilePath + "] is empty.");
136 for (unsigned int i = 0; i < csvRows.size(); ++i)
138 if (csvRows[i].values.size() != 3)
140 throw armnn::Exception("CSV file [" + m_CsvFilePath + "] does not have correct number of entries " +
141 "on line " + std::to_string(i) + ". Expected 3 entries " +
142 "but was " + std::to_string(csvRows[i].values.size()));
145 unsigned int passId = GetPassIdFromCsvRow(csvRows, i);
146 armnn::LayerBindingId bindingId = GetBindingIdFromCsvRow(csvRows, i);
147 std::string rawFileName = GetFileNameFromCsvRow(csvRows, i);
149 AddInputData(passId, bindingId, rawFileName, passIdToQuantizationInput);
152 if (passIdToQuantizationInput.empty())
154 throw armnn::Exception("Could not parse CSV file.");
157 // Once all entries in CSV file are parsed successfully and QuantizationInput map is populated, populate
158 // QuantizationInputs iterator for easier access and clear the map
159 for (auto itr = passIdToQuantizationInput.begin(); itr != passIdToQuantizationInput.end(); ++itr)
161 m_QuantizationInputs.emplace_back(itr->second);