blob: 8705c8ed1e3688ef76d24413c5f9ff426bbe15ee [file] [log] [blame]
Georgios Pinitas6f669f02017-09-26 12:32:57 +01001/*
Anthony Barbier6db0ff52018-01-05 10:59:12 +00002 * Copyright (c) 2017, 2018 ARM Limited.
Georgios Pinitas6f669f02017-09-26 12:32:57 +01003 *
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 Pinitas6f669f02017-09-26 12:32:57 +010024#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010026#include "support/ToolchainSupport.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30#include <cstdlib>
31#include <iostream>
32#include <memory>
33
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000034using namespace arm_compute::utils;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010035using namespace arm_compute::graph;
36using namespace arm_compute::graph_utils;
37
Georgios Pinitas6f669f02017-09-26 12:32:57 +010038/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
39 *
40 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000041 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +010042 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000043class GraphAlexnetExample : public Example
Georgios Pinitas6f669f02017-09-26 12:32:57 +010044{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000045public:
46 void do_setup(int argc, char **argv) override
47 {
48 std::string data_path; /* Path to the trainable data */
49 std::string image; /* Image data */
50 std::string label; /* Label data */
Gian Marco44ec2e72017-10-19 14:13:38 +010051
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000052 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
53 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
54 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitas6f669f02017-09-26 12:32:57 +010055
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000056 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
57 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
58 ConvolutionMethodHint convolution_hint = target_hint == TargetHint::NEON ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT;
Gian Marcobfa3b522017-12-12 10:08:38 +000059
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000060 // Parse arguments
61 if(argc < 2)
62 {
63 // Print help
64 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
65 std::cout << "No data folder provided: using random values\n\n";
66 }
67 else if(argc == 2)
68 {
69 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
70 std::cout << "No data folder provided: using random values\n\n";
71 }
72 else if(argc == 3)
73 {
74 data_path = argv[2];
75 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
76 std::cout << "No image provided: using random values\n\n";
77 }
78 else if(argc == 4)
79 {
80 data_path = argv[2];
81 image = argv[3];
82 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
83 std::cout << "No text file with labels provided: skipping output accessor\n\n";
84 }
85 else
86 {
87 data_path = argv[2];
88 image = argv[3];
89 label = argv[4];
90 }
91
92 graph << target_hint
93 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
94 get_input_accessor(image, mean_r, mean_g, mean_b))
95 // Layer 1
96 << ConvolutionLayer(
97 11U, 11U, 96U,
98 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
99 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
100 PadStrideInfo(4, 4, 0, 0))
101 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
102 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
103 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
104 // Layer 2
105 << convolution_hint
106 << ConvolutionLayer(
107 5U, 5U, 256U,
108 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
109 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
110 PadStrideInfo(1, 1, 2, 2), 2)
111 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
112 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
113 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
114 // Layer 3
115 << ConvolutionLayer(
116 3U, 3U, 384U,
117 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
118 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
119 PadStrideInfo(1, 1, 1, 1))
120 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
121 // Layer 4
122 << ConvolutionLayer(
123 3U, 3U, 384U,
124 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
125 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
126 PadStrideInfo(1, 1, 1, 1), 2)
127 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
128 // Layer 5
129 << ConvolutionLayer(
130 3U, 3U, 256U,
131 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
132 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
133 PadStrideInfo(1, 1, 1, 1), 2)
134 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
135 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
136 // Layer 6
137 << FullyConnectedLayer(
138 4096U,
139 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
140 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
142 // Layer 7
143 << FullyConnectedLayer(
144 4096U,
145 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
146 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
147 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
148 // Layer 8
149 << FullyConnectedLayer(
150 1000U,
151 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
152 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
153 // Softmax
154 << SoftmaxLayer()
155 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100156 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000157 void do_run() override
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100158 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000159 // Run graph
160 graph.run();
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100161 }
162
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000163private:
164 Graph graph{};
165};
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100166
167/** Main program for AlexNet
168 *
169 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000170 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100171 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000172int main(int argc, char **argv)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100173{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000174 return arm_compute::utils::run_example<GraphAlexnetExample>(argc, argv);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100175}