blob: 6ba3ebc7aebe91cdd9f32f8cde9ce5043f760712 [file] [log] [blame]
Georgios Pinitas6f669f02017-09-26 12:32:57 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +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 Pinitasee33ea52018-03-08 16:01:29 +000024#include "arm_compute/graph2.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
30#include <iostream>
31#include <memory>
32
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000033using namespace arm_compute::utils;
Georgios Pinitasee33ea52018-03-08 16:01:29 +000034using namespace arm_compute::graph2::frontend;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010035using namespace arm_compute::graph_utils;
36
Georgios Pinitas6f669f02017-09-26 12:32:57 +010037/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
38 *
39 * @param[in] argc Number of arguments
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000040 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +010041 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000042class GraphAlexnetExample : public Example
Georgios Pinitas6f669f02017-09-26 12:32:57 +010043{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000044public:
45 void do_setup(int argc, char **argv) override
46 {
47 std::string data_path; /* Path to the trainable data */
48 std::string image; /* Image data */
49 std::string label; /* Label data */
Gian Marco44ec2e72017-10-19 14:13:38 +010050
Georgios Pinitas140fdc72018-02-16 11:42:38 +000051 // Create a preprocessor object
52 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
53 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Georgios Pinitas6f669f02017-09-26 12:32:57 +010054
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000055 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Georgios Pinitasee33ea52018-03-08 16:01:29 +000056 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
57 Target target_hint = set_target_hint2(target);
58 bool enable_tuning = (target == 2);
59 bool enable_memory_management = true;
Gian Marco36a0a462018-01-12 10:21:40 +000060
Georgios Pinitasee33ea52018-03-08 16:01:29 +000061 // TODO (geopin01) : Get GPU target somehow and set gemm also for midgard ?
62 const bool is_gemm_convolution5x5 = (target_hint == Target::NEON);
63 const bool is_winograd_convolution3x3 = target_hint == Target::CL;
64 ConvolutionMethod convolution_5x5_hint = is_gemm_convolution5x5 ? ConvolutionMethod::GEMM : ConvolutionMethod::DIRECT;
65 ConvolutionMethod convolution_3x3_hint = is_winograd_convolution3x3 ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
Gian Marcobfa3b522017-12-12 10:08:38 +000066
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000067 // Parse arguments
68 if(argc < 2)
69 {
70 // Print help
71 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
72 std::cout << "No data folder provided: using random values\n\n";
73 }
74 else if(argc == 2)
75 {
76 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
77 std::cout << "No data folder provided: using random values\n\n";
78 }
79 else if(argc == 3)
80 {
81 data_path = argv[2];
82 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
83 std::cout << "No image provided: using random values\n\n";
84 }
85 else if(argc == 4)
86 {
87 data_path = argv[2];
88 image = argv[3];
89 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
90 std::cout << "No text file with labels provided: skipping output accessor\n\n";
91 }
92 else
93 {
94 data_path = argv[2];
95 image = argv[3];
96 label = argv[4];
97 }
98
99 graph << target_hint
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000100 << InputLayer(TensorDescriptor(TensorShape(227U, 227U, 3U, 1U), DataType::F32),
101 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000102 // Layer 1
103 << ConvolutionLayer(
104 11U, 11U, 96U,
105 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
106 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
107 PadStrideInfo(4, 4, 0, 0))
108 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
109 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
110 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
111 // Layer 2
Gian Marco36a0a462018-01-12 10:21:40 +0000112 << convolution_5x5_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000113 << ConvolutionLayer(
114 5U, 5U, 256U,
115 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
116 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
117 PadStrideInfo(1, 1, 2, 2), 2)
118 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
119 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
120 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
Gian Marco Iodiceed99f412018-03-21 17:45:31 +0000121 << convolution_3x3_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000122 // Layer 3
123 << ConvolutionLayer(
124 3U, 3U, 384U,
125 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
126 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
127 PadStrideInfo(1, 1, 1, 1))
128 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
129 // Layer 4
130 << ConvolutionLayer(
131 3U, 3U, 384U,
132 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
133 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
134 PadStrideInfo(1, 1, 1, 1), 2)
135 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
136 // Layer 5
137 << ConvolutionLayer(
138 3U, 3U, 256U,
139 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
140 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
141 PadStrideInfo(1, 1, 1, 1), 2)
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
143 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
144 // Layer 6
145 << FullyConnectedLayer(
146 4096U,
147 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
148 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 // Layer 7
151 << FullyConnectedLayer(
152 4096U,
153 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
154 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
156 // Layer 8
157 << FullyConnectedLayer(
158 1000U,
159 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
160 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
161 // Softmax
162 << SoftmaxLayer()
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000163 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000164
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000165 // Finalize graph
166 graph.finalize(target_hint, enable_tuning, enable_memory_management);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100167 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000168 void do_run() override
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100169 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000170 // Run graph
171 graph.run();
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100172 }
173
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000174private:
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000175 Stream graph{ 0, "AlexNet" };
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000176};
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100177
178/** Main program for AlexNet
179 *
180 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000181 * @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 +0100182 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000183int main(int argc, char **argv)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100184{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000185 return arm_compute::utils::run_example<GraphAlexnetExample>(argc, argv);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100186}