2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/graph/Graph.h"
25 #include "arm_compute/graph/Nodes.h"
26 #include "support/ToolchainSupport.h"
27 #include "utils/GraphUtils.h"
28 #include "utils/Utils.h"
34 using namespace arm_compute::graph;
35 using namespace arm_compute::graph_utils;
37 /** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
39 * @param[in] argc Number of arguments
40 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
42 void main_graph_alexnet(int argc, const char **argv)
44 std::string data_path; /* Path to the trainable data */
45 std::string image; /* Image data */
46 std::string label; /* Label data */
48 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
49 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
50 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
52 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
53 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
54 ConvolutionMethodHint convolution_hint = target_hint == TargetHint::NEON ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT;
60 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
61 std::cout << "No data folder provided: using random values\n\n";
65 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
66 std::cout << "No data folder provided: using random values\n\n";
71 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
72 std::cout << "No image provided: using random values\n\n";
78 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
79 std::cout << "No text file with labels provided: skipping output accessor\n\n";
91 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
92 get_input_accessor(image, mean_r, mean_g, mean_b))
96 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
97 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
98 PadStrideInfo(4, 4, 0, 0))
99 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
100 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
101 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
106 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
107 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
108 PadStrideInfo(1, 1, 2, 2), 2)
109 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
110 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
111 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
115 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
116 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
117 PadStrideInfo(1, 1, 1, 1))
118 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
122 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
123 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
124 PadStrideInfo(1, 1, 1, 1), 2)
125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
129 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
130 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
131 PadStrideInfo(1, 1, 1, 1), 2)
132 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
133 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
135 << FullyConnectedLayer(
137 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
138 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
139 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
141 << FullyConnectedLayer(
143 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
144 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
145 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
147 << FullyConnectedLayer(
149 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
150 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
153 << Tensor(get_output_accessor(label, 5));
159 /** Main program for AlexNet
161 * @param[in] argc Number of arguments
162 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
164 int main(int argc, const char **argv)
166 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);