AI-MXNet-Gluon-ModelZoo
view release on metacpan or search on metacpan
examples/image_classification.pl view on Meta::CPAN
$image = mx->image->imread($image);
$image = mx->image->resize_short($image, $model =~ /inception/ ? 330 : 256);
($image) = mx->image->center_crop($image, [($model =~ /inception/ ? 299 : 224)x2]);
## 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",
lib/AI/MXNet/Gluon/ModelZoo/Vision/ResNet.pm view on Meta::CPAN
}
method hybrid_forward(GluonClass $F, GluonInput $x)
{
my $residual = $x;
$x = $self->body->($x);
if(defined $self->downsample)
{
$residual = $self->downsample->($residual);
}
$x = $F->Activation($residual+$x, act_type=>'relu');
return $x;
}
package AI::MXNet::Gluon::ModelZoo::Vision::ResNet::BottleneckV1;
use AI::MXNet::Gluon::Mouse;
extends 'AI::MXNet::Gluon::HybridBlock';
=head1 NAME
AI::MXNet::Gluon::ModelZoo::Vision::ResNet::BottleneckV1 - Bottleneck V1 from "Deep Residual Learning for Image Recognition"
lib/AI/MXNet/Gluon/ModelZoo/Vision/ResNet.pm view on Meta::CPAN
}
method hybrid_forward(GluonClass $F, GluonInput $x)
{
my $residual = $x;
$x = $self->body->($x);
if(defined $self->downsample)
{
$residual = $self->downsample->($residual);
}
$x = $F->Activation($residual+$x, act_type=>'relu');
return $x;
}
package AI::MXNet::Gluon::ModelZoo::Vision::ResNet::BasicBlockV2;
use AI::MXNet::Gluon::Mouse;
extends 'AI::MXNet::Gluon::HybridBlock';
=head1 NAME
AI::MXNet::Gluon::ModelZoo::Vision::ResNet::BasicBlockV2 - BasicBlock V2 from "Identity Mappings in Deep Residual Networks"
lib/AI/MXNet/Gluon/ModelZoo/Vision/ResNet.pm view on Meta::CPAN
else
{
$self->downsample(undef);
}
}
method hybrid_forward(GluonClass $F, GluonInput $x)
{
my $residual = $x;
$x = $self->bn1->($x);
$x = $F->Activation($x, act_type=>'relu');
if(defined $self->downsample)
{
$residual = $self->downsample->($x);
}
$x = $self->conv1->($x);
$x = $self->bn2->($x);
$x = $F->Activation($x, act_type=>'relu');
$x = $self->conv2->($x);
return $x + $residual;
}
package AI::MXNet::Gluon::ModelZoo::Vision::ResNet::BottleneckV2;
use AI::MXNet::Gluon::Mouse;
extends 'AI::MXNet::Gluon::HybridBlock';
lib/AI/MXNet/Gluon/ModelZoo/Vision/ResNet.pm view on Meta::CPAN
else
{
$self->downsample(undef);
}
}
method hybrid_forward(GluonClass $F, GluonInput $x)
{
my $residual = $x;
$x = $self->bn1->($x);
$x = $F->Activation($x, act_type=>'relu');
if(defined $self->downsample)
{
$residual = $self->downsample->($x);
}
$x = $self->conv1->($x);
$x = $self->bn2->($x);
$x = $F->Activation($x, act_type=>'relu');
$x = $self->conv2->($x);
$x = $self->bn3->($x);
$x = $F->Activation($x, act_type=>'relu');
$x = $self->conv3->($x);
return $x + $residual;
}
# Nets
package AI::MXNet::Gluon::ModelZoo::Vision::ResNet::V1;
use AI::MXNet::Gluon::Mouse;
extends 'AI::MXNet::Gluon::HybridBlock';
lib/AI/MXNet/Gluon/ModelZoo/Vision/ResNet.pm view on Meta::CPAN
# Constructor
method get_resnet(
Int $version, Int $num_layers, Bool :$pretrained=0,
AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(),
Str :$root='~/.mxnet/models',
Maybe[Int] :$classes=,
Maybe[Bool] :$thumbnail=
)
{
my ($block_type, $layers, $channels) = @{ $resnet_spec{$num_layers} };
my $resnet_class = $resnet_net_versions[$version-1];
confess("invalid resnet $version [$version], can be 1,2") unless $resnet_class;
my $block_class = $resnet_block_versions[$version-1]{$block_type};
my $net = $resnet_class->new(
$block_class, $layers, $channels,
(defined($classes) ? (classes => $classes) : ()),
(defined($thumbnail) ? (thumbnail => $thumbnail) : ())
);
if($pretrained)
{
$net->load_parameters(
AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file(
"resnet${num_layers}_v$version",
lib/AI/MXNet/Gluon/ModelZoo/Vision/VGG.pm view on Meta::CPAN
my $featurizer = nn->HybridSequential(prefix=>'');
for(enumerate($self->layers))
{
my ($i, $num) = @$_;
for(0..$num-1)
{
$featurizer->add(
nn->Conv2D(
$self->filters->[$i], kernel_size => 3, padding => 1,
weight_initializer => mx->init->Xavier(
rnd_type => 'gaussian',
factor_type => 'out',
magnitude => 2
),
bias_initializer=>'zeros'
)
);
if($self->batch_norm)
{
$featurizer->add(nn->BatchNorm());
}
$featurizer->add(nn->Activation('relu'));
( run in 1.599 second using v1.01-cache-2.11-cpan-df04353d9ac )