AI-MXNet-Gluon-ModelZoo

 view release on metacpan or  search on metacpan

lib/AI/MXNet/Gluon/ModelZoo/Vision/MobileNet.pm  view on Meta::CPAN

                $self->features->add(
                    AI::MXNet::Gluon::ModelZoo::Vision::MobileNet::LinearBottleneck->new(
                        in_channels=>$in_c, channels=>$c,
                        t=>$t, stride=>$s
                    )
                );
            }

            my $last_channels = $self->multiplier > 1 ? int(1280 * $self->multiplier) : 1280;
            _add_conv($self->features, $last_channels, relu6=>1);
            $self->features->add(nn->GlobalAvgPool2D());
        });

        $self->output(nn->HybridSequential(prefix=>'output_'));
        $self->output->name_scope(sub {
            $self->output->add(
                nn->Conv2D($self->classes, 1, use_bias=>0, prefix=>'pred_'),
                nn->Flatten()
            );
        });
    });
}

method hybrid_forward(GluonClass $F, GluonInput $x)
{
    $x = $self->features->($x);
    $x = $self->output->($x);
    return $x;
}

package AI::MXNet::Gluon::ModelZoo::Vision;

=head2 get_mobilenet

    MobileNet model from the
    "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
    <https://arxiv.org/abs/1704.04861> paper.

    Parameters
    ----------
    $multiplier : Num
        The width multiplier for controling the model size. Only multipliers that are no
        less than 0.25 are supported. The actual number of channels is equal to the original
        channel size multiplied by this multiplier.
    :$pretrained : Bool, default 0
        Whether to load the pretrained weights for model.
    :$ctx : AI::MXNet::Context, default CPU
        The context in which to load the pretrained weights.
    :$root : Str, default '~/.mxnet/models'
        Location for keeping the model parameters.
=cut

method get_mobilenet(
    Num $multiplier, Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(),
    Str :$root='~/.mxnet/models'
)
{
    my $net = AI::MXNet::Gluon::ModelZoo::Vision::MobileNet->new($multiplier);
    if($pretrained)
    {
        my $version_suffix = sprintf("%.2f", $multiplier);
        if($version_suffix eq '1.00' or $version_suffix eq '0.50')
        {
            $version_suffix =~ s/.$//;
        }
        $net->load_parameters(
            AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file(
                "mobilenet$version_suffix",
                root=>$root
            ),
            ctx=>$ctx
        );
    }
    return $net;
}

=head2 get_mobilenet_v2

    MobileNetV2 model from the
    "Inverted Residuals and Linear Bottlenecks:
      Mobile Networks for Classification, Detection and Segmentation"
    <https://arxiv.org/abs/1801.04381> paper.

    Parameters
    ----------
    $multiplier : Num
        The width multiplier for controling the model size. Only multipliers that are no
        less than 0.25 are supported. The actual number of channels is equal to the original
        channel size multiplied by this multiplier.
    :$pretrained : Bool, default 0
        Whether to load the pretrained weights for model.
    :$ctx : AI::MXNet::Context, default CPU
        The context in which to load the pretrained weights.
    :$root : Str, default '~/.mxnet/models'
        Location for keeping the model parameters.
=cut

method get_mobilenet_v2(
    Num $multiplier, Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(),
    Str :$root='~/.mxnet/models'
)
{
    my $net = AI::MXNet::Gluon::ModelZoo::Vision::MobileNetV2->new($multiplier);
    if($pretrained)
    {
        my $version_suffix = sprintf("%.2f", $multiplier);
        if($version_suffix eq '1.00' or $version_suffix eq '0.50')
        {
            $version_suffix =~ s/.$//;
        }
        $net->load_parameters(
            AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file(
                "mobilenetv2_$version_suffix",
                root=>$root
            ),
            ctx=>$ctx
        );
    }
    return $net;
}

=head2 mobilenet1_0

    MobileNet model from the
    "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
    <https://arxiv.org/abs/1704.04861> paper, with width multiplier 1.0.

    Parameters
    ----------
    :$pretrained : Bool, default 0
        Whether to load the pretrained weights for model.
    :$ctx : AI::MXNet::Context, default CPU
        The context in which to load the pretrained weights.
=cut

method mobilenet1_0(%kwargs)
{
    return __PACKAGE__->get_mobilenet(1.0, %kwargs);
}

=head2 mobilenet_v2_1_0

    MobileNetV2 model from the
    "Inverted Residuals and Linear Bottlenecks:
      Mobile Networks for Classification, Detection and Segmentation"
    <https://arxiv.org/abs/1801.04381> paper.

    Parameters
    ----------
    :$pretrained : Bool, default 0
        Whether to load the pretrained weights for model.
    :$ctx : AI::MXNet::Context, default CPU
        The context in which to load the pretrained weights.
=cut

method mobilenet_v2_1_0(%kwargs)
{
    return __PACKAGE__->get_mobilenet_v2(1.0, %kwargs);
}

=head2 mobilenet0_75

    MobileNet model from the
    "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
    <https://arxiv.org/abs/1704.04861> paper, with width multiplier 0.75.



( run in 0.452 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )