AI-MXNet
view release on metacpan or search on metacpan
lib/AI/MXNet/NDArray.pm view on Meta::CPAN
__PACKAGE__->_crop_assign(
$ret, $arr,
{
out => $ret,
begin => $begin,
end => $end
}
);
}
$idx += $arr->shape->[$axis];
}
return $ret
}
=head2 arange
Similar function in the MXNet ndarray as numpy.arange
See Also https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html.
Parameters
----------
:$start=0 : number, optional
Start of interval. The interval includes this value. The default start value is 0.
$stop= : number, optional
End of interval. The interval does not include this value.
:$step=1 : number, optional
Spacing between the values
:$repeat=1 : number, optional
The repeating time of all elements.
E.g repeat=3, the element a will be repeated three times --> a, a, a.
:$ctx : Context, optional
The context of the NDArray, defaultw to current default context.
:$dtype : data type, optional
The value type of the NDArray, defaults to float32
Returns
-------
$out : NDArray
The created NDArray
=cut
method arange(Index :$start=0, Index :$stop=, Index :$step=1, Index :$repeat=1,
AI::MXNet::Context :$ctx=AI::MXNet::Context->current_ctx, Dtype :$dtype='float32')
{
return __PACKAGE__->_arange({
start => $start,
(defined $stop ? (stop => $stop) : ()),
step => $step,
repeat => $repeat,
dtype => $dtype,
ctx => "$ctx"
});
}
=head2 load
Loads ndarrays from a binary file.
You can also use Storable to do the job if you only work with Perl.
The advantage of load/save is the file is language agnostic.
This means the file saved using save can be loaded by other language binding of mxnet.
You also get the benefit being able to directly load/save from cloud storage(S3, HDFS)
Parameters
----------
fname : str
The name of the file.Can be S3 or HDFS address (remember built with S3 support).
Example of fname:
- `s3://my-bucket/path/my-s3-ndarray`
- `hdfs://my-bucket/path/my-hdfs-ndarray`
- `/path-to/my-local-ndarray`
Returns
-------
$out : array ref of NDArrays or hash ref with NDArrays
=cut
method load(Str $filename)
{
my ($handles, $names) = check_call(AI::MXNetCAPI::NDArrayLoad($filename));
if (not @$names)
{
return [map { __PACKAGE__->new(handle => $_) } @$handles];
}
else
{
my $n = @$names;
my $h = @$handles;
confess("Handles [$h] and names [$n] count mismatch") unless $h == $n;
my %ret;
@ret{ @$names } = map { __PACKAGE__->new(handle => $_) } @$handles;
return \%ret;
}
}
=head2 save
Save array ref of NDArray or hash of str->NDArray to a binary file.
You can also use Storable to do the job if you only work with Perl.
The advantage of load/save is the file is language agnostic.
This means the file saved using save can be loaded by other language binding of mxnet.
You also get the benefit being able to directly load/save from cloud storage(S3, HDFS)
Parameters
----------
fname : str
The name of the file.Can be S3 or HDFS address (remember built with S3 support).
Example of fname:
- `s3://my-bucket/path/my-s3-ndarray`
- `hdfs://my-bucket/path/my-hdfs-ndarray`
- `/path-to/my-local-ndarray`
$data : array ref of NDArrays or hash ref of NDArrays
The data to be saved.
=cut
method save(Str $filename, ArrayRef[AI::MXNet::NDArray]|HashRef[AI::MXNet::NDArray] $data)
{
my $handles = [];
my $names = [];
if(ref $data eq 'HASH')
{
for my $name (keys %$data)
{
push @$names, $name;
push @$handles, $data->{ $name }->handle;
}
}
else
{
@$handles = map { $_->handle } @$data;
}
check_call(
AI::MXNetCAPI::NDArraySave(
$filename,
scalar(@$handles),
$handles,
$names
)
);
}
=head2 imdecode
Decode an image from string. Requires OpenCV to work.
Parameters
----------
$str_img : str
binary image data
:$clip_rect : iterable of 4 int
clip decoded image to rectangle (x0, y0, x1, y1)
:$out= : Maybe[NDArray]
output buffer. can be 3 dimensional (c, h, w) or 4 dimensional (n, c, h, w)
:$index : int
output decoded image to i-th slice of 4 dimensional buffer
:$channels=3 : int
number of channels to output. Decode to grey scale when channels = 1.
$mean= : Maybe[NDArray]
subtract mean from decode image before outputting.
( run in 0.875 second using v1.01-cache-2.11-cpan-39bf76dae61 )