12 #if defined(ARMNN_SERIALIZER) 15 #if defined(ARMNN_TF_LITE_PARSER) 18 #if defined(ARMNN_ONNX_PARSER) 22 template<armnn::DataType NonQuantizedType>
25 template<armnn::DataType QuantizedType>
27 const float& quantizationScale,
28 const int32_t& quantizationOffset);
31 auto ParseDataArray<armnn::DataType::Float32>(std::istream& stream)
33 return ParseArrayImpl<float>(stream, [](
const std::string& s) {
return std::stof(s); });
37 auto ParseDataArray<armnn::DataType::Signed32>(std::istream& stream)
39 return ParseArrayImpl<int>(stream, [](
const std::string& s) {
return std::stoi(s); });
43 auto ParseDataArray<armnn::DataType::QAsymmU8>(std::istream& stream)
45 return ParseArrayImpl<uint8_t>(stream,
51 auto ParseDataArray<armnn::DataType::QSymmS8>(std::istream& stream)
53 return ParseArrayImpl<int8_t>(stream,
60 auto ParseDataArray<armnn::DataType::QAsymmU8>(std::istream& stream,
61 const float& quantizationScale,
62 const int32_t& quantizationOffset)
64 return ParseArrayImpl<uint8_t>(stream,
65 [&quantizationScale, &quantizationOffset](
const std::string& s)
68 armnn::Quantize<uint8_t>(std::stof(s),
74 template<armnn::DataType ArmnnType,
typename T = armnn::ResolveType<ArmnnType>>
77 return std::vector<T>(numElements,
static_cast<T
>(0));
81 std::vector<unsigned int>
ParseArray(std::istream& stream)
83 return ParseArrayImpl<unsigned int>(
88 std::vector<std::string>
ParseStringList(
const std::string& inputString,
const char* delimiter)
90 std::stringstream stream(inputString);
91 return ParseArrayImpl<std::string>(stream, [](
const std::string& s) {
98 const std::string& outputTensorFile,
99 bool dequantizeOutput)
100 : m_OutputBinding(binding)
101 , m_Scale(info.GetQuantizationScale())
102 , m_Offset(info.GetQuantizationOffset())
103 , m_OutputTensorFile(outputTensorFile)
104 , m_DequantizeOutput(dequantizeOutput) {}
108 ForEachValue(values, [](
float value)
110 printf(
"%f ", value);
117 if(m_DequantizeOutput)
119 auto& scale = m_Scale;
120 auto& offset = m_Offset;
121 std::vector<float> dequantizedValues;
122 ForEachValue(values, [&scale, &offset, &dequantizedValues](uint8_t value)
125 printf(
"%f ", dequantizedValue);
126 dequantizedValues.push_back(dequantizedValue);
128 WriteToFile(dequantizedValues);
132 const std::vector<int> intValues(values.begin(), values.end());
139 ForEachValue(values, [](int8_t value)
141 printf(
"%d ", value);
148 ForEachValue(values, [](
int value)
150 printf(
"%d ", value);
155 template<
typename Container,
typename Delegate>
156 void TensorPrinter::ForEachValue(
const Container& c, Delegate delegate)
158 std::cout << m_OutputBinding <<
": ";
159 for (
const auto& value : c)
167 void TensorPrinter::WriteToFile(
const std::vector<T>& values)
169 if (!m_OutputTensorFile.empty())
171 std::ofstream outputTensorFile;
172 outputTensorFile.open(m_OutputTensorFile, std::ofstream::out | std::ofstream::trunc);
173 if (outputTensorFile.is_open())
175 outputTensorFile << m_OutputBinding <<
": ";
176 std::copy(values.begin(), values.end(), std::ostream_iterator<T>(outputTensorFile,
" "));
180 ARMNN_LOG(info) <<
"Output Tensor File: " << m_OutputTensorFile <<
" could not be opened!";
182 outputTensorFile.close();
187 mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>, std::vector<int8_t>>;
191 unsigned int numElements,
192 const std::string& dataTypeStr,
196 const bool readFromFile = dataFile.
has_value() && !dataFile.
value().empty();
197 const bool quantizeData = qParams.
has_value();
199 std::ifstream inputTensorFile;
202 inputTensorFile = std::ifstream(dataFile.
value());
205 if (dataTypeStr.compare(
"float") == 0)
209 const float qScale = qParams.
value().first;
210 const int qOffset = qParams.
value().second;
212 tensorData = readFromFile ?
213 ParseDataArray<armnn::DataType::QAsymmU8>(inputTensorFile, qScale, qOffset) :
214 GenerateDummyTensorData<armnn::DataType::QAsymmU8>(numElements);
218 tensorData = readFromFile ?
219 ParseDataArray<armnn::DataType::Float32>(inputTensorFile) :
220 GenerateDummyTensorData<armnn::DataType::Float32>(numElements);
223 else if (dataTypeStr.compare(
"int") == 0)
225 tensorData = readFromFile ?
226 ParseDataArray<armnn::DataType::Signed32>(inputTensorFile) :
227 GenerateDummyTensorData<armnn::DataType::Signed32>(numElements);
229 else if (dataTypeStr.compare(
"qsymms8") == 0)
231 tensorData = readFromFile ?
232 ParseDataArray<armnn::DataType::QSymmS8>(inputTensorFile) :
233 GenerateDummyTensorData<armnn::DataType::QSymmS8>(numElements);
235 else if (dataTypeStr.compare(
"qasymm8") == 0)
237 tensorData = readFromFile ?
238 ParseDataArray<armnn::DataType::QAsymmU8>(inputTensorFile) :
239 GenerateDummyTensorData<armnn::DataType::QAsymmU8>(numElements);
243 std::string errorMessage =
"Unsupported tensor data type " + dataTypeStr;
246 inputTensorFile.close();
250 inputTensorFile.close();
255 if (!fs::exists(file))
257 std::cerr <<
"Given file path '" << file <<
"' does not exist" << std::endl;
260 if (!fs::is_regular_file(file) && expectFile)
262 std::cerr <<
"Given file path '" << file <<
"' is not a regular file" << std::endl;
268 bool ValidatePaths(
const std::vector<std::string>& fileVec,
const bool expectFile)
270 bool allPathsValid =
true;
271 for (
auto const& file : fileVec)
275 allPathsValid =
false;
278 return allPathsValid;
float Dequantize(QuantizedType value, float scale, int32_t offset)
Dequantize an 8-bit data type into a floating point data type.
std::vector< unsigned int > ParseArray(std::istream &stream)
void operator()(const std::vector< float > &values)
std::vector< std::string > ParseStringList(const std::string &inputString, const char *delimiter)
Splits a given string at every accurance of delimiter into a vector of string.
#define ARMNN_LOG(severity)
std::vector< T > GenerateDummyTensorData(unsigned int numElements)
void PopulateTensorWithData(TContainer &tensorData, unsigned int numElements, const std::string &dataTypeStr, const armnn::Optional< QuantizationParams > &qParams, const armnn::Optional< std::string > &dataFile)
std::string StringTrimCopy(const std::string &str, const std::string &chars="\\\")
Trim from both the start and the end of a string, returns a trimmed copy of the string.
auto ParseDataArray(std::istream &stream)
bool has_value() const noexcept
bool ValidatePaths(const std::vector< std::string > &fileVec, const bool expectFile)
Verifies if a given vector of strings are valid paths.
TensorPrinter(const std::string &binding, const armnn::TensorInfo &info, const std::string &outputTensorFile, bool dequantizeOutput)
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
Base class for all ArmNN exceptions so that users can filter to just those.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
bool ValidatePath(const std::string &file, const bool expectFile)
Verifies if the given string is a valid path.
std::pair< float, int32_t > QuantizationParams