ModelZoo/Vision/VGG.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 93 CC 1 SCAR 6.9
CC
1
Cov
0%
BUILD
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::VGG;
22 use AI::MXNet::Gluon::Mouse;
23 extends 'AI::MXNet::Gluon::HybridBlock';
24 use AI::MXNet::Base;
25  
26 =head1 NAME
27  
28 AI::MXNet::Gluon::ModelZoo::Vision::VGG - VGG model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
29 =cut
30  
31 =head1 DESCRIPTION
32  
33 VGG model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
34 <https://arxiv.org/abs/1409.1556> paper.
35  
36 Parameters
37 ----------
38 layers : array ref of Int
39 Numbers of layers in each feature block.
40 filters : array ref of Int
41 Numbers of filters in each feature block. List length should match the layers.
42 classes : Int, default 1000
43 Number of classification classes.
44 batch_norm : Bool, default 0
45 Use batch normalization.
46 =cut
47 method python_constructor_arguments() { [qw/layers filters classes batch_norm/] }
48 has ['layers',
49 'filters'] => (is => 'ro', isa => 'ArrayRef[Int]', required => 1);
50 has 'classes' => (is => 'ro', isa => 'Int', default => 1000);
51 has 'batch_norm' => (is => 'ro', isa => 'Bool', default => 0);
52  
53 sub BUILD
54 {
55 my $self = shift;
56 assert(@{ $self->layers } == @{ $self->filters });
57 $self->name_scope(sub {
58 $self->features($self->_make_features());
59 $self->features->add(nn->Dense(4096, activation=>'relu',
60 weight_initializer=>'normal',
61 bias_initializer=>'zeros'));
62 $self->features->add(nn->Dropout(rate=>0.5));
63 $self->features->add(nn->Dense(4096, activation=>'relu',
64 weight_initializer=>'normal',
65 bias_initializer=>'zeros'));
66 $self->features->add(nn->Dropout(rate=>0.5));
67 $self->output(nn->Dense($self->classes,
68 weight_initializer=>'normal',
69 bias_initializer=>'zeros'));
70 });
71 }
72  
73 method _make_features()
74 {
75 my $featurizer = nn->HybridSequential(prefix=>'');
76 for(enumerate($self->layers))
77 {
78 my ($i, $num) = @$_;
79 for(0..$num-1)
80 {
81 $featurizer->add(
82 nn->Conv2D(
83 $self->filters->[$i], kernel_size => 3, padding => 1,
84 weight_initializer => mx->init->Xavier(
85 rnd_type => 'gaussian',
86 factor_type => 'out',
87 magnitude => 2
88 ),
89 bias_initializer=>'zeros'
90 )
91 );
92 if($self->batch_norm)
93 {
94 $featurizer->add(nn->BatchNorm());
95 }
96 $featurizer->add(nn->Activation('relu'));
97 }
98 $featurizer->add(nn->MaxPool2D(strides=>2));
99 }
100 return $featurizer;
101 }
102  
103 method hybrid_forward(GluonClass $F, GluonInput $x)
104 {
105 $x = $self->features->($x);
106 $x = $self->output->($x);
107 return $x;
108 }
109  
110 package AI::MXNet::Gluon::ModelZoo::Vision;
111  
112 # Specification
113 my %vgg_spec = (
114 11 => [[1, 1, 2, 2, 2], [64, 128, 256, 512, 512]],
115 13 => [[2, 2, 2, 2, 2], [64, 128, 256, 512, 512]],
116 16 => [[2, 2, 3, 3, 3], [64, 128, 256, 512, 512]],
117 19 => [[2, 2, 4, 4, 4], [64, 128, 256, 512, 512]]
118 );
119  
120 =head2 get_vgg
121  
122 VGG model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
123 <https://arxiv.org/abs/1409.1556> paper.
124  
125 Parameters
126 ----------
127 $num_layers : Int
128 Number of layers for the variant of densenet. Options are 11, 13, 16, 19.
129 :$pretrained : Bool, default 0
130 Whether to load the pretrained weights for model.
131 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
132 The context in which to load the pretrained weights.
133 :$root : Str, default '~/.mxnet/models'
134 Location for keeping the model parameters.
135 =cut
136  
137 method get_vgg(
138 Int $num_layers, Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(),
139 Str :$root='~/.mxnet/models', Int :$classes=1000, Bool :$batch_norm=0
140 )
141 {
142 my ($layers, $filters) = @{ $vgg_spec{$num_layers} };
143 my $net = AI::MXNet::Gluon::ModelZoo::Vision::VGG->new($layers, $filters, $classes, $batch_norm);
144 if($pretrained)
145 {
146 $net->load_parameters(
147 AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file(
148 "vgg$num_layers".($batch_norm ? '_bn' : ''),
149 root=>$root
150 ),
151 ctx=>$ctx
152 );
153 }
154 return $net;
155 }
156  
157 =head2 vgg11
158  
159 VGG-11 model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
160 <https://arxiv.org/abs/1409.1556> paper.
161  
162 Parameters
163 ----------
164 :$pretrained : Bool, default 0
165 Whether to load the pretrained weights for model.
166 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
167 The context in which to load the pretrained weights.
168 :$root : Str, default '~/.mxnet/models'
169 Location for keeping the model parameters.
170 =cut
171  
172 method vgg11(%kwargs)
173 {
174 return __PACKAGE__->get_vgg(11, %kwargs);
175 }
176  
177 =head2 vgg13
178  
179 VGG-13 model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
180 <https://arxiv.org/abs/1409.1556> paper.
181  
182 Parameters
183 ----------
184 :$pretrained : Bool, default 0
185 Whether to load the pretrained weights for model.
186 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
187 The context in which to load the pretrained weights.
188 :$root : Str, default '~/.mxnet/models'
189 Location for keeping the model parameters.
190 =cut
191  
192 method vgg13(%kwargs)
193 {
194 return __PACKAGE__->get_vgg(13, %kwargs);
195 }
196  
197 =head2 vgg16
198  
199 VGG-16 model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
200 <https://arxiv.org/abs/1409.1556> paper.
201  
202 Parameters
203 ----------
204 :$pretrained : Bool, default 0
205 Whether to load the pretrained weights for model.
206 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
207 The context in which to load the pretrained weights.
208 :$root : Str, default '~/.mxnet/models'
209 Location for keeping the model parameters.
210 =cut
211  
212 method vgg16(%kwargs)
213 {
214 return __PACKAGE__->get_vgg(16, %kwargs);
215 }
216  
217 =head2 vgg19
218  
219 VGG-19 model from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
220 <https://arxiv.org/abs/1409.1556> paper.
221  
222 Parameters
223 ----------
224 :$pretrained : Bool, default 0
225 Whether to load the pretrained weights for model.
226 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
227 The context in which to load the pretrained weights.
228 :$root : Str, default '~/.mxnet/models'
229 Location for keeping the model parameters.
230 =cut
231  
232 method vgg19(%kwargs)
233 {
234 return __PACKAGE__->get_vgg(19, %kwargs);
235 }
236  
237 =head2 vgg11_bn
238  
239 VGG-11 model with batch normalization from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
240 <https://arxiv.org/abs/1409.1556> paper.
241  
242 Parameters
243 ----------
244 :$pretrained : Bool, default 0
245 Whether to load the pretrained weights for model.
246 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
247 The context in which to load the pretrained weights.
248 :$root : Str, default '~/.mxnet/models'
249 Location for keeping the model parameters.
250 =cut
251  
252 method vgg11_bn(%kwargs)
253 {
254 $kwargs{batch_norm} = 1;
255 return __PACKAGE__->get_vgg(11, %kwargs);
256 }
257  
258 =head2 vgg13_bn
259  
260 VGG-13 model with batch normalization from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
261 <https://arxiv.org/abs/1409.1556> paper.
262  
263 Parameters
264 ----------
265 :$pretrained : Bool, default 0
266 Whether to load the pretrained weights for model.
267 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
268 The context in which to load the pretrained weights.
269 :$root : Str, default '~/.mxnet/models'
270 Location for keeping the model parameters.
271 =cut
272  
273 method vgg13_bn(%kwargs)
274 {
275 $kwargs{batch_norm} = 1;
276 return __PACKAGE__->get_vgg(13, %kwargs);
277 }
278  
279 =head2 vgg16_bn
280  
281 VGG-16 model with batch normalization from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
282 <https://arxiv.org/abs/1409.1556> paper.
283  
284 Parameters
285 ----------
286 :$pretrained : Bool, default 0
287 Whether to load the pretrained weights for model.
288 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
289 The context in which to load the pretrained weights.
290 :$root : Str, default '~/.mxnet/models'
291 Location for keeping the model parameters.
292 =cut
293  
294 method vgg16_bn(%kwargs)
295 {
296 $kwargs{batch_norm} = 1;
297 return __PACKAGE__->get_vgg(16, %kwargs);
298 }
299  
300 =head2 vgg19_bn
301  
302 VGG-19 model with batch normalization from the "Very Deep Convolutional Networks for Large-Scale Image Recognition"
303 <https://arxiv.org/abs/1409.1556> paper.
304  
305 Parameters
306 ----------
307 :$pretrained : Bool, default 0
308 Whether to load the pretrained weights for model.
309 :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu
310 The context in which to load the pretrained weights.
311 :$root : Str, default '~/.mxnet/models'
312 Location for keeping the model parameters.
313 =cut
314  
315 method vgg19_bn(%kwargs)
316 {
317 $kwargs{batch_norm} = 1;
318 return __PACKAGE__->get_vgg(19, %kwargs);
319 }
320  
321 1;