aboutsummaryrefslogtreecommitdiff
path: root/tests/MultipleNetworksCifar10
diff options
context:
space:
mode:
authortelsoa01 <telmo.soares@arm.com>2018-03-09 14:13:49 +0000
committertelsoa01 <telmo.soares@arm.com>2018-03-09 14:13:49 +0000
commit4fcda0101ec3d110c1d6d7bee5c83416b645528a (patch)
treec9a70aeb2887006160c1b3d265c27efadb7bdbae /tests/MultipleNetworksCifar10
downloadarmnn-4fcda0101ec3d110c1d6d7bee5c83416b645528a.tar.gz
Release 18.02
Change-Id: Id3c11dc5ee94ef664374a988fcc6901e9a232fa6
Diffstat (limited to 'tests/MultipleNetworksCifar10')
-rw-r--r--tests/MultipleNetworksCifar10/MultipleNetworksCifar10.cpp196
1 files changed, 196 insertions, 0 deletions
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 <iostream>
+#include <chrono>
+#include <vector>
+#include <array>
+#include <boost/log/trivial.hpp>
+
+#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<std::string>(&modelDir)->required(),
+ "Path to directory containing the Cifar10 model file")
+ ("compute,c", po::value<armnn::Compute>(&computeDevice)->default_value(armnn::Compute::CpuAcc),
+ "Which device to run layers on by default. Possible choices: CpuAcc, CpuRef, GpuAcc")
+ ("data-dir,d", po::value<std::string>(&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<armnn::LayerBindingId, armnn::TensorInfo>& in,
+ const std::pair<armnn::LayerBindingId, armnn::TensorInfo>& out)
+ : m_Network(netId)
+ , m_InputBindingInfo(in)
+ , m_OutputBindingInfo(out)
+ {}
+
+ armnn::NetworkId m_Network;
+ std::pair<armnn::LayerBindingId, armnn::TensorInfo> m_InputBindingInfo;
+ std::pair<armnn::LayerBindingId, armnn::TensorInfo> m_OutputBindingInfo;
+ };
+ std::vector<Net> 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 ("<<e.what()<<") caught from optimize.";
+ BOOST_LOG_TRIVIAL(fatal) << message.str();
+ return 1;
+ }
+
+ // Load the network into the runtime
+ armnn::NetworkId networkId;
+ status = runtime->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<Cifar10Database::TTestCaseData> testCaseData = cifar10.GetTestCaseData(i);
+
+ // Test inference
+ std::vector<std::array<float, 10>> 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