AI-MXNet-Gluon-ModelZoo

 view release on metacpan or  search on metacpan

examples/image_classification.pl  view on Meta::CPAN

## CV that is used to read image is column major (as PDL)
$image = $image->transpose([2,0,1])->expand_dims(axis=>0);

## normalizing the image
my $rgb_mean = nd->array([0.485, 0.456, 0.406])->reshape([1,3,1,1]);
my $rgb_std = nd->array([0.229, 0.224, 0.225])->reshape([1,3,1,1]);
$image = ($image->astype('float32') / 255 - $rgb_mean) / $rgb_std;

# Now we can recognize the object in the image.
# We perform an additional softmax on the output to obtain probability scores.
# And then print the top-5 recognized objects.
my $prob = $net->($image)->softmax;
for my $idx (@{ $prob->topk(k=>5)->at(0) })
{
    my $i = $idx->asscalar;
    printf(
        "With prob = %.5f, it contains %s\n",
        $prob->at(0)->at($i)->asscalar, $text_labels[$i]
    );
}

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


    AI::MXNet::Gluon::ModelZoo - A collection of pretrained MXNet Gluon models
=cut

=head1 SYNOPSIS

    ## run forward prediction on random data
    use AI::MXNet::Gluon::ModelZoo qw(get_model);
    my $alexnet = get_model('alexnet', pretrained => 1);
    my $out = $alexnet->(mx->nd->random->uniform(shape=>[1, 3, 224, 224]));
    print $out->aspdl;
=cut

=head1 DESCRIPTION

    This module houses a collection of pretrained models (the parameters are hosted on public mxnet servers).
    https://mxnet.incubator.apache.org/api/python/gluon/model_zoo.html
    See examples/image_classification.pl for the example of real time image classification
    using a pretrained model from the ModelZoo
=cut

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

        make_path($root);
    }

    my $zip_file_path = "$root/$file_name.zip";
    my $repo_url = $ENV{MXNET_GLUON_REPO}//$apache_repo_url;
    if($repo_url !~ /\/$/)
    {
        $repo_url .= '/';
    }
    download(
        sprintf($_url_format, $repo_url, $file_name),
        path=>$zip_file_path,
        overwrite=>1
    );
    unzip($zip_file_path, $file_path);
    unlink $zip_file_path;
    if(check_sha1($file_path, $sha1_hash))
    {
        return $file_path;
    }
    else

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

=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

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

=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



( run in 0.867 second using v1.01-cache-2.11-cpan-de7293f3b23 )