Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 5 | |
| 6 | #include "armnn/ArmNN.hpp" |
| 7 | #include "armnn/Utils.hpp" |
| 8 | #include "armnn/INetwork.hpp" |
Nikhil Raj | 5d955cf | 2021-04-19 16:59:48 +0100 | [diff] [blame] | 9 | #include "armnnTfLiteParser/TfLiteParser.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include "../Cifar10Database.hpp" |
| 11 | #include "../InferenceTest.hpp" |
| 12 | #include "../InferenceModel.hpp" |
| 13 | |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 14 | #include <cxxopts/cxxopts.hpp> |
| 15 | |
| 16 | #include <iostream> |
| 17 | #include <chrono> |
| 18 | #include <vector> |
| 19 | #include <array> |
| 20 | |
| 21 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | using namespace std; |
| 23 | using namespace std::chrono; |
| 24 | using namespace armnn::test; |
| 25 | |
| 26 | int main(int argc, char* argv[]) |
| 27 | { |
| 28 | #ifdef NDEBUG |
| 29 | armnn::LogSeverity level = armnn::LogSeverity::Info; |
| 30 | #else |
| 31 | armnn::LogSeverity level = armnn::LogSeverity::Debug; |
| 32 | #endif |
| 33 | |
| 34 | try |
| 35 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | // Configures logging for both the ARMNN library and this test program. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | armnn::ConfigureLogging(true, true, level); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 38 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 39 | std::vector<armnn::BackendId> computeDevice; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | std::string modelDir; |
| 41 | std::string dataDir; |
| 42 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 43 | const std::string backendsMessage = "Which device to run layers on by default. Possible choices: " |
| 44 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 45 | |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 46 | cxxopts::Options in_options("MultipleNetworksCifar10", |
| 47 | "Run multiple networks inference tests using Cifar-10 data."); |
| 48 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | try |
| 50 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | // Adds generic options needed for all inference tests. |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 52 | in_options.add_options() |
| 53 | ("h,help", "Display help messages") |
| 54 | ("m,model-dir", "Path to directory containing the Cifar10 model file", |
| 55 | cxxopts::value<std::string>(modelDir)) |
| 56 | ("c,compute", backendsMessage.c_str(), |
| 57 | cxxopts::value<std::vector<armnn::BackendId>>(computeDevice)->default_value("CpuAcc,CpuRef")) |
| 58 | ("d,data-dir", "Path to directory containing the Cifar10 test data", |
| 59 | cxxopts::value<std::string>(dataDir)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 61 | auto result = in_options.parse(argc, argv); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 63 | if(result.count("help") > 0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | { |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 65 | std::cout << in_options.help() << std::endl; |
| 66 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | } |
| 68 | |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 69 | //ensure mandatory parameters given |
| 70 | std::string mandatorySingleParameters[] = {"model-dir", "data-dir"}; |
| 71 | for (auto param : mandatorySingleParameters) |
| 72 | { |
| 73 | if(result.count(param) > 0) |
| 74 | { |
| 75 | std::string dir = result[param].as<std::string>(); |
| 76 | |
| 77 | if(!ValidateDirectory(dir)) { |
| 78 | return EXIT_FAILURE; |
| 79 | } |
| 80 | } else { |
| 81 | std::cerr << "Parameter \'--" << param << "\' is required but missing." << std::endl; |
| 82 | return EXIT_FAILURE; |
| 83 | } |
| 84 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | } |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 86 | catch (const cxxopts::OptionException& e) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 87 | { |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 88 | std::cerr << e.what() << std::endl << in_options.help() << std::endl; |
| 89 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Nikhil Raj | 6dd178f | 2021-04-02 22:04:39 +0100 | [diff] [blame] | 92 | fs::path modelPath = fs::path(modelDir + "/cifar10_tf.prototxt"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 93 | |
Narumol Prangnawarat | d9d61f5 | 2020-01-02 17:46:53 +0000 | [diff] [blame] | 94 | // Create runtime |
| 95 | // This will also load dynamic backend in case that the dynamic backend path is specified |
| 96 | armnn::IRuntime::CreationOptions options; |
| 97 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 98 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 99 | // Check if the requested backend are all valid |
| 100 | std::string invalidBackends; |
| 101 | if (!CheckRequestedBackendsAreValid(computeDevice, armnn::Optional<std::string&>(invalidBackends))) |
| 102 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 103 | ARMNN_LOG(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 104 | << invalidBackends; |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 105 | return EXIT_FAILURE; |
| 106 | } |
| 107 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | // Loads networks. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | armnn::Status status; |
| 110 | struct Net |
| 111 | { |
| 112 | Net(armnn::NetworkId netId, |
| 113 | const std::pair<armnn::LayerBindingId, armnn::TensorInfo>& in, |
| 114 | const std::pair<armnn::LayerBindingId, armnn::TensorInfo>& out) |
| 115 | : m_Network(netId) |
| 116 | , m_InputBindingInfo(in) |
| 117 | , m_OutputBindingInfo(out) |
| 118 | {} |
| 119 | |
| 120 | armnn::NetworkId m_Network; |
| 121 | std::pair<armnn::LayerBindingId, armnn::TensorInfo> m_InputBindingInfo; |
| 122 | std::pair<armnn::LayerBindingId, armnn::TensorInfo> m_OutputBindingInfo; |
| 123 | }; |
| 124 | std::vector<Net> networks; |
| 125 | |
Nikhil Raj | 5d955cf | 2021-04-19 16:59:48 +0100 | [diff] [blame] | 126 | armnnTfLiteParser::ITfLiteParserPtr parser(armnnTfLiteParser::ITfLiteParserPtr::Create()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 127 | |
| 128 | const int networksCount = 4; |
| 129 | for (int i = 0; i < networksCount; ++i) |
| 130 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 131 | // Creates a network from a file on the disk. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | armnn::INetworkPtr network = parser->CreateNetworkFromBinaryFile(modelPath.c_str(), {}, { "prob" }); |
| 133 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 134 | // Optimizes the network. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | armnn::IOptimizedNetworkPtr optimizedNet(nullptr, nullptr); |
| 136 | try |
| 137 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | optimizedNet = armnn::Optimize(*network, computeDevice, runtime->GetDeviceSpec()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 139 | } |
Pavel Macenauer | 855a47b | 2020-05-26 10:54:22 +0000 | [diff] [blame] | 140 | catch (const armnn::Exception& e) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | { |
| 142 | std::stringstream message; |
| 143 | message << "armnn::Exception ("<<e.what()<<") caught from optimize."; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 144 | ARMNN_LOG(fatal) << message.str(); |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 145 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 146 | } |
| 147 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 148 | // Loads the network into the runtime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 149 | armnn::NetworkId networkId; |
| 150 | status = runtime->LoadNetwork(networkId, std::move(optimizedNet)); |
| 151 | if (status == armnn::Status::Failure) |
| 152 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 153 | ARMNN_LOG(fatal) << "armnn::IRuntime: Failed to load network"; |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 154 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | networks.emplace_back(networkId, |
| 158 | parser->GetNetworkInputBindingInfo("data"), |
| 159 | parser->GetNetworkOutputBindingInfo("prob")); |
| 160 | } |
| 161 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | // Loads a test case and tests inference. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 163 | if (!ValidateDirectory(dataDir)) |
| 164 | { |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 165 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 166 | } |
| 167 | Cifar10Database cifar10(dataDir); |
| 168 | |
| 169 | for (unsigned int i = 0; i < 3; ++i) |
| 170 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 171 | // Loads test case data (including image data). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 172 | std::unique_ptr<Cifar10Database::TTestCaseData> testCaseData = cifar10.GetTestCaseData(i); |
| 173 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 174 | // Tests inference. |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 175 | std::vector<TContainer> outputs; |
| 176 | outputs.reserve(networksCount); |
| 177 | |
| 178 | for (unsigned int j = 0; j < networksCount; ++j) |
| 179 | { |
| 180 | outputs.push_back(std::vector<float>(10)); |
| 181 | } |
| 182 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 183 | for (unsigned int k = 0; k < networksCount; ++k) |
| 184 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 185 | std::vector<armnn::BindingPointInfo> inputBindings = { networks[k].m_InputBindingInfo }; |
| 186 | std::vector<armnn::BindingPointInfo> outputBindings = { networks[k].m_OutputBindingInfo }; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 187 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 188 | std::vector<TContainer> inputDataContainers = { testCaseData->m_InputImage }; |
| 189 | std::vector<TContainer> outputDataContainers = { outputs[k] }; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 190 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 191 | status = runtime->EnqueueWorkload(networks[k].m_Network, |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 192 | armnnUtils::MakeInputTensors(inputBindings, inputDataContainers), |
| 193 | armnnUtils::MakeOutputTensors(outputBindings, outputDataContainers)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 194 | if (status == armnn::Status::Failure) |
| 195 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 196 | ARMNN_LOG(fatal) << "armnn::IRuntime: Failed to enqueue workload"; |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 197 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 201 | // Compares outputs. |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 202 | std::vector<float> output0 = mapbox::util::get<std::vector<float>>(outputs[0]); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 203 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | for (unsigned int k = 1; k < networksCount; ++k) |
| 205 | { |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 206 | std::vector<float> outputK = mapbox::util::get<std::vector<float>>(outputs[k]); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 207 | |
| 208 | if (!std::equal(output0.begin(), output0.end(), outputK.begin(), outputK.end())) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 209 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 210 | ARMNN_LOG(error) << "Multiple networks inference failed!"; |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 211 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 216 | ARMNN_LOG(info) << "Multiple networks inference ran successfully!"; |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 217 | return EXIT_SUCCESS; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 218 | } |
Pavel Macenauer | 855a47b | 2020-05-26 10:54:22 +0000 | [diff] [blame] | 219 | catch (const armnn::Exception& e) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 220 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 221 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 222 | // exception of type std::length_error. |
| 223 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 224 | std::cerr << "Armnn Error: " << e.what() << std::endl; |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 225 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 226 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 227 | catch (const std::exception& e) |
| 228 | { |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 229 | // Coverity fix: various boost exceptions can be thrown by methods called by this test. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 230 | std::cerr << "WARNING: MultipleNetworksCifar10: An error has occurred when running the " |
| 231 | "multiple networks inference tests: " << e.what() << std::endl; |
James Ward | 7a1966c | 2020-10-05 17:11:23 +0100 | [diff] [blame] | 232 | return EXIT_FAILURE; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 233 | } |
| 234 | } |