22 #include <fmt/format.h> 24 #include "flatbuffers/idl.h" 25 #include "flatbuffers/util.h" 26 #include "flatbuffers/flexbuffers.h" 28 #include <schema_generated.h> 43 ITfLiteParser::TfLiteParserOptions options;
44 options.m_StandInLayerForUnsupported =
true;
45 options.m_InferAndValidate =
true;
69 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
76 m_Runtime->GetDeviceSpec());
77 std::string errorMessage;
79 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
84 fmt::format(
"The runtime failed to load the network. " 85 "Error was: {}. in {} [{}:{}]",
96 m_SingleInputName = inputName;
97 m_SingleOutputName = outputName;
106 flatbuffers::Parser parser;
108 bool ok = parser.Parse(schemafile.c_str());
111 ok &= parser.Parse(m_JsonString.c_str());
120 const uint8_t * bufferPtr = parser.builder_.GetBufferPointer();
121 size_t size =
static_cast<size_t>(parser.builder_.GetSize());
122 m_GraphBinary.assign(bufferPtr, bufferPtr+size);
129 template <std::size_t NumOutputDimensions,
131 void RunTest(
size_t subgraphId,
137 template <std::size_t NumOutputDimensions,
139 void RunTest(
size_t subgraphId,
147 template <std::size_t NumOutputDimensions,
150 void RunTest(
size_t subgraphId,
153 bool isDynamic =
false);
162 void RunTest(std::size_t subgraphId,
169 flexbuffers::Builder detectPostProcess;
170 detectPostProcess.Map([&]() {
175 detectPostProcess.Int(
"num_classes", descriptor.
m_NumClasses);
178 detectPostProcess.Float(
"h_scale", descriptor.
m_ScaleH);
179 detectPostProcess.Float(
"w_scale", descriptor.
m_ScaleW);
180 detectPostProcess.Float(
"x_scale", descriptor.
m_ScaleX);
181 detectPostProcess.Float(
"y_scale", descriptor.
m_ScaleY);
183 detectPostProcess.Finish();
186 std::stringstream strStream;
187 std::vector<uint8_t> buffer = detectPostProcess.GetBuffer();
188 std::copy(buffer.begin(), buffer.end(),std::ostream_iterator<int>(strStream,
","));
190 return strStream.str();
194 tflite::TensorType tensorType, uint32_t buffer,
const std::string& name,
195 const std::vector<float>& min,
const std::vector<float>& max,
196 const std::vector<float>& scale,
const std::vector<int64_t>& zeroPoint)
198 BOOST_CHECK(tensors);
199 BOOST_CHECK_EQUAL(shapeSize, tensors->shape.size());
200 BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end());
201 BOOST_CHECK_EQUAL(tensorType, tensors->type);
202 BOOST_CHECK_EQUAL(buffer, tensors->buffer);
203 BOOST_CHECK_EQUAL(name, tensors->name);
204 BOOST_CHECK(tensors->quantization);
205 BOOST_CHECK_EQUAL_COLLECTIONS(min.begin(), min.end(), tensors->quantization.get()->min.begin(),
206 tensors->quantization.get()->min.end());
207 BOOST_CHECK_EQUAL_COLLECTIONS(max.begin(), max.end(), tensors->quantization.get()->max.begin(),
208 tensors->quantization.get()->max.end());
209 BOOST_CHECK_EQUAL_COLLECTIONS(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(),
210 tensors->quantization.get()->scale.end());
211 BOOST_CHECK_EQUAL_COLLECTIONS(zeroPoint.begin(), zeroPoint.end(),
212 tensors->quantization.get()->zero_point.begin(),
213 tensors->quantization.get()->zero_point.end());
220 template <std::size_t NumOutputDimensions,
226 RunTest<NumOutputDimensions, armnnType>(subgraphId,
234 template <std::size_t NumOutputDimensions,
240 RunTest<NumOutputDimensions, armnnType, armnnType>(subgraphId, inputData, expectedOutputData);
247 template <std::size_t NumOutputDimensions,
259 for (
auto&& it : inputData)
263 inputTensors.push_back({ bindingInfo.first,
armnn::ConstTensor(bindingInfo.second, it.second.data()) });
267 std::map<std::string, boost::multi_array<DataType2, NumOutputDimensions>> outputStorage;
269 for (
auto&& it : expectedOutputData)
276 BOOST_CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions),
277 fmt::format(
"Number of dimensions expected {}, but got {} for output layer {}",
283 outputStorage.emplace(it.first, MakeTensor<DataType2, NumOutputDimensions>(outputTensorInfo));
284 outputTensors.push_back(
285 { outputBindingId,
armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) });
291 for (
auto&& it : expectedOutputData)
294 auto outputExpected = MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second, it.second, isDynamic);
295 BOOST_TEST(
CompareTensors(outputExpected, outputStorage[it.first],
false, isDynamic));
313 for (
auto&& it : inputData)
318 inputTensors.push_back({ bindingInfo.first,
armnn::ConstTensor(bindingInfo.second, it.second.data()) });
322 outputTensors.reserve(expectedOutputData.size());
323 std::map<std::string, std::vector<DataType2>> outputStorage;
324 for (
auto&& it : expectedOutputData)
329 std::vector<DataType2> out(it.second.size());
330 outputStorage.emplace(it.first, out);
331 outputTensors.push_back({ bindingInfo.first,
333 outputStorage.at(it.first).data()) });
339 for (
auto&& it : expectedOutputData)
341 std::vector<armnn::ResolveType<armnnType2>> out = outputStorage.at(it.first);
343 for (
unsigned int i = 0; i < out.size(); ++i)
345 BOOST_TEST(it.second[i] == out[i], boost::test_tools::tolerance(0.000001f));
armnn::NetworkId m_NetworkIdentifier
float m_ScaleW
Center size encoding scale weight.
static std::string GenerateDetectionPostProcessJsonString(const armnn::DetectionPostProcessDescriptor &descriptor)
CPU Execution: Reference C++ kernels.
float m_ScaleX
Center size encoding scale x.
ParserFlatbuffersFixture()
boost::test_tools::predicate_result CompareTensors(const boost::multi_array< T, n > &a, const boost::multi_array< T, n > &b, bool compareBoolean=false, bool isDynamic=false)
void CheckTensors(const TensorRawPtr &tensors, size_t shapeSize, const std::vector< int32_t > &shape, tflite::TensorType tensorType, uint32_t buffer, const std::string &name, const std::vector< float > &min, const std::vector< float > &max, const std::vector< float > &scale, const std::vector< int64_t > &zeroPoint)
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
uint32_t m_DetectionsPerClass
Detections per classes, used in Regular NMS.
typename ResolveTypeImpl< DT >::Type ResolveType
bool ReadStringToBinary()
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
std::unique_ptr< ITfLiteParser, void(*)(ITfLiteParser *parser)> ITfLiteParserPtr
Copyright (c) 2020 ARM Limited.
void RunTest(size_t subgraphId, const std::vector< armnn::ResolveType< ArmnnType >> &inputData, const std::vector< armnn::ResolveType< ArmnnType >> &expectedOutputData)
Executes the network with the given input tensor and checks the result against the given output tenso...
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
uint32_t m_MaxDetections
Maximum numbers of detections.
float m_NmsIouThreshold
Intersection over union threshold.
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
#define ARMNN_ASSERT_MSG(COND, MSG)
armnnSerializer::TensorInfo * TensorRawPtr
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
uint32_t m_NumClasses
Number of classes.
bool m_UseRegularNms
Use Regular NMS.
std::string m_SingleOutputName
std::vector< uint8_t > m_GraphBinary
armnn::IRuntimePtr m_Runtime
float m_ScaleH
Center size encoding scale height.
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
void SetupSingleInputSingleOutput(const std::string &inputName, const std::string &outputName)
Base class for all ArmNN exceptions so that users can filter to just those.
void VerifyTensorInfoDataType(const armnn::TensorInfo &info, armnn::DataType dataType)
float m_ScaleY
Center size encoding scale y.
unsigned char g_TfLiteSchemaText[]
float m_NmsScoreThreshold
NMS score threshold.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
std::string m_SingleInputName
If the single-input-single-output overload of Setup() is called, these will store the input and outpu...
unsigned int GetNumDimensions() const
ITfLiteParserPtr m_Parser
unsigned int g_TfLiteSchemaText_len