diff options
-rw-r--r-- | arm_compute/graph/nodes/ConvolutionLayer.h | 56 | ||||
-rw-r--r-- | arm_compute/graph/nodes/DepthwiseConvolutionLayer.h | 21 | ||||
-rw-r--r-- | examples/graph_cl_mobilenet_qasymm8.cpp | 223 | ||||
-rw-r--r-- | src/graph/nodes/BranchLayer.cpp | 7 | ||||
-rw-r--r-- | src/graph/nodes/ConvolutionLayer.cpp | 14 | ||||
-rw-r--r-- | src/graph/nodes/DepthwiseConvolutionLayer.cpp | 9 | ||||
-rw-r--r-- | src/graph/nodes/ReshapeLayer.cpp | 4 | ||||
-rw-r--r-- | utils/GraphUtils.cpp | 32 | ||||
-rw-r--r-- | utils/GraphUtils.h | 5 | ||||
-rw-r--r-- | utils/Utils.h | 3 |
10 files changed, 318 insertions, 56 deletions
diff --git a/arm_compute/graph/nodes/ConvolutionLayer.h b/arm_compute/graph/nodes/ConvolutionLayer.h index 0905524de8..1806190971 100644 --- a/arm_compute/graph/nodes/ConvolutionLayer.h +++ b/arm_compute/graph/nodes/ConvolutionLayer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -44,24 +44,28 @@ class ConvolutionLayer final : public INode public: /** Default Constructor * - * @param[in] conv_width Convolution width - * @param[in] conv_height Convolution height - * @param[in] ofm Output feature map - * @param[in] weights Weights of the convolution layer - * @param[in] biases Bias of the convolution layer - * @param[in] conv_info Convolution information - * @param[in] num_groups (Optional) Number of groups, default = 1 - * @param[in] weights_info (Optional) Weights information + * @param[in] conv_width Convolution width + * @param[in] conv_height Convolution height + * @param[in] ofm Output feature map + * @param[in] weights Weights of the convolution layer + * @param[in] biases Bias of the convolution layer + * @param[in] conv_info Convolution information + * @param[in] num_groups (Optional) Number of groups, default = 1 + * @param[in] weights_info (Optional) Weights information + * @param[in] weights_quant_info (Optional) Weights quantization information + * @param[in] out_quant_info (Optional) Output quantization info */ template <typename AccessorTypeWeights, typename AccessorTypeBiases> - ConvolutionLayer(unsigned int conv_width, - unsigned int conv_height, - unsigned int ofm, + ConvolutionLayer(unsigned int conv_width, + unsigned int conv_height, + unsigned int ofm, AccessorTypeWeights &&weights, - AccessorTypeBiases &&biases, - const PadStrideInfo conv_info, - unsigned int num_groups = 1, - const WeightsInfo weights_info = WeightsInfo()) + AccessorTypeBiases &&biases, + const PadStrideInfo conv_info, + unsigned int num_groups = 1, + const WeightsInfo weights_info = WeightsInfo(), + const QuantizationInfo weights_quant_info = QuantizationInfo(), + const QuantizationInfo out_quant_info = QuantizationInfo()) : _conv_width(conv_width), _conv_height(conv_height), _ofm(ofm), @@ -70,6 +74,8 @@ public: _conv_info(std::move(conv_info)), _num_groups(num_groups), _weights_info(std::move(weights_info)), + _weights_quant_info(std::move(weights_quant_info)), + _out_quant_info(std::move(out_quant_info)), _is(nullptr), _os(nullptr), _ws(nullptr), @@ -101,14 +107,16 @@ private: std::unique_ptr<arm_compute::IFunction> instantiate_grouped_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint); private: - unsigned int _conv_width; /**< Convolution width */ - unsigned int _conv_height; /**< Convolution height */ - unsigned int _ofm; /**< Output feature maps */ - Tensor _weights; /**< Weights tensor */ - Tensor _biases; /**< Biases tensor */ - const PadStrideInfo _conv_info; /**< Convolution layer information */ - unsigned int _num_groups; /**< Number of groups */ - const WeightsInfo _weights_info; /**< Convolution layer weights information */ + unsigned int _conv_width; /**< Convolution width */ + unsigned int _conv_height; /**< Convolution height */ + unsigned int _ofm; /**< Output feature maps */ + Tensor _weights; /**< Weights tensor */ + Tensor _biases; /**< Biases tensor */ + const PadStrideInfo _conv_info; /**< Convolution layer information */ + unsigned int _num_groups; /**< Number of groups */ + const WeightsInfo _weights_info; /**< Convolution layer weights information */ + const QuantizationInfo _weights_quant_info; /**< Output quantization information */ + const QuantizationInfo _out_quant_info; /**< Output quantization information */ std::unique_ptr<SubTensor[]> _is; /**< Input tensor sub-tensors used for grouped convolution */ std::unique_ptr<SubTensor[]> _os; /**< Output tensor sub-tensors used for grouped convolution */ diff --git a/arm_compute/graph/nodes/DepthwiseConvolutionLayer.h b/arm_compute/graph/nodes/DepthwiseConvolutionLayer.h index 8b7e3b8296..2d4bd1e0c2 100644 --- a/arm_compute/graph/nodes/DepthwiseConvolutionLayer.h +++ b/arm_compute/graph/nodes/DepthwiseConvolutionLayer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -50,10 +50,12 @@ public: * @param[in] biases Biases values tensor * @param[in] conv_info Convolution info * @param[in] opt3x3 (Optional) If true executes DepthwiseConvolutionLayer3x3 + * @param[in] quant_info (Optional) Quantization info used for weights */ template <typename AccessorType> - DepthwiseConvolutionLayer(unsigned int conv_width, unsigned int conv_height, AccessorType &&weights, AccessorType &&biases, const PadStrideInfo conv_info, bool opt3x3 = true) - : _conv_width(conv_width), _conv_height(conv_height), _weights(std::move(weights)), _biases(std::move(biases)), _conv_info(conv_info), _opt3x3(opt3x3) + DepthwiseConvolutionLayer(unsigned int conv_width, unsigned int conv_height, AccessorType &&weights, AccessorType &&biases, const PadStrideInfo conv_info, bool opt3x3 = true, + const QuantizationInfo quant_info = QuantizationInfo()) + : _conv_width(conv_width), _conv_height(conv_height), _weights(std::move(weights)), _biases(std::move(biases)), _conv_info(conv_info), _opt3x3(opt3x3), _quant_info(std::move(quant_info)) { } @@ -61,12 +63,13 @@ public: std::unique_ptr<arm_compute::IFunction> instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output) override; private: - unsigned int _conv_width; - unsigned int _conv_height; - Tensor _weights; - Tensor _biases; - const PadStrideInfo _conv_info; - bool _opt3x3; + unsigned int _conv_width; + unsigned int _conv_height; + Tensor _weights; + Tensor _biases; + const PadStrideInfo _conv_info; + bool _opt3x3; + const QuantizationInfo _quant_info; }; } // namespace graph } // namespace arm_compute diff --git a/examples/graph_cl_mobilenet_qasymm8.cpp b/examples/graph_cl_mobilenet_qasymm8.cpp new file mode 100644 index 0000000000..046c7779b1 --- /dev/null +++ b/examples/graph_cl_mobilenet_qasymm8.cpp @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2017-2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/Graph.h" +#include "arm_compute/graph/Nodes.h" +#include "support/ToolchainSupport.h" +#include "utils/GraphUtils.h" +#include "utils/Utils.h" + +using namespace arm_compute; +using namespace arm_compute::graph; +using namespace arm_compute::graph_utils; + +/** Example demonstrating how to implement QASYMM8 MobileNet's network using the Compute Library's graph API + * + * @param[in] argc Number of arguments + * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] npy_input, [optional] labels ) + */ +class GraphMobileNetQASYMM8Example : public utils::Example +{ +public: + void do_setup(int argc, char **argv) override + { + std::string data_path; /* Path to the trainable data */ + std::string input; /* Image data */ + std::string label; /* Label data */ + + // Quantization info taken from the AndroidNN QASYMM8 MobileNet example + const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128); + const QuantizationInfo mid_quant_info = QuantizationInfo(0.0784313753247f, 128); + + const std::vector<QuantizationInfo> conv_weights_quant_info = + { + QuantizationInfo(0.031778190285f, 156), // conv0 + QuantizationInfo(0.00604454148561f, 66) // conv14 + }; + + const std::vector<QuantizationInfo> depth_weights_quant_info = + { + QuantizationInfo(0.254282623529f, 129), // dwsc1 + QuantizationInfo(0.12828284502f, 172), // dwsc2 + QuantizationInfo(0.265911251307f, 83), // dwsc3 + QuantizationInfo(0.0985597148538f, 30), // dwsc4 + QuantizationInfo(0.0631204470992f, 54), // dwsc5 + QuantizationInfo(0.0137207424268f, 141), // dwsc6 + QuantizationInfo(0.0817828401923f, 125), // dwsc7 + QuantizationInfo(0.0393880493939f, 164), // dwsc8 + QuantizationInfo(0.211694166064f, 129), // dwsc9 + QuantizationInfo(0.158015936613f, 103), // dwsc10 + QuantizationInfo(0.0182712618262f, 137), // dwsc11 + QuantizationInfo(0.0127998134121f, 134), // dwsc12 + QuantizationInfo(0.299285322428f, 161) // dwsc13 + }; + + const std::vector<QuantizationInfo> point_weights_quant_info = + { + QuantizationInfo(0.0425766184926f, 129), // dwsc1 + QuantizationInfo(0.0250773020089f, 94), // dwsc2 + QuantizationInfo(0.015851572156f, 93), // dwsc3 + QuantizationInfo(0.0167811904103f, 98), // dwsc4 + QuantizationInfo(0.00951790809631f, 135), // dwsc5 + QuantizationInfo(0.00999817531556f, 128), // dwsc6 + QuantizationInfo(0.00590536883101f, 126), // dwsc7 + QuantizationInfo(0.00576109671965f, 133), // dwsc8 + QuantizationInfo(0.00830461271107f, 142), // dwsc9 + QuantizationInfo(0.0152327232063f, 72), // dwsc10 + QuantizationInfo(0.00741417845711f, 125), // dwsc11 + QuantizationInfo(0.0135628981516f, 142), // dwsc12 + QuantizationInfo(0.0338749065995f, 140) // dwsc13 + }; + + // Parse arguments + if(argc < 2) + { + // Print help + std::cout << "Usage: " << argv[0] << " [path_to_data] [npy_input] [labels]\n\n"; + std::cout << "No data folder provided: using random values\n\n"; + } + else if(argc == 2) + { + data_path = argv[1]; + std::cout << "Usage: " << argv[0] << " " << argv[1] << " [npy_input] [labels]\n\n"; + std::cout << "No input provided: using random values\n\n"; + } + else if(argc == 3) + { + data_path = argv[1]; + input = argv[2]; + std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n"; + std::cout << "No text file with labels provided: skipping output accessor\n\n"; + } + else + { + data_path = argv[1]; + input = argv[2]; + label = argv[3]; + } + + graph << TargetHint::OPENCL + << arm_compute::graph::Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::QASYMM8, in_quant_info), + get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/" + input)) + << ConvolutionLayer( + 3U, 3U, 32U, + get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_weights.npy"), + get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_bias.npy"), + PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), + 1, WeightsInfo(), + conv_weights_quant_info.at(0), + mid_quant_info) + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) + << get_dwsc_node(data_path, "Conv2d_1", 64U, PadStrideInfo(1U, 1U, 1U, 1U), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(0), point_weights_quant_info.at(0)) + << get_dwsc_node(data_path, "Conv2d_2", 128U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(1), + point_weights_quant_info.at(1)) + << get_dwsc_node(data_path, "Conv2d_3", 128U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(2), + point_weights_quant_info.at(2)) + << get_dwsc_node(data_path, "Conv2d_4", 256U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(3), + point_weights_quant_info.at(3)) + << get_dwsc_node(data_path, "Conv2d_5", 256U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(4), + point_weights_quant_info.at(4)) + << get_dwsc_node(data_path, "Conv2d_6", 512U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(5), + point_weights_quant_info.at(5)) + << get_dwsc_node(data_path, "Conv2d_7", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(6), + point_weights_quant_info.at(6)) + << get_dwsc_node(data_path, "Conv2d_8", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(7), + point_weights_quant_info.at(7)) + << get_dwsc_node(data_path, "Conv2d_9", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(8), + point_weights_quant_info.at(8)) + << get_dwsc_node(data_path, "Conv2d_10", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(9), + point_weights_quant_info.at(9)) + << get_dwsc_node(data_path, "Conv2d_11", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(10), + point_weights_quant_info.at(10)) + << get_dwsc_node(data_path, "Conv2d_12", 1024U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(11), + point_weights_quant_info.at(11)) + << get_dwsc_node(data_path, "Conv2d_13", 1024U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(12), + point_weights_quant_info.at(12)) + << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)) + << ConvolutionLayer( + 1U, 1U, 1001U, + get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_weights.npy"), + get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_bias.npy"), + PadStrideInfo(1U, 1U, 0U, 0U), 1, WeightsInfo(), conv_weights_quant_info.at(1)) + << ReshapeLayer(TensorShape(1001U)) + << SoftmaxLayer() + << arm_compute::graph::Tensor(get_output_accessor(label, 5)); + } + void do_run() override + { + // Run graph + graph.run(); + } + +private: + Graph graph{}; + + /** This function produces a depthwise separable convolution node (i.e. depthwise + pointwise layers) with ReLU6 activation after each layer. + * + * @param[in] data_path Path to trainable data folder + * @param[in] param_path Prefix of specific set of weights/biases data + * @param[in] conv_filt Filters depths for pointwise convolution + * @param[in] dwc_pad_stride_info PadStrideInfo for depthwise convolution + * @param[in] conv_pad_stride_info PadStrideInfo for pointwise convolution + * @param[in] depth_weights_quant_info QuantizationInfo for depthwise convolution's weights + * @param[in] point_weights_quant_info QuantizationInfo for pointwise convolution's weights + * + * @return The complete dwsc node + */ + BranchLayer get_dwsc_node(const std::string &data_path, std::string &¶m_path, + const unsigned int conv_filt, + PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info, + QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info) + { + std::string total_path = "/cnn_data/mobilenet_qasymm8_model/" + param_path + "_"; + SubGraph sg; + + sg << DepthwiseConvolutionLayer( + 3U, 3U, + get_weights_accessor(data_path, total_path + "depthwise_weights.npy"), + get_weights_accessor(data_path, total_path + "depthwise_bias.npy"), + dwc_pad_stride_info, + true, + depth_weights_quant_info) + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) + << ConvolutionLayer( + 1U, 1U, conv_filt, + get_weights_accessor(data_path, total_path + "pointwise_weights.npy"), + get_weights_accessor(data_path, total_path + "pointwise_bias.npy"), + conv_pad_stride_info, + 1, WeightsInfo(), + point_weights_quant_info) + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)); + + return BranchLayer(std::move(sg)); + } +}; +/** Main program for MobileNetQASYMM8 + * + * @param[in] argc Number of arguments + * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] npy_input, [optional] labels ) + */ +int main(int argc, char **argv) +{ + return utils::run_example<GraphMobileNetQASYMM8Example>(argc, argv); +} diff --git a/src/graph/nodes/BranchLayer.cpp b/src/graph/nodes/BranchLayer.cpp index eea0540741..6352bfc1e3 100644 --- a/src/graph/nodes/BranchLayer.cpp +++ b/src/graph/nodes/BranchLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -53,7 +53,7 @@ void depth_concatenate_output_info(ITensorInfo *info, ITensorInfo *sub_tensor_in arm_compute::auto_init_if_empty(*info, sub_tensor_info->tensor_shape(), sub_tensor_info->num_channels(), - sub_tensor_info->data_type(), sub_tensor_info->fixed_point_position()); + sub_tensor_info->data_type(), sub_tensor_info->fixed_point_position(), sub_tensor_info->quantization_info()); info->set_valid_region(sub_tensor_info->valid_region()); } else @@ -170,7 +170,8 @@ std::unique_ptr<arm_compute::IFunction> BranchLayer::instantiate_node(GraphConte out_info.tensor_shape(), out_info.num_channels(), out_info.data_type(), - out_info.fixed_point_position()); + out_info.fixed_point_position(), + out_info.quantization_info()); return std::move(func); }
\ No newline at end of file diff --git a/src/graph/nodes/ConvolutionLayer.cpp b/src/graph/nodes/ConvolutionLayer.cpp index ae4a8d7e6b..53d06ea75f 100644 --- a/src/graph/nodes/ConvolutionLayer.cpp +++ b/src/graph/nodes/ConvolutionLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -184,14 +184,17 @@ std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(Graph // Set weights and biases info if(_weights.tensor() == nullptr) { - _weights.set_info(TensorInfo(TensorShape(_conv_width, _conv_height, in->info()->dimension(2) / _num_groups, _ofm), + TensorInfo info = TensorInfo(TensorShape(_conv_width, _conv_height, in->info()->dimension(2) / _num_groups, _ofm), in->info()->num_channels(), in->info()->data_type(), - in->info()->fixed_point_position())); + in->info()->fixed_point_position()); + info.set_quantization_info(_weights_quant_info); + _weights.set_info(std::move(info)); } if(_biases.has_accessor() && _biases.tensor() == nullptr) { - _biases.set_info(TensorInfo(TensorShape(_ofm), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position())); + DataType dt = in->info()->data_type(); + _biases.set_info(TensorInfo(TensorShape(_ofm), in->info()->num_channels(), is_data_type_quantized_asymmetric(dt) ? DataType::S32 : dt, in->info()->fixed_point_position())); } std::unique_ptr<arm_compute::IFunction> func; @@ -213,7 +216,8 @@ std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(Graph TensorShape output_shape = calculate_convolution_layer_output_shape(in->info()->tensor_shape(), _weights.info().tensor_shape(), _conv_info); // Output auto inizialitation if not yet initialized - arm_compute::auto_init_if_empty(*out->info(), output_shape, 1, in->info()->data_type(), in->info()->fixed_point_position()); + arm_compute::auto_init_if_empty(*out->info(), output_shape, 1, in->info()->data_type(), in->info()->fixed_point_position(), + (_out_quant_info.empty()) ? in->info()->quantization_info() : _out_quant_info); // Create appropriate convolution function if(_num_groups == 1) diff --git a/src/graph/nodes/DepthwiseConvolutionLayer.cpp b/src/graph/nodes/DepthwiseConvolutionLayer.cpp index b4598538c1..1209d0376e 100644 --- a/src/graph/nodes/DepthwiseConvolutionLayer.cpp +++ b/src/graph/nodes/DepthwiseConvolutionLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -43,11 +43,14 @@ std::unique_ptr<arm_compute::IFunction> DepthwiseConvolutionLayer::instantiate_n TensorShape shape = in->info()->tensor_shape(); shape.set(Window::DimX, _conv_width); shape.set(Window::DimY, _conv_height); - _weights.set_info(TensorInfo(TensorShape(shape), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position())); + TensorInfo info = TensorInfo(TensorShape(shape), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()); + info.set_quantization_info(_quant_info); + _weights.set_info(std::move(info)); } if(_biases.has_accessor() && _biases.tensor() == nullptr) { - _biases.set_info(TensorInfo(TensorShape(in->info()->dimension(2)), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position())); + DataType dt = in->info()->data_type(); + _biases.set_info(TensorInfo(TensorShape(in->info()->dimension(2)), in->info()->num_channels(), is_data_type_quantized_asymmetric(dt) ? DataType::S32 : dt, in->info()->fixed_point_position())); } bool weights_is_loaded = _weights.tensor() != nullptr; diff --git a/src/graph/nodes/ReshapeLayer.cpp b/src/graph/nodes/ReshapeLayer.cpp index bbe0739e64..b0c117e418 100644 --- a/src/graph/nodes/ReshapeLayer.cpp +++ b/src/graph/nodes/ReshapeLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -44,7 +44,7 @@ std::unique_ptr<arm_compute::IFunction> ReshapeLayer::instantiate_node(GraphCont arm_compute::ITensor *out = output->tensor(); // Auto configure output - arm_compute::auto_init_if_empty(*out->info(), _shape, 1, in->info()->data_type(), in->info()->fixed_point_position()); + arm_compute::auto_init_if_empty(*out->info(), _shape, 1, in->info()->data_type(), in->info()->fixed_point_position(), in->info()->quantization_info()); // Create node context NodeContext node_ctx(OperationType::ReshapeLayer); diff --git a/utils/GraphUtils.cpp b/utils/GraphUtils.cpp index b9be9d4085..6b3dffc1a4 100644 --- a/utils/GraphUtils.cpp +++ b/utils/GraphUtils.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -140,16 +140,14 @@ TopNPredictionsAccessor::TopNPredictionsAccessor(const std::string &labels_path, } } -bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) +template <typename T> +void TopNPredictionsAccessor::access_predictions_tensor(ITensor &tensor) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON(_labels.size() != tensor.info()->dimension(0)); - // Get the predicted class - std::vector<float> classes_prob; + std::vector<T> classes_prob; std::vector<size_t> index; - const auto output_net = reinterpret_cast<float *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); + const auto output_net = reinterpret_cast<T *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); const size_t num_classes = tensor.info()->dimension(0); classes_prob.resize(num_classes); @@ -170,10 +168,28 @@ bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) for(size_t i = 0; i < _top_n; ++i) { _output_stream << std::fixed << std::setprecision(4) - << classes_prob[index.at(i)] + << +classes_prob[index.at(i)] << " - [id = " << index.at(i) << "]" << ", " << _labels[index.at(i)] << std::endl; } +} + +bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32, DataType::QASYMM8); + ARM_COMPUTE_ERROR_ON(_labels.size() != tensor.info()->dimension(0)); + + switch(tensor.info()->data_type()) + { + case DataType::QASYMM8: + access_predictions_tensor<uint8_t>(tensor); + break; + case DataType::F32: + access_predictions_tensor<float>(tensor); + break; + default: + ARM_COMPUTE_ERROR("NOT SUPPORTED!"); + } return false; } diff --git a/utils/GraphUtils.h b/utils/GraphUtils.h index ae6ecad881..e97bbf1c49 100644 --- a/utils/GraphUtils.h +++ b/utils/GraphUtils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -128,6 +128,9 @@ public: bool access_tensor(ITensor &tensor) override; private: + template <typename T> + void access_predictions_tensor(ITensor &tensor); + std::vector<std::string> _labels; std::ostream &_output_stream; size_t _top_n; diff --git a/utils/Utils.h b/utils/Utils.h index 01e5137669..ff4c4c99fd 100644 --- a/utils/Utils.h +++ b/utils/Utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2018 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -138,6 +138,7 @@ inline std::string get_typestring(DataType data_type) switch(data_type) { case DataType::U8: + case DataType::QASYMM8: return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t)); case DataType::S8: return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t)); |