From 4fcda0101ec3d110c1d6d7bee5c83416b645528a Mon Sep 17 00:00:00 2001 From: telsoa01 Date: Fri, 9 Mar 2018 14:13:49 +0000 Subject: Release 18.02 Change-Id: Id3c11dc5ee94ef664374a988fcc6901e9a232fa6 --- .../MultipleNetworksCifar10.cpp | 196 +++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 tests/MultipleNetworksCifar10/MultipleNetworksCifar10.cpp (limited to 'tests/MultipleNetworksCifar10/MultipleNetworksCifar10.cpp') diff --git a/tests/MultipleNetworksCifar10/MultipleNetworksCifar10.cpp b/tests/MultipleNetworksCifar10/MultipleNetworksCifar10.cpp new file mode 100644 index 0000000000..3c75ed7f24 --- /dev/null +++ b/tests/MultipleNetworksCifar10/MultipleNetworksCifar10.cpp @@ -0,0 +1,196 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// +#include +#include +#include +#include +#include + +#include "armnn/ArmNN.hpp" +#include "armnn/Utils.hpp" +#include "armnn/INetwork.hpp" +#include "armnnCaffeParser/ICaffeParser.hpp" +#include "../Cifar10Database.hpp" +#include "../InferenceTest.hpp" +#include "../InferenceModel.hpp" + +using namespace std; +using namespace std::chrono; +using namespace armnn::test; + +int main(int argc, char* argv[]) +{ +#ifdef NDEBUG + armnn::LogSeverity level = armnn::LogSeverity::Info; +#else + armnn::LogSeverity level = armnn::LogSeverity::Debug; +#endif + + try + { + // Configure logging for both the ARMNN library and this test program + armnn::ConfigureLogging(true, true, level); + armnnUtils::ConfigureLogging(boost::log::core::get().get(), true, true, level); + + namespace po = boost::program_options; + + armnn::Compute computeDevice; + std::string modelDir; + std::string dataDir; + + po::options_description desc("Options"); + try + { + // Add generic options needed for all inference tests + desc.add_options() + ("help", "Display help messages") + ("model-dir,m", po::value(&modelDir)->required(), + "Path to directory containing the Cifar10 model file") + ("compute,c", po::value(&computeDevice)->default_value(armnn::Compute::CpuAcc), + "Which device to run layers on by default. Possible choices: CpuAcc, CpuRef, GpuAcc") + ("data-dir,d", po::value(&dataDir)->required(), + "Path to directory containing the Cifar10 test data"); + } + catch (const std::exception& e) + { + // Coverity points out that default_value(...) can throw a bad_lexical_cast, + // and that desc.add_options() can throw boost::io::too_few_args. + // They really won't in any of these cases. + BOOST_ASSERT_MSG(false, "Caught unexpected exception"); + std::cerr << "Fatal internal error: " << e.what() << std::endl; + return 1; + } + + po::variables_map vm; + + try + { + po::store(po::parse_command_line(argc, argv, desc), vm); + + if (vm.count("help")) + { + std::cout << desc << std::endl; + return 1; + } + + po::notify(vm); + } + catch (po::error& e) + { + std::cerr << e.what() << std::endl << std::endl; + std::cerr << desc << std::endl; + return 1; + } + + if (!ValidateDirectory(modelDir)) + { + return 1; + } + string modelPath = modelDir + "cifar10_full_iter_60000.caffemodel"; + + // Create runtime + armnn::IRuntimePtr runtime(armnn::IRuntime::Create(computeDevice)); + + // Load networks + armnn::Status status; + struct Net + { + Net(armnn::NetworkId netId, + const std::pair& in, + const std::pair& out) + : m_Network(netId) + , m_InputBindingInfo(in) + , m_OutputBindingInfo(out) + {} + + armnn::NetworkId m_Network; + std::pair m_InputBindingInfo; + std::pair m_OutputBindingInfo; + }; + std::vector networks; + + armnnCaffeParser::ICaffeParserPtr parser(armnnCaffeParser::ICaffeParser::Create()); + + const int networksCount = 4; + for (int i = 0; i < networksCount; ++i) + { + // Create a network from a file on disk + armnn::INetworkPtr network = parser->CreateNetworkFromBinaryFile(modelPath.c_str(), {}, { "prob" }); + + // optimize the network + armnn::IOptimizedNetworkPtr optimizedNet(nullptr, nullptr); + try + { + optimizedNet = armnn::Optimize(*network, runtime->GetDeviceSpec()); + } + catch (armnn::Exception& e) + { + std::stringstream message; + message << "armnn::Exception ("<LoadNetwork(networkId, std::move(optimizedNet)); + if (status == armnn::Status::Failure) + { + BOOST_LOG_TRIVIAL(fatal) << "armnn::IRuntime: Failed to load network"; + return 1; + } + + networks.emplace_back(networkId, + parser->GetNetworkInputBindingInfo("data"), + parser->GetNetworkOutputBindingInfo("prob")); + } + + // Load a test case and test inference + if (!ValidateDirectory(dataDir)) + { + return 1; + } + Cifar10Database cifar10(dataDir); + + for (unsigned int i = 0; i < 3; ++i) + { + // Load test case data (including image data) + std::unique_ptr testCaseData = cifar10.GetTestCaseData(i); + + // Test inference + std::vector> outputs(networksCount); + + for (unsigned int k = 0; k < networksCount; ++k) + { + status = runtime->EnqueueWorkload(networks[k].m_Network, + MakeInputTensors(networks[k].m_InputBindingInfo, testCaseData->m_InputImage), + MakeOutputTensors(networks[k].m_OutputBindingInfo, outputs[k])); + if (status == armnn::Status::Failure) + { + BOOST_LOG_TRIVIAL(fatal) << "armnn::IRuntime: Failed to enqueue workload"; + return 1; + } + } + + // Compare outputs + for (unsigned int k = 1; k < networksCount; ++k) + { + if (!std::equal(outputs[0].begin(), outputs[0].end(), outputs[k].begin(), outputs[k].end())) + { + BOOST_LOG_TRIVIAL(error) << "Multiple networks inference failed!"; + return 1; + } + } + } + + BOOST_LOG_TRIVIAL(info) << "Multiple networks inference ran successfully!"; + return 0; + } + catch (armnn::Exception const& e) + { + BOOST_LOG_TRIVIAL(fatal) <<"Armnn Error: "<< e.what(); + return 1; + } +} \ No newline at end of file -- cgit v1.2.1