xref: /aosp_15_r20/external/ComputeLibrary/examples/graph_inception_resnet_v1.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/graph.h"
25*c217d954SCole Faust #include "support/ToolchainSupport.h"
26*c217d954SCole Faust #include "utils/CommonGraphOptions.h"
27*c217d954SCole Faust #include "utils/GraphUtils.h"
28*c217d954SCole Faust #include "utils/Utils.h"
29*c217d954SCole Faust 
30*c217d954SCole Faust using namespace arm_compute::utils;
31*c217d954SCole Faust using namespace arm_compute::graph::frontend;
32*c217d954SCole Faust using namespace arm_compute::graph_utils;
33*c217d954SCole Faust 
34*c217d954SCole Faust const float batch_norm_epsilon = 0.0010000000474974513f;
35*c217d954SCole Faust 
36*c217d954SCole Faust /** Example demonstrating how to implement Inception ResNet V1 network using the Compute Library's graph API */
37*c217d954SCole Faust class InceptionResNetV1Example final : public Example
38*c217d954SCole Faust {
39*c217d954SCole Faust public:
InceptionResNetV1Example()40*c217d954SCole Faust     InceptionResNetV1Example()
41*c217d954SCole Faust         : cmd_parser(), common_opts(cmd_parser), common_params(), model_input_width(nullptr), model_input_height(nullptr), graph(0, "InceptionResNetV1")
42*c217d954SCole Faust     {
43*c217d954SCole Faust         model_input_width  = cmd_parser.add_option<SimpleOption<unsigned int>>("image-width", 512);
44*c217d954SCole Faust         model_input_height = cmd_parser.add_option<SimpleOption<unsigned int>>("image-height", 512);
45*c217d954SCole Faust 
46*c217d954SCole Faust         // Add model id option
47*c217d954SCole Faust         model_input_width->set_help("Input image width.");
48*c217d954SCole Faust         model_input_height->set_help("Input image height.");
49*c217d954SCole Faust     }
50*c217d954SCole Faust     InceptionResNetV1Example(const InceptionResNetV1Example &) = delete;
51*c217d954SCole Faust     InceptionResNetV1Example &operator=(const InceptionResNetV1Example &) = delete;
52*c217d954SCole Faust     ~InceptionResNetV1Example() override                                  = default;
do_setup(int argc,char ** argv)53*c217d954SCole Faust     bool do_setup(int argc, char **argv) override
54*c217d954SCole Faust     {
55*c217d954SCole Faust         // Parse arguments
56*c217d954SCole Faust         cmd_parser.parse(argc, argv);
57*c217d954SCole Faust         cmd_parser.validate();
58*c217d954SCole Faust 
59*c217d954SCole Faust         // Consume common parameters
60*c217d954SCole Faust         common_params = consume_common_graph_parameters(common_opts);
61*c217d954SCole Faust 
62*c217d954SCole Faust         // Return when help menu is requested
63*c217d954SCole Faust         if(common_params.help)
64*c217d954SCole Faust         {
65*c217d954SCole Faust             cmd_parser.print_help(argv[0]);
66*c217d954SCole Faust             return false;
67*c217d954SCole Faust         }
68*c217d954SCole Faust         // Get input image width and height
69*c217d954SCole Faust         const unsigned int image_width  = model_input_width->value();
70*c217d954SCole Faust         const unsigned int image_height = model_input_height->value();
71*c217d954SCole Faust 
72*c217d954SCole Faust         // Set default layout if needed
73*c217d954SCole Faust         if(!common_opts.data_layout->is_set() && common_params.target == Target::NEON)
74*c217d954SCole Faust         {
75*c217d954SCole Faust             common_params.data_layout = DataLayout::NCHW;
76*c217d954SCole Faust         }
77*c217d954SCole Faust 
78*c217d954SCole Faust         // Checks
79*c217d954SCole Faust         ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
80*c217d954SCole Faust 
81*c217d954SCole Faust         // Print parameter values
82*c217d954SCole Faust         std::cout << common_params << std::endl;
83*c217d954SCole Faust         std::cout << "Image width: " << image_width << std::endl;
84*c217d954SCole Faust         std::cout << "Image height: " << image_height << std::endl;
85*c217d954SCole Faust 
86*c217d954SCole Faust         // Create model path
87*c217d954SCole Faust         std::string data_path  = common_params.data_path;
88*c217d954SCole Faust         std::string model_path = "/cnn_data/inception_resnet_v1_model/";
89*c217d954SCole Faust         if(!data_path.empty())
90*c217d954SCole Faust         {
91*c217d954SCole Faust             data_path += model_path;
92*c217d954SCole Faust         }
93*c217d954SCole Faust 
94*c217d954SCole Faust         // Create a preprocessor object
95*c217d954SCole Faust         std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>(0.f, 1.f);
96*c217d954SCole Faust 
97*c217d954SCole Faust         // Create input descriptor
98*c217d954SCole Faust         const auto        operation_layout = common_params.data_layout;
99*c217d954SCole Faust         const TensorShape tensor_shape     = permute_shape(TensorShape(image_width, image_height, 3U, common_params.batches), DataLayout::NCHW, operation_layout);
100*c217d954SCole Faust         TensorDescriptor  input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
101*c217d954SCole Faust 
102*c217d954SCole Faust         // Set weights trained layout
103*c217d954SCole Faust         const DataLayout weights_layout = DataLayout::NCHW;
104*c217d954SCole Faust 
105*c217d954SCole Faust         graph << common_params.target
106*c217d954SCole Faust               << common_params.fast_math_hint
107*c217d954SCole Faust               << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false))
108*c217d954SCole Faust               // Conv2d_1a_3x3
109*c217d954SCole Faust               << ConvolutionLayer(3U, 3U, 32U,
110*c217d954SCole Faust                                   get_weights_accessor(data_path, "Conv2d_1a_3x3_weights.npy", weights_layout),
111*c217d954SCole Faust                                   std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
112*c217d954SCole Faust                                   PadStrideInfo(2, 2, 0, 0))
113*c217d954SCole Faust               .set_name("Conv2d_1a_3x3/convolution")
114*c217d954SCole Faust               << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_1a_3x3_BatchNorm_moving_mean.npy"),
115*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_1a_3x3_BatchNorm_moving_variance.npy"),
116*c217d954SCole Faust                                          get_random_accessor(1.f, 1.f),
117*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_1a_3x3_BatchNorm_beta.npy"),
118*c217d954SCole Faust                                          batch_norm_epsilon)
119*c217d954SCole Faust               .set_name("Conv2d_1a_3x3/BatchNorm")
120*c217d954SCole Faust               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv2d_1a_3x3/Relu")
121*c217d954SCole Faust               // Conv2d_2a_3x3
122*c217d954SCole Faust               << ConvolutionLayer(3U, 3U, 32U,
123*c217d954SCole Faust                                   get_weights_accessor(data_path, "Conv2d_2a_3x3_weights.npy", weights_layout),
124*c217d954SCole Faust                                   std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
125*c217d954SCole Faust                                   PadStrideInfo(1, 1, 0, 0))
126*c217d954SCole Faust               .set_name("Conv2d_2a_3x3/convolution")
127*c217d954SCole Faust               << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_2a_3x3_BatchNorm_moving_mean.npy"),
128*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_2a_3x3_BatchNorm_moving_variance.npy"),
129*c217d954SCole Faust                                          get_random_accessor(1.f, 1.f),
130*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_2a_3x3_BatchNorm_beta.npy"),
131*c217d954SCole Faust                                          batch_norm_epsilon)
132*c217d954SCole Faust               .set_name("Conv2d_2a_3x3/BatchNorm")
133*c217d954SCole Faust               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv2d_2a_3x3/Relu")
134*c217d954SCole Faust               // Conv2d_2b_3x3
135*c217d954SCole Faust               << ConvolutionLayer(3U, 3U, 64U,
136*c217d954SCole Faust                                   get_weights_accessor(data_path, "Conv2d_2b_3x3_weights.npy", weights_layout),
137*c217d954SCole Faust                                   std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
138*c217d954SCole Faust                                   PadStrideInfo(1, 1, 1, 1))
139*c217d954SCole Faust               .set_name("Conv2d_2b_3x3/convolution")
140*c217d954SCole Faust               << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_2b_3x3_BatchNorm_moving_mean.npy"),
141*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_2b_3x3_BatchNorm_moving_variance.npy"),
142*c217d954SCole Faust                                          get_random_accessor(1.f, 1.f),
143*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_2b_3x3_BatchNorm_beta.npy"),
144*c217d954SCole Faust                                          batch_norm_epsilon)
145*c217d954SCole Faust               .set_name("Conv2d_2b_3x3/BatchNorm")
146*c217d954SCole Faust               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv2d_2b_3x3/Relu")
147*c217d954SCole Faust               // MaxPool_3a_3x3
148*c217d954SCole Faust               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, operation_layout, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL), true)).set_name("MaxPool_3a_3x3/MaxPool")
149*c217d954SCole Faust               // Conv2d_3b_1x1
150*c217d954SCole Faust               << ConvolutionLayer(1U, 1U, 80U,
151*c217d954SCole Faust                                   get_weights_accessor(data_path, "Conv2d_3b_1x1_weights.npy", weights_layout),
152*c217d954SCole Faust                                   std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
153*c217d954SCole Faust                                   PadStrideInfo(1, 1, 0, 0))
154*c217d954SCole Faust               .set_name("Conv2d_3b_1x1/convolution")
155*c217d954SCole Faust               << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_3b_1x1_BatchNorm_moving_mean.npy"),
156*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_3b_1x1_BatchNorm_moving_variance.npy"),
157*c217d954SCole Faust                                          get_random_accessor(1.f, 1.f),
158*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_3b_1x1_BatchNorm_beta.npy"),
159*c217d954SCole Faust                                          batch_norm_epsilon)
160*c217d954SCole Faust               .set_name("Conv2d_3b_1x1/BatchNorm")
161*c217d954SCole Faust               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv2d_3b_1x1/Relu")
162*c217d954SCole Faust               // Conv2d_4a_3x3
163*c217d954SCole Faust               << ConvolutionLayer(3U, 3U, 192U,
164*c217d954SCole Faust                                   get_weights_accessor(data_path, "Conv2d_4a_3x3_weights.npy", weights_layout),
165*c217d954SCole Faust                                   std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
166*c217d954SCole Faust                                   PadStrideInfo(1, 1, 0, 0))
167*c217d954SCole Faust               .set_name("Conv2d_4a_3x3/convolution")
168*c217d954SCole Faust               << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_4a_3x3_BatchNorm_moving_mean.npy"),
169*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_4a_3x3_BatchNorm_moving_variance.npy"),
170*c217d954SCole Faust                                          get_random_accessor(1.f, 1.f),
171*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_4a_3x3_BatchNorm_beta.npy"),
172*c217d954SCole Faust                                          batch_norm_epsilon)
173*c217d954SCole Faust               .set_name("Conv2d_4a_3x3/BatchNorm")
174*c217d954SCole Faust               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv2d_4a_3x3/Relu")
175*c217d954SCole Faust               // Conv2d_4b_3x3
176*c217d954SCole Faust               << ConvolutionLayer(3U, 3U, 256U,
177*c217d954SCole Faust                                   get_weights_accessor(data_path, "Conv2d_4b_3x3_weights.npy", weights_layout),
178*c217d954SCole Faust                                   std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
179*c217d954SCole Faust                                   PadStrideInfo(2, 2, 0, 0))
180*c217d954SCole Faust               .set_name("Conv2d_4a_3x3/convolution")
181*c217d954SCole Faust               << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_4b_3x3_BatchNorm_moving_mean.npy"),
182*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_4b_3x3_BatchNorm_moving_variance.npy"),
183*c217d954SCole Faust                                          get_random_accessor(1.f, 1.f),
184*c217d954SCole Faust                                          get_weights_accessor(data_path, "Conv2d_4b_3x3_BatchNorm_beta.npy"),
185*c217d954SCole Faust                                          batch_norm_epsilon)
186*c217d954SCole Faust               .set_name("Conv2d_4b_3x3/BatchNorm")
187*c217d954SCole Faust               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv2d_4b_3x3/Relu");
188*c217d954SCole Faust 
189*c217d954SCole Faust         // 5 x Inception-resnet-A
190*c217d954SCole Faust         block35_repeat(data_path, weights_layout, 5);
191*c217d954SCole Faust         // Reduction-A
192*c217d954SCole Faust         reduction_a(data_path, weights_layout);
193*c217d954SCole Faust         // 10 x Inception-Resnet-B
194*c217d954SCole Faust         block17_repeat(data_path, weights_layout, 10);
195*c217d954SCole Faust         // Reduction-B
196*c217d954SCole Faust         reduction_b(data_path, weights_layout);
197*c217d954SCole Faust         // 5 x Inception-resnet-C
198*c217d954SCole Faust         block8_repeat(data_path, weights_layout, 5, 0.2f, true);
199*c217d954SCole Faust 
200*c217d954SCole Faust         block8_repeat(data_path, weights_layout, 1, 1.f, false);
201*c217d954SCole Faust 
202*c217d954SCole Faust         // Logits tail
203*c217d954SCole Faust         graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, operation_layout)).set_name("Logits/AvgPool_1a_8x8")
204*c217d954SCole Faust               << FlattenLayer().set_name("Logits/Flatten")
205*c217d954SCole Faust               << FullyConnectedLayer(
206*c217d954SCole Faust                   128U,
207*c217d954SCole Faust                   get_weights_accessor(data_path, "Logits_Logits_weights.npy", weights_layout),
208*c217d954SCole Faust                   get_weights_accessor(data_path, "Logits_Logits_biases.npy"))
209*c217d954SCole Faust               .set_name("Logits/Logits")
210*c217d954SCole Faust               << OutputLayer(std::make_unique<DummyAccessor>(0));
211*c217d954SCole Faust 
212*c217d954SCole Faust         // Finalize graph
213*c217d954SCole Faust         GraphConfig config;
214*c217d954SCole Faust         config.num_threads = common_params.threads;
215*c217d954SCole Faust         config.use_tuner   = common_params.enable_tuner;
216*c217d954SCole Faust         config.tuner_mode  = common_params.tuner_mode;
217*c217d954SCole Faust         config.tuner_file  = common_params.tuner_file;
218*c217d954SCole Faust         config.mlgo_file   = common_params.mlgo_file;
219*c217d954SCole Faust 
220*c217d954SCole Faust         graph.finalize(common_params.target, config);
221*c217d954SCole Faust 
222*c217d954SCole Faust         return true;
223*c217d954SCole Faust     }
224*c217d954SCole Faust 
do_run()225*c217d954SCole Faust     void do_run() override
226*c217d954SCole Faust     {
227*c217d954SCole Faust         graph.run();
228*c217d954SCole Faust     }
229*c217d954SCole Faust 
230*c217d954SCole Faust private:
231*c217d954SCole Faust     CommandLineParser           cmd_parser;
232*c217d954SCole Faust     CommonGraphOptions          common_opts;
233*c217d954SCole Faust     CommonGraphParams           common_params;
234*c217d954SCole Faust     SimpleOption<unsigned int> *model_input_width{ nullptr };
235*c217d954SCole Faust     SimpleOption<unsigned int> *model_input_height{ nullptr };
236*c217d954SCole Faust     Stream                      graph;
237*c217d954SCole Faust 
238*c217d954SCole Faust private:
block35_repeat(const std::string & data_path,DataLayout weights_layout,unsigned int num_blocks)239*c217d954SCole Faust     void block35_repeat(const std::string &data_path, DataLayout weights_layout, unsigned int num_blocks)
240*c217d954SCole Faust     {
241*c217d954SCole Faust         for(unsigned int i = 0; i < num_blocks; ++i)
242*c217d954SCole Faust         {
243*c217d954SCole Faust             std::stringstream unit_path_ss;
244*c217d954SCole Faust             unit_path_ss << "Repeat_block35_" << (i + 1) << "_";
245*c217d954SCole Faust             std::stringstream unit_name_ss;
246*c217d954SCole Faust             unit_name_ss << "Repeat/block35_" << (i + 1) << "/";
247*c217d954SCole Faust 
248*c217d954SCole Faust             std::string unit_path = unit_path_ss.str();
249*c217d954SCole Faust             std::string unit_name = unit_name_ss.str();
250*c217d954SCole Faust 
251*c217d954SCole Faust             // Create left and write substreams
252*c217d954SCole Faust             SubStream i_l(graph);
253*c217d954SCole Faust             SubStream i_r(graph);
254*c217d954SCole Faust 
255*c217d954SCole Faust             // Branch 0
256*c217d954SCole Faust             SubStream i_la(i_l);
257*c217d954SCole Faust             i_la << ConvolutionLayer(1U, 1U, 32U,
258*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_weights.npy", weights_layout),
259*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
260*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
261*c217d954SCole Faust                  .set_name(unit_name + "Branch_0/Conv2d_1x1/convolution")
262*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_moving_mean.npy"),
263*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_moving_variance.npy"),
264*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
265*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_beta.npy"),
266*c217d954SCole Faust                                             batch_norm_epsilon)
267*c217d954SCole Faust                  .set_name(unit_name + "Branch_0/Conv2d_1x1/BatchNorm")
268*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_0/Conv2d_1x1/Relu");
269*c217d954SCole Faust 
270*c217d954SCole Faust             // Branch 1
271*c217d954SCole Faust             SubStream i_lb(i_l);
272*c217d954SCole Faust             i_lb << ConvolutionLayer(1U, 1U, 32U,
273*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_weights.npy", weights_layout),
274*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
275*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
276*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0a_1x1/convolution")
277*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
278*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
279*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
280*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_beta.npy"),
281*c217d954SCole Faust                                             batch_norm_epsilon)
282*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0a_1x1/BatchNorm")
283*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0a_1x1/Relu")
284*c217d954SCole Faust                  << ConvolutionLayer(3U, 3U, 32U,
285*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_3x3_weights.npy", weights_layout),
286*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
287*c217d954SCole Faust                                      PadStrideInfo(1, 1, 1, 1))
288*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0b_3x3/convolution")
289*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_3x3_BatchNorm_moving_mean.npy"),
290*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_3x3_BatchNorm_moving_variance.npy"),
291*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
292*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_3x3_BatchNorm_beta.npy"),
293*c217d954SCole Faust                                             batch_norm_epsilon)
294*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0b_3x3/BatchNorm")
295*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0b_3x3/Relu");
296*c217d954SCole Faust 
297*c217d954SCole Faust             // Branch 2
298*c217d954SCole Faust             SubStream i_lc(i_l);
299*c217d954SCole Faust             i_lc << ConvolutionLayer(1U, 1U, 32U,
300*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0a_1x1_weights.npy", weights_layout),
301*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
302*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
303*c217d954SCole Faust                  .set_name(unit_name + "Branch_2/Conv2d_0a_1x1/convolution")
304*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
305*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
306*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
307*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0a_1x1_BatchNorm_beta.npy"),
308*c217d954SCole Faust                                             batch_norm_epsilon)
309*c217d954SCole Faust                  .set_name(unit_name + "Branch_2/Conv2d_0a_1x1/BatchNorm")
310*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_2/Conv2d_0a_1x1/Relu")
311*c217d954SCole Faust                  << ConvolutionLayer(3U, 3U, 32U,
312*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0b_3x3_weights.npy", weights_layout),
313*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
314*c217d954SCole Faust                                      PadStrideInfo(1, 1, 1, 1))
315*c217d954SCole Faust                  .set_name(unit_name + "Branch_2/Conv2d_0b_3x3/convolution")
316*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0b_3x3_BatchNorm_moving_mean.npy"),
317*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0b_3x3_BatchNorm_moving_variance.npy"),
318*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
319*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0b_3x3_BatchNorm_beta.npy"),
320*c217d954SCole Faust                                             batch_norm_epsilon)
321*c217d954SCole Faust                  .set_name(unit_name + "Branch_2/Conv2d_0b_3x3/BatchNorm")
322*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_2/Conv2d_0b_3x3/Relu")
323*c217d954SCole Faust                  << ConvolutionLayer(3U, 3U, 32U,
324*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0c_3x3_weights.npy", weights_layout),
325*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
326*c217d954SCole Faust                                      PadStrideInfo(1, 1, 1, 1))
327*c217d954SCole Faust                  .set_name(unit_name + "Branch_2/Conv2d_0c_3x3/convolution")
328*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0c_3x3_BatchNorm_moving_mean.npy"),
329*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0c_3x3_BatchNorm_moving_variance.npy"),
330*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
331*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_2_Conv2d_0c_3x3_BatchNorm_beta.npy"),
332*c217d954SCole Faust                                             batch_norm_epsilon)
333*c217d954SCole Faust                  .set_name(unit_name + "Branch_2/Conv2d_0c_3x3/BatchNorm")
334*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_2/Conv2d_0c_3x3/Relu");
335*c217d954SCole Faust 
336*c217d954SCole Faust             // Concatenate
337*c217d954SCole Faust             i_l << ConcatLayer(std::move(i_la), std::move(i_lb), std::move(i_lc)).set_name(unit_name + "concat")
338*c217d954SCole Faust                 << ConvolutionLayer(1U, 1U, 256U,
339*c217d954SCole Faust                                     get_weights_accessor(data_path, unit_path + "Conv2d_1x1_weights.npy", weights_layout),
340*c217d954SCole Faust                                     get_weights_accessor(data_path, unit_path + "Conv2d_1x1_biases.npy", weights_layout),
341*c217d954SCole Faust                                     PadStrideInfo(1, 1, 0, 0))
342*c217d954SCole Faust                 .set_name(unit_name + "Conv2d_1x1/convolution")
343*c217d954SCole Faust                 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, 0.17f, 0.f)).set_name(unit_name + "mul");
344*c217d954SCole Faust 
345*c217d954SCole Faust             graph << EltwiseLayer(std::move(i_l), std::move(i_r), EltwiseOperation::Add).set_name(unit_name + "add")
346*c217d954SCole Faust                   << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
347*c217d954SCole Faust         }
348*c217d954SCole Faust     }
349*c217d954SCole Faust 
block17_repeat(const std::string & data_path,DataLayout weights_layout,unsigned int num_blocks)350*c217d954SCole Faust     void block17_repeat(const std::string &data_path, DataLayout weights_layout, unsigned int num_blocks)
351*c217d954SCole Faust     {
352*c217d954SCole Faust         for(unsigned int i = 0; i < num_blocks; ++i)
353*c217d954SCole Faust         {
354*c217d954SCole Faust             std::stringstream unit_path_ss;
355*c217d954SCole Faust             unit_path_ss << "Repeat_1_block17_" << (i + 1) << "_";
356*c217d954SCole Faust             std::stringstream unit_name_ss;
357*c217d954SCole Faust             unit_name_ss << "Repeat_1/block17_" << (i + 1) << "/";
358*c217d954SCole Faust 
359*c217d954SCole Faust             std::string unit_path = unit_path_ss.str();
360*c217d954SCole Faust             std::string unit_name = unit_name_ss.str();
361*c217d954SCole Faust 
362*c217d954SCole Faust             // Create left and write substreams
363*c217d954SCole Faust             SubStream i_l(graph);
364*c217d954SCole Faust             SubStream i_r(graph);
365*c217d954SCole Faust 
366*c217d954SCole Faust             // Branch 0
367*c217d954SCole Faust             SubStream i_la(i_l);
368*c217d954SCole Faust             i_la << ConvolutionLayer(1U, 1U, 128U,
369*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_weights.npy", weights_layout),
370*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
371*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
372*c217d954SCole Faust                  .set_name(unit_name + "Branch_0/Conv2d_1x1/convolution")
373*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_moving_mean.npy"),
374*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_moving_variance.npy"),
375*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
376*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_beta.npy"),
377*c217d954SCole Faust                                             batch_norm_epsilon)
378*c217d954SCole Faust                  .set_name(unit_name + "Branch_0/Conv2d_1x1/BatchNorm")
379*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_0/Conv2d_1x1/Relu");
380*c217d954SCole Faust 
381*c217d954SCole Faust             // Branch 1
382*c217d954SCole Faust             SubStream i_lb(i_l);
383*c217d954SCole Faust             i_lb << ConvolutionLayer(1U, 1U, 128U,
384*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_weights.npy", weights_layout),
385*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
386*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
387*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0a_1x1/convolution")
388*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
389*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
390*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
391*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_beta.npy"),
392*c217d954SCole Faust                                             batch_norm_epsilon)
393*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0a_1x1/BatchNorm")
394*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0a_1x1/Relu")
395*c217d954SCole Faust                  << ConvolutionLayer(7U, 1U, 128U,
396*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x7_weights.npy", weights_layout),
397*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
398*c217d954SCole Faust                                      PadStrideInfo(1, 1, 3, 0))
399*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0b_1x7/convolution")
400*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x7_BatchNorm_moving_mean.npy"),
401*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x7_BatchNorm_moving_variance.npy"),
402*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
403*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x7_BatchNorm_beta.npy"),
404*c217d954SCole Faust                                             batch_norm_epsilon)
405*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0b_1x7/BatchNorm")
406*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0b_1x7/Relu")
407*c217d954SCole Faust                  << ConvolutionLayer(1U, 7U, 128U,
408*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_7x1_weights.npy", weights_layout),
409*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
410*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 3))
411*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0c_7x1/convolution")
412*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_7x1_BatchNorm_moving_mean.npy"),
413*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_7x1_BatchNorm_moving_variance.npy"),
414*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
415*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_7x1_BatchNorm_beta.npy"),
416*c217d954SCole Faust                                             batch_norm_epsilon)
417*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0c_7x1/BatchNorm")
418*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0c_7x1/Relu");
419*c217d954SCole Faust 
420*c217d954SCole Faust             // Concatenate
421*c217d954SCole Faust             i_l << ConcatLayer(std::move(i_la), std::move(i_lb)).set_name(unit_name + "concat")
422*c217d954SCole Faust                 << ConvolutionLayer(1U, 1U, 896U,
423*c217d954SCole Faust                                     get_weights_accessor(data_path, unit_path + "Conv2d_1x1_weights.npy", weights_layout),
424*c217d954SCole Faust                                     get_weights_accessor(data_path, unit_path + "Conv2d_1x1_biases.npy", weights_layout),
425*c217d954SCole Faust                                     PadStrideInfo(1, 1, 0, 0))
426*c217d954SCole Faust                 .set_name(unit_name + "Conv2d_1x1/convolution")
427*c217d954SCole Faust                 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, 0.10f, 0.f)).set_name(unit_name + "mul");
428*c217d954SCole Faust 
429*c217d954SCole Faust             graph << EltwiseLayer(std::move(i_l), std::move(i_r), EltwiseOperation::Add).set_name(unit_name + "add")
430*c217d954SCole Faust                   << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
431*c217d954SCole Faust         }
432*c217d954SCole Faust     }
433*c217d954SCole Faust 
block8_repeat(const std::string & data_path,DataLayout weights_layout,unsigned int num_blocks,float scale,bool has_activation)434*c217d954SCole Faust     void block8_repeat(const std::string &data_path, DataLayout weights_layout, unsigned int num_blocks, float scale, bool has_activation)
435*c217d954SCole Faust     {
436*c217d954SCole Faust         for(unsigned int i = 0; i < num_blocks; ++i)
437*c217d954SCole Faust         {
438*c217d954SCole Faust             std::stringstream unit_path_ss;
439*c217d954SCole Faust             std::stringstream unit_name_ss;
440*c217d954SCole Faust             if(num_blocks != 1)
441*c217d954SCole Faust             {
442*c217d954SCole Faust                 unit_path_ss << "Repeat_2_block8_" << (i + 1) << "_";
443*c217d954SCole Faust                 unit_name_ss << "Repeat_2/block8_" << (i + 1) << "/";
444*c217d954SCole Faust             }
445*c217d954SCole Faust             else
446*c217d954SCole Faust             {
447*c217d954SCole Faust                 unit_path_ss << "Block8_";
448*c217d954SCole Faust                 unit_name_ss << "Block8/";
449*c217d954SCole Faust             }
450*c217d954SCole Faust 
451*c217d954SCole Faust             std::string unit_path = unit_path_ss.str();
452*c217d954SCole Faust             std::string unit_name = unit_name_ss.str();
453*c217d954SCole Faust 
454*c217d954SCole Faust             // Create left and write substreams
455*c217d954SCole Faust             SubStream i_l(graph);
456*c217d954SCole Faust             SubStream i_r(graph);
457*c217d954SCole Faust 
458*c217d954SCole Faust             // Branch 0
459*c217d954SCole Faust             SubStream i_la(i_l);
460*c217d954SCole Faust             i_la << ConvolutionLayer(1U, 1U, 192U,
461*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_weights.npy", weights_layout),
462*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
463*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
464*c217d954SCole Faust                  .set_name(unit_name + "Branch_0/Conv2d_1x1/convolution")
465*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_moving_mean.npy"),
466*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_moving_variance.npy"),
467*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
468*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_0_Conv2d_1x1_BatchNorm_beta.npy"),
469*c217d954SCole Faust                                             batch_norm_epsilon)
470*c217d954SCole Faust                  .set_name(unit_name + "Branch_0/Conv2d_1x1/BatchNorm")
471*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_0/Conv2d_1x1/Relu");
472*c217d954SCole Faust 
473*c217d954SCole Faust             // Branch 1
474*c217d954SCole Faust             SubStream i_lb(i_l);
475*c217d954SCole Faust             i_lb << ConvolutionLayer(1U, 1U, 192U,
476*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_weights.npy", weights_layout),
477*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
478*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 0))
479*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0a_1x1/convolution")
480*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
481*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
482*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
483*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0a_1x1_BatchNorm_beta.npy"),
484*c217d954SCole Faust                                             batch_norm_epsilon)
485*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0a_1x1/BatchNorm")
486*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0a_1x1/Relu")
487*c217d954SCole Faust                  << ConvolutionLayer(3U, 1U, 192U,
488*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x3_weights.npy", weights_layout),
489*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
490*c217d954SCole Faust                                      PadStrideInfo(1, 1, 1, 0))
491*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0b_1x3/convolution")
492*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x3_BatchNorm_moving_mean.npy"),
493*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x3_BatchNorm_moving_variance.npy"),
494*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
495*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0b_1x3_BatchNorm_beta.npy"),
496*c217d954SCole Faust                                             batch_norm_epsilon)
497*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0b_1x3/BatchNorm")
498*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0b_1x3/Relu")
499*c217d954SCole Faust                  << ConvolutionLayer(1U, 3U, 192U,
500*c217d954SCole Faust                                      get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_3x1_weights.npy", weights_layout),
501*c217d954SCole Faust                                      std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
502*c217d954SCole Faust                                      PadStrideInfo(1, 1, 0, 1))
503*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0c_3x1/convolution")
504*c217d954SCole Faust                  << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_3x1_BatchNorm_moving_mean.npy"),
505*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_3x1_BatchNorm_moving_variance.npy"),
506*c217d954SCole Faust                                             get_random_accessor(1.f, 1.f),
507*c217d954SCole Faust                                             get_weights_accessor(data_path, unit_path + "Branch_1_Conv2d_0c_3x1_BatchNorm_beta.npy"),
508*c217d954SCole Faust                                             batch_norm_epsilon)
509*c217d954SCole Faust                  .set_name(unit_name + "Branch_1/Conv2d_0c_3x1/BatchNorm")
510*c217d954SCole Faust                  << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Branch_1/Conv2d_0c_3x1/Relu");
511*c217d954SCole Faust 
512*c217d954SCole Faust             // Concatenate
513*c217d954SCole Faust             i_l << ConcatLayer(std::move(i_la), std::move(i_lb)).set_name(unit_name + "concat")
514*c217d954SCole Faust                 << ConvolutionLayer(1U, 1U, 1792U,
515*c217d954SCole Faust                                     get_weights_accessor(data_path, unit_path + "Conv2d_1x1_weights.npy", weights_layout),
516*c217d954SCole Faust                                     get_weights_accessor(data_path, unit_path + "Conv2d_1x1_biases.npy", weights_layout),
517*c217d954SCole Faust                                     PadStrideInfo(1, 1, 0, 0))
518*c217d954SCole Faust                 .set_name(unit_name + "Conv2d_1x1/convolution");
519*c217d954SCole Faust 
520*c217d954SCole Faust             // Scale result
521*c217d954SCole Faust             if(scale != 1.f)
522*c217d954SCole Faust             {
523*c217d954SCole Faust                 i_l << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, scale, 0.f)).set_name(unit_name + "mul");
524*c217d954SCole Faust             }
525*c217d954SCole Faust 
526*c217d954SCole Faust             // Residual add
527*c217d954SCole Faust             graph << EltwiseLayer(std::move(i_l), std::move(i_r), EltwiseOperation::Add).set_name(unit_name + "add");
528*c217d954SCole Faust 
529*c217d954SCole Faust             // Apply activation if needed
530*c217d954SCole Faust             if(has_activation)
531*c217d954SCole Faust             {
532*c217d954SCole Faust                 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
533*c217d954SCole Faust             }
534*c217d954SCole Faust         }
535*c217d954SCole Faust     }
536*c217d954SCole Faust 
reduction_a(const std::string & data_path,DataLayout weights_layout)537*c217d954SCole Faust     void reduction_a(const std::string &data_path, DataLayout weights_layout)
538*c217d954SCole Faust     {
539*c217d954SCole Faust         // Branch 0
540*c217d954SCole Faust         SubStream i_a(graph);
541*c217d954SCole Faust         i_a << ConvolutionLayer(3U, 3U, 384U,
542*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_6a_Branch_0_Conv2d_1a_3x3_weights.npy", weights_layout),
543*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
544*c217d954SCole Faust                                 PadStrideInfo(2, 2, 0, 0))
545*c217d954SCole Faust             .set_name("Mixed_6a/Branch_0/Conv2d_1a_3x3/convolution")
546*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_6a_Branch_0_Conv2d_1a_3x3_BatchNorm_moving_mean.npy"),
547*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_0_Conv2d_1a_3x3_BatchNorm_moving_variance.npy"),
548*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
549*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_0_Conv2d_1a_3x3_BatchNorm_beta.npy"),
550*c217d954SCole Faust                                        batch_norm_epsilon)
551*c217d954SCole Faust             .set_name("Mixed_6a/Branch_0/Conv2d_1a_3x3/BatchNorm")
552*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_6a/Branch_0/Conv2d_1a_3x3/Relu");
553*c217d954SCole Faust 
554*c217d954SCole Faust         // Branch 1
555*c217d954SCole Faust         SubStream i_b(graph);
556*c217d954SCole Faust         i_b << ConvolutionLayer(1U, 1U, 192U,
557*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0a_1x1_weights.npy", weights_layout),
558*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
559*c217d954SCole Faust                                 PadStrideInfo(1, 1, 0, 0))
560*c217d954SCole Faust             .set_name("Mixed_6a/Branch_1/Conv2d_0a_1x1/convolution")
561*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
562*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
563*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
564*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0a_1x1_BatchNorm_beta.npy"),
565*c217d954SCole Faust                                        batch_norm_epsilon)
566*c217d954SCole Faust             .set_name("Mixed_6a/Branch_1/Conv2d_0a_1x1/BatchNorm")
567*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_6a/Branch_1/Conv2d_0a_1x1/Relu")
568*c217d954SCole Faust             << ConvolutionLayer(3U, 3U, 192U,
569*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0b_3x3_weights.npy", weights_layout),
570*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
571*c217d954SCole Faust                                 PadStrideInfo(1, 1, 1, 1))
572*c217d954SCole Faust             .set_name("Mixed_6a/Branch_1/Conv2d_0b_3x3/convolution")
573*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0b_3x3_BatchNorm_moving_mean.npy"),
574*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0b_3x3_BatchNorm_moving_variance.npy"),
575*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
576*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_0b_3x3_BatchNorm_beta.npy"),
577*c217d954SCole Faust                                        batch_norm_epsilon)
578*c217d954SCole Faust             .set_name("Mixed_6a/Branch_1/Conv2d_0b_3x3/BatchNorm")
579*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_6a/Branch_1/Conv2d_0b_3x3/Relu")
580*c217d954SCole Faust             << ConvolutionLayer(3U, 3U, 256U,
581*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_1a_3x3_weights.npy", weights_layout),
582*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
583*c217d954SCole Faust                                 PadStrideInfo(2, 2, 0, 0))
584*c217d954SCole Faust             .set_name("Mixed_6a/Branch_1/Conv2d_1a_3x3/convolution")
585*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_1a_3x3_BatchNorm_moving_mean.npy"),
586*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_1a_3x3_BatchNorm_moving_variance.npy"),
587*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
588*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_6a_Branch_1_Conv2d_1a_3x3_BatchNorm_beta.npy"),
589*c217d954SCole Faust                                        batch_norm_epsilon)
590*c217d954SCole Faust             .set_name("Mixed_6a/Branch_1/Conv2d_1a_3x3/BatchNorm")
591*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_6a/Branch_1/Conv2d_1a_3x3/Relu");
592*c217d954SCole Faust 
593*c217d954SCole Faust         // Branch 2
594*c217d954SCole Faust         SubStream i_c(graph);
595*c217d954SCole Faust         i_c << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, common_params.data_layout, PadStrideInfo(2, 2, 0, 0), true)).set_name("Mixed_6a/Branch_2/MaxPool_1a_3x3");
596*c217d954SCole Faust 
597*c217d954SCole Faust         // Concatenate
598*c217d954SCole Faust         graph << ConcatLayer(std::move(i_a), std::move(i_b), std::move(i_c)).set_name("Mixed_6a/concat");
599*c217d954SCole Faust     }
600*c217d954SCole Faust 
reduction_b(const std::string & data_path,DataLayout weights_layout)601*c217d954SCole Faust     void reduction_b(const std::string &data_path, DataLayout weights_layout)
602*c217d954SCole Faust     {
603*c217d954SCole Faust         // Branch 0
604*c217d954SCole Faust         SubStream i_a(graph);
605*c217d954SCole Faust         i_a << ConvolutionLayer(1U, 1U, 256U,
606*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_0a_1x1_weights.npy", weights_layout),
607*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
608*c217d954SCole Faust                                 PadStrideInfo(1, 1, 0, 0))
609*c217d954SCole Faust             .set_name("Mixed_7a/Branch_0/Conv2d_0a_1x1/convolution")
610*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
611*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
612*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
613*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_0a_1x1_BatchNorm_beta.npy"),
614*c217d954SCole Faust                                        batch_norm_epsilon)
615*c217d954SCole Faust             .set_name("Mixed_7a/Branch_0/Conv2d_0a_1x1/BatchNorm")
616*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_0/Conv2d_0a_1x1/Relu")
617*c217d954SCole Faust             << ConvolutionLayer(3U, 3U, 384U,
618*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_1a_3x3_weights.npy", weights_layout),
619*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
620*c217d954SCole Faust                                 PadStrideInfo(2, 2, 0, 0))
621*c217d954SCole Faust             .set_name("Mixed_7a/Branch_0/Conv2d_1a_3x3/convolution")
622*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_1a_3x3_BatchNorm_moving_mean.npy"),
623*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_1a_3x3_BatchNorm_moving_variance.npy"),
624*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
625*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_0_Conv2d_1a_3x3_BatchNorm_beta.npy"),
626*c217d954SCole Faust                                        batch_norm_epsilon)
627*c217d954SCole Faust             .set_name("Mixed_7a/Branch_0/Conv2d_1a_3x3/BatchNorm")
628*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_0/Conv2d_1a_3x3/Relu");
629*c217d954SCole Faust 
630*c217d954SCole Faust         // Branch 1
631*c217d954SCole Faust         SubStream i_b(graph);
632*c217d954SCole Faust         i_b << ConvolutionLayer(1U, 1U, 256U,
633*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_0a_1x1_weights.npy", weights_layout),
634*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
635*c217d954SCole Faust                                 PadStrideInfo(1, 1, 0, 0))
636*c217d954SCole Faust             .set_name("Mixed_7a/Branch_1/Conv2d_0a_1x1/convolution")
637*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
638*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
639*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
640*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_0a_1x1_BatchNorm_beta.npy"),
641*c217d954SCole Faust                                        batch_norm_epsilon)
642*c217d954SCole Faust             .set_name("Mixed_7a/Branch_1/Conv2d_0a_1x1/BatchNorm")
643*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_1/Conv2d_0a_1x1/Relu")
644*c217d954SCole Faust             << ConvolutionLayer(3U, 3U, 256U,
645*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_1a_3x3_weights.npy", weights_layout),
646*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
647*c217d954SCole Faust                                 PadStrideInfo(2, 2, 0, 0))
648*c217d954SCole Faust             .set_name("Mixed_7a/Branch_1/Conv2d_1a_3x3/convolution")
649*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_1a_3x3_BatchNorm_moving_mean.npy"),
650*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_1a_3x3_BatchNorm_moving_variance.npy"),
651*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
652*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_1_Conv2d_1a_3x3_BatchNorm_beta.npy"),
653*c217d954SCole Faust                                        batch_norm_epsilon)
654*c217d954SCole Faust             .set_name("Mixed_7a/Branch_1/Conv2d_1a_3x3/BatchNorm")
655*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_1/Conv2d_1a_3x3/Relu");
656*c217d954SCole Faust 
657*c217d954SCole Faust         // Branch 2
658*c217d954SCole Faust         SubStream i_c(graph);
659*c217d954SCole Faust         i_c << ConvolutionLayer(1U, 1U, 256U,
660*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0a_1x1_weights.npy", weights_layout),
661*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
662*c217d954SCole Faust                                 PadStrideInfo(1, 1, 0, 0))
663*c217d954SCole Faust             .set_name("Mixed_7a/Branch_2/Conv2d_0a_1x1/convolution")
664*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0a_1x1_BatchNorm_moving_mean.npy"),
665*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0a_1x1_BatchNorm_moving_variance.npy"),
666*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
667*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0a_1x1_BatchNorm_beta.npy"),
668*c217d954SCole Faust                                        batch_norm_epsilon)
669*c217d954SCole Faust             .set_name("Mixed_7a/Branch_2/Conv2d_0a_1x1/BatchNorm")
670*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_2/Conv2d_0a_1x1/Relu")
671*c217d954SCole Faust             << ConvolutionLayer(3U, 3U, 256U,
672*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0b_3x3_weights.npy", weights_layout),
673*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
674*c217d954SCole Faust                                 PadStrideInfo(1, 1, 1, 1))
675*c217d954SCole Faust             .set_name("Mixed_7a/Branch_2/Conv2d_0b_3x3/convolution")
676*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0b_3x3_BatchNorm_moving_mean.npy"),
677*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0b_3x3_BatchNorm_moving_variance.npy"),
678*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
679*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_0b_3x3_BatchNorm_beta.npy"),
680*c217d954SCole Faust                                        batch_norm_epsilon)
681*c217d954SCole Faust             .set_name("Mixed_7a/Branch_2/Conv2d_0b_3x3/BatchNorm")
682*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_2/Conv2d_0b_3x3/Relu")
683*c217d954SCole Faust             << ConvolutionLayer(3U, 3U, 256U,
684*c217d954SCole Faust                                 get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_1a_3x3_weights.npy", weights_layout),
685*c217d954SCole Faust                                 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
686*c217d954SCole Faust                                 PadStrideInfo(2, 2, 0, 0))
687*c217d954SCole Faust             .set_name("Mixed_7a/Branch_2/Conv2d_1a_3x3/convolution")
688*c217d954SCole Faust             << BatchNormalizationLayer(get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_1a_3x3_BatchNorm_moving_mean.npy"),
689*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_1a_3x3_BatchNorm_moving_variance.npy"),
690*c217d954SCole Faust                                        get_random_accessor(1.f, 1.f),
691*c217d954SCole Faust                                        get_weights_accessor(data_path, "Mixed_7a_Branch_2_Conv2d_1a_3x3_BatchNorm_beta.npy"),
692*c217d954SCole Faust                                        batch_norm_epsilon)
693*c217d954SCole Faust             .set_name("Mixed_7a/Branch_2/Conv2d_1a_3x3/BatchNorm")
694*c217d954SCole Faust             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Mixed_7a/Branch_2/Conv2d_1a_3x3/Relu");
695*c217d954SCole Faust 
696*c217d954SCole Faust         // Branch 3
697*c217d954SCole Faust         SubStream i_d(graph);
698*c217d954SCole Faust         i_d << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, common_params.data_layout, PadStrideInfo(2, 2, 0, 0), true)).set_name("Mixed_7a/Branch_3/MaxPool_1a_3x3");
699*c217d954SCole Faust 
700*c217d954SCole Faust         // Concatenate
701*c217d954SCole Faust         graph << ConcatLayer(std::move(i_a), std::move(i_b), std::move(i_c), std::move(i_d)).set_name("Mixed_7a/concat");
702*c217d954SCole Faust     }
703*c217d954SCole Faust };
704*c217d954SCole Faust 
705*c217d954SCole Faust /** Main program for Inception ResNet V1
706*c217d954SCole Faust  *
707*c217d954SCole Faust  * Model is based on:
708*c217d954SCole Faust  *      https://arxiv.org/abs/1602.07261
709*c217d954SCole Faust  *      "Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning"
710*c217d954SCole Faust  *      Christian Szegedy, Sergey Ioffe, Vincent Vanhoucke, Alex Alemi
711*c217d954SCole Faust  *
712*c217d954SCole Faust  * @note To list all the possible arguments execute the binary appended with the --help option
713*c217d954SCole Faust  *
714*c217d954SCole Faust  * @param[in] argc Number of arguments
715*c217d954SCole Faust  * @param[in] argv Arguments
716*c217d954SCole Faust  */
main(int argc,char ** argv)717*c217d954SCole Faust int main(int argc, char **argv)
718*c217d954SCole Faust {
719*c217d954SCole Faust     return arm_compute::utils::run_example<InceptionResNetV1Example>(argc, argv);
720*c217d954SCole Faust }
721