ModelZoo/Vision/MobileNet.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 199 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 use strict;
19 use warnings;
20 use AI::MXNet::Function::Parameters;
21 package AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::RELU6;
22 use AI::MXNet::Gluon::Mouse;
23 extends 'AI::MXNet::Gluon::HybridBlock';
24  
25 method hybrid_forward(GluonClass $F, GluonInput $x)
26 {
27 return $F->clip($x, a_min => 0, a_max => 6, name=>"relu6");
28 }
29  
30 package AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::LinearBottleneck;
31 use AI::MXNet::Gluon::Mouse;
32 extends 'AI::MXNet::Gluon::HybridBlock';
33 has [qw/in_channels channels t stride/] => (is => 'ro', isa => 'Int', required => 1);
34 method python_constructor_arguments(){ [qw/in_channels channels t stride/] }
35  
36 =head1 NAME
37  
38 AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::LinearBottleneck - LinearBottleneck used in MobileNetV2 model
39 =cut
40  
41 =head1 DESCRIPTION
42  
43 LinearBottleneck used in MobileNetV2 model from the
44 "Inverted Residuals and Linear Bottlenecks:
45 Mobile Networks for Classification, Detection and Segmentation"
46 <https://arxiv.org/abs/1801.04381> paper.
47  
48 Parameters
49 ----------
50 in_channels : Int
51 Number of input channels.
52 channels : Int
53 Number of output channels.
54 t : Int
55 Layer expansion ratio.
56 stride : Int
57 stride
58 =cut
59  
60 func _add_conv(
61 $out, $channels, :$kernel=1, :$stride=1, :$pad=0,
62 :$num_group=1, :$active=1, :$relu6=0
63 )
64 {
65 $out->add(nn->Conv2D($channels, $kernel, $stride, $pad, groups=>$num_group, use_bias=>0));
66 $out->add(nn->BatchNorm(scale=>1));
67 if($active)
68 {
69 $out->add($relu6 ? AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::RELU6->new : nn->Activation('relu'));
70 }
71 }
72  
73 sub BUILD
74 {
75 my $self = shift;
76 $self->use_shortcut($self->stride == 1 and $self->in_channels == $self->channels);
77 $self->name_scope(sub {
78 $self->out(nn->HybridSequential());
79 _add_conv($self->out, $self->in_channels * $self->t, relu6=>1);
80 _add_conv(
81 $self->out, $self->in_channels * $self->t, kernel=>3, stride=>$self->stride,
82 pad=>1, num_group=>$self->in_channels * $self->t, relu6=>1
83 );
84 _add_conv($self->out, $self->channels, active=>0, relu6=>1);
85 });
86 }
87  
88 method hybrid_forward($F, $x)
89 {
90 my $out = $self->out->($x);
91 if($self->use_shortcut)
92 {
93 $out = $F->elemwise_add($out, $x);
94 }
95 return $out;
96 }
97  
98 package AI::MXNet::Gluon::ModelZoo::Vision::MobileNet;
99 use AI::MXNet::Gluon::Mouse;
100 use AI::MXNet::Base;
101 extends 'AI::MXNet::Gluon::HybridBlock';
102 has 'multiplier' => (is => 'ro', isa => 'Num', default => 1);
103 has 'classes' => (is => 'ro', isa => 'Int', default => 1000);
104 method python_constructor_arguments(){ [qw/multiplier classes/] }
105  
106 =head1 NAME
107  
108 AI::MXNet::Gluon::ModelZoo::Vision::MobileNet - MobileNet model from the
109 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
110 =cut
111  
112 =head1 DESCRIPTION
113  
114 MobileNet model from the
115 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
116 <https://arxiv.org/abs/1704.04861> paper.
117  
118 Parameters
119 ----------
120 multiplier : Num, default 1.0
121 The width multiplier for controling the model size. Only multipliers that are no
122 less than 0.25 are supported. The actual number of channels is equal to the original
123 channel size multiplied by this multiplier.
124 classes : Int, default 1000
125 Number of classes for the output layer.
126 =cut
127  
128 func _add_conv(
129 $out, :$channels=1, :$kernel=1, :$stride=1, :$pad=0,
130 :$num_group=1, :$active=1, :$relu6=0
131 )
132 {
133 $out->add(nn->Conv2D($channels, $kernel, $stride, $pad, groups=>$num_group, use_bias=>0));
134 $out->add(nn->BatchNorm(scale=>1));
135 if($active)
136 {
137 $out->add($relu6 ? AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::RELU6->new : nn->Activation('relu'));
138 }
139 }
140  
141  
142 func _add_conv_dw($out, :$dw_channels=, :$channels=, :$stride=, :$relu6=0)
143 {
144 _add_conv($out, channels=>$dw_channels, kernel=>3, stride=>$stride,
145 pad=>1, num_group=>$dw_channels, relu6=>$relu6);
146 _add_conv($out, channels=>$channels, relu6=>$relu6);
147 }
148  
149 sub BUILD
150 {
151 my $self = shift;
152 $self->name_scope(sub {
153 $self->features(nn->HybridSequential(prefix=>''));
154 $self->features->name_scope(sub {
155 _add_conv($self->features, channels=>int(32 * $self->multiplier), kernel=>3, pad=>1, stride=>2);
156 my $dw_channels = [map { int($_ * $self->multiplier) } (32, 64, (128)x2, (256)x2, (512)x6, 1024)];
157 my $channels = [map { int($_ * $self->multiplier) } (64, (128)x2, (256)x2, (512)x6, (1024)x2)];
158 my $strides = [(1, 2)x3, (1)x5, 2, 1];
159 for(zip($dw_channels, $channels, $strides))
160 {
161 my ($dwc, $c, $s) = @$_;
162 _add_conv_dw($self->features, dw_channels=>$dwc, channels=>$c, stride=>$s);
163 }
164 $self->features->add(nn->GlobalAvgPool2D());
165 $self->features->add(nn->Flatten());
166 });
167 $self->output(nn->Dense($self->classes));
168 });
169 }
170  
171 method hybrid_forward(GluonClass $F, GluonInput $x)
172 {
173 $x = $self->features->($x);
174 $x = $self->output->($x);
175 return $x;
176 }
177  
178 package AI::MXNet::Gluon::ModelZoo::Vision::MobileNetV2;
179 use AI::MXNet::Gluon::Mouse;
180 use AI::MXNet::Base;
181 extends 'AI::MXNet::Gluon::HybridBlock';
182 has 'multiplier' => (is => 'ro', isa => 'Num', default => 1);
183 has 'classes' => (is => 'ro', isa => 'Int', default => 1000);
184 method python_constructor_arguments(){ [qw/multiplier classes/] }
185  
186 =head1 NAME
187  
188 AI::MXNet::Gluon::ModelZoo::Vision::MobileNetV2 - MobileNet model from the
189 "Inverted Residuals and Linear Bottlenecks: Mobile Networks for Classification, Detection and Segmentation"
190 =cut
191  
192 =head1 DESCRIPTION
193  
194 MobileNetV2 model from the
195 "Inverted Residuals and Linear Bottlenecks:
196 Mobile Networks for Classification, Detection and Segmentation"
197 <https://arxiv.org/abs/1801.04381> paper.
198  
199 Parameters
200 ----------
201 multiplier : Num, default 1.0
202 The width multiplier for controling the model size. Only multipliers that are no
203 less than 0.25 are supported. The actual number of channels is equal to the original
204 channel size multiplied by this multiplier.
205 classes : Int, default 1000
206 Number of classes for the output layer.
207 =cut
208  
209 func _add_conv(
210 $out, $channels, :$kernel=1, :$stride=1, :$pad=0,
211 :$num_group=1, :$active=1, :$relu6=0
212 )
213 {
214 $out->add(nn->Conv2D($channels, $kernel, $stride, $pad, groups=>$num_group, use_bias=>0));
215 $out->add(nn->BatchNorm(scale=>1));
216 if($active)
217 {
218 $out->add($relu6 ? AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::RELU6->new : nn->Activation('relu'));
219 }
220 }
221  
222 sub BUILD
223 {
224 my $self = shift;
225 $self->name_scope(sub {
226 $self->features(nn->HybridSequential(prefix=>'features_'));
227 $self->features->name_scope(sub {
228 _add_conv(
229 $self->features, int(32 * $self->multiplier), kernel=>3,
230 stride=>2, pad=>1, relu6=>1
231 );
232  
233 my $in_channels_group = [map { int($_ * $self->multiplier) } (32, 16, (24)x2, (32)x3, (64)x4, (96)x3, (160)x3)];
234 my $channels_group = [map { int($_ * $self->multiplier) } (16, (24)x2, (32)x3, (64)x4, (96)x3, (160)x3, 320)];
235 my $ts = [1, (6)x16];
236 my $strides = [(1, 2)x2, 1, 1, 2, (1)x6, 2, (1)x3];
237  
238 for(zip($in_channels_group, $channels_group, $ts, $strides))
239 {
240 my ($in_c, $c, $t, $s) = @$_;
241 $self->features->add(
242 AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::LinearBottleneck->new(
243 in_channels=>$in_c, channels=>$c,
244 t=>$t, stride=>$s
245 )
246 );
247 }
248  
249 my $last_channels = $self->multiplier > 1 ? int(1280 * $self->multiplier) : 1280;
250 _add_conv($self->features, $last_channels, relu6=>1);
251 $self->features->add(nn->GlobalAvgPool2D());
252 });
253  
254 $self->output(nn->HybridSequential(prefix=>'output_'));
255 $self->output->name_scope(sub {
256 $self->output->add(
257 nn->Conv2D($self->classes, 1, use_bias=>0, prefix=>'pred_'),
258 nn->Flatten()
259 );
260 });
261 });
262 }
263  
264 method hybrid_forward(GluonClass $F, GluonInput $x)
265 {
266 $x = $self->features->($x);
267 $x = $self->output->($x);
268 return $x;
269 }
270  
271 package AI::MXNet::Gluon::ModelZoo::Vision;
272  
273 =head2 get_mobilenet
274  
275 MobileNet model from the
276 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
277 <https://arxiv.org/abs/1704.04861> paper.
278  
279 Parameters
280 ----------
281 $multiplier : Num
282 The width multiplier for controling the model size. Only multipliers that are no
283 less than 0.25 are supported. The actual number of channels is equal to the original
284 channel size multiplied by this multiplier.
285 :$pretrained : Bool, default 0
286 Whether to load the pretrained weights for model.
287 :$ctx : AI::MXNet::Context, default CPU
288 The context in which to load the pretrained weights.
289 :$root : Str, default '~/.mxnet/models'
290 Location for keeping the model parameters.
291 =cut
292  
293 method get_mobilenet(
294 Num $multiplier, Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(),
295 Str :$root='~/.mxnet/models'
296 )
297 {
298 my $net = AI::MXNet::Gluon::ModelZoo::Vision::MobileNet->new($multiplier);
299 if($pretrained)
300 {
301 my $version_suffix = sprintf("%.2f", $multiplier);
302 if($version_suffix eq '1.00' or $version_suffix eq '0.50')
303 {
304 $version_suffix =~ s/.$//;
305 }
306 $net->load_parameters(
307 AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file(
308 "mobilenet$version_suffix",
309 root=>$root
310 ),
311 ctx=>$ctx
312 );
313 }
314 return $net;
315 }
316  
317 =head2 get_mobilenet_v2
318  
319 MobileNetV2 model from the
320 "Inverted Residuals and Linear Bottlenecks:
321 Mobile Networks for Classification, Detection and Segmentation"
322 <https://arxiv.org/abs/1801.04381> paper.
323  
324 Parameters
325 ----------
326 $multiplier : Num
327 The width multiplier for controling the model size. Only multipliers that are no
328 less than 0.25 are supported. The actual number of channels is equal to the original
329 channel size multiplied by this multiplier.
330 :$pretrained : Bool, default 0
331 Whether to load the pretrained weights for model.
332 :$ctx : AI::MXNet::Context, default CPU
333 The context in which to load the pretrained weights.
334 :$root : Str, default '~/.mxnet/models'
335 Location for keeping the model parameters.
336 =cut
337  
338 method get_mobilenet_v2(
339 Num $multiplier, Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(),
340 Str :$root='~/.mxnet/models'
341 )
342 {
343 my $net = AI::MXNet::Gluon::ModelZoo::Vision::MobileNetV2->new($multiplier);
344 if($pretrained)
345 {
346 my $version_suffix = sprintf("%.2f", $multiplier);
347 if($version_suffix eq '1.00' or $version_suffix eq '0.50')
348 {
349 $version_suffix =~ s/.$//;
350 }
351 $net->load_parameters(
352 AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file(
353 "mobilenetv2_$version_suffix",
354 root=>$root
355 ),
356 ctx=>$ctx
357 );
358 }
359 return $net;
360 }
361  
362 =head2 mobilenet1_0
363  
364 MobileNet model from the
365 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
366 <https://arxiv.org/abs/1704.04861> paper, with width multiplier 1.0.
367  
368 Parameters
369 ----------
370 :$pretrained : Bool, default 0
371 Whether to load the pretrained weights for model.
372 :$ctx : AI::MXNet::Context, default CPU
373 The context in which to load the pretrained weights.
374 =cut
375  
376 method mobilenet1_0(%kwargs)
377 {
378 return __PACKAGE__->get_mobilenet(1.0, %kwargs);
379 }
380  
381 =head2 mobilenet_v2_1_0
382  
383 MobileNetV2 model from the
384 "Inverted Residuals and Linear Bottlenecks:
385 Mobile Networks for Classification, Detection and Segmentation"
386 <https://arxiv.org/abs/1801.04381> paper.
387  
388 Parameters
389 ----------
390 :$pretrained : Bool, default 0
391 Whether to load the pretrained weights for model.
392 :$ctx : AI::MXNet::Context, default CPU
393 The context in which to load the pretrained weights.
394 =cut
395  
396 method mobilenet_v2_1_0(%kwargs)
397 {
398 return __PACKAGE__->get_mobilenet_v2(1.0, %kwargs);
399 }
400  
401 =head2 mobilenet0_75
402  
403 MobileNet model from the
404 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
405 <https://arxiv.org/abs/1704.04861> paper, with width multiplier 0.75.
406  
407 Parameters
408 ----------
409 :$pretrained : Bool, default 0
410 Whether to load the pretrained weights for model.
411 :$ctx : AI::MXNet::Context, default CPU
412 The context in which to load the pretrained weights.
413 =cut
414  
415 method mobilenet0_75(%kwargs)
416 {
417 return __PACKAGE__->get_mobilenet(0.75, %kwargs);
418 }
419  
420 =head2 mobilenet_v2_0_75
421  
422 MobileNetV2 model from the
423 "Inverted Residuals and Linear Bottlenecks:
424 Mobile Networks for Classification, Detection and Segmentation"
425 <https://arxiv.org/abs/1801.04381> paper.
426  
427 Parameters
428 ----------
429 :$pretrained : Bool, default 0
430 Whether to load the pretrained weights for model.
431 :$ctx : AI::MXNet::Context, default CPU
432 The context in which to load the pretrained weights.
433 =cut
434  
435 method mobilenet_v2_0_75(%kwargs)
436 {
437 return __PACKAGE__->get_mobilenet_v2(0.75, %kwargs);
438 }
439  
440 =head2 mobilenet0_5
441  
442 MobileNet model from the
443 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
444 <https://arxiv.org/abs/1704.04861> paper, with width multiplier 0.5.
445  
446 Parameters
447 ----------
448 :$pretrained : Bool, default 0
449 Whether to load the pretrained weights for model.
450 :$ctx : AI::MXNet::Context, default CPU
451 The context in which to load the pretrained weights.
452 =cut
453  
454 method mobilenet0_5(%kwargs)
455 {
456 return __PACKAGE__->get_mobilenet(0.5, %kwargs);
457 }
458  
459 =head2 mobilenet_v2_0_5
460  
461 MobileNetV2 model from the
462 "Inverted Residuals and Linear Bottlenecks:
463 Mobile Networks for Classification, Detection and Segmentation"
464 <https://arxiv.org/abs/1801.04381> paper.
465  
466 Parameters
467 ----------
468 :$pretrained : Bool, default 0
469 Whether to load the pretrained weights for model.
470 :$ctx : AI::MXNet::Context, default CPU
471 The context in which to load the pretrained weights.
472 =cut
473  
474 method mobilenet_v2_0_5(%kwargs)
475 {
476 return __PACKAGE__->get_mobilenet_v2(0.5, %kwargs);
477 }
478  
479 =head2 mobilenet0_25
480  
481 MobileNet model from the
482 "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
483 <https://arxiv.org/abs/1704.04861> paper, with width multiplier 0.25.
484  
485 Parameters
486 ----------
487 :$pretrained : Bool, default 0
488 Whether to load the pretrained weights for model.
489 :$ctx : AI::MXNet::Context, default CPU
490 The context in which to load the pretrained weights.
491 =cut
492  
493 method mobilenet0_25(%kwargs)
494 {
495 return __PACKAGE__->get_mobilenet(0.25, %kwargs);
496 }
497  
498 =head2 mobilenet_v2_0_25
499  
500 MobileNetV2 model from the
501 "Inverted Residuals and Linear Bottlenecks:
502 Mobile Networks for Classification, Detection and Segmentation"
503 <https://arxiv.org/abs/1801.04381> paper.
504  
505 Parameters
506 ----------
507 :$pretrained : Bool, default 0
508 Whether to load the pretrained weights for model.
509 :$ctx : AI::MXNet::Context, default CPU
510 The context in which to load the pretrained weights.
511 =cut
512  
513 method mobilenet_v2_0_25(%kwargs)
514 {
515 return __PACKAGE__->get_mobilenet_v2(0.25, %kwargs);
516 }
517  
518 1;