From 108ab0bc0395b558a96951f41d7580d308708bc5 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Fri, 14 Sep 2018 18:35:11 +0100 Subject: COMPMID-1578: Add ShuffleNet graph example Change-Id: I0e577568b74cf5b2285e6f8c51f8403c9d5f78d2 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/148747 Tested-by: bsgcomp Reviewed-by: Michalis Spyrou --- examples/graph_alexnet.cpp | 6 +- examples/graph_cl_shufflenet.cpp | 251 +++++++++++++++++++++++++++++++++ examples/graph_googlenet.cpp | 6 +- examples/graph_inception_resnet_v2.cpp | 6 +- examples/graph_inception_v3.cpp | 6 +- examples/graph_inception_v4.cpp | 6 +- examples/graph_lenet.cpp | 6 +- examples/graph_mobilenet.cpp | 6 +- examples/graph_resnet50.cpp | 6 +- examples/graph_resnet_v2_50.cpp | 6 +- examples/graph_resnext50.cpp | 6 +- examples/graph_squeezenet.cpp | 6 +- examples/graph_squeezenet_v1_1.cpp | 6 +- examples/graph_vgg16.cpp | 6 +- examples/graph_vgg19.cpp | 6 +- examples/graph_yolov3.cpp | 6 +- 16 files changed, 266 insertions(+), 75 deletions(-) create mode 100644 examples/graph_cl_shufflenet.cpp diff --git a/examples/graph_alexnet.cpp b/examples/graph_alexnet.cpp index 124f672e72..97788d8cd9 100644 --- a/examples/graph_alexnet.cpp +++ b/examples/graph_alexnet.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API */ class GraphAlexnetExample : public Example { public: diff --git a/examples/graph_cl_shufflenet.cpp b/examples/graph_cl_shufflenet.cpp new file mode 100644 index 0000000000..70695bac56 --- /dev/null +++ b/examples/graph_cl_shufflenet.cpp @@ -0,0 +1,251 @@ +/* + * Copyright (c) 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.h" +#include "support/ToolchainSupport.h" +#include "utils/CommonGraphOptions.h" +#include "utils/GraphUtils.h" +#include "utils/Utils.h" + +using namespace arm_compute::utils; +using namespace arm_compute::graph::frontend; +using namespace arm_compute::graph_utils; + +/** Example demonstrating how to implement ShuffleNet network using the Compute Library's graph API */ +class ShuffleNetExample : public Example +{ +public: + ShuffleNetExample() + : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ShuffleNet") + { + } + bool do_setup(int argc, char **argv) override + { + // Parse arguments + cmd_parser.parse(argc, argv); + + // Consume common parameters + common_params = consume_common_graph_parameters(common_opts); + + // Return when help menu is requested + if(common_params.help) + { + cmd_parser.print_help(argv[0]); + return false; + } + + // Set default layout if needed (Single kernel grouped convolution not yet supported int NHWC) + if(!common_opts.data_layout->is_set()) + { + common_params.data_layout = DataLayout::NCHW; + } + + // Checks + ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); + ARM_COMPUTE_EXIT_ON_MSG(common_params.target != Target::CL, "Only CL backend is supported for this network"); + + // Print parameter values + std::cout << common_params << std::endl; + std::cout << "Model: Shufflenet_1_g4" << std::endl; + + // Create model path + std::string model_path = "/cnn_data/shufflenet_model/"; + + // Get trainable parameters data path + std::string data_path = common_params.data_path; + + // Add model path to data path + if(!data_path.empty()) + { + data_path += model_path; + } + + // Create input descriptor + const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout); + TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); + + // Set weights trained layout + const DataLayout weights_layout = DataLayout::NCHW; + + // Create preprocessor + std::unique_ptr preprocessor = arm_compute::support::cpp14::make_unique(0); + + graph << common_params.target + << common_params.fast_math_hint + << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */)) + << ConvolutionLayer( + 3U, 3U, 24U, + get_weights_accessor(data_path, "conv3_0_w_0.npy", weights_layout), + get_weights_accessor(data_path, "conv3_0_b_0.npy", weights_layout), + PadStrideInfo(2, 2, 1, 1)) + .set_name("Conv1/convolution") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "conv3_0_bn_rm_0.npy"), + get_weights_accessor(data_path, "conv3_0_bn_riv_0.npy"), + get_weights_accessor(data_path, "conv3_0_bn_s_0.npy"), + get_weights_accessor(data_path, "conv3_0_bn_b_0.npy"), + 1e-5f) + .set_name("Conv1/BatchNorm") + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv1/Relu") + << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 1, 1))).set_name("pool1/MaxPool"); + + // Stage 2 + add_residual_block(data_path, DataLayout::NCHW, 0U /* unit */, 112U /* depth */, 2U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 1U /* unit */, 136U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 2U /* unit */, 136U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 3U /* unit */, 136U /* depth */, 1U /* stride */); + + // Stage 3 + add_residual_block(data_path, DataLayout::NCHW, 4U /* unit */, 136U /* depth */, 2U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 5U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 6U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 7U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 8U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 9U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 10U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 11U /* unit */, 272U /* depth */, 1U /* stride */); + + // Stage 4 + add_residual_block(data_path, DataLayout::NCHW, 12U /* unit */, 272U /* depth */, 2U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 13U /* unit */, 544U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 14U /* unit */, 544U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 15U /* unit */, 544U /* depth */, 1U /* stride */); + + graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("predictions/AvgPool") + << FlattenLayer().set_name("predictions/Reshape") + << FullyConnectedLayer( + 1000U, + get_weights_accessor(data_path, "pred_w_0.npy", weights_layout), + get_weights_accessor(data_path, "pred_b_0.npy")) + .set_name("predictions/FC") + << SoftmaxLayer().set_name("predictions/Softmax") + << OutputLayer(get_output_accessor(common_params, 5)); + + // Finalize graph + GraphConfig config; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + + graph.finalize(common_params.target, config); + + return true; + } + + void do_run() override + { + // Run graph + graph.run(); + } + +private: + CommandLineParser cmd_parser; + CommonGraphOptions common_opts; + CommonGraphParams common_params; + Stream graph; + + void add_residual_block(const std::string &data_path, DataLayout weights_layout, + unsigned int unit, unsigned int depth, unsigned int stride) + { + PadStrideInfo dwc_info = PadStrideInfo(1, 1, 1, 1); + const unsigned int gconv_id = unit * 2; + const unsigned int num_groups = 4; + const std::string unit_id_name = arm_compute::support::cpp11::to_string(unit); + const std::string gconv_id_name = arm_compute::support::cpp11::to_string(gconv_id); + const std::string gconv_id_1_name = arm_compute::support::cpp11::to_string(gconv_id + 1); + const std::string unit_name = "unit" + unit_id_name; + + SubStream left_ss(graph); + SubStream right_ss(graph); + + if(stride == 2) + { + right_ss << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(2, 2, 1, 1))).set_name(unit_name + "/pool_1/AveragePool"); + dwc_info = PadStrideInfo(2, 2, 1, 1); + } + + left_ss << ConvolutionLayer( + 1U, 1U, depth, + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_w_0.npy", weights_layout), + std::unique_ptr(nullptr), + PadStrideInfo(1, 1, 0, 0), num_groups) + .set_name(unit_name + "/gconv1_" + gconv_id_name + "/convolution") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_rm_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_riv_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_s_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_b_0.npy"), + 1e-5f) + .set_name(unit_name + "/gconv1_" + gconv_id_name + "/BatchNorm") + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/gconv1_" + gconv_id_name + "/Relu") + << ChannelShuffleLayer(num_groups).set_name(unit_name + "/shuffle_0/ChannelShufle") + << DepthwiseConvolutionLayer( + 3U, 3U, + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_w_0.npy", weights_layout), + std::unique_ptr(nullptr), + dwc_info) + .set_name(unit_name + "/gconv3_" + unit_id_name + "/depthwise") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_rm_0.npy"), + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_riv_0.npy"), + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_s_0.npy"), + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_b_0.npy"), + 1e-5f) + .set_name(unit_name + "/gconv3_" + unit_id_name + "/BatchNorm") + << ConvolutionLayer( + 1U, 1U, depth, + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_w_0.npy", weights_layout), + std::unique_ptr(nullptr), + PadStrideInfo(1, 1, 0, 0), num_groups) + .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/convolution") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_rm_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_riv_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_s_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_b_0.npy"), + 1e-5f) + .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/BatchNorm"); + + if(stride == 2) + { + graph << ConcatLayer(std::move(left_ss), std::move(right_ss)).set_name(unit_name + "/Concat"); + } + else + { + graph << EltwiseLayer(std::move(left_ss), std::move(right_ss), EltwiseOperation::Add).set_name(unit_name + "/Add"); + } + graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/Relu"); + } +}; + +/** Main program for ShuffleNet + * + * @note To list all the possible arguments execute the binary appended with the --help option + * + * @param[in] argc Number of arguments + * @param[in] argv Arguments + */ +int main(int argc, char **argv) +{ + return arm_compute::utils::run_example(argc, argv); +} diff --git a/examples/graph_googlenet.cpp b/examples/graph_googlenet.cpp index f6aad5d3e8..c0954f775c 100644 --- a/examples/graph_googlenet.cpp +++ b/examples/graph_googlenet.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API */ class GraphGooglenetExample : public Example { public: diff --git a/examples/graph_inception_resnet_v2.cpp b/examples/graph_inception_resnet_v2.cpp index 150de7dcc9..7e9fd412af 100644 --- a/examples/graph_inception_resnet_v2.cpp +++ b/examples/graph_inception_resnet_v2.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement InceptionV4's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement InceptionV4's network using the Compute Library's graph API */ class InceptionResNetV2Example final : public Example { public: diff --git a/examples/graph_inception_v3.cpp b/examples/graph_inception_v3.cpp index 80e771b135..d51be62f9f 100644 --- a/examples/graph_inception_v3.cpp +++ b/examples/graph_inception_v3.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement InceptionV3's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement InceptionV3's network using the Compute Library's graph API */ class InceptionV3Example : public Example { public: diff --git a/examples/graph_inception_v4.cpp b/examples/graph_inception_v4.cpp index 00a6c7d781..b64f176c17 100644 --- a/examples/graph_inception_v4.cpp +++ b/examples/graph_inception_v4.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement InceptionV4's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement InceptionV4's network using the Compute Library's graph API */ class InceptionV4Example final : public Example { public: diff --git a/examples/graph_lenet.cpp b/examples/graph_lenet.cpp index 6b9f302312..79cf12233e 100644 --- a/examples/graph_lenet.cpp +++ b/examples/graph_lenet.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** 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] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) - */ +/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API */ class GraphLenetExample : public Example { public: diff --git a/examples/graph_mobilenet.cpp b/examples/graph_mobilenet.cpp index ab6a4a842f..7a6f9813a6 100644 --- a/examples/graph_mobilenet.cpp +++ b/examples/graph_mobilenet.cpp @@ -32,11 +32,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API */ class GraphMobilenetExample : public Example { public: diff --git a/examples/graph_resnet50.cpp b/examples/graph_resnet50.cpp index 5b6a4815a1..f13383e5b7 100644 --- a/examples/graph_resnet50.cpp +++ b/examples/graph_resnet50.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement ResNetV1_50 network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement ResNetV1_50 network using the Compute Library's graph API */ class GraphResNetV1_50Example : public Example { public: diff --git a/examples/graph_resnet_v2_50.cpp b/examples/graph_resnet_v2_50.cpp index 03c21ac719..b2c85e8938 100644 --- a/examples/graph_resnet_v2_50.cpp +++ b/examples/graph_resnet_v2_50.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement ResNetV2_50 network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement ResNetV2_50 network using the Compute Library's graph API */ class GraphResNetV2_50Example : public Example { public: diff --git a/examples/graph_resnext50.cpp b/examples/graph_resnext50.cpp index c369fa91bd..a52e18111c 100644 --- a/examples/graph_resnext50.cpp +++ b/examples/graph_resnext50.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API */ class GraphResNeXt50Example : public Example { public: diff --git a/examples/graph_squeezenet.cpp b/examples/graph_squeezenet.cpp index cee0ffb43d..ddb4474aac 100644 --- a/examples/graph_squeezenet.cpp +++ b/examples/graph_squeezenet.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API */ class GraphSqueezenetExample : public Example { public: diff --git a/examples/graph_squeezenet_v1_1.cpp b/examples/graph_squeezenet_v1_1.cpp index 013664da10..de3d4e88c3 100644 --- a/examples/graph_squeezenet_v1_1.cpp +++ b/examples/graph_squeezenet_v1_1.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement Squeezenet's v1.1 network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement Squeezenet's v1.1 network using the Compute Library's graph API */ class GraphSqueezenet_v1_1Example : public Example { public: diff --git a/examples/graph_vgg16.cpp b/examples/graph_vgg16.cpp index 69b3a9d955..8e1e621cdc 100644 --- a/examples/graph_vgg16.cpp +++ b/examples/graph_vgg16.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API */ class GraphVGG16Example : public Example { public: diff --git a/examples/graph_vgg19.cpp b/examples/graph_vgg19.cpp index 8d64c1cdbb..6127c745ea 100644 --- a/examples/graph_vgg19.cpp +++ b/examples/graph_vgg19.cpp @@ -30,11 +30,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement VGG19's network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement VGG19's network using the Compute Library's graph API */ class GraphVGG19Example : public Example { public: diff --git a/examples/graph_yolov3.cpp b/examples/graph_yolov3.cpp index 24186d54ea..098855e280 100644 --- a/examples/graph_yolov3.cpp +++ b/examples/graph_yolov3.cpp @@ -31,11 +31,7 @@ using namespace arm_compute::utils; using namespace arm_compute::graph::frontend; using namespace arm_compute::graph_utils; -/** Example demonstrating how to implement YOLOv3 network using the Compute Library's graph API - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ +/** Example demonstrating how to implement YOLOv3 network using the Compute Library's graph API */ class GraphYOLOv3Example : public Example { public: -- cgit v1.2.1