Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 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 | */ |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/Graph.h" |
| 25 | #include "arm_compute/graph/Nodes.h" |
| 26 | #include "arm_compute/graph/SubGraph.h" |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 27 | #include "support/ToolchainSupport.h" |
| 28 | #include "utils/GraphUtils.h" |
| 29 | #include "utils/Utils.h" |
| 30 | |
| 31 | #include <cstdlib> |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 32 | #include <tuple> |
| 33 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 34 | using namespace arm_compute::utils; |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 35 | using namespace arm_compute::graph; |
| 36 | using namespace arm_compute::graph_utils; |
Isabella Gottardi | 4398bec | 2017-10-19 16:10:59 +0100 | [diff] [blame] | 37 | using namespace arm_compute::logging; |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 38 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 39 | namespace |
| 40 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 41 | } // namespace |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 42 | |
| 43 | /** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API |
| 44 | * |
| 45 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 46 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 47 | */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 48 | class GraphSqueezenetExample : public Example |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 49 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 50 | public: |
| 51 | void do_setup(int argc, char **argv) override |
| 52 | { |
| 53 | std::string data_path; /* Path to the trainable data */ |
| 54 | std::string image; /* Image data */ |
| 55 | std::string label; /* Label data */ |
Isabella Gottardi | 97988a4 | 2017-11-03 14:39:44 +0000 | [diff] [blame] | 56 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 57 | constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */ |
| 58 | constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */ |
| 59 | constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */ |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 60 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 61 | // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 62 | TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0); |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 63 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 64 | // Parse arguments |
| 65 | if(argc < 2) |
| 66 | { |
| 67 | // Print help |
| 68 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
| 69 | std::cout << "No data folder provided: using random values\n\n"; |
| 70 | } |
| 71 | else if(argc == 2) |
| 72 | { |
| 73 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 74 | std::cout << "No data folder provided: using random values\n\n"; |
| 75 | } |
| 76 | else if(argc == 3) |
| 77 | { |
| 78 | data_path = argv[2]; |
| 79 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 80 | std::cout << "No image provided: using random values\n\n"; |
| 81 | } |
| 82 | else if(argc == 4) |
| 83 | { |
| 84 | data_path = argv[2]; |
| 85 | image = argv[3]; |
| 86 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
| 87 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | data_path = argv[2]; |
| 92 | image = argv[3]; |
| 93 | label = argv[4]; |
| 94 | } |
| 95 | |
| 96 | graph << target_hint |
| 97 | << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32), |
| 98 | get_input_accessor(image, mean_r, mean_g, mean_b)) |
| 99 | << ConvolutionLayer( |
| 100 | 7U, 7U, 96U, |
| 101 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"), |
| 102 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"), |
| 103 | PadStrideInfo(2, 2, 0, 0)) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 104 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 105 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 106 | << ConvolutionLayer( |
| 107 | 1U, 1U, 16U, |
| 108 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"), |
| 109 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"), |
| 110 | PadStrideInfo(1, 1, 0, 0)) |
| 111 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 112 | << get_expand_fire_node(data_path, "fire2", 64U, 64U) |
| 113 | << ConvolutionLayer( |
| 114 | 1U, 1U, 16U, |
| 115 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"), |
| 116 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"), |
| 117 | PadStrideInfo(1, 1, 0, 0)) |
| 118 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 119 | << get_expand_fire_node(data_path, "fire3", 64U, 64U) |
| 120 | << ConvolutionLayer( |
| 121 | 1U, 1U, 32U, |
| 122 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"), |
| 123 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"), |
| 124 | PadStrideInfo(1, 1, 0, 0)) |
| 125 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 126 | << get_expand_fire_node(data_path, "fire4", 128U, 128U) |
| 127 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 128 | << ConvolutionLayer( |
| 129 | 1U, 1U, 32U, |
| 130 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"), |
| 131 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"), |
| 132 | PadStrideInfo(1, 1, 0, 0)) |
| 133 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 134 | << get_expand_fire_node(data_path, "fire5", 128U, 128U) |
| 135 | << ConvolutionLayer( |
| 136 | 1U, 1U, 48U, |
| 137 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"), |
| 138 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"), |
| 139 | PadStrideInfo(1, 1, 0, 0)) |
| 140 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 141 | << get_expand_fire_node(data_path, "fire6", 192U, 192U) |
| 142 | << ConvolutionLayer( |
| 143 | 1U, 1U, 48U, |
| 144 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"), |
| 145 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"), |
| 146 | PadStrideInfo(1, 1, 0, 0)) |
| 147 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 148 | << get_expand_fire_node(data_path, "fire7", 192U, 192U) |
| 149 | << ConvolutionLayer( |
| 150 | 1U, 1U, 64U, |
| 151 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"), |
| 152 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"), |
| 153 | PadStrideInfo(1, 1, 0, 0)) |
| 154 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 155 | << get_expand_fire_node(data_path, "fire8", 256U, 256U) |
| 156 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 157 | << ConvolutionLayer( |
| 158 | 1U, 1U, 64U, |
| 159 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"), |
| 160 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"), |
| 161 | PadStrideInfo(1, 1, 0, 0)) |
| 162 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 163 | << get_expand_fire_node(data_path, "fire9", 256U, 256U) |
| 164 | << ConvolutionLayer( |
| 165 | 1U, 1U, 1000U, |
| 166 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"), |
| 167 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"), |
| 168 | PadStrideInfo(1, 1, 0, 0)) |
| 169 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 170 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)) |
| 171 | << FlattenLayer() |
| 172 | << SoftmaxLayer() |
| 173 | << Tensor(get_output_accessor(label, 5)); |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 174 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 175 | void do_run() override |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 176 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 177 | // Run graph |
| 178 | graph.run(); |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 181 | private: |
| 182 | Graph graph{}; |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 183 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 184 | BranchLayer get_expand_fire_node(const std::string &data_path, std::string &¶m_path, unsigned int expand1_filt, unsigned int expand3_filt) |
| 185 | { |
| 186 | std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_"; |
| 187 | SubGraph i_a; |
| 188 | i_a << ConvolutionLayer( |
| 189 | 1U, 1U, expand1_filt, |
| 190 | get_weights_accessor(data_path, total_path + "expand1x1_w.npy"), |
| 191 | get_weights_accessor(data_path, total_path + "expand1x1_b.npy"), |
| 192 | PadStrideInfo(1, 1, 0, 0)) |
| 193 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 194 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 195 | SubGraph i_b; |
| 196 | i_b << ConvolutionLayer( |
| 197 | 3U, 3U, expand3_filt, |
| 198 | get_weights_accessor(data_path, total_path + "expand3x3_w.npy"), |
| 199 | get_weights_accessor(data_path, total_path + "expand3x3_b.npy"), |
| 200 | PadStrideInfo(1, 1, 1, 1)) |
| 201 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 202 | |
| 203 | return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b)); |
| 204 | } |
| 205 | }; |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 206 | |
| 207 | /** Main program for Squeezenet v1.0 |
| 208 | * |
| 209 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 210 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 211 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 212 | int main(int argc, char **argv) |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 213 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 214 | return arm_compute::utils::run_example<GraphSqueezenetExample>(argc, argv); |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 215 | } |