blob: b2a5be647f2d57bdd2e5a8958be7921c0d626784 [file] [log] [blame]
Georgios Pinitas6f669f02017-09-26 12:32:57 +01001/*
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
28#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/Nodes.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CPP/CPPScheduler.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
41using namespace arm_compute::graph;
42using namespace arm_compute::graph_utils;
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010043using namespace arm_compute::logging;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010044
Georgios Pinitas6f669f02017-09-26 12:32:57 +010045/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
46 *
47 * @param[in] argc Number of arguments
Gian Marco44ec2e72017-10-19 14:13:38 +010048 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +010049 */
50void main_graph_alexnet(int argc, const char **argv)
51{
Gian Marco44ec2e72017-10-19 14:13:38 +010052 std::string data_path; /* Path to the trainable data */
53 std::string image; /* Image data */
54 std::string label; /* Label data */
55
56 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
57 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
58 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitas6f669f02017-09-26 12:32:57 +010059
60 // Parse arguments
61 if(argc < 2)
62 {
63 // Print help
Gian Marco44ec2e72017-10-19 14:13:38 +010064 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +010065 std::cout << "No data folder provided: using random values\n\n";
66 }
67 else if(argc == 2)
68 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +010069 data_path = argv[1];
Gian Marco44ec2e72017-10-19 14:13:38 +010070 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
71 std::cout << "No image provided: using random values\n\n";
72 }
73 else if(argc == 3)
74 {
75 data_path = argv[1];
76 image = argv[2];
77 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
78 std::cout << "No text file with labels provided: skipping output accessor\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +010079 }
80 else
81 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +010082 data_path = argv[1];
Gian Marco44ec2e72017-10-19 14:13:38 +010083 image = argv[2];
84 label = argv[3];
Georgios Pinitas6f669f02017-09-26 12:32:57 +010085 }
86
87 // Check if OpenCL is available and initialize the scheduler
Georgios Pinitasff421f22017-10-04 16:53:58 +010088 TargetHint hint = TargetHint::NEON;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010089 if(arm_compute::opencl_is_available())
90 {
91 arm_compute::CLScheduler::get().default_init();
Georgios Pinitasff421f22017-10-04 16:53:58 +010092 hint = TargetHint::OPENCL;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010093 }
94
95 Graph graph;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010096
97 graph << hint
Gian Marco44ec2e72017-10-19 14:13:38 +010098 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
99 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100100 // Layer 1
101 << ConvolutionLayer(
102 11U, 11U, 96U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000103 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
104 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100105 PadStrideInfo(4, 4, 0, 0))
106 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
107 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
108 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
109 // Layer 2
Georgios Pinitasff421f22017-10-04 16:53:58 +0100110 << ConvolutionMethodHint::DIRECT
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100111 << ConvolutionLayer(
112 5U, 5U, 256U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000113 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
114 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100115 PadStrideInfo(1, 1, 2, 2), 2)
116 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
117 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
118 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
119 // Layer 3
120 << ConvolutionLayer(
121 3U, 3U, 384U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000122 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
123 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100124 PadStrideInfo(1, 1, 1, 1))
125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
126 // Layer 4
127 << ConvolutionLayer(
128 3U, 3U, 384U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000129 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
130 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100131 PadStrideInfo(1, 1, 1, 1), 2)
132 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
133 // Layer 5
134 << ConvolutionLayer(
135 3U, 3U, 256U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000136 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
137 get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100138 PadStrideInfo(1, 1, 1, 1), 2)
139 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
140 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
141 // Layer 6
142 << FullyConnectedLayer(
143 4096U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000144 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
145 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100146 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
147 // Layer 7
148 << FullyConnectedLayer(
149 4096U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000150 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
151 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100152 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
153 // Layer 8
154 << FullyConnectedLayer(
155 1000U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000156 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
157 get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100158 // Softmax
159 << SoftmaxLayer()
Gian Marco44ec2e72017-10-19 14:13:38 +0100160 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100161
162 // Run graph
163 graph.run();
164}
165
166/** Main program for AlexNet
167 *
168 * @param[in] argc Number of arguments
Gian Marco44ec2e72017-10-19 14:13:38 +0100169 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100170 */
171int main(int argc, const char **argv)
172{
173 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
174}