Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/graph.h" |
| 25 | #include "support/ToolchainSupport.h" |
| 26 | #include "utils/CommonGraphOptions.h" |
| 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | using namespace arm_compute::utils; |
| 32 | using namespace arm_compute::graph::frontend; |
| 33 | using namespace arm_compute::graph_utils; |
| 34 | |
| 35 | /** Example demonstrating how to implement MobileNetV2's network using the Compute Library's graph API */ |
| 36 | class GraphMobilenetV2Example : public Example |
| 37 | { |
| 38 | public: |
| 39 | GraphMobilenetV2Example() |
| 40 | : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV2") |
| 41 | { |
| 42 | } |
| 43 | GraphMobilenetV2Example(const GraphMobilenetV2Example &) = delete; |
| 44 | GraphMobilenetV2Example &operator=(const GraphMobilenetV2Example &) = delete; |
| 45 | GraphMobilenetV2Example(GraphMobilenetV2Example &&) = default; // NOLINT |
| 46 | GraphMobilenetV2Example &operator=(GraphMobilenetV2Example &&) = default; // NOLINT |
| 47 | ~GraphMobilenetV2Example() override = default; |
| 48 | |
| 49 | bool do_setup(int argc, char **argv) override |
| 50 | { |
| 51 | // Parse arguments |
| 52 | cmd_parser.parse(argc, argv); |
| 53 | |
| 54 | // Consume common parameters |
| 55 | common_params = consume_common_graph_parameters(common_opts); |
| 56 | |
| 57 | // Return when help menu is requested |
| 58 | if(common_params.help) |
| 59 | { |
| 60 | cmd_parser.print_help(argv[0]); |
| 61 | return false; |
| 62 | } |
| 63 | |
Anthony Barbier | cdd68c0 | 2018-08-23 15:03:41 +0100 | [diff] [blame] | 64 | // Checks |
| 65 | ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); |
| 66 | ARM_COMPUTE_EXIT_ON_MSG(common_params.data_type == DataType::F16 && common_params.target == Target::NEON, "F16 NEON not supported for this graph"); |
| 67 | |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 68 | // Print parameter values |
| 69 | std::cout << common_params << std::endl; |
| 70 | |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 71 | // Create model path |
| 72 | std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_model/"; |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 73 | |
| 74 | // Create input descriptor |
| 75 | const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout); |
| 76 | TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
| 77 | |
| 78 | // Create a preprocessor object |
| 79 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>(); |
| 80 | |
| 81 | // Get trainable parameters data path |
| 82 | std::string data_path = common_params.data_path; |
| 83 | |
| 84 | // Add model path to data path |
| 85 | if(!data_path.empty()) |
| 86 | { |
| 87 | data_path += model_path; |
| 88 | } |
| 89 | |
| 90 | // Create graph |
| 91 | graph << common_params.target |
| 92 | << DepthwiseConvolutionMethod::Optimized3x3 // FIXME(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method |
| 93 | << common_params.fast_math_hint |
| 94 | << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false)) |
| 95 | << ConvolutionLayer(3U, 3U, 32U, |
| 96 | get_weights_accessor(data_path, "Conv_weights.npy", DataLayout::NCHW), |
| 97 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 98 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL)) |
| 99 | .set_name("Conv") |
| 100 | << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_BatchNorm_moving_mean.npy"), |
| 101 | get_weights_accessor(data_path, "Conv_BatchNorm_moving_variance.npy"), |
| 102 | get_weights_accessor(data_path, "Conv_BatchNorm_gamma.npy"), |
| 103 | get_weights_accessor(data_path, "Conv_BatchNorm_beta.npy"), |
| 104 | 0.0010000000474974513f) |
| 105 | .set_name("Conv/BatchNorm") |
| 106 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
| 107 | .set_name("Conv/Relu6"); |
| 108 | |
| 109 | get_expanded_conv(data_path, "expanded_conv", 32U, 16U, PadStrideInfo(1, 1, 1, 1)); |
| 110 | get_expanded_conv(data_path, "expanded_conv_1", 16U, 24U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true); |
| 111 | get_expanded_conv(data_path, "expanded_conv_2", 24U, 24U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 112 | get_expanded_conv(data_path, "expanded_conv_3", 24U, 32U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true); |
| 113 | get_expanded_conv(data_path, "expanded_conv_4", 32U, 32U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 114 | get_expanded_conv(data_path, "expanded_conv_5", 32U, 32U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 115 | get_expanded_conv(data_path, "expanded_conv_6", 32U, 64U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true); |
| 116 | get_expanded_conv(data_path, "expanded_conv_7", 64U, 64U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 117 | get_expanded_conv(data_path, "expanded_conv_8", 64U, 64U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 118 | get_expanded_conv(data_path, "expanded_conv_9", 64U, 64U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 119 | get_expanded_conv(data_path, "expanded_conv_10", 64U, 96U, PadStrideInfo(1, 1, 1, 1), true); |
| 120 | get_expanded_conv(data_path, "expanded_conv_11", 96U, 96U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 121 | get_expanded_conv(data_path, "expanded_conv_12", 96U, 96U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 122 | get_expanded_conv(data_path, "expanded_conv_13", 96U, 160U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true); |
| 123 | get_expanded_conv(data_path, "expanded_conv_14", 160U, 160U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 124 | get_expanded_conv(data_path, "expanded_conv_15", 160U, 160U, PadStrideInfo(1, 1, 1, 1), true, true); |
| 125 | get_expanded_conv(data_path, "expanded_conv_16", 160U, 320U, PadStrideInfo(1, 1, 1, 1), true); |
| 126 | |
| 127 | graph << ConvolutionLayer(1U, 1U, 1280U, |
| 128 | get_weights_accessor(data_path, "Conv_1_weights.npy", DataLayout::NCHW), |
| 129 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 130 | PadStrideInfo(1, 1, 0, 0)) |
| 131 | .set_name("Conv_1") |
| 132 | << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_mean.npy"), |
| 133 | get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_variance.npy"), |
| 134 | get_weights_accessor(data_path, "Conv_1_BatchNorm_gamma.npy"), |
| 135 | get_weights_accessor(data_path, "Conv_1_BatchNorm_beta.npy"), |
| 136 | 0.0010000000474974513f) |
| 137 | .set_name("Conv_1/BatchNorm") |
| 138 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
| 139 | .set_name("Conv_1/Relu6") |
| 140 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool") |
| 141 | << ConvolutionLayer(1U, 1U, 1001U, |
| 142 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW), |
| 143 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"), |
| 144 | PadStrideInfo(1, 1, 0, 0)) |
| 145 | .set_name("Logits/Conv2d_1c_1x1") |
| 146 | << ReshapeLayer(TensorShape(1001U)).set_name("Predictions/Reshape") |
| 147 | << SoftmaxLayer().set_name("Predictions/Softmax") |
| 148 | << OutputLayer(get_output_accessor(common_params, 5)); |
| 149 | |
| 150 | // Finalize graph |
| 151 | GraphConfig config; |
| 152 | config.num_threads = common_params.threads; |
| 153 | config.use_tuner = common_params.enable_tuner; |
| 154 | config.tuner_file = common_params.tuner_file; |
| 155 | |
| 156 | graph.finalize(common_params.target, config); |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | void do_run() override |
| 162 | { |
| 163 | // Run graph |
| 164 | graph.run(); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | CommandLineParser cmd_parser; |
| 169 | CommonGraphOptions common_opts; |
| 170 | CommonGraphParams common_params; |
| 171 | Stream graph; |
| 172 | |
| 173 | void get_expanded_conv(const std::string &data_path, std::string &¶m_path, |
| 174 | unsigned int input_channels, unsigned int output_channels, |
| 175 | PadStrideInfo dwc_pad_stride_info, |
| 176 | bool has_expand = false, bool is_residual = false, unsigned int expansion_size = 6) |
| 177 | { |
| 178 | std::string total_path = param_path + "_"; |
| 179 | SubStream left(graph); |
| 180 | |
| 181 | // Add expand node |
| 182 | if(has_expand) |
| 183 | { |
| 184 | left << ConvolutionLayer(1U, 1U, input_channels * expansion_size, |
| 185 | get_weights_accessor(data_path, total_path + "expand_weights.npy", DataLayout::NCHW), |
| 186 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0)) |
| 187 | .set_name(param_path + "/expand/Conv2D") |
| 188 | << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_mean.npy"), |
| 189 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_variance.npy"), |
| 190 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_gamma.npy"), |
| 191 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_beta.npy"), |
| 192 | 0.0010000000474974513f) |
| 193 | .set_name(param_path + "/expand/BatchNorm") |
| 194 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
| 195 | .set_name(param_path + "/expand/Relu6"); |
| 196 | } |
| 197 | |
| 198 | // Add depthwise node |
| 199 | left << DepthwiseConvolutionLayer(3U, 3U, |
| 200 | get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW), |
| 201 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 202 | dwc_pad_stride_info) |
| 203 | .set_name(param_path + "/depthwise/depthwise") |
| 204 | << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"), |
| 205 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"), |
| 206 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"), |
| 207 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"), |
| 208 | 0.0010000000474974513f) |
| 209 | .set_name(param_path + "/depthwise/BatchNorm") |
| 210 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
| 211 | .set_name(param_path + "/depthwise/Relu6"); |
| 212 | |
| 213 | // Add project node |
| 214 | left << ConvolutionLayer(1U, 1U, output_channels, |
| 215 | get_weights_accessor(data_path, total_path + "project_weights.npy", DataLayout::NCHW), |
| 216 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0)) |
| 217 | .set_name(param_path + "/project/Conv2D") |
| 218 | << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_mean.npy"), |
| 219 | get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_variance.npy"), |
| 220 | get_weights_accessor(data_path, total_path + "project_BatchNorm_gamma.npy"), |
| 221 | get_weights_accessor(data_path, total_path + "project_BatchNorm_beta.npy"), |
| 222 | 0.0010000000474974513) |
| 223 | .set_name(param_path + "/project/BatchNorm"); |
| 224 | |
| 225 | if(is_residual) |
| 226 | { |
| 227 | // Add residual node |
| 228 | SubStream right(graph); |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 229 | graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add"); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 230 | } |
| 231 | else |
| 232 | { |
| 233 | graph.forward_tail(left.tail_node()); |
| 234 | } |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | /** Main program for MobileNetV2 |
| 239 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame^] | 240 | * Model is based on: |
| 241 | * https://arxiv.org/abs/1801.04381 |
| 242 | * "MobileNetV2: Inverted Residuals and Linear Bottlenecks" |
| 243 | * Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen |
| 244 | * |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 245 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 246 | * |
| 247 | * @param[in] argc Number of arguments |
| 248 | * @param[in] argv Arguments |
| 249 | */ |
| 250 | int main(int argc, char **argv) |
| 251 | { |
| 252 | return arm_compute::utils::run_example<GraphMobilenetV2Example>(argc, argv); |
| 253 | } |