AI-MXNet

 view release on metacpan or  search on metacpan

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

    {
        $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);
    $self->is_open(1);
}

=head2 close

    Close record file.
=cut

method close()
{
    return if not $self->is_open;
    if($self->writable)
    {
        check_call(AI::MXNetCAPI::RecordIOWriterFree($self->handle));
    }
    else
    {
        check_call(AI::MXNetCAPI::RecordIOReaderFree($self->handle));
    }
    $self->is_open(0);
}

=head2 reset

    Reset pointer to first item. If record is opened with 'w',
    this will truncate the file to empty.
=cut

method reset()
{
    $self->close;
    $self->open;
}

=head2 write

    Write a string buffer as a record.

    Parameters
    ----------
    $buf : a buffer to write.
=cut

method write(Str $buf)
{
    assert($self->writable);
    check_call(
        AI::MXNetCAPI::RecordIOWriterWriteRecord(
            $self->handle,
            $buf,
            length($buf)
        )
    );
}

=head2 read

    Read a record as a string.

    Returns
    ----------
    $buf : string
=cut

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
    ----------
    s : str
        string buffer from MXRecordIO.read

    Returns
    -------



( run in 0.869 second using v1.01-cache-2.11-cpan-39bf76dae61 )