AI-MXNet-Gluon-ModelZoo
view release on metacpan or search on metacpan
lib/AI/MXNet/Gluon/ModelZoo/Vision/MobileNet.pm view on Meta::CPAN
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
use strict;
use warnings;
use AI::MXNet::Function::Parameters;
package AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::RELU6;
use AI::MXNet::Gluon::Mouse;
extends 'AI::MXNet::Gluon::HybridBlock';
method hybrid_forward(GluonClass $F, GluonInput $x)
{
return $F->clip($x, a_min => 0, a_max => 6, name=>"relu6");
}
package AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::LinearBottleneck;
use AI::MXNet::Gluon::Mouse;
extends 'AI::MXNet::Gluon::HybridBlock';
has [qw/in_channels channels t stride/] => (is => 'ro', isa => 'Int', required => 1);
method python_constructor_arguments(){ [qw/in_channels channels t stride/] }
=head1 NAME
AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::LinearBottleneck - LinearBottleneck used in MobileNetV2 model
=cut
=head1 DESCRIPTION
LinearBottleneck used in MobileNetV2 model from the
"Inverted Residuals and Linear Bottlenecks:
Mobile Networks for Classification, Detection and Segmentation"
<https://arxiv.org/abs/1801.04381> paper.
Parameters
----------
in_channels : Int
Number of input channels.
channels : Int
Number of output channels.
t : Int
Layer expansion ratio.
stride : Int
stride
=cut
func _add_conv(
$out, $channels, :$kernel=1, :$stride=1, :$pad=0,
:$num_group=1, :$active=1, :$relu6=0
)
{
$out->add(nn->Conv2D($channels, $kernel, $stride, $pad, groups=>$num_group, use_bias=>0));
$out->add(nn->BatchNorm(scale=>1));
if($active)
{
$out->add($relu6 ? AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::RELU6->new : nn->Activation('relu'));
}
}
sub BUILD
{
my $self = shift;
$self->use_shortcut($self->stride == 1 and $self->in_channels == $self->channels);
$self->name_scope(sub {
$self->out(nn->HybridSequential());
_add_conv($self->out, $self->in_channels * $self->t, relu6=>1);
_add_conv(
$self->out, $self->in_channels * $self->t, kernel=>3, stride=>$self->stride,
pad=>1, num_group=>$self->in_channels * $self->t, relu6=>1
);
_add_conv($self->out, $self->channels, active=>0, relu6=>1);
});
}
method hybrid_forward($F, $x)
{
my $out = $self->out->($x);
if($self->use_shortcut)
{
$out = $F->elemwise_add($out, $x);
( run in 0.686 second using v1.01-cache-2.11-cpan-39bf76dae61 )