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