summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorKaizen <kaizen@arm.com>2017-09-28 14:38:23 +0100
committerAnthony Barbier <anthony.barbier@arm.com>2017-09-28 16:31:13 +0100
commit8938bd3f40ea62ff56d6ed4e2db0a8aee34dd64a (patch)
treec234331232f227e0cdfb567a54ecaa5460aaa064 /utils
parentf4a254c2745aeaab6f7276a675147d707002fe7a (diff)
downloadarmcl-8938bd3f40ea62ff56d6ed4e2db0a8aee34dd64a.tar.gz
armcl-8938bd3f40ea62ff56d6ed4e2db0a8aee34dd64a.tar.bz2
armcl-8938bd3f40ea62ff56d6ed4e2db0a8aee34dd64a.zip
arm_compute v17.09
Change-Id: I4bf8f4e6e5f84ce0d5b6f5ba570d276879f42a81
Diffstat (limited to 'utils')
-rw-r--r--utils/GraphUtils.cpp165
-rw-r--r--utils/GraphUtils.h97
-rw-r--r--utils/TypePrinter.h638
-rw-r--r--utils/Utils.h178
4 files changed, 1055 insertions, 23 deletions
diff --git a/utils/GraphUtils.cpp b/utils/GraphUtils.cpp
new file mode 100644
index 000000000..d76360686
--- /dev/null
+++ b/utils/GraphUtils.cpp
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) 2017 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 "utils/GraphUtils.h"
+#include "utils/Utils.h"
+
+#ifdef ARM_COMPUTE_CL
+#include "arm_compute/core/CL/OpenCL.h"
+#include "arm_compute/runtime/CL/CLTensor.h"
+#endif /* ARM_COMPUTE_CL */
+
+#include "arm_compute/core/Error.h"
+#include "libnpy/npy.hpp"
+
+#include <sstream>
+
+using namespace arm_compute::graph_utils;
+
+PPMWriter::PPMWriter(std::string name, unsigned int maximum)
+ : _name(std::move(name)), _iterator(0), _maximum(maximum)
+{
+}
+
+bool PPMWriter::access_tensor(ITensor &tensor)
+{
+ std::stringstream ss;
+ ss << _name << _iterator << ".ppm";
+ if(dynamic_cast<Tensor *>(&tensor) != nullptr)
+ {
+ arm_compute::utils::save_to_ppm(dynamic_cast<Tensor &>(tensor), ss.str());
+ }
+#ifdef ARM_COMPUTE_CL
+ else if(dynamic_cast<CLTensor *>(&tensor) != nullptr)
+ {
+ arm_compute::utils::save_to_ppm(dynamic_cast<CLTensor &>(tensor), ss.str());
+ }
+#endif /* ARM_COMPUTE_CL */
+
+ _iterator++;
+ if(_maximum == 0)
+ {
+ return true;
+ }
+ return _iterator < _maximum;
+}
+
+DummyAccessor::DummyAccessor(unsigned int maximum)
+ : _iterator(0), _maximum(maximum)
+{
+}
+
+bool DummyAccessor::access_tensor(ITensor &tensor)
+{
+ ARM_COMPUTE_UNUSED(tensor);
+ bool ret = _maximum == 0 || _iterator < _maximum;
+ if(_iterator == _maximum)
+ {
+ _iterator = 0;
+ }
+ else
+ {
+ _iterator++;
+ }
+ return ret;
+}
+
+NumPyBinLoader::NumPyBinLoader(std::string filename)
+ : _filename(std::move(filename))
+{
+}
+
+bool NumPyBinLoader::access_tensor(ITensor &tensor)
+{
+ const TensorShape tensor_shape = tensor.info()->tensor_shape();
+ std::vector<unsigned long> shape;
+
+ // Open file
+ std::ifstream stream(_filename, std::ios::in | std::ios::binary);
+ ARM_COMPUTE_ERROR_ON_MSG(!stream.good(), "Failed to load binary data");
+ // Check magic bytes and version number
+ unsigned char v_major = 0;
+ unsigned char v_minor = 0;
+ npy::read_magic(stream, &v_major, &v_minor);
+
+ // Read header
+ std::string header;
+ if(v_major == 1 && v_minor == 0)
+ {
+ header = npy::read_header_1_0(stream);
+ }
+ else if(v_major == 2 && v_minor == 0)
+ {
+ header = npy::read_header_2_0(stream);
+ }
+ else
+ {
+ ARM_COMPUTE_ERROR("Unsupported file format version");
+ }
+
+ // Parse header
+ bool fortran_order = false;
+ std::string typestr;
+ npy::ParseHeader(header, typestr, &fortran_order, shape);
+
+ // Check if the typestring matches the given one
+ std::string expect_typestr = arm_compute::utils::get_typestring(tensor.info()->data_type());
+ ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
+
+ // Validate tensor shape
+ ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor_shape.num_dimensions(), "Tensor ranks mismatch");
+ if(fortran_order)
+ {
+ for(size_t i = 0; i < shape.size(); ++i)
+ {
+ ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[i], "Tensor dimensions mismatch");
+ }
+ }
+ else
+ {
+ for(size_t i = 0; i < shape.size(); ++i)
+ {
+ ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
+ }
+ }
+
+ // Read data
+ if(tensor.info()->padding().empty())
+ {
+ // If tensor has no padding read directly from stream.
+ stream.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
+ }
+ else
+ {
+ // If tensor has padding accessing tensor elements through execution window.
+ Window window;
+ window.use_tensor_dimensions(tensor_shape);
+
+ execute_window_loop(window, [&](const Coordinates & id)
+ {
+ stream.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size());
+ });
+ }
+ return true;
+}
diff --git a/utils/GraphUtils.h b/utils/GraphUtils.h
new file mode 100644
index 000000000..a19f7e510
--- /dev/null
+++ b/utils/GraphUtils.h
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2017 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.
+ */
+#ifndef __ARM_COMPUTE_GRAPH_UTILS_H__
+#define __ARM_COMPUTE_GRAPH_UTILS_H__
+
+#include "arm_compute/graph/ITensorAccessor.h"
+#include "arm_compute/graph/Types.h"
+
+namespace arm_compute
+{
+namespace graph_utils
+{
+/** PPM writer class */
+class PPMWriter : public graph::ITensorAccessor
+{
+public:
+ /** Constructor
+ *
+ * @param[in] name PPM file name
+ * @param[in] maximum Maximum elements to access
+ */
+ PPMWriter(std::string name, unsigned int maximum = 1);
+ /** Allows instances to move constructed */
+ PPMWriter(PPMWriter &&) = default;
+
+ // Inherited methods overriden:
+ bool access_tensor(ITensor &tensor) override;
+
+private:
+ const std::string _name;
+ unsigned int _iterator;
+ unsigned int _maximum;
+};
+
+/** Dummy accessor class */
+class DummyAccessor : public graph::ITensorAccessor
+{
+public:
+ /** Constructor
+ *
+ * @param[in] maximum Maximum elements to write
+ */
+ DummyAccessor(unsigned int maximum = 1);
+ /** Allows instances to move constructed */
+ DummyAccessor(DummyAccessor &&) = default;
+
+ // Inherited methods overriden:
+ bool access_tensor(ITensor &tensor) override;
+
+private:
+ unsigned int _iterator;
+ unsigned int _maximum;
+};
+
+/** Numpy Binary loader class*/
+class NumPyBinLoader : public graph::ITensorAccessor
+{
+public:
+ /** Default Constructor
+ *
+ * @param filename Binary file name
+ */
+ NumPyBinLoader(std::string filename);
+ /** Allows instances to move constructed */
+ NumPyBinLoader(NumPyBinLoader &&) = default;
+
+ // Inherited methods overriden:
+ bool access_tensor(ITensor &tensor) override;
+
+private:
+ const std::string _filename;
+};
+} // namespace graph
+} // namespace arm_compute
+
+#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */
diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h
new file mode 100644
index 000000000..d0c568922
--- /dev/null
+++ b/utils/TypePrinter.h
@@ -0,0 +1,638 @@
+/*
+ * Copyright (c) 2017 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.
+ */
+#ifndef __ARM_COMPUTE_TEST_TYPE_PRINTER_H__
+#define __ARM_COMPUTE_TEST_TYPE_PRINTER_H__
+
+#include "arm_compute/core/Dimensions.h"
+#include "arm_compute/core/Error.h"
+#include "arm_compute/core/Types.h"
+
+#include <ostream>
+#include <sstream>
+#include <string>
+
+namespace arm_compute
+{
+/** Formatted output of the Dimensions type. */
+template <typename T>
+inline ::std::ostream &operator<<(::std::ostream &os, const Dimensions<T> &dimensions)
+{
+ if(dimensions.num_dimensions() > 0)
+ {
+ os << dimensions[0];
+
+ for(unsigned int d = 1; d < dimensions.num_dimensions(); ++d)
+ {
+ os << "x" << dimensions[d];
+ }
+ }
+
+ return os;
+}
+
+/** Formatted output of the NonLinearFilterFunction type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const NonLinearFilterFunction &function)
+{
+ switch(function)
+ {
+ case NonLinearFilterFunction::MAX:
+ os << "MAX";
+ break;
+ case NonLinearFilterFunction::MEDIAN:
+ os << "MEDIAN";
+ break;
+ case NonLinearFilterFunction::MIN:
+ os << "MIN";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const NonLinearFilterFunction &function)
+{
+ std::stringstream str;
+ str << function;
+ return str.str();
+}
+
+/** Formatted output of the MatrixPattern type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const MatrixPattern &pattern)
+{
+ switch(pattern)
+ {
+ case MatrixPattern::BOX:
+ os << "BOX";
+ break;
+ case MatrixPattern::CROSS:
+ os << "CROSS";
+ break;
+ case MatrixPattern::DISK:
+ os << "DISK";
+ break;
+ case MatrixPattern::OTHER:
+ os << "OTHER";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const MatrixPattern &pattern)
+{
+ std::stringstream str;
+ str << pattern;
+ return str.str();
+}
+
+/** Formatted output of the RoundingPolicy type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const RoundingPolicy &rounding_policy)
+{
+ switch(rounding_policy)
+ {
+ case RoundingPolicy::TO_ZERO:
+ os << "TO_ZERO";
+ break;
+ case RoundingPolicy::TO_NEAREST_UP:
+ os << "TO_NEAREST_UP";
+ break;
+ case RoundingPolicy::TO_NEAREST_EVEN:
+ os << "TO_NEAREST_EVEN";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+/** Formatted output of the WeightsInfo type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const WeightsInfo &weights_info)
+{
+ os << weights_info.are_reshaped() << ";";
+ os << weights_info.num_kernels() << ";" << weights_info.kernel_size().first << "," << weights_info.kernel_size().second;
+
+ return os;
+}
+
+/** Formatted output of the ROIPoolingInfo type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const ROIPoolingLayerInfo &pool_info)
+{
+ os << pool_info.pooled_width() << "x" << pool_info.pooled_height() << "~" << pool_info.spatial_scale();
+ return os;
+}
+
+/** Formatted output of the activation function type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const ActivationLayerInfo::ActivationFunction &act_function)
+{
+ switch(act_function)
+ {
+ case ActivationLayerInfo::ActivationFunction::ABS:
+ os << "ABS";
+ break;
+ case ActivationLayerInfo::ActivationFunction::LINEAR:
+ os << "LINEAR";
+ break;
+ case ActivationLayerInfo::ActivationFunction::LOGISTIC:
+ os << "LOGISTIC";
+ break;
+ case ActivationLayerInfo::ActivationFunction::RELU:
+ os << "RELU";
+ break;
+ case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
+ os << "BOUNDED_RELU";
+ break;
+ case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
+ os << "LEAKY_RELU";
+ break;
+ case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
+ os << "SOFT_RELU";
+ break;
+ case ActivationLayerInfo::ActivationFunction::SQRT:
+ os << "SQRT";
+ break;
+ case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
+ os << "LU_BOUNDED_RELU";
+ case ActivationLayerInfo::ActivationFunction::SQUARE:
+ os << "SQUARE";
+ break;
+ case ActivationLayerInfo::ActivationFunction::TANH:
+ os << "TANH";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const arm_compute::ActivationLayerInfo &info)
+{
+ std::stringstream str;
+ str << info.activation();
+ return str.str();
+}
+
+inline std::string to_string(const arm_compute::ActivationLayerInfo::ActivationFunction &function)
+{
+ std::stringstream str;
+ str << function;
+ return str.str();
+}
+
+/** Formatted output of the NormType type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const NormType &norm_type)
+{
+ switch(norm_type)
+ {
+ case NormType::CROSS_MAP:
+ os << "CROSS_MAP";
+ break;
+ case NormType::IN_MAP_1D:
+ os << "IN_MAP_1D";
+ break;
+ case NormType::IN_MAP_2D:
+ os << "IN_MAP_2D";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const arm_compute::NormalizationLayerInfo &info)
+{
+ std::stringstream str;
+ str << info.type();
+ return str.str();
+}
+
+/** Formatted output of the PoolingType type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const PoolingType &pool_type)
+{
+ switch(pool_type)
+ {
+ case PoolingType::AVG:
+ os << "AVG";
+ break;
+ case PoolingType::MAX:
+ os << "MAX";
+ break;
+ case PoolingType::L2:
+ os << "L2";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+/** Formatted output of @ref PoolingLayerInfo. */
+inline ::std::ostream &operator<<(::std::ostream &os, const PoolingLayerInfo &info)
+{
+ os << info.pool_type();
+
+ return os;
+}
+
+/** Formatted output of the DataType type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const DataType &data_type)
+{
+ switch(data_type)
+ {
+ case DataType::UNKNOWN:
+ os << "UNKNOWN";
+ break;
+ case DataType::U8:
+ os << "U8";
+ break;
+ case DataType::QS8:
+ os << "QS8";
+ break;
+ case DataType::S8:
+ os << "S8";
+ break;
+ case DataType::U16:
+ os << "U16";
+ break;
+ case DataType::S16:
+ os << "S16";
+ break;
+ case DataType::QS16:
+ os << "QS16";
+ break;
+ case DataType::U32:
+ os << "U32";
+ break;
+ case DataType::S32:
+ os << "S32";
+ break;
+ case DataType::U64:
+ os << "U64";
+ break;
+ case DataType::S64:
+ os << "S64";
+ break;
+ case DataType::F16:
+ os << "F16";
+ break;
+ case DataType::F32:
+ os << "F32";
+ break;
+ case DataType::F64:
+ os << "F64";
+ break;
+ case DataType::SIZET:
+ os << "SIZET";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const arm_compute::DataType &data_type)
+{
+ std::stringstream str;
+ str << data_type;
+ return str.str();
+}
+
+/** Formatted output of the Format type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const Format &format)
+{
+ switch(format)
+ {
+ case Format::UNKNOWN:
+ os << "UNKNOWN";
+ break;
+ case Format::U8:
+ os << "U8";
+ break;
+ case Format::S16:
+ os << "S16";
+ break;
+ case Format::U16:
+ os << "U16";
+ break;
+ case Format::S32:
+ os << "S32";
+ break;
+ case Format::U32:
+ os << "U32";
+ break;
+ case Format::F16:
+ os << "F16";
+ break;
+ case Format::F32:
+ os << "F32";
+ break;
+ case Format::UV88:
+ os << "UV88";
+ break;
+ case Format::RGB888:
+ os << "RGB888";
+ break;
+ case Format::RGBA8888:
+ os << "RGBA8888";
+ break;
+ case Format::YUV444:
+ os << "YUV444";
+ break;
+ case Format::YUYV422:
+ os << "YUYV422";
+ break;
+ case Format::NV12:
+ os << "NV12";
+ break;
+ case Format::NV21:
+ os << "NV21";
+ break;
+ case Format::IYUV:
+ os << "IYUV";
+ break;
+ case Format::UYVY422:
+ os << "UYVY422";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const Format &format)
+{
+ std::stringstream str;
+ str << format;
+ return str.str();
+}
+
+/** Formatted output of the Channel type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const Channel &channel)
+{
+ switch(channel)
+ {
+ case Channel::UNKNOWN:
+ os << "UNKNOWN";
+ break;
+ case Channel::C0:
+ os << "C0";
+ break;
+ case Channel::C1:
+ os << "C1";
+ break;
+ case Channel::C2:
+ os << "C2";
+ break;
+ case Channel::C3:
+ os << "C3";
+ break;
+ case Channel::R:
+ os << "R";
+ break;
+ case Channel::G:
+ os << "G";
+ break;
+ case Channel::B:
+ os << "B";
+ break;
+ case Channel::A:
+ os << "A";
+ break;
+ case Channel::Y:
+ os << "Y";
+ break;
+ case Channel::U:
+ os << "U";
+ break;
+ case Channel::V:
+ os << "V";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+/** Formatted output of the BorderMode type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const BorderMode &mode)
+{
+ switch(mode)
+ {
+ case BorderMode::UNDEFINED:
+ os << "UNDEFINED";
+ break;
+ case BorderMode::CONSTANT:
+ os << "CONSTANT";
+ break;
+ case BorderMode::REPLICATE:
+ os << "REPLICATE";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+/** Formatted output of the BorderSize type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const BorderSize &border)
+{
+ os << border.top << ","
+ << border.right << ","
+ << border.bottom << ","
+ << border.left;
+
+ return os;
+}
+
+/** Formatted output of the InterpolationPolicy type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const InterpolationPolicy &policy)
+{
+ switch(policy)
+ {
+ case InterpolationPolicy::NEAREST_NEIGHBOR:
+ os << "NEAREST_NEIGHBOR";
+ break;
+ case InterpolationPolicy::BILINEAR:
+ os << "BILINEAR";
+ break;
+ case InterpolationPolicy::AREA:
+ os << "AREA";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+template <typename T>
+inline std::string to_string(const Dimensions<T> &dimensions)
+{
+ std::stringstream str;
+ str << dimensions;
+ return str.str();
+}
+
+/** Formatted output of the TensorShape type. */
+inline std::string to_string(const TensorShape &shape)
+{
+ std::stringstream str;
+ str << shape;
+ return str.str();
+}
+
+/** Formatted output of the Rectangle type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const Rectangle &rect)
+{
+ os << rect.width << "x" << rect.height;
+ os << "+" << rect.x << "+" << rect.y;
+
+ return os;
+}
+
+/** Formatted output of the PadStridInfo type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const PadStrideInfo &pad_stride_info)
+{
+ os << pad_stride_info.stride().first << "," << pad_stride_info.stride().second;
+ os << ";";
+ os << pad_stride_info.pad().first << "," << pad_stride_info.pad().second;
+
+ return os;
+}
+
+inline std::string to_string(const PadStrideInfo &pad_stride_info)
+{
+ std::stringstream str;
+ str << pad_stride_info;
+ return str.str();
+}
+
+inline std::string to_string(const BorderMode &mode)
+{
+ std::stringstream str;
+ str << mode;
+ return str.str();
+}
+
+inline std::string to_string(const InterpolationPolicy &policy)
+{
+ std::stringstream str;
+ str << policy;
+ return str.str();
+}
+
+/** Formatted output of the ConversionPolicy type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const ConvertPolicy &policy)
+{
+ switch(policy)
+ {
+ case ConvertPolicy::WRAP:
+ os << "WRAP";
+ break;
+ case ConvertPolicy::SATURATE:
+ os << "SATURATE";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const ConvertPolicy &policy)
+{
+ std::stringstream str;
+ str << policy;
+ return str.str();
+}
+
+/** Formatted output of the Reduction Operations. */
+inline ::std::ostream &operator<<(::std::ostream &os, const ReductionOperation &op)
+{
+ switch(op)
+ {
+ case ReductionOperation::SUM_SQUARE:
+ os << "SUM_SQUARE";
+ break;
+ default:
+ ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
+ }
+
+ return os;
+}
+
+inline std::string to_string(const ReductionOperation &op)
+{
+ std::stringstream str;
+ str << op;
+ return str.str();
+}
+
+inline std::string to_string(const NormType &type)
+{
+ std::stringstream str;
+ str << type;
+ return str.str();
+}
+
+inline std::string to_string(const PoolingType &type)
+{
+ std::stringstream str;
+ str << type;
+ return str.str();
+}
+
+inline std::string to_string(const PoolingLayerInfo &info)
+{
+ std::stringstream str;
+ str << info.pool_type();
+ return str.str();
+}
+
+/** Formatted output of the KeyPoint type. */
+inline ::std::ostream &operator<<(::std::ostream &os, const KeyPoint &point)
+{
+ os << "{x=" << point.x << ","
+ << "y=" << point.y << ","
+ << "strength=" << point.strength << ","
+ << "scale=" << point.scale << ","
+ << "orientation=" << point.orientation << ","
+ << "tracking_status=" << point.tracking_status << ","
+ << "error=" << point.error << "}";
+
+ return os;
+}
+} // namespace arm_compute
+#endif /* __ARM_COMPUTE_TEST_TYPE_PRINTER_H__ */
diff --git a/utils/Utils.h b/utils/Utils.h
index b519f83a8..47a352e90 100644
--- a/utils/Utils.h
+++ b/utils/Utils.h
@@ -28,7 +28,9 @@
#include "arm_compute/core/ITensor.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/Validate.h"
+#include "arm_compute/core/Window.h"
#include "arm_compute/runtime/Tensor.h"
+#include "support/ToolchainSupport.h"
#ifdef ARM_COMPUTE_CL
#include "arm_compute/core/CL/OpenCL.h"
@@ -78,6 +80,49 @@ void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::D
*/
std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
+/** Maps a tensor if needed
+ *
+ * @param[in] tensor Tensor to be mapped
+ * @param[in] blocking Specified if map is blocking or not
+ */
+template <typename T>
+void map(T &tensor, bool blocking)
+{
+ ARM_COMPUTE_UNUSED(tensor);
+ ARM_COMPUTE_UNUSED(blocking);
+}
+
+/** Unmaps a tensor if needed
+ *
+ * @param tensor Tensor to be unmapped
+ */
+template <typename T>
+void unmap(T &tensor)
+{
+ ARM_COMPUTE_UNUSED(tensor);
+}
+
+#ifdef ARM_COMPUTE_CL
+/** Maps a tensor if needed
+ *
+ * @param[in] tensor Tensor to be mapped
+ * @param[in] blocking Specified if map is blocking or not
+ */
+void map(CLTensor &tensor, bool blocking)
+{
+ tensor.map(blocking);
+}
+
+/** Unmaps a tensor if needed
+ *
+ * @param tensor Tensor to be unmapped
+ */
+void unmap(CLTensor &tensor)
+{
+ tensor.unmap();
+}
+#endif /* ARM_COMPUTE_CL */
+
/** Class to load the content of a PPM file into an Image
*/
class PPMLoader
@@ -146,13 +191,9 @@ public:
ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&image, arm_compute::Format::U8, arm_compute::Format::RGB888);
try
{
-#ifdef ARM_COMPUTE_CL
// Map buffer if creating a CLTensor
- if(std::is_same<typename std::decay<T>::type, arm_compute::CLImage>::value)
- {
- image.map();
- }
-#endif
+ map(image, true);
+
// Check if the file is large enough to fill the image
const size_t current_position = _fs.tellg();
_fs.seekg(0, std::ios_base::end);
@@ -213,13 +254,8 @@ public:
ARM_COMPUTE_ERROR("Unsupported format");
}
-#ifdef ARM_COMPUTE_CL
// Unmap buffer if creating a CLTensor
- if(std::is_same<typename std::decay<T>::type, arm_compute::CLTensor>::value)
- {
- image.unmap();
- }
-#endif
+ unmap(image);
}
catch(const std::ifstream::failure &e)
{
@@ -260,13 +296,8 @@ void save_to_ppm(T &tensor, const std::string &ppm_filename)
fs << "P6\n"
<< width << " " << height << " 255\n";
-#ifdef ARM_COMPUTE_CL
// Map buffer if creating a CLTensor
- if(std::is_same<typename std::decay<T>::type, arm_compute::CLTensor>::value)
- {
- tensor.map();
- }
-#endif
+ map(tensor, true);
switch(tensor.info()->format())
{
@@ -307,17 +338,118 @@ void save_to_ppm(T &tensor, const std::string &ppm_filename)
default:
ARM_COMPUTE_ERROR("Unsupported format");
}
-#ifdef ARM_COMPUTE_CL
+
// Unmap buffer if creating a CLTensor
- if(std::is_same<typename std::decay<T>::type, arm_compute::CLTensor>::value)
+ unmap(tensor);
+ }
+ catch(const std::ofstream::failure &e)
+ {
+ ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
+ }
+}
+
+/** Load the tensor with pre-trained data from a binary file
+ *
+ * @param[in] tensor The tensor to be filled. Data type supported: F32.
+ * @param[in] filename Filename of the binary file to load from.
+ */
+template <typename T>
+void load_trained_data(T &tensor, const std::string &filename)
+{
+ ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
+
+ std::ifstream fs;
+
+ try
+ {
+ fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
+ // Open file
+ fs.open(filename, std::ios::in | std::ios::binary);
+
+ if(!fs.good())
+ {
+ throw std::runtime_error("Could not load binary data: " + filename);
+ }
+
+ // Map buffer if creating a CLTensor
+ map(tensor, true);
+
+ Window window;
+
+ window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
+
+ for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
{
- tensor.unmap();
+ window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
}
-#endif
+
+ arm_compute::Iterator in(&tensor, window);
+
+ execute_window_loop(window, [&](const Coordinates & id)
+ {
+ fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
+ },
+ in);
+
+#ifdef ARM_COMPUTE_CL
+ // Unmap buffer if creating a CLTensor
+ unmap(tensor);
+#endif /* ARM_COMPUTE_CL */
}
catch(const std::ofstream::failure &e)
{
- ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
+ ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what());
+ }
+}
+
+/** Obtain numpy type string from DataType.
+ *
+ * @param[in] data_type Data type.
+ *
+ * @return numpy type string.
+ */
+inline std::string get_typestring(DataType data_type)
+{
+ // Check endianness
+ const unsigned int i = 1;
+ const char *c = reinterpret_cast<const char *>(&i);
+ std::string endianness;
+ if(*c == 1)
+ {
+ endianness = std::string("<");
+ }
+ else
+ {
+ endianness = std::string(">");
+ }
+ const std::string no_endianness("|");
+
+ switch(data_type)
+ {
+ case DataType::U8:
+ 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));
+ case DataType::U16:
+ return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
+ case DataType::S16:
+ return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
+ case DataType::U32:
+ return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
+ case DataType::S32:
+ return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
+ case DataType::U64:
+ return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
+ case DataType::S64:
+ return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
+ case DataType::F32:
+ return endianness + "f" + support::cpp11::to_string(sizeof(float));
+ case DataType::F64:
+ return endianness + "f" + support::cpp11::to_string(sizeof(double));
+ case DataType::SIZET:
+ return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
+ default:
+ ARM_COMPUTE_ERROR("NOT SUPPORTED!");
}
}
} // namespace utils