MXNet/Gluon/NN/ConvLayers.pm untested

filter: Statementstmt 0.0% 0 / 446 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 26 Podpod n/a totaltotal 0.0% 0 / 577 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 package AI::MXNet::Gluon::NN::Conv;
21 use AI::MXNet::Function::Parameters;
22 use AI::MXNet::Symbol;
23 use AI::MXNet::Gluon::Mouse;
24 extends 'AI::MXNet::Gluon::HybridBlock';
25  
26 func _infer_weight_shape($op_name, $data_shape, $kwargs)
27 {
28 my $sym = AI::MXNet::Symbol->$op_name(
29 AI::MXNet::Symbol->var('data', shape => $data_shape), %{ $kwargs }
30 );
31 return ($sym->infer_shape_partial)[0];
32 }
33  
34 =head1 NAME
35  
36 AI::MXNet::Gluon::NN::Conv
37 =cut
38  
39 =head1 DESCRIPTION
40  
41 Abstract nD convolution layer (private, used as implementation base).
42  
43 This layer creates a convolution kernel that is convolved
44 with the layer input to produce a tensor of outputs.
45 If `use_bias` is `True`, a bias vector is created and added to the outputs.
46 Finally, if `activation` is not `None`,
47 it is applied to the outputs as well.
48  
49 Parameters
50 ----------
51 channels : int
52 The dimensionality of the output space
53 i.e. the number of output channels in the convolution.
54 kernel_size : int or tuple/list of n ints
55 Specifies the dimensions of the convolution window.
56 strides: int or tuple/list of n ints,
57 Specifies the strides of the convolution.
58 padding : int or tuple/list of n ints,
59 If padding is non-zero, then the input is implicitly zero-padded
60 on both sides for padding number of points
61 dilation: int or tuple/list of n ints,
62 Specifies the dilation rate to use for dilated convolution.
63 groups : int
64 Controls the connections between inputs and outputs.
65 At groups=1, all inputs are convolved to all outputs.
66 At groups=2, the operation becomes equivalent to having two convolution
67 layers side by side, each seeing half the input channels, and producing
68 half the output channels, and both subsequently concatenated.
69 layout : str,
70 Dimension ordering of data and weight. Can be 'NCW', 'NWC', 'NCHW',
71 'NHWC', 'NCDHW', 'NDHWC', etc. 'N', 'C', 'H', 'W', 'D' stands for
72 batch, channel, height, width and depth dimensions respectively.
73 Convolution is performed over 'D', 'H', and 'W' dimensions.
74 in_channels : int, default 0
75 The number of input channels to this layer. If not specified,
76 initialization will be deferred to the first time `forward` is called
77 and `in_channels` will be inferred from the shape of input data.
78 activation : str
79 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
80 If you don't specify anything, no activation is applied
81 (ie. "linear" activation: `a(x) = x`).
82 use_bias: bool
83 Whether the layer uses a bias vector.
84 weight_initializer : str or `Initializer`
85 Initializer for the `weight` weights matrix.
86 bias_initializer: str or `Initializer`
87 Initializer for the bias vector.
88 =cut
89  
90 has 'channels' => (is => 'rw', isa => 'Int', required => 1);
91 has 'in_channels' => (is => 'rw', isa => 'Int', default => 0);
92 has 'kernel_size' => (is => 'rw', isa => 'DimSize|Shape', required => 1);
93 has [qw/strides
94 padding
95 dilation/] => (is => 'rw', isa => 'DimSize|Shape');
96 has 'groups' => (is => 'rw', isa => 'Int');
97 has [qw/layout
98 activation/] => (is => 'rw', isa => 'Str');
99 has 'op_name' => (is => 'rw', isa => 'Str', default => 'Convolution');
100 has 'use_bias' => (is => 'rw', isa => 'Bool', default => 1);
101 has 'weight_initializer' => (is => 'rw', isa => 'Maybe[Initializer]');
102 has 'bias_initializer' => (is => 'rw', isa => 'Maybe[Initializer]', default => 'zeros');
103 has 'adj' => (is => 'rw');
104 has [qw/weight bias
105 kwargs act/] => (is => 'rw', init_arg => undef);
106 method python_constructor_arguments() { [qw/channels kernel_size strides padding dilation/] }
107  
108 sub BUILD
109 {
110 my $self = shift;
111 $self->_update_kernel_size;
112 $self->name_scope(sub {
113 if(not ref $self->strides)
114 {
115 $self->strides([($self->strides) x @{ $self->kernel_size }]);
116 }
117 if(not ref $self->padding)
118 {
119 $self->padding([($self->padding) x @{ $self->kernel_size }]);
120 }
121 if(not ref $self->dilation)
122 {
123 $self->dilation([($self->dilation) x @{ $self->kernel_size }]);
124 }
125 $self->kwargs({
126 kernel => $self->kernel_size, stride => $self->strides, dilate => $self->dilation,
127 pad => $self->padding, num_filter => $self->channels, num_group => $self->groups,
128 no_bias => $self->use_bias ? 0 : 1, layout => $self->layout
129 });
130 if(defined $self->adj)
131 {
132 $self->kwargs->{adj} = $self->adj;
133 }
134  
135 my @dshape = (0)x(@{ $self->kernel_size } + 2);
136 $dshape[index($self->layout, 'N')] = 1;
137 $dshape[index($self->layout, 'C')] = $self->in_channels;
138 my $wshapes = _infer_weight_shape($self->op_name, \@dshape, $self->kwargs);
139 $self->weight(
140 $self->params->get(
141 'weight', shape => $wshapes->[1],
142 init => $self->weight_initializer,
143 allow_deferred_init => 1
144 )
145 );
146 if($self->use_bias)
147 {
148 $self->bias(
149 $self->params->get(
150 'bias', shape => $wshapes->[2],
151 init => $self->bias_initializer,
152 allow_deferred_init => 1
153 )
154 );
155 }
156 if(defined $self->activation)
157 {
158 $self->act(
159 AI::MXNet::Gluon::NN->Activation(
160 activation => $self->activation,
161 prefix => $self->activation.'_'
162 )
163 );
164 }
165 });
166 }
167  
168 method hybrid_forward(GluonClass $F, GluonInput $x, GluonInput :$weight, Maybe[GluonInput] :$bias=)
169 {
170 my $op_name = $self->op_name;
171 my $act = $F->$op_name($x, $weight, defined $bias ? $bias : (), name => 'fwd', %{ $self->kwargs });
172 if(defined $self->act)
173 {
174 $act = $self->act->($act);
175 }
176 return $act;
177 }
178  
179 method _alias() { 'conv' }
180  
181 use Data::Dumper;
182 use overload '""' => sub {
183 my $self = shift;
184 my $s = '%s(%s, kernel_size=(%s), stride=(%s)';
185 my $len_kernel_size = @{ $self->kwargs->{kernel} };
186 if(Dumper($self->kwargs->{pad}) ne Dumper([(0)x$len_kernel_size]))
187 {
188 $s .= ', padding=(' . join(',', @{ $self->kwargs->{pad} }) . ')';
189 }
190 if(Dumper($self->kwargs->{dilate}) ne Dumper([(1)x$len_kernel_size]))
191 {
192 $s .= ', dilation=(' . join(',', @{ $self->kwargs->{dilate} }) . ')';
193 }
194 if($self->can('out_pad') and Dumper($self->out_pad) ne Dumper([(0)x$len_kernel_size]))
195 {
196 $s .= ', output_padding=(' . join(',', @{ $self->kwargs->{dilate} }) . ')';
197 }
198 if($self->kwargs->{num_group} != 1)
199 {
200 $s .= ', groups=' . $self->kwargs->{num_group};
201 }
202 if(not defined $self->bias)
203 {
204 $s .= ', bias=False';
205 }
206 $s .= ')';
207 return sprintf(
208 $s,
209 $self->_class_name,
210 $self->in_channels
211 ? sprintf("%d -> %d", $self->in_channels, $self->channels)
212 : sprintf("%d", $self->channels),
213 join(',', @{ $self->kwargs->{kernel} }),
214 join(',', @{ $self->kwargs->{stride} })
215 );
216 };
217  
218 package AI::MXNet::Gluon::NN::Conv1D;
219 use AI::MXNet::Gluon::Mouse;
220 extends 'AI::MXNet::Gluon::NN::Conv';
221  
222 =head1 NAME
223  
224 AI::MXNet::Gluon::NN::Conv1D
225 =cut
226  
227 =head1 DESCRIPTION
228  
229 1D convolution layer (e.g. temporal convolution).
230  
231 This layer creates a convolution kernel that is convolved
232 with the layer input over a single spatial (or temporal) dimension
233 to produce a tensor of outputs.
234 If `use_bias` is True, a bias vector is created and added to the outputs.
235 Finally, if `activation` is not `None`,
236 it is applied to the outputs as well.
237  
238 If `in_channels` is not specified, `Parameter` initialization will be
239 deferred to the first time `forward` is called and `in_channels` will be
240 inferred from the shape of input data.
241  
242  
243 Parameters
244 ----------
245 channels : int
246 The dimensionality of the output space, i.e. the number of output
247 channels (filters) in the convolution.
248 kernel_size :int or tuple/list of 1 int
249 Specifies the dimensions of the convolution window.
250 strides : int or tuple/list of 1 int,
251 Specify the strides of the convolution.
252 padding : int or a tuple/list of 1 int,
253 If padding is non-zero, then the input is implicitly zero-padded
254 on both sides for padding number of points
255 dilation : int or tuple/list of 1 int
256 Specifies the dilation rate to use for dilated convolution.
257 groups : int
258 Controls the connections between inputs and outputs.
259 At groups=1, all inputs are convolved to all outputs.
260 At groups=2, the operation becomes equivalent to having two conv
261 layers side by side, each seeing half the input channels, and producing
262 half the output channels, and both subsequently concatenated.
263 layout: str, default 'NCW'
264 Dimension ordering of data and weight. Can be 'NCW', 'NWC', etc.
265 'N', 'C', 'W' stands for batch, channel, and width (time) dimensions
266 respectively. Convolution is applied on the 'W' dimension.
267 in_channels : int, default 0
268 The number of input channels to this layer. If not specified,
269 initialization will be deferred to the first time `forward` is called
270 and `in_channels` will be inferred from the shape of input data.
271 activation : str
272 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
273 If you don't specify anything, no activation is applied
274 (ie. "linear" activation: `a(x) = x`).
275 use_bias : bool
276 Whether the layer uses a bias vector.
277 weight_initializer : str or `Initializer`
278 Initializer for the `weight` weights matrix.
279 bias_initializer : str or `Initializer`
280 Initializer for the bias vector.
281  
282  
283 Input shape:
284 This depends on the `layout` parameter. Input is 3D array of shape
285 (batch_size, in_channels, width) if `layout` is `NCW`.
286  
287 Output shape:
288 This depends on the `layout` parameter. Output is 3D array of shape
289 (batch_size, channels, out_width) if `layout` is `NCW`.
290 out_width is calculated as::
291  
292 out_width = floor((width+2*padding-dilation*(kernel_size-1)-1)/stride)+1
293 =cut
294  
295 has '+strides' => (default => 1);
296 has '+padding' => (default => 0);
297 has '+dilation' => (default => 1);
298 has '+groups' => (default => 1);
299 has '+layout' => (default => 'NCW');
300  
301 method _update_kernel_size()
302 {
303 if(not ref $self->kernel_size)
304 {
305 $self->kernel_size([$self->kernel_size]);
306 }
307 confess("kernel_size must be a number or an array ref of 1 ints")
308 unless @{ $self->kernel_size } == 1;
309 }
310  
311 __PACKAGE__->register('AI::MXNet::Gluon::NN');
312  
313 package AI::MXNet::Gluon::NN::Conv2D;
314 use AI::MXNet::Gluon::Mouse;
315 extends 'AI::MXNet::Gluon::NN::Conv';
316  
317 =head1 NAME
318  
319 AI::MXNet::Gluon::NN::Conv2D
320 =cut
321  
322 =head1 DESCRIPTION
323  
324 2D convolution layer (e.g. spatial convolution over images).
325  
326 This layer creates a convolution kernel that is convolved
327 with the layer input to produce a tensor of
328 outputs. If `use_bias` is True,
329 a bias vector is created and added to the outputs. Finally, if
330 `activation` is not `None`, it is applied to the outputs as well.
331  
332 If `in_channels` is not specified, `Parameter` initialization will be
333 deferred to the first time `forward` is called and `in_channels` will be
334 inferred from the shape of input data.
335  
336 Parameters
337 ----------
338 channels : int
339 The dimensionality of the output space, i.e. the number of output
340 channels (filters) in the convolution.
341 kernel_size :int or tuple/list of 2 int
342 Specifies the dimensions of the convolution window.
343 strides : int or tuple/list of 2 int,
344 Specify the strides of the convolution.
345 padding : int or a tuple/list of 2 int,
346 If padding is non-zero, then the input is implicitly zero-padded
347 on both sides for padding number of points
348 dilation : int or tuple/list of 2 int
349 Specifies the dilation rate to use for dilated convolution.
350 groups : int
351 Controls the connections between inputs and outputs.
352 At groups=1, all inputs are convolved to all outputs.
353 At groups=2, the operation becomes equivalent to having two conv
354 layers side by side, each seeing half the input channels, and producing
355 half the output channels, and both subsequently concatenated.
356 layout : str, default 'NCHW'
357 Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc.
358 'N', 'C', 'H', 'W' stands for batch, channel, height, and width
359 dimensions respectively. Convolution is applied on the 'H' and
360 'W' dimensions.
361 in_channels : int, default 0
362 The number of input channels to this layer. If not specified,
363 initialization will be deferred to the first time `forward` is called
364 and `in_channels` will be inferred from the shape of input data.
365 activation : str
366 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
367 If you don't specify anything, no activation is applied
368 (ie. "linear" activation: `a(x) = x`).
369 use_bias : bool
370 Whether the layer uses a bias vector.
371 weight_initializer : str or `Initializer`
372 Initializer for the `weight` weights matrix.
373 bias_initializer : str or `Initializer`
374 Initializer for the bias vector.
375  
376  
377 Input shape:
378 This depends on the `layout` parameter. Input is 4D array of shape
379 (batch_size, in_channels, height, width) if `layout` is `NCHW`.
380  
381 Output shape:
382 This depends on the `layout` parameter. Output is 4D array of shape
383 (batch_size, channels, out_height, out_width) if `layout` is `NCHW`.
384  
385 out_height and out_width are calculated as::
386  
387 out_height = floor((height+2*padding[0]-dilation[0]*(kernel_size[0]-1)-1)/stride[0])+1
388 out_width = floor((width+2*padding[1]-dilation[1]*(kernel_size[1]-1)-1)/stride[1])+1
389 =cut
390  
391 has '+strides' => (default => sub { [1, 1] });
392 has '+padding' => (default => sub { [0, 0] });
393 has '+dilation' => (default => sub { [1, 1] });
394 has '+groups' => (default => 1);
395 has '+layout' => (default => 'NCHW');
396  
397 method _update_kernel_size()
398 {
399 if(not ref $self->kernel_size)
400 {
401 $self->kernel_size([($self->kernel_size)x2]);
402 }
403 confess("kernel_size must be a number or an array ref of 2 ints")
404 unless @{ $self->kernel_size } == 2;
405 }
406  
407 __PACKAGE__->register('AI::MXNet::Gluon::NN');
408  
409 package AI::MXNet::Gluon::NN::Conv3D;
410 use AI::MXNet::Gluon::Mouse;
411 extends 'AI::MXNet::Gluon::NN::Conv';
412  
413 =head1 NAME
414  
415 AI::MXNet::Gluon::NN::Conv3D
416 =cut
417  
418 =head1 DESCRIPTION
419  
420 3D convolution layer (e.g. spatial convolution over volumes).
421  
422 This layer creates a convolution kernel that is convolved
423 with the layer input to produce a tensor of
424 outputs. If `use_bias` is `True`,
425 a bias vector is created and added to the outputs. Finally, if
426 `activation` is not `None`, it is applied to the outputs as well.
427  
428 If `in_channels` is not specified, `Parameter` initialization will be
429 deferred to the first time `forward` is called and `in_channels` will be
430 inferred from the shape of input data.
431  
432 Parameters
433 ----------
434 channels : int
435 The dimensionality of the output space, i.e. the number of output
436 channels (filters) in the convolution.
437 kernel_size :int or tuple/list of 3 int
438 Specifies the dimensions of the convolution window.
439 strides : int or tuple/list of 3 int,
440 Specify the strides of the convolution.
441 padding : int or a tuple/list of 3 int,
442 If padding is non-zero, then the input is implicitly zero-padded
443 on both sides for padding number of points
444 dilation : int or tuple/list of 3 int
445 Specifies the dilation rate to use for dilated convolution.
446 groups : int
447 Controls the connections between inputs and outputs.
448 At groups=1, all inputs are convolved to all outputs.
449 At groups=2, the operation becomes equivalent to having two conv
450 layers side by side, each seeing half the input channels, and producing
451 half the output channels, and both subsequently concatenated.
452 layout : str, default 'NCDHW'
453 Dimension ordering of data and weight. Can be 'NCDHW', 'NDHWC', etc.
454 'N', 'C', 'H', 'W', 'D' stands for batch, channel, height, width and
455 depth dimensions respectively. Convolution is applied on the 'D',
456 'H' and 'W' dimensions.
457 in_channels : int, default 0
458 The number of input channels to this layer. If not specified,
459 initialization will be deferred to the first time `forward` is called
460 and `in_channels` will be inferred from the shape of input data.
461 activation : str
462 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
463 If you don't specify anything, no activation is applied
464 (ie. "linear" activation: `a(x) = x`).
465 use_bias : bool
466 Whether the layer uses a bias vector.
467 weight_initializer : str or `Initializer`
468 Initializer for the `weight` weights matrix.
469 bias_initializer : str or `Initializer`
470 Initializer for the bias vector.
471  
472  
473 Input shape:
474 This depends on the `layout` parameter. Input is 5D array of shape
475 (batch_size, in_channels, depth, height, width) if `layout` is `NCDHW`.
476  
477 Output shape:
478 This depends on the `layout` parameter. Output is 5D array of shape
479 (batch_size, channels, out_depth, out_height, out_width) if `layout` is
480 `NCDHW`.
481  
482 out_depth, out_height and out_width are calculated as::
483  
484 out_depth = floor((depth+2*padding[0]-dilation[0]*(kernel_size[0]-1)-1)/stride[0])+1
485 out_height = floor((height+2*padding[1]-dilation[1]*(kernel_size[1]-1)-1)/stride[1])+1
486 out_width = floor((width+2*padding[2]-dilation[2]*(kernel_size[2]-1)-1)/stride[2])+1
487 =cut
488  
489 has '+strides' => (default => sub { [1, 1, 1] });
490 has '+padding' => (default => sub { [0, 0, 0] });
491 has '+dilation' => (default => sub { [1, 1, 1] });
492 has '+groups' => (default => 1);
493 has '+layout' => (default => 'NCDHW');
494  
495 method _update_kernel_size()
496 {
497 if(not ref $self->kernel_size)
498 {
499 $self->kernel_size([($self->kernel_size)x3]);
500 }
501 confess("kernel_size must be a number or an array ref of 3 ints")
502 unless @{ $self->kernel_size } == 3;
503 }
504  
505 __PACKAGE__->register('AI::MXNet::Gluon::NN');
506  
507 package AI::MXNet::Gluon::NN::Conv1DTranspose;
508 use AI::MXNet::Gluon::Mouse;
509 extends 'AI::MXNet::Gluon::NN::Conv';
510  
511 =head1 NAME
512  
513 AI::MXNet::Gluon::NN::Conv1DTranspose
514 =cut
515  
516 =head1 DESCRIPTION
517  
518 Transposed 1D convolution layer (sometimes called Deconvolution).
519  
520 The need for transposed convolutions generally arises
521 from the desire to use a transformation going in the opposite direction
522 of a normal convolution, i.e., from something that has the shape of the
523 output of some convolution to something that has the shape of its input
524 while maintaining a connectivity pattern that is compatible with
525 said convolution.
526  
527 If `in_channels` is not specified, `Parameter` initialization will be
528 deferred to the first time `forward` is called and `in_channels` will be
529 inferred from the shape of input data.
530  
531 Parameters
532 ----------
533 channels : int
534 The dimensionality of the output space, i.e. the number of output
535 channels (filters) in the convolution.
536 kernel_size :int or tuple/list of 3 int
537 Specifies the dimensions of the convolution window.
538 strides : int or tuple/list of 3 int,
539 Specify the strides of the convolution.
540 padding : int or a tuple/list of 3 int,
541 If padding is non-zero, then the input is implicitly zero-padded
542 on both sides for padding number of points
543 dilation : int or tuple/list of 3 int
544 Specifies the dilation rate to use for dilated convolution.
545 groups : int
546 Controls the connections between inputs and outputs.
547 At groups=1, all inputs are convolved to all outputs.
548 At groups=2, the operation becomes equivalent to having two conv
549 layers side by side, each seeing half the input channels, and producing
550 half the output channels, and both subsequently concatenated.
551 layout : str, default 'NCW'
552 Dimension ordering of data and weight. Can be 'NCW', 'NWC', etc.
553 'N', 'C', 'W' stands for batch, channel, and width (time) dimensions
554 respectively. Convolution is applied on the 'W' dimension.
555 in_channels : int, default 0
556 The number of input channels to this layer. If not specified,
557 initialization will be deferred to the first time `forward` is called
558 and `in_channels` will be inferred from the shape of input data.
559 activation : str
560 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
561 If you don't specify anything, no activation is applied
562 (ie. "linear" activation: `a(x) = x`).
563 use_bias : bool
564 Whether the layer uses a bias vector.
565 weight_initializer : str or `Initializer`
566 Initializer for the `weight` weights matrix.
567 bias_initializer : str or `Initializer`
568 Initializer for the bias vector.
569  
570  
571 Input shape:
572 This depends on the `layout` parameter. Input is 3D array of shape
573 (batch_size, in_channels, width) if `layout` is `NCW`.
574  
575 Output shape:
576 This depends on the `layout` parameter. Output is 3D array of shape
577 (batch_size, channels, out_width) if `layout` is `NCW`.
578  
579 out_width is calculated as::
580  
581 out_width = (width-1)*strides-2*padding+kernel_size+output_padding
582 =cut
583  
584 has 'output_padding' => (is => 'rw', isa => 'DimSize|Shape', default => 0);
585 has '+adj' => (default => sub { shift->output_padding }, lazy => 1);
586 has '+op_name' => (default => 'Deconvolution');
587 has '+strides' => (default => 1);
588 has '+padding' => (default => 0 );
589 has '+dilation' => (default => 1);
590 has '+groups' => (default => 1);
591 has '+layout' => (default => 'NCW');
592  
593 method _update_kernel_size()
594 {
595 if(not ref $self->kernel_size)
596 {
597 $self->kernel_size([$self->kernel_size]);
598 }
599 if(not ref $self->output_padding)
600 {
601 $self->output_padding([$self->output_padding]);
602 }
603 confess("kernel_size must be a number or an array ref of 1 ints")
604 unless @{ $self->kernel_size } == 1;
605 confess("output_padding must be a number or an array ref of 1 ints")
606 unless @{ $self->output_padding } == 1;
607 }
608  
609 __PACKAGE__->register('AI::MXNet::Gluon::NN');
610  
611 package AI::MXNet::Gluon::NN::Conv2DTranspose;
612 use AI::MXNet::Gluon::Mouse;
613 extends 'AI::MXNet::Gluon::NN::Conv';
614  
615 =head1 NAME
616  
617 AI::MXNet::Gluon::NN::Conv2DTranspose
618 =cut
619  
620 =head1 DESCRIPTION
621  
622 Transposed 2D convolution layer (sometimes called Deconvolution).
623  
624 The need for transposed convolutions generally arises
625 from the desire to use a transformation going in the opposite direction
626 of a normal convolution, i.e., from something that has the shape of the
627 output of some convolution to something that has the shape of its input
628 while maintaining a connectivity pattern that is compatible with
629 said convolution.
630  
631 If `in_channels` is not specified, `Parameter` initialization will be
632 deferred to the first time `forward` is called and `in_channels` will be
633 inferred from the shape of input data.
634  
635  
636 Parameters
637 ----------
638 channels : int
639 The dimensionality of the output space, i.e. the number of output
640 channels (filters) in the convolution.
641 kernel_size :int or tuple/list of 3 int
642 Specifies the dimensions of the convolution window.
643 strides : int or tuple/list of 3 int,
644 Specify the strides of the convolution.
645 padding : int or a tuple/list of 3 int,
646 If padding is non-zero, then the input is implicitly zero-padded
647 on both sides for padding number of points
648 dilation : int or tuple/list of 3 int
649 Specifies the dilation rate to use for dilated convolution.
650 groups : int
651 Controls the connections between inputs and outputs.
652 At groups=1, all inputs are convolved to all outputs.
653 At groups=2, the operation becomes equivalent to having two conv
654 layers side by side, each seeing half the input channels, and producing
655 half the output channels, and both subsequently concatenated.
656 layout : str, default 'NCHW'
657 Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc.
658 'N', 'C', 'H', 'W' stands for batch, channel, height, and width
659 dimensions respectively. Convolution is applied on the 'H' and
660 'W' dimensions.
661 in_channels : int, default 0
662 The number of input channels to this layer. If not specified,
663 initialization will be deferred to the first time `forward` is called
664 and `in_channels` will be inferred from the shape of input data.
665 activation : str
666 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
667 If you don't specify anything, no activation is applied
668 (ie. "linear" activation: `a(x) = x`).
669 use_bias : bool
670 Whether the layer uses a bias vector.
671 weight_initializer : str or `Initializer`
672 Initializer for the `weight` weights matrix.
673 bias_initializer : str or `Initializer`
674 Initializer for the bias vector.
675  
676  
677 Input shape:
678 This depends on the `layout` parameter. Input is 4D array of shape
679 (batch_size, in_channels, height, width) if `layout` is `NCHW`.
680  
681 Output shape:
682 This depends on the `layout` parameter. Output is 4D array of shape
683 (batch_size, channels, out_height, out_width) if `layout` is `NCHW`.
684  
685 out_height and out_width are calculated as::
686  
687 out_height = (height-1)*strides[0]-2*padding[0]+kernel_size[0]+output_padding[0]
688 out_width = (width-1)*strides[1]-2*padding[1]+kernel_size[1]+output_padding[1]
689 =cut
690  
691 has 'output_padding' => (is => 'rw', isa => 'DimSize|Shape', default => 0);
692 has '+adj' => (default => sub { shift->output_padding }, lazy => 1);
693 has '+op_name' => (default => 'Deconvolution');
694 has '+strides' => (default => sub { [1, 1] });
695 has '+padding' => (default => sub { [0, 0] });
696 has '+dilation' => (default => sub { [1, 1] });
697 has '+groups' => (default => 1);
698 has '+layout' => (default => 'NCHW');
699  
700 method _update_kernel_size()
701 {
702 if(not ref $self->kernel_size)
703 {
704 $self->kernel_size([($self->kernel_size)x2]);
705 }
706 if(not ref $self->output_padding)
707 {
708 $self->output_padding([($self->output_padding)x2]);
709 }
710 confess("kernel_size must be a number or an array ref of 2 ints")
711 unless @{ $self->kernel_size } == 2;
712 confess("output_padding must be a number or an array ref of 2 ints")
713 unless @{ $self->output_padding } == 2;
714 }
715  
716 __PACKAGE__->register('AI::MXNet::Gluon::NN');
717  
718 package AI::MXNet::Gluon::NN::Conv3DTranspose;
719 use AI::MXNet::Gluon::Mouse;
720 extends 'AI::MXNet::Gluon::NN::Conv';
721  
722 =head1 NAME
723  
724 AI::MXNet::Gluon::NN::Conv3DTranspose
725 =cut
726  
727 =head1 DESCRIPTION
728  
729 Transposed 3D convolution layer (sometimes called Deconvolution).
730  
731 The need for transposed convolutions generally arises
732 from the desire to use a transformation going in the opposite direction
733 of a normal convolution, i.e., from something that has the shape of the
734 output of some convolution to something that has the shape of its input
735 while maintaining a connectivity pattern that is compatible with
736 said convolution.
737  
738 If `in_channels` is not specified, `Parameter` initialization will be
739 deferred to the first time `forward` is called and `in_channels` will be
740 inferred from the shape of input data.
741  
742  
743 Parameters
744 ----------
745 channels : int
746 The dimensionality of the output space, i.e. the number of output
747 channels (filters) in the convolution.
748 kernel_size :int or tuple/list of 3 int
749 Specifies the dimensions of the convolution window.
750 strides : int or tuple/list of 3 int,
751 Specify the strides of the convolution.
752 padding : int or a tuple/list of 3 int,
753 If padding is non-zero, then the input is implicitly zero-padded
754 on both sides for padding number of points
755 dilation : int or tuple/list of 3 int
756 Specifies the dilation rate to use for dilated convolution.
757 groups : int
758 Controls the connections between inputs and outputs.
759 At groups=1, all inputs are convolved to all outputs.
760 At groups=2, the operation becomes equivalent to having two conv
761 layers side by side, each seeing half the input channels, and producing
762 half the output channels, and both subsequently concatenated.
763 layout : str, default 'NCDHW'
764 Dimension ordering of data and weight. Can be 'NCDHW', 'NDHWC', etc.
765 'N', 'C', 'H', 'W', 'D' stands for batch, channel, height, width and
766 depth dimensions respectively. Convolution is applied on the 'D',
767 'H', and 'W' dimensions.
768 in_channels : int, default 0
769 The number of input channels to this layer. If not specified,
770 initialization will be deferred to the first time `forward` is called
771 and `in_channels` will be inferred from the shape of input data.
772 activation : str
773 Activation function to use. See :func:`~mxnet.ndarray.Activation`.
774 If you don't specify anything, no activation is applied
775 (ie. "linear" activation: `a(x) = x`).
776 use_bias : bool
777 Whether the layer uses a bias vector.
778 weight_initializer : str or `Initializer`
779 Initializer for the `weight` weights matrix.
780 bias_initializer : str or `Initializer`
781 Initializer for the bias vector.
782  
783  
784 Input shape:
785 This depends on the `layout` parameter. Input is 5D array of shape
786 (batch_size, in_channels, depth, height, width) if `layout` is `NCDHW`.
787  
788 Output shape:
789 This depends on the `layout` parameter. Output is 5D array of shape
790 (batch_size, channels, out_depth, out_height, out_width) if `layout` is `NCDHW`.
791 out_depth, out_height and out_width are calculated as::
792  
793 out_depth = (depth-1)*strides[0]-2*padding[0]+kernel_size[0]+output_padding[0]
794 out_height = (height-1)*strides[1]-2*padding[1]+kernel_size[1]+output_padding[1]
795 out_width = (width-1)*strides[2]-2*padding[2]+kernel_size[2]+output_padding[2]
796 =cut
797  
798 has 'output_padding' => (is => 'rw', isa => 'DimSize|Shape', default => 0);
799 has '+adj' => (default => sub { shift->output_padding }, lazy => 1);
800 has '+op_name' => (default => 'Deconvolution');
801 has '+strides' => (default => sub { [1, 1, 1] });
802 has '+padding' => (default => sub { [0, 0, 0] });
803 has '+dilation' => (default => sub { [1, 1, 1] });
804 has '+groups' => (default => 1);
805 has '+layout' => (default => 'NCDHW');
806  
807 method _update_kernel_size()
808 {
809 if(not ref $self->kernel_size)
810 {
811 $self->kernel_size([($self->kernel_size)x3]);
812 }
813 if(not ref $self->output_padding)
814 {
815 $self->output_padding([($self->output_padding)x3]);
816 }
817 confess("kernel_size must be a number or an array ref of 3 ints")
818 unless @{ $self->kernel_size } == 3;
819 confess("output_padding must be a number or an array ref of 3 ints")
820 unless @{ $self->output_padding } == 3;
821 }
822  
823 __PACKAGE__->register('AI::MXNet::Gluon::NN');
824  
825 # Abstract class for different pooling layers.
826 package AI::MXNet::Gluon::NN::Pooling;
827 use AI::MXNet::Gluon::Mouse;
828 extends 'AI::MXNet::Gluon::HybridBlock';
829  
830 has 'pool_size' => (is => 'rw', isa => 'DimSize|Shape', required => 1);
831 has 'strides' => (is => 'rw', isa => 'Maybe[DimSize|Shape]');
832 has 'padding' => (is => 'rw', isa => 'DimSize|Shape');
833 has 'ceil_mode' => (is => 'rw', isa => 'Bool', default => 0);
834 has 'global_pool' => (is => 'rw', isa => 'Bool', default => 0);
835 has 'kwargs' => (is => 'rw', init_arg => undef);
836 has 'pool_type' => (is => 'rw', isa => 'PoolType');
837 has 'layout' => (is => 'rw');
838 has 'count_include_pad' => (is => 'rw', isa => 'Bool');
839 method python_constructor_arguments() { [qw/pool_size strides padding/] }
840  
841 sub BUILD
842 {
843 my $self = shift;
844 $self->_update_pool_size;
845 if(not defined $self->strides)
846 {
847 $self->strides($self->pool_size);
848 }
849 if(not ref $self->strides)
850 {
851 $self->strides([($self->strides)x@{ $self->pool_size }]);
852 }
853 if(not ref $self->padding)
854 {
855 $self->padding([($self->padding)x@{ $self->pool_size }]);
856 }
857 $self->kwargs({
858 kernel => $self->pool_size, stride => $self->strides, pad => $self->padding,
859 global_pool => $self->global_pool, pool_type => $self->pool_type,
860 pooling_convention => $self->ceil_mode ? 'full' : 'valid',
861 (defined $self->count_include_pad ? (count_include_pad => $self->count_include_pad) : ())
862 });
863 }
864  
865 method _alias() { 'pool' }
866  
867 method hybrid_forward(GluonClass $F, GluonInput $x)
868 {
869 return $F->Pooling($x, name=>'fwd', %{ $self->kwargs });
870 }
871  
872 use overload '""' => sub {
873 my $self = shift;
874 sprintf(
875 '%s(size=(%s), stride=(%s), padding=(%s), ceil_mode=%d)',
876 $self->_class_name,
877 join(',', @{ $self->kwargs->{kernel} }),
878 join(',', @{ $self->kwargs->{stride} }),
879 join(',', @{ $self->kwargs->{pad} }),
880 $self->kwargs->{pooling_convention} eq 'full' ? 1 : 0
881 )
882 };
883  
884 package AI::MXNet::Gluon::NN::MaxPool1D;
885 use AI::MXNet::Gluon::Mouse;
886 extends 'AI::MXNet::Gluon::NN::Pooling';
887 method python_constructor_arguments() { [qw/pool_size strides padding layout ceil_mode/] }
888  
889 =head1 NAME
890  
891 AI::MXNet::Gluon::NN::MaxPool1D
892 =cut
893  
894 =head1 DESCRIPTION
895  
896 Max pooling operation for one dimensional data.
897  
898  
899 Parameters
900 ----------
901 pool_size: int
902 Size of the max pooling windows.
903 strides: int, or None
904 Factor by which to downscale. E.g. 2 will halve the input size.
905 If `None`, it will default to `pool_size`.
906 padding: int
907 If padding is non-zero, then the input is implicitly
908 zero-padded on both sides for padding number of points.
909 layout : str, default 'NCW'
910 Dimension ordering of data and weight. Can be 'NCW', 'NWC', etc.
911 'N', 'C', 'W' stands for batch, channel, and width (time) dimensions
912 respectively. Pooling is applied on the W dimension.
913 ceil_mode : bool, default False
914 When `True`, will use ceil instead of floor to compute the output shape.
915  
916  
917 Input shape:
918 This depends on the `layout` parameter. Input is 3D array of shape
919 (batch_size, channels, width) if `layout` is `NCW`.
920  
921 Output shape:
922 This depends on the `layout` parameter. Output is 3D array of shape
923 (batch_size, channels, out_width) if `layout` is `NCW`.
924  
925 out_width is calculated as::
926  
927 out_width = floor((width+2*padding-pool_size)/strides)+1
928  
929 When `ceil_mode` is `True`, ceil will be used instead of floor in this
930 equation.
931 =cut
932  
933  
934 has '+pool_size' => (default => 2);
935 has '+padding' => (default => 0);
936 has '+layout' => (default => 'NCW');
937 has '+pool_type' => (default => 'max');
938  
939 method _update_pool_size()
940 {
941 confess("Only supports NCW layout for now")
942 unless $self->layout eq 'NCW';
943 if(not ref $self->pool_size)
944 {
945 $self->pool_size([$self->pool_size]);
946 }
947 confess("pool_size must be a number or an array ref of 1 ints")
948 unless @{ $self->pool_size } == 1;
949 }
950  
951 __PACKAGE__->register('AI::MXNet::Gluon::NN');
952  
953 package AI::MXNet::Gluon::NN::MaxPool2D;
954 use AI::MXNet::Gluon::Mouse;
955 extends 'AI::MXNet::Gluon::NN::Pooling';
956  
957 =head1 NAME
958  
959 AI::MXNet::Gluon::NN::MaxPool2D
960 =cut
961  
962 =head1 DESCRIPTION
963  
964 Max pooling operation for two dimensional (spatial) data.
965  
966  
967 Parameters
968 ----------
969 pool_size: int or list/tuple of 2 ints,
970 Size of the max pooling windows.
971 strides: int, list/tuple of 2 ints, or None.
972 Factor by which to downscale. E.g. 2 will halve the input size.
973 If `None`, it will default to `pool_size`.
974 padding: int or list/tuple of 2 ints,
975 If padding is non-zero, then the input is implicitly
976 zero-padded on both sides for padding number of points.
977 layout : str, default 'NCHW'
978 Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc.
979 'N', 'C', 'H', 'W' stands for batch, channel, height, and width
980 dimensions respectively. padding is applied on 'H' and 'W' dimension.
981 ceil_mode : bool, default False
982 When `True`, will use ceil instead of floor to compute the output shape.
983  
984  
985 Input shape:
986 This depends on the `layout` parameter. Input is 4D array of shape
987 (batch_size, channels, height, width) if `layout` is `NCHW`.
988  
989 Output shape:
990 This depends on the `layout` parameter. Output is 4D array of shape
991 (batch_size, channels, out_height, out_width) if `layout` is `NCHW`.
992  
993 out_height and out_width are calculated as::
994  
995 out_height = floor((height+2*padding[0]-pool_size[0])/strides[0])+1
996 out_width = floor((width+2*padding[1]-pool_size[1])/strides[1])+1
997  
998 When `ceil_mode` is `True`, ceil will be used instead of floor in this
999 equation.
1000 =cut
1001  
1002 has '+pool_size' => (default => sub { [2, 2] });
1003 has '+padding' => (default => 0);
1004 has '+layout' => (default => 'NCHW');
1005 has '+pool_type' => (default => 'max');
1006  
1007 method _update_pool_size()
1008 {
1009 confess("Only supports NCHW layout for now")
1010 unless $self->layout eq 'NCHW';
1011 if(not ref $self->pool_size)
1012 {
1013 $self->pool_size([($self->pool_size)x2]);
1014 }
1015 confess("pool_size must be a number or an array ref of 2 ints")
1016 unless @{ $self->pool_size } == 2;
1017 }
1018  
1019 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1020  
1021 package AI::MXNet::Gluon::NN::MaxPool3D;
1022 use AI::MXNet::Gluon::Mouse;
1023 extends 'AI::MXNet::Gluon::NN::Pooling';
1024  
1025 =head1 NAME
1026  
1027 AI::MXNet::Gluon::NN::MaxPool3D
1028 =cut
1029  
1030 =head1 DESCRIPTION
1031  
1032 Max pooling operation for 3D data (spatial or spatio-temporal).
1033  
1034  
1035 Parameters
1036 ----------
1037 pool_size: int or list/tuple of 3 ints,
1038 Size of the max pooling windows.
1039 strides: int, list/tuple of 3 ints, or None.
1040 Factor by which to downscale. E.g. 2 will halve the input size.
1041 If `None`, it will default to `pool_size`.
1042 padding: int or list/tuple of 3 ints,
1043 If padding is non-zero, then the input is implicitly
1044 zero-padded on both sides for padding number of points.
1045 layout : str, default 'NCDHW'
1046 Dimension ordering of data and weight. Can be 'NCDHW', 'NDHWC', etc.
1047 'N', 'C', 'H', 'W', 'D' stands for batch, channel, height, width and
1048 depth dimensions respectively. padding is applied on 'D', 'H' and 'W'
1049 dimension.
1050 ceil_mode : bool, default False
1051 When `True`, will use ceil instead of floor to compute the output shape.
1052  
1053  
1054 Input shape:
1055 This depends on the `layout` parameter. Input is 5D array of shape
1056 (batch_size, channels, depth, height, width) if `layout` is `NCDHW`.
1057  
1058 Output shape:
1059 This depends on the `layout` parameter. Output is 5D array of shape
1060 (batch_size, channels, out_depth, out_height, out_width) if `layout`
1061 is `NCDHW`.
1062  
1063 out_depth, out_height and out_width are calculated as ::
1064  
1065 out_depth = floor((depth+2*padding[0]-pool_size[0])/strides[0])+1
1066 out_height = floor((height+2*padding[1]-pool_size[1])/strides[1])+1
1067 out_width = floor((width+2*padding[2]-pool_size[2])/strides[2])+1
1068  
1069 When `ceil_mode` is `True`, ceil will be used instead of floor in this
1070 equation.
1071 =cut
1072  
1073 has '+pool_size' => (default => sub { [2, 2, 2] });
1074 has '+padding' => (default => 0);
1075 has '+layout' => (default => 'NCDHW');
1076 has '+pool_type' => (default => 'max');
1077  
1078 method _update_pool_size()
1079 {
1080 confess("Only supports NCDHW layout for now")
1081 unless $self->layout eq 'NCDHW';
1082 if(not ref $self->pool_size)
1083 {
1084 $self->pool_size([($self->pool_size)x3]);
1085 }
1086 confess("pool_size must be a number or an array ref of 3 ints")
1087 unless @{ $self->pool_size } == 3;
1088 }
1089  
1090 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1091  
1092 package AI::MXNet::Gluon::NN::AvgPool1D;
1093 use AI::MXNet::Gluon::Mouse;
1094 extends 'AI::MXNet::Gluon::NN::MaxPool1D';
1095  
1096 =head1 NAME
1097  
1098 AI::MXNet::Gluon::NN::AvgPool1D
1099 =cut
1100  
1101 =head1 DESCRIPTION
1102  
1103 Average pooling operation for temporal data.
1104  
1105 Parameters
1106 ----------
1107 pool_size: int
1108 Size of the max pooling windows.
1109 strides: int, or None
1110 Factor by which to downscale. E.g. 2 will halve the input size.
1111 If `None`, it will default to `pool_size`.
1112 padding: int
1113 If padding is non-zero, then the input is implicitly
1114 zero-padded on both sides for padding number of points.
1115 layout : str, default 'NCW'
1116 Dimension ordering of data and weight. Can be 'NCW', 'NWC', etc.
1117 'N', 'C', 'W' stands for batch, channel, and width (time) dimensions
1118 respectively. padding is applied on 'W' dimension.
1119 ceil_mode : bool, default False
1120 When `True`, will use ceil instead of floor to compute the output shape.
1121 count_include_pad : bool, default True
1122 When 'False', will exclude padding elements when computing the average value.
1123  
1124  
1125 Input shape:
1126 This depends on the `layout` parameter. Input is 3D array of shape
1127 (batch_size, channels, width) if `layout` is `NCW`.
1128  
1129 Output shape:
1130 This depends on the `layout` parameter. Output is 3D array of shape
1131 (batch_size, channels, out_width) if `layout` is `NCW`.
1132  
1133 out_width is calculated as::
1134  
1135 out_width = floor((width+2*padding-pool_size)/strides)+1
1136  
1137 When `ceil_mode` is `True`, ceil will be used instead of floor in this
1138 equation.
1139 =cut
1140  
1141 has '+pool_type' => (default => 'avg');
1142 has '+count_include_pad' => (default => 1);
1143  
1144 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1145  
1146 package AI::MXNet::Gluon::NN::AvgPool2D;
1147 use AI::MXNet::Gluon::Mouse;
1148 extends 'AI::MXNet::Gluon::NN::MaxPool2D';
1149  
1150 =head1 NAME
1151  
1152 AI::MXNet::Gluon::NN::AvgPool2D
1153 =cut
1154  
1155 =head1 DESCRIPTION
1156  
1157 Average pooling operation for spatial data.
1158  
1159 Parameters
1160 ----------
1161 pool_size: int or list/tuple of 2 ints,
1162 Size of the max pooling windows.
1163 strides: int, list/tuple of 2 ints, or None.
1164 Factor by which to downscale. E.g. 2 will halve the input size.
1165 If `None`, it will default to `pool_size`.
1166 padding: int or list/tuple of 2 ints,
1167 If padding is non-zero, then the input is implicitly
1168 zero-padded on both sides for padding number of points.
1169 layout : str, default 'NCHW'
1170 Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc.
1171 'N', 'C', 'H', 'W' stands for batch, channel, height, and width
1172 dimensions respectively. padding is applied on 'H' and 'W' dimension.
1173 ceil_mode : bool, default False
1174 When True, will use ceil instead of floor to compute the output shape.
1175 count_include_pad : bool, default True
1176 When 'False', will exclude padding elements when computing the average value.
1177  
1178  
1179 Input shape:
1180 This depends on the `layout` parameter. Input is 4D array of shape
1181 (batch_size, channels, height, width) if `layout` is `NCHW`.
1182  
1183 Output shape:
1184 This depends on the `layout` parameter. Output is 4D array of shape
1185 (batch_size, channels, out_height, out_width) if `layout` is `NCHW`.
1186  
1187 out_height and out_width are calculated as::
1188  
1189 out_height = floor((height+2*padding[0]-pool_size[0])/strides[0])+1
1190 out_width = floor((width+2*padding[1]-pool_size[1])/strides[1])+1
1191  
1192 When `ceil_mode` is `True`, ceil will be used instead of floor in this
1193 equation.
1194 =cut
1195  
1196 has '+pool_type' => (default => 'avg');
1197 has '+count_include_pad' => (default => 1);
1198  
1199 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1200  
1201 package AI::MXNet::Gluon::NN::AvgPool3D;
1202 use AI::MXNet::Gluon::Mouse;
1203 extends 'AI::MXNet::Gluon::NN::MaxPool3D';
1204  
1205 =head1 NAME
1206  
1207 AI::MXNet::Gluon::NN::AvgPool3D
1208 =cut
1209  
1210 =head1 DESCRIPTION
1211  
1212 Average pooling operation for 3D data (spatial or spatio-temporal).
1213  
1214 Parameters
1215 ----------
1216 pool_size: int or list/tuple of 3 ints,
1217 Size of the max pooling windows.
1218 strides: int, list/tuple of 3 ints, or None.
1219 Factor by which to downscale. E.g. 2 will halve the input size.
1220 If `None`, it will default to `pool_size`.
1221 padding: int or list/tuple of 3 ints,
1222 If padding is non-zero, then the input is implicitly
1223 zero-padded on both sides for padding number of points.
1224 layout : str, default 'NCDHW'
1225 Dimension ordering of data and weight. Can be 'NCDHW', 'NDHWC', etc.
1226 'N', 'C', 'H', 'W', 'D' stands for batch, channel, height, width and
1227 depth dimensions respectively. padding is applied on 'D', 'H' and 'W'
1228 dimension.
1229 ceil_mode : bool, default False
1230 When True, will use ceil instead of floor to compute the output shape.
1231 count_include_pad : bool, default True
1232 When 'False', will exclude padding elements when computing the average value.
1233  
1234  
1235 Input shape:
1236 This depends on the `layout` parameter. Input is 5D array of shape
1237 (batch_size, channels, depth, height, width) if `layout` is `NCDHW`.
1238  
1239 Output shape:
1240 This depends on the `layout` parameter. Output is 5D array of shape
1241 (batch_size, channels, out_depth, out_height, out_width) if `layout`
1242 is `NCDHW`.
1243  
1244 out_depth, out_height and out_width are calculated as ::
1245  
1246 out_depth = floor((depth+2*padding[0]-pool_size[0])/strides[0])+1
1247 out_height = floor((height+2*padding[1]-pool_size[1])/strides[1])+1
1248 out_width = floor((width+2*padding[2]-pool_size[2])/strides[2])+1
1249  
1250 When `ceil_mode` is `True,` ceil will be used instead of floor in this
1251 equation.
1252 =cut
1253  
1254 has '+pool_type' => (default => 'avg');
1255 has '+count_include_pad' => (default => 1);
1256  
1257 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1258  
1259 package AI::MXNet::Gluon::NN::GlobalMaxPool1D;
1260  
1261 =head1 NAME
1262  
1263 AI::MXNet::Gluon::NN::GlobalMaxPool1D
1264 =cut
1265  
1266 =head1 DESCRIPTION
1267  
1268 Global max pooling operation for temporal data.
1269 =cut
1270  
1271 use AI::MXNet::Gluon::Mouse;
1272 extends 'AI::MXNet::Gluon::NN::MaxPool1D';
1273 has '+pool_size' => (default => sub { [1] });
1274 has '+global_pool' => (default => 1);
1275 has '+ceil_mode' => (default => 1);
1276  
1277 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1278  
1279 package AI::MXNet::Gluon::NN::GlobalMaxPool2D;
1280 =head1 NAME
1281  
1282 AI::MXNet::Gluon::NN::GlobalMaxPool2D
1283 =cut
1284  
1285 =head1 DESCRIPTION
1286  
1287 Global max pooling operation for spatial data.
1288 =cut
1289  
1290 use AI::MXNet::Gluon::Mouse;
1291 extends 'AI::MXNet::Gluon::NN::MaxPool2D';
1292  
1293 has '+pool_size' => (default => sub { [1, 1] });
1294 has '+global_pool' => (default => 1);
1295 has '+ceil_mode' => (default => 1);
1296  
1297 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1298  
1299 package AI::MXNet::Gluon::NN::GlobalMaxPool3D;
1300 =head1 NAME
1301  
1302 AI::MXNet::Gluon::NN::GlobalMaxPool3D
1303 =cut
1304  
1305 =head1 DESCRIPTION
1306  
1307 Global max pooling operation for 3D data.
1308 =cut
1309  
1310 use AI::MXNet::Gluon::Mouse;
1311 extends 'AI::MXNet::Gluon::NN::MaxPool3D';
1312 has '+pool_size' => (default => sub { [1, 1, 1] });
1313 has '+global_pool' => (default => 1);
1314 has '+ceil_mode' => (default => 1);
1315  
1316 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1317  
1318 package AI::MXNet::Gluon::NN::GlobalAvgPool1D;
1319  
1320 =head1 NAME
1321  
1322 AI::MXNet::Gluon::NN::GlobalAvgPool1D
1323 =cut
1324  
1325 =head1 DESCRIPTION
1326  
1327 Global average pooling operation for temporal data.
1328 =cut
1329  
1330 use AI::MXNet::Gluon::Mouse;
1331 extends 'AI::MXNet::Gluon::NN::AvgPool1D';
1332 has '+pool_size' => (default => sub { [1] });
1333 has '+global_pool' => (default => 1);
1334 has '+ceil_mode' => (default => 1);
1335  
1336 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1337  
1338 package AI::MXNet::Gluon::NN::GlobalAvgPool2D;
1339 =head1 NAME
1340  
1341 AI::MXNet::Gluon::NN::GlobalAvgPool2D
1342 =cut
1343  
1344 =head1 DESCRIPTION
1345  
1346 Global average pooling operation for spatial data.
1347 =cut
1348  
1349 use AI::MXNet::Gluon::Mouse;
1350 extends 'AI::MXNet::Gluon::NN::AvgPool2D';
1351  
1352 has '+pool_size' => (default => sub { [1, 1] });
1353 has '+global_pool' => (default => 1);
1354 has '+ceil_mode' => (default => 1);
1355  
1356 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1357  
1358 package AI::MXNet::Gluon::NN::GlobalAvgPool3D;
1359 =head1 NAME
1360  
1361 AI::MXNet::Gluon::NN::GlobalAvgPool2D
1362 =cut
1363  
1364 =head1 DESCRIPTION
1365  
1366 Global average pooling operation for 3D data.
1367 =cut
1368  
1369 use AI::MXNet::Gluon::Mouse;
1370 extends 'AI::MXNet::Gluon::NN::AvgPool3D';
1371 has '+pool_size' => (default => sub { [1, 1, 1] });
1372 has '+global_pool' => (default => 1);
1373 has '+ceil_mode' => (default => 1);
1374  
1375 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1376  
1377 package AI::MXNet::Gluon::NN::ReflectionPad2D;
1378 use AI::MXNet::Gluon::Mouse;
1379 extends 'AI::MXNet::Gluon::HybridBlock';
1380  
1381 =head1 NAME
1382  
1383 AI::MXNet::Gluon::NN::ReflectionPad2D
1384 =cut
1385  
1386 =head1 DESCRIPTION
1387  
1388 Pads the input tensor using the reflection of the input boundary.
1389  
1390 Parameters
1391 ----------
1392 padding: int
1393 An integer padding size
1394  
1395 Examples
1396 --------
1397 >>> $m = nn->ReflectionPad2D(3);
1398 >>> $input = mx->nd->random->normal(shape=>[16, 3, 224, 224]);
1399 >>> $output = $m->($input);
1400 =cut
1401  
1402 has 'padding' => (is => 'rw', isa => 'Int|ArrayRef[Int]', default => 0);
1403 method python_constructor_arguments() { ['padding'] }
1404 sub BUILD
1405 {
1406 my $self = shift;
1407 $self->padding([(0)x4, ($self->padding)x4]) if not ref $self->padding;
1408 confess("pading must be 8 integer long") unless @{ $self->padding } == 8;
1409 }
1410  
1411 method hybrid_forward(GluonClass $F, GluonInput $x)
1412 {
1413 return $F->pad($x, mode=>'reflect', pad_width=>$self->padding);
1414 }
1415  
1416 __PACKAGE__->register('AI::MXNet::Gluon::NN');
1417  
1418 1;