blob: 6423fe48d30b273c326d35cc298849b75f6bb1b7 [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
34using namespace arm_compute::graph;
35using 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
Gian Marcobfa3b522017-12-12 10:08:38 +000040 * @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 +010041 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000042void main_graph_alexnet(int argc, char **argv)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010043{
Gian Marco44ec2e72017-10-19 14:13:38 +010044 std::string data_path; /* Path to the trainable data */
45 std::string image; /* Image data */
46 std::string label; /* Label data */
47
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 */
Georgios Pinitas6f669f02017-09-26 12:32:57 +010051
Gian Marcobfa3b522017-12-12 10:08:38 +000052 // 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;
55
Georgios Pinitas6f669f02017-09-26 12:32:57 +010056 // Parse arguments
57 if(argc < 2)
58 {
59 // Print help
Gian Marcobfa3b522017-12-12 10:08:38 +000060 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +010061 std::cout << "No data folder provided: using random values\n\n";
62 }
63 else if(argc == 2)
64 {
Gian Marcobfa3b522017-12-12 10:08:38 +000065 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";
Gian Marco44ec2e72017-10-19 14:13:38 +010067 }
68 else if(argc == 3)
69 {
Gian Marcobfa3b522017-12-12 10:08:38 +000070 data_path = argv[2];
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";
73 }
74 else if(argc == 4)
75 {
76 data_path = argv[2];
77 image = argv[3];
78 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
Gian Marco44ec2e72017-10-19 14:13:38 +010079 std::cout << "No text file with labels provided: skipping output accessor\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +010080 }
81 else
82 {
Gian Marcobfa3b522017-12-12 10:08:38 +000083 data_path = argv[2];
84 image = argv[3];
85 label = argv[4];
Georgios Pinitas6f669f02017-09-26 12:32:57 +010086 }
87
88 Graph graph;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010089
Gian Marcobfa3b522017-12-12 10:08:38 +000090 graph << target_hint
Gian Marco44ec2e72017-10-19 14:13:38 +010091 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
92 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitas6f669f02017-09-26 12:32:57 +010093 // Layer 1
94 << ConvolutionLayer(
95 11U, 11U, 96U,
Isabella Gottardia4c61882017-11-03 12:11:55 +000096 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"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +010098 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)))
102 // Layer 2
Gian Marcobfa3b522017-12-12 10:08:38 +0000103 << convolution_hint
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100104 << ConvolutionLayer(
105 5U, 5U, 256U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000106 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"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100108 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)))
112 // Layer 3
113 << ConvolutionLayer(
114 3U, 3U, 384U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000115 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"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100117 PadStrideInfo(1, 1, 1, 1))
118 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
119 // Layer 4
120 << ConvolutionLayer(
121 3U, 3U, 384U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000122 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"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100124 PadStrideInfo(1, 1, 1, 1), 2)
125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
126 // Layer 5
127 << ConvolutionLayer(
128 3U, 3U, 256U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000129 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"),
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100131 PadStrideInfo(1, 1, 1, 1), 2)
132 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
133 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
134 // Layer 6
135 << FullyConnectedLayer(
136 4096U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000137 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"))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100139 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
140 // Layer 7
141 << FullyConnectedLayer(
142 4096U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000143 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"))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100145 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
146 // Layer 8
147 << FullyConnectedLayer(
148 1000U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000149 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"))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100151 // Softmax
152 << SoftmaxLayer()
Gian Marco44ec2e72017-10-19 14:13:38 +0100153 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100154
155 // Run graph
156 graph.run();
157}
158
159/** Main program for AlexNet
160 *
161 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000162 * @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 +0100163 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000164int main(int argc, char **argv)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100165{
166 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
167}