Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "QuantizationInput.hpp" |
| 7 | |
| 8 | #include <iostream> |
| 9 | #include <fstream> |
| 10 | #include <cstring> |
| 11 | #include "armnn/Exceptions.hpp" |
| 12 | |
| 13 | namespace armnnQuantizer |
| 14 | { |
| 15 | |
| 16 | QuantizationInput::QuantizationInput(const unsigned int passId, |
| 17 | const armnn::LayerBindingId bindingId, |
| 18 | const std::string fileName): |
| 19 | m_PassId(passId) |
| 20 | { |
| 21 | m_LayerBindingIdToFileName.emplace(bindingId, fileName); |
| 22 | } |
| 23 | |
| 24 | QuantizationInput::QuantizationInput(const QuantizationInput& other) |
| 25 | { |
| 26 | m_PassId = other.GetPassId(); |
| 27 | m_LayerBindingIdToFileName.clear(); |
| 28 | for (armnn::LayerBindingId bindingId : other.GetLayerBindingIds()) |
| 29 | { |
| 30 | std::string filename = other.GetFileName(bindingId); |
| 31 | AddEntry(bindingId, filename); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void QuantizationInput::AddEntry(const armnn::LayerBindingId bindingId, const std::string fileName) |
| 36 | { |
| 37 | m_LayerBindingIdToFileName.emplace(bindingId, fileName); |
| 38 | } |
| 39 | |
| 40 | std::vector<float> QuantizationInput::GetDataForEntry(const armnn::LayerBindingId bindingId) const |
| 41 | { |
| 42 | if (m_LayerBindingIdToFileName.at(bindingId).empty()) |
| 43 | { |
| 44 | throw armnn::Exception("Layer binding ID not found"); |
| 45 | } |
| 46 | |
| 47 | std::string fileName = m_LayerBindingIdToFileName.at(bindingId); |
| 48 | std::ifstream in(fileName.c_str(), std::ifstream::binary); |
| 49 | if (!in.is_open()) |
| 50 | { |
| 51 | throw armnn::Exception("Failed to open input tensor file " + fileName); |
| 52 | } |
| 53 | |
| 54 | std::string line; |
| 55 | std::vector<float> values; |
| 56 | char* pEnd; |
| 57 | |
| 58 | while (std::getline(in, line, ' ')) |
| 59 | { |
| 60 | values.emplace_back(std::strtof(line.c_str(), &pEnd)); |
| 61 | } |
| 62 | return values; |
| 63 | } |
| 64 | |
| 65 | std::vector<armnn::LayerBindingId> QuantizationInput::GetLayerBindingIds() const |
| 66 | { |
| 67 | std::vector<armnn::LayerBindingId> layerBindingIDs; |
| 68 | |
| 69 | for (auto iterator = m_LayerBindingIdToFileName.begin(); iterator != m_LayerBindingIdToFileName.end(); ++iterator) |
| 70 | { |
| 71 | layerBindingIDs.emplace_back(iterator->first); |
| 72 | } |
| 73 | return layerBindingIDs; |
| 74 | } |
| 75 | |
| 76 | unsigned long QuantizationInput::GetNumberOfInputs() const |
| 77 | { |
| 78 | return m_LayerBindingIdToFileName.size(); |
| 79 | } |
| 80 | |
| 81 | unsigned int QuantizationInput::GetPassId() const |
| 82 | { |
| 83 | return m_PassId; |
| 84 | } |
| 85 | |
| 86 | std::string QuantizationInput::GetFileName(const armnn::LayerBindingId bindingId) const |
| 87 | { |
| 88 | auto iterator = m_LayerBindingIdToFileName.find(bindingId); |
| 89 | if (iterator != m_LayerBindingIdToFileName.end()) |
| 90 | { |
| 91 | return m_LayerBindingIdToFileName.at(bindingId); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | throw armnn::Exception("Could not retrieve filename for binding ID " + std::to_string(bindingId)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | QuantizationInput::~QuantizationInput() noexcept |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | } |