AI-MXNet

 view release on metacpan or  search on metacpan

lib/AI/MXNet/Image.pm  view on Meta::CPAN

=cut

=head2 imdecode

    Decode an image from string. Requires OpenCV to work.

    Parameters
    ----------
    $buf : str, array ref, pdl, ndarray
        Binary image data.
    :$flag : int
        0 for grayscale. 1 for colored.
    :$to_rgb : int
        0 for BGR format (OpenCV default). 1 for RGB format (MXNet default).
    :$out : NDArray
        Output buffer. Do not specify for automatic allocation.
=cut

method imdecode(Str|PDL $buf, Int :$flag=1, Int :$to_rgb=1, Maybe[AI::MXNet::NDArray] :$out=)
{
    if(not ref $buf)
    {
        my $pdl_type = PDL::Type->new(DTYPE_MX_TO_PDL->{'uint8'});
        my $len; { use bytes; $len = length $buf; }
        my $pdl = PDL->new_from_specification($pdl_type, $len);
        ${$pdl->get_dataref} = $buf;
        $pdl->upd_data;
        $buf = $pdl;
    }
    if(not (blessed $buf and $buf->isa('AI::MXNet::NDArray')))
    {
        $buf = AI::MXNet::NDArray->array($buf, dtype=>'uint8');
    }
    return AI::MXNet::NDArray->_cvimdecode($buf, { flag => $flag, to_rgb => $to_rgb, ($out ? (out => $out) : ()) });
}

=head2 scale_down

Scale down crop size if it's bigger than the image size.

    Parameters:
    -----------
    Shape $src_size
    Shape $size

lib/AI/MXNet/Image.pm  view on Meta::CPAN

    assert($self->path_imgrec or $self->path_imglist or ref $self->imglist eq 'ARRAY');
    if($self->path_imgrec)
    {
        print("loading recordio...\n");
        if($self->path_imgidx)
        {
            $self->imgrec(
                AI::MXNet::IndexedRecordIO->new(
                    idx_path => $self->path_imgidx,
                    uri => $self->path_imgrec,
                    flag => 'r'
                )
            );
            $self->imgidx([@{ $self->imgrec->keys }]);
        }
        else
        {
            $self->imgrec(AI::MXNet::RecordIO->new(uri => $self->path_imgrec, flag => 'r'));
        }
    }
    my %imglist;
    my @imgkeys;
    if($self->path_imglist)
    {
        print("loading image list...\n");
        open(my $f, $self->path_imglist) or confess("can't open ${\ $self->path_imglist } : $!");
        while(my $line = <$f>)
        {

lib/AI/MXNet/RecordIO.pm  view on Meta::CPAN


    AI::MXNet::Function::Parameters - Read/write RecordIO format data
=cut

=head2 new

    Parameters
    ----------
    uri : Str
        uri path to recordIO file.
    flag: Str
        "r" for reading or "w" writing.
=cut

has 'uri'         => (is => 'ro', isa => 'Str', required => 1);
has 'flag'        => (is => 'ro', isa => enum([qw/r w/]), required => 1);
has 'handle'      => (is => 'rw', isa => 'RecordIOHandle');
has [qw/writable 
        is_open/] => (is => 'rw', isa => 'Bool');

sub BUILD
{
    my $self = shift;
    $self->is_open(0);
    $self->open();
}

lib/AI/MXNet/RecordIO.pm  view on Meta::CPAN

}

=head2 open

    Open record file.
=cut

method open()
{
    my $handle;
    if($self->flag eq 'w')
    {
        $handle = check_call(AI::MXNetCAPI::RecordIOWriterCreate($self->uri));
        $self->writable(1);
    }
    else
    {
        $handle = check_call(AI::MXNetCAPI::RecordIOReaderCreate($self->uri));
        $self->writable(0);
    }
    $self->handle($handle);

lib/AI/MXNet/RecordIO.pm  view on Meta::CPAN

method read()
{
    assert(not $self->writable);
    return scalar(check_call(
        AI::MXNetCAPI::RecordIOReaderReadRecord(
            $self->handle,
        )
    ));
}

method MXRecordIO(@args) { return AI::MXNet::RecordIO->new(uri => $args[0], flag => $args[1]) }
method MXIndexedRecordIO(@args)
{
    return AI::MXNet::IndexedRecordIO->new(
        idx_path => $args[0], uri => $args[1], flag => $args[2]
    )
}

package AI::MXNet::IRHeader;
use Mouse;
has [qw/flag id id2/] => (is => 'rw', isa => 'Int');
has 'label'           => (is => 'rw', isa => 'AcceptableInput');
around BUILDARGS => sub {
    my $orig  = shift;
    my $class = shift;
    if(@_ == 4)
    {
        return $class->$orig(flag => $_[0], label => $_[1], id => $_[2], id2 => $_[3]);
    }
    return $class->$orig(@_);
};
my @order = qw/flag label id id2/;
use overload '@{}' => sub { my $self = shift; [map { $self->$_ } @order] };

package AI::MXNet::RecordIO;

=head2 unpack

    unpack a MXImageRecord to a string

    Parameters
    ----------

lib/AI/MXNet/RecordIO.pm  view on Meta::CPAN

    s : str
        unpacked string
=cut

method unpack(Str $s)
{
    my $h;
    my $h_size = 24;
    ($h, $s) = (substr($s, 0, $h_size), substr($s, $h_size));
    my $header = AI::MXNet::IRHeader->new(unpack('IfQQ', $h));
    if($header->flag > 0)
    {
        my $label;
        ($label, $s) = (substr($s, 0, 4*$header->flag), substr($s, 4*$header->flag));
        my $pdl_type = PDL::Type->new(DTYPE_MX_TO_PDL->{float32});
        my $pdl = PDL->new_from_specification($pdl_type, $header->flag);
        ${$pdl->get_dataref} = $label;
        $pdl->upd_data;
        $header->label($pdl);
    }
    return ($header, $s)
}

=head2 pack

    pack a string into MXImageRecord

lib/AI/MXNet/RecordIO.pm  view on Meta::CPAN

        $header->label can be a number or an array ref.
    s : str
        string to pack
=cut

method pack(AI::MXNet::IRHeader|ArrayRef $header, Str $s)
{
    $header = AI::MXNet::IRHeader->new(@$header) unless blessed $header;
    if(not ref $header->label)
    {
        $header->flag(0);
    }
    else
    {
        my $label = AI::MXNet::NDArray->array($header->label, dtype=>'float32')->aspdl;
        $header->label(0);
        $header->flag($label->nelem);
        my $buf = ${$label->get_dataref};
        $s = "$buf$s";
    }
    $s = pack('IfQQ', @{ $header }) . $s;
    return $s;
}

package AI::MXNet::IndexedRecordIO;
use Mouse;
use AI::MXNet::Base;

lib/AI/MXNet/RecordIO.pm  view on Meta::CPAN

=cut

=head2 new

    Parameters
    ----------
    idx_path : str
        Path to index file
    uri : str
        Path to record file. Only support file types that are seekable.
    flag : str
        'w' for write or 'r' for read
=cut

has 'idx_path'  => (is => 'ro', isa => 'Str', required => 1);
has [qw/idx
    keys fidx/] => (is => 'rw', init_arg => undef);

method open()
{
    $self->SUPER::open();
    $self->idx({});
    $self->keys([]);
    open(my $f, $self->flag eq 'r' ? '<' : '>', $self->idx_path);
    $self->fidx($f);
    if(not $self->writable)
    {
        while(<$f>)
        {
            chomp;
            my ($key, $val) = split(/\t/);
            push @{ $self->keys }, $key;
            $self->idx->{$key} = $val;
        }



( run in 5.713 seconds using v1.01-cache-2.11-cpan-94b05bcf43c )