arm_compute v17.12
[platform/upstream/armcl.git] / examples / graph_alexnet.cpp
1 /*
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 #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"
29
30 #include <cstdlib>
31 #include <iostream>
32 #include <memory>
33
34 using namespace arm_compute::graph;
35 using namespace arm_compute::graph_utils;
36
37 /** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
38  *
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 )
41  */
42 void main_graph_alexnet(int argc, const char **argv)
43 {
44     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 */
51
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;
55
56     // Parse arguments
57     if(argc < 2)
58     {
59         // Print help
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";
62     }
63     else if(argc == 2)
64     {
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";
67     }
68     else if(argc == 3)
69     {
70         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";
79         std::cout << "No text file with labels provided: skipping output accessor\n\n";
80     }
81     else
82     {
83         data_path = argv[2];
84         image     = argv[3];
85         label     = argv[4];
86     }
87
88     Graph graph;
89
90     graph << target_hint
91           << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
92                     get_input_accessor(image, mean_r, mean_g, mean_b))
93           // Layer 1
94           << ConvolutionLayer(
95               11U, 11U, 96U,
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)))
102           // Layer 2
103           << convolution_hint
104           << ConvolutionLayer(
105               5U, 5U, 256U,
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)))
112           // Layer 3
113           << ConvolutionLayer(
114               3U, 3U, 384U,
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))
119           // Layer 4
120           << ConvolutionLayer(
121               3U, 3U, 384U,
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))
126           // Layer 5
127           << ConvolutionLayer(
128               3U, 3U, 256U,
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)))
134           // Layer 6
135           << FullyConnectedLayer(
136               4096U,
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))
140           // Layer 7
141           << FullyConnectedLayer(
142               4096U,
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))
146           // Layer 8
147           << FullyConnectedLayer(
148               1000U,
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"))
151           // Softmax
152           << SoftmaxLayer()
153           << Tensor(get_output_accessor(label, 5));
154
155     // Run graph
156     graph.run();
157 }
158
159 /** Main program for AlexNet
160  *
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 )
163  */
164 int main(int argc, const char **argv)
165 {
166     return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
167 }