Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */ |
| 25 | #error "This example needs to be built with -DARM_COMPUTE_CL" |
| 26 | #endif /* ARM_COMPUTE_CL */ |
| 27 | |
Isabella Gottardi | 4398bec | 2017-10-19 16:10:59 +0100 | [diff] [blame^] | 28 | #include "arm_compute/core/utils/logging/LoggerRegistry.h" |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/Graph.h" |
| 30 | #include "arm_compute/graph/Nodes.h" |
| 31 | #include "arm_compute/graph/SubGraph.h" |
| 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | #include "arm_compute/runtime/Scheduler.h" |
| 34 | #include "support/ToolchainSupport.h" |
| 35 | #include "utils/GraphUtils.h" |
| 36 | #include "utils/Utils.h" |
| 37 | |
| 38 | #include <cstdlib> |
| 39 | #include <iostream> |
| 40 | #include <memory> |
| 41 | #include <tuple> |
| 42 | |
| 43 | using namespace arm_compute::graph; |
| 44 | using namespace arm_compute::graph_utils; |
Isabella Gottardi | 4398bec | 2017-10-19 16:10:59 +0100 | [diff] [blame^] | 45 | using namespace arm_compute::logging; |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 46 | |
| 47 | /** Generates appropriate accessor according to the specified path |
| 48 | * |
| 49 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 50 | * |
| 51 | * @param path Path to the data files |
| 52 | * @param data_file Relative path to the data files from path |
| 53 | * |
| 54 | * @return An appropriate tensor accessor |
| 55 | */ |
| 56 | std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file) |
| 57 | { |
| 58 | if(path.empty()) |
| 59 | { |
| 60 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | BranchLayer get_expand_fire_node(const std::string &data_path, std::string &¶m_path, unsigned int expand1_filt, unsigned int expand3_filt) |
| 69 | { |
| 70 | std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_"; |
| 71 | SubGraph i_a; |
| 72 | i_a << ConvolutionLayer( |
| 73 | 1U, 1U, expand1_filt, |
| 74 | get_accessor(data_path, total_path + "expand1x1_w.npy"), |
| 75 | get_accessor(data_path, total_path + "expand1x1_b.npy"), |
| 76 | PadStrideInfo(1, 1, 0, 0)) |
| 77 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 78 | |
| 79 | SubGraph i_b; |
| 80 | i_b << ConvolutionLayer( |
| 81 | 3U, 3U, expand3_filt, |
| 82 | get_accessor(data_path, total_path + "expand3x3_w.npy"), |
| 83 | get_accessor(data_path, total_path + "expand3x3_b.npy"), |
| 84 | PadStrideInfo(1, 1, 1, 1)) |
| 85 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 86 | |
| 87 | return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b)); |
| 88 | } |
| 89 | |
| 90 | /** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API |
| 91 | * |
| 92 | * @param[in] argc Number of arguments |
| 93 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches ) |
| 94 | */ |
| 95 | void main_graph_squeezenet(int argc, const char **argv) |
| 96 | { |
| 97 | std::string data_path; /** Path to the trainable data */ |
| 98 | unsigned int batches = 4; /** Number of batches */ |
| 99 | |
| 100 | // Parse arguments |
| 101 | if(argc < 2) |
| 102 | { |
| 103 | // Print help |
| 104 | std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n"; |
| 105 | std::cout << "No data folder provided: using random values\n\n"; |
| 106 | } |
| 107 | else if(argc == 2) |
| 108 | { |
| 109 | //Do something with argv[1] |
| 110 | data_path = argv[1]; |
| 111 | std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n"; |
| 112 | std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n"; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | //Do something with argv[1] and argv[2] |
| 117 | data_path = argv[1]; |
| 118 | batches = std::strtol(argv[2], nullptr, 0); |
| 119 | } |
| 120 | |
| 121 | // Check if OpenCL is available and initialize the scheduler |
| 122 | if(arm_compute::opencl_is_available()) |
| 123 | { |
| 124 | arm_compute::CLScheduler::get().default_init(); |
| 125 | } |
| 126 | |
| 127 | Graph graph; |
Isabella Gottardi | 4398bec | 2017-10-19 16:10:59 +0100 | [diff] [blame^] | 128 | LoggerRegistry::get().create_reserved_loggers(LogLevel::INFO, { std::make_shared<StdPrinter>() }); |
Georgios Pinitas | 3756186 | 2017-10-19 10:51:03 +0100 | [diff] [blame] | 129 | |
| 130 | graph << TargetHint::OPENCL |
| 131 | << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, batches), 1, DataType::F32), DummyAccessor()) |
| 132 | << ConvolutionLayer( |
| 133 | 7U, 7U, 96U, |
| 134 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"), |
| 135 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"), |
| 136 | PadStrideInfo(2, 2, 0, 0)) |
| 137 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 138 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 139 | << ConvolutionLayer( |
| 140 | 1U, 1U, 16U, |
| 141 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"), |
| 142 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"), |
| 143 | PadStrideInfo(1, 1, 0, 0)) |
| 144 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 145 | << get_expand_fire_node(data_path, "fire2", 64U, 64U) |
| 146 | << ConvolutionLayer( |
| 147 | 1U, 1U, 16U, |
| 148 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"), |
| 149 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"), |
| 150 | PadStrideInfo(1, 1, 0, 0)) |
| 151 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 152 | << get_expand_fire_node(data_path, "fire3", 64U, 64U) |
| 153 | << ConvolutionLayer( |
| 154 | 1U, 1U, 32U, |
| 155 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"), |
| 156 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"), |
| 157 | PadStrideInfo(1, 1, 0, 0)) |
| 158 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 159 | << get_expand_fire_node(data_path, "fire4", 128U, 128U) |
| 160 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 161 | << ConvolutionLayer( |
| 162 | 1U, 1U, 32U, |
| 163 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"), |
| 164 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"), |
| 165 | PadStrideInfo(1, 1, 0, 0)) |
| 166 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 167 | << get_expand_fire_node(data_path, "fire5", 128U, 128U) |
| 168 | << ConvolutionLayer( |
| 169 | 1U, 1U, 48U, |
| 170 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"), |
| 171 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"), |
| 172 | PadStrideInfo(1, 1, 0, 0)) |
| 173 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 174 | << get_expand_fire_node(data_path, "fire6", 192U, 192U) |
| 175 | << ConvolutionLayer( |
| 176 | 1U, 1U, 48U, |
| 177 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"), |
| 178 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"), |
| 179 | PadStrideInfo(1, 1, 0, 0)) |
| 180 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 181 | << get_expand_fire_node(data_path, "fire7", 192U, 192U) |
| 182 | << ConvolutionLayer( |
| 183 | 1U, 1U, 64U, |
| 184 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"), |
| 185 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"), |
| 186 | PadStrideInfo(1, 1, 0, 0)) |
| 187 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 188 | << get_expand_fire_node(data_path, "fire8", 256U, 256U) |
| 189 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 190 | << ConvolutionLayer( |
| 191 | 1U, 1U, 64U, |
| 192 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"), |
| 193 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"), |
| 194 | PadStrideInfo(1, 1, 0, 0)) |
| 195 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 196 | << get_expand_fire_node(data_path, "fire9", 256U, 256U) |
| 197 | << ConvolutionLayer( |
| 198 | 1U, 1U, 1000U, |
| 199 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"), |
| 200 | get_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"), |
| 201 | PadStrideInfo(1, 1, 0, 0)) |
| 202 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 203 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 13, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL))) |
| 204 | << SoftmaxLayer() |
| 205 | << Tensor(DummyAccessor()); |
| 206 | |
| 207 | graph.run(); |
| 208 | } |
| 209 | |
| 210 | /** Main program for Squeezenet v1.0 |
| 211 | * |
| 212 | * @param[in] argc Number of arguments |
| 213 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches ) |
| 214 | */ |
| 215 | int main(int argc, const char **argv) |
| 216 | { |
| 217 | return arm_compute::utils::run_example(argc, argv, main_graph_squeezenet); |
| 218 | } |