summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/SConscript24
-rw-r--r--examples/cl_convolution.cpp5
-rw-r--r--examples/cl_events.cpp5
-rw-r--r--examples/graph_lenet.cpp142
-rw-r--r--examples/neon_cnn.cpp8
-rw-r--r--examples/neon_copy_objects.cpp4
-rw-r--r--examples/neoncl_scale_median_gaussian.cpp5
7 files changed, 179 insertions, 14 deletions
diff --git a/examples/SConscript b/examples/SConscript
index 748f771ec..853a1bb51 100644
--- a/examples/SConscript
+++ b/examples/SConscript
@@ -23,8 +23,6 @@ import SCons
import os.path
Import('env')
-Import('arm_compute_a')
-Import('arm_compute_so')
if env['opencl']:
Import('opencl')
@@ -38,17 +36,33 @@ examples_env.Append(LIBPATH = ["#build/%s/opencl-1.2-stubs" % env['build_dir']])
# Build examples
utils = examples_env.Object("../utils/Utils.cpp")
-if env['os'] in ['android', 'bare_metal']:
+if env['os'] in ['android', 'bare_metal'] or env['standalone']:
+ Import('arm_compute_a')
arm_compute_lib = arm_compute_a
arm_compute_dependency = arm_compute_a
else:
+ Import('arm_compute_so')
arm_compute_lib = "arm_compute"
arm_compute_dependency = arm_compute_so
if env['opencl'] and env['neon']:
for file in Glob("./neoncl_*.cpp"):
example = os.path.basename(os.path.splitext(str(file))[0])
- prog = examples_env.Program(example, ["{}.cpp".format(example), utils], LIBS = [arm_compute_lib, "OpenCL"])
+ prog = examples_env.Program(example, ["{}.cpp".format(example), utils], CPPDEFINES=['ARM_COMPUTE_CL'], LIBS = [arm_compute_lib, "OpenCL"])
+ Depends(prog, [arm_compute_dependency, opencl])
+ alias = examples_env.Alias(example, prog)
+ Default(alias)
+ Import('arm_compute_graph_a')
+ Import('arm_compute_graph_so')
+ if env['os'] == 'android':
+ arm_compute_graph_lib = arm_compute_graph_a
+ else:
+ arm_compute_graph_lib = "arm_compute_graph"
+
+ graph_utils = examples_env.Object("../utils/GraphUtils.cpp")
+ for file in Glob("./graph_*.cpp"):
+ example = os.path.basename(os.path.splitext(str(file))[0])
+ prog = examples_env.Program(example, ["{}.cpp".format(example), utils, graph_utils], CPPDEFINES=['ARM_COMPUTE_CL'], LIBS = [arm_compute_graph_lib, "OpenCL"])
Depends(prog, [arm_compute_dependency, opencl])
alias = examples_env.Alias(example, prog)
Default(alias)
@@ -56,7 +70,7 @@ if env['opencl'] and env['neon']:
if env['opencl']:
for file in Glob("./cl_*.cpp"):
example = os.path.basename(os.path.splitext(str(file))[0])
- prog = examples_env.Program(example, ["{}.cpp".format(example), utils], LIBS = [arm_compute_lib, "OpenCL"])
+ prog = examples_env.Program(example, ["{}.cpp".format(example), utils], CPPDEFINES=['ARM_COMPUTE_CL'], LIBS = [arm_compute_lib, "OpenCL"])
Depends(prog, [arm_compute_dependency, opencl])
alias = examples_env.Alias(example, prog)
Default(alias)
diff --git a/examples/cl_convolution.cpp b/examples/cl_convolution.cpp
index 06f6f144e..b780193f1 100644
--- a/examples/cl_convolution.cpp
+++ b/examples/cl_convolution.cpp
@@ -21,7 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#define ARM_COMPUTE_CL /* So that OpenCL exceptions get caught too */
+#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
+#error "This example needs to be built with -DARM_COMPUTE_CL"
+#endif /* ARM_COMPUTE_CL */
+
#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/CL/CLFunctions.h"
#include "arm_compute/runtime/CL/CLScheduler.h"
diff --git a/examples/cl_events.cpp b/examples/cl_events.cpp
index 768f62062..213f4a19d 100644
--- a/examples/cl_events.cpp
+++ b/examples/cl_events.cpp
@@ -21,7 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#define ARM_COMPUTE_CL /* So that OpenCL exceptions get caught too */
+#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
+#error "This example needs to be built with -DARM_COMPUTE_CL"
+#endif /* ARM_COMPUTE_CL */
+
#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/CL/CLFunctions.h"
#include "arm_compute/runtime/CL/CLScheduler.h"
diff --git a/examples/graph_lenet.cpp b/examples/graph_lenet.cpp
new file mode 100644
index 000000000..676fdb9ce
--- /dev/null
+++ b/examples/graph_lenet.cpp
@@ -0,0 +1,142 @@
+/*
+ * 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_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
+#error "This example needs to be built with -DARM_COMPUTE_CL"
+#endif /* ARM_COMPUTE_CL */
+
+#include "arm_compute/graph/Graph.h"
+#include "arm_compute/graph/Nodes.h"
+#include "arm_compute/runtime/CL/CLScheduler.h"
+#include "arm_compute/runtime/Scheduler.h"
+#include "support/ToolchainSupport.h"
+#include "utils/GraphUtils.h"
+#include "utils/Utils.h"
+
+#include <cstdlib>
+#include <iostream>
+#include <memory>
+
+using namespace arm_compute::graph;
+using namespace arm_compute::graph_utils;
+
+/** Generates appropriate accessor according to the specified path
+ *
+ * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
+ *
+ * @param path Path to the data files
+ * @param data_file Relative path to the data files from path
+ *
+ * @return An appropriate tensor accessor
+ */
+std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
+{
+ if(path.empty())
+ {
+ return arm_compute::support::cpp14::make_unique<DummyAccessor>();
+ }
+ else
+ {
+ return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
+ }
+}
+
+/** Example demonstrating how to implement LeNet'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] batches )
+ */
+void main_graph_lenet(int argc, const char **argv)
+{
+ std::string data_path; /** Path to the trainable data */
+ unsigned int batches = 4; /** Number of batches */
+
+ // Parse arguments
+ if(argc < 2)
+ {
+ // Print help
+ std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
+ std::cout << "No data folder provided: using random values\n\n";
+ }
+ else if(argc == 2)
+ {
+ //Do something with argv[1]
+ data_path = argv[1];
+ std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
+ std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
+ }
+ else
+ {
+ //Do something with argv[1] and argv[2]
+ data_path = argv[1];
+ batches = std::strtol(argv[2], nullptr, 0);
+ }
+
+ // Check if OpenCL is available and initialize the scheduler
+ if(arm_compute::opencl_is_available())
+ {
+ arm_compute::CLScheduler::get().default_init();
+ }
+
+ Graph graph;
+ graph.set_info_enablement(true);
+
+ //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
+ graph << Hint::OPENCL
+ << Tensor(TensorInfo(TensorShape(28U, 28U, 1U, batches), 1, DataType::F32), DummyAccessor())
+ << ConvolutionLayer(
+ 5U, 5U, 20U,
+ get_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"),
+ get_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
+ PadStrideInfo(1, 1, 0, 0))
+ << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
+ << ConvolutionLayer(
+ 5U, 5U, 50U,
+ get_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"),
+ get_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
+ PadStrideInfo(1, 1, 0, 0))
+ << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
+ << FullyConnectedLayer(
+ 500U,
+ get_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"),
+ get_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
+ << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
+ << FullyConnectedLayer(
+ 10U,
+ get_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"),
+ get_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
+ << SoftmaxLayer()
+ << Tensor(DummyAccessor());
+
+ graph.run();
+}
+
+/** Main program for LeNet
+ *
+ * @param[in] argc Number of arguments
+ * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
+ */
+int main(int argc, const char **argv)
+{
+ return arm_compute::utils::run_example(argc, argv, main_graph_lenet);
+}
diff --git a/examples/neon_cnn.cpp b/examples/neon_cnn.cpp
index 952ae4d48..238f0572d 100644
--- a/examples/neon_cnn.cpp
+++ b/examples/neon_cnn.cpp
@@ -143,22 +143,22 @@ void main_cnn(int argc, const char **argv)
/* [Configure functions] */
// in:32x32x1: 5x5 convolution, 8 output features maps (OFM)
- conv0.configure(&src, &weights0, &biases0, &out_conv0, PadStrideInfo());
+ conv0.configure(&src, &weights0, &biases0, &out_conv0, PadStrideInfo(1 /* stride_x */, 1 /* stride_y */, 2 /* pad_x */, 2 /* pad_y */));
// in:32x32x8, out:32x32x8, Activation function: relu
act0.configure(&out_conv0, &out_act0, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
// in:32x32x8, out:16x16x8 (2x2 pooling), Pool type function: Max
- pool0.configure(&out_act0, &out_pool0, PoolingLayerInfo(PoolingType::MAX, 2));
+ pool0.configure(&out_act0, &out_pool0, PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */)));
// in:16x16x8: 3x3 convolution, 16 output features maps (OFM)
- conv1.configure(&out_pool0, &weights1, &biases1, &out_conv1, PadStrideInfo());
+ conv1.configure(&out_pool0, &weights1, &biases1, &out_conv1, PadStrideInfo(1 /* stride_x */, 1 /* stride_y */, 1 /* pad_x */, 1 /* pad_y */));
// in:16x16x16, out:16x16x16, Activation function: relu
act1.configure(&out_conv1, &out_act1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
// in:16x16x16, out:8x8x16 (2x2 pooling), Pool type function: Average
- pool1.configure(&out_act1, &out_pool1, PoolingLayerInfo(PoolingType::AVG, 2));
+ pool1.configure(&out_act1, &out_pool1, PoolingLayerInfo(PoolingType::AVG, 2, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */)));
// in:8x8x16, out:128
fc0.configure(&out_pool1, &weights2, &biases2, &out_fc0);
diff --git a/examples/neon_copy_objects.cpp b/examples/neon_copy_objects.cpp
index 191f45555..04024530d 100644
--- a/examples/neon_copy_objects.cpp
+++ b/examples/neon_copy_objects.cpp
@@ -75,7 +75,7 @@ void main_neon_copy_objects(int argc, const char **argv)
// Fill the input tensor:
// Simplest way: create an iterator to iterate through each element of the input tensor:
Window input_window;
- input_window.use_tensor_dimensions(input.info());
+ input_window.use_tensor_dimensions(input.info()->tensor_shape());
std::cout << " Dimensions of the input's iterator:\n";
std::cout << " X = [start=" << input_window.x().start() << ", end=" << input_window.x().end() << ", step=" << input_window.x().step() << "]\n";
std::cout << " Y = [start=" << input_window.y().start() << ", end=" << input_window.y().end() << ", step=" << input_window.y().step() << "]\n";
@@ -109,7 +109,7 @@ void main_neon_copy_objects(int argc, const char **argv)
// More efficient way: create an iterator to iterate through each row (instead of each element) of the output tensor:
Window output_window;
- output_window.use_tensor_dimensions(output.info(), /* first_dimension =*/Window::DimY); // Iterate through the rows (not each element)
+ output_window.use_tensor_dimensions(output.info()->tensor_shape(), /* first_dimension =*/Window::DimY); // Iterate through the rows (not each element)
std::cout << " Dimensions of the output's iterator:\n";
std::cout << " X = [start=" << output_window.x().start() << ", end=" << output_window.x().end() << ", step=" << output_window.x().step() << "]\n";
std::cout << " Y = [start=" << output_window.y().start() << ", end=" << output_window.y().end() << ", step=" << output_window.y().step() << "]\n";
diff --git a/examples/neoncl_scale_median_gaussian.cpp b/examples/neoncl_scale_median_gaussian.cpp
index a32ba6daf..e53a48e07 100644
--- a/examples/neoncl_scale_median_gaussian.cpp
+++ b/examples/neoncl_scale_median_gaussian.cpp
@@ -21,7 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#define ARM_COMPUTE_CL /* So that OpenCL exceptions get caught too */
+#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
+#error "This example needs to be built with -DARM_COMPUTE_CL"
+#endif /* ARM_COMPUTE_CL */
+
#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/CL/CLFunctions.h"
#include "arm_compute/runtime/CL/CLScheduler.h"