| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | package AI::MXNet::Gluon::Data::Set; | ||
| 21 | use AI::MXNet::Function::Parameters; | ||
| 22 | use Mouse; | ||
| 23 | around BUILDARGS => \&AI::MXNet::Base::process_arguments; | ||
| 24 | method _class_name() | ||
| 25 | { | ||
| 26 | my $class = ref $self || $self; | ||
| 27 | $class =~ s/^.+:://; | ||
| 28 | $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | method register(Str $container) | ||
| 32 | { | ||
| 33 | my $sub_name = $self->_class_name; | ||
| 34 | no strict 'refs'; | ||
| 35 | *{$container.'::'.$sub_name} = sub { shift; $self->new(@_) }; | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | AI::MXNet::Gluon::Data::Set | ||
| 41 | =cut | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | Abstract dataset class. All datasets should have this interface. | ||
| 46 | |||
| 47 | Subclasses need to override method at($i), which returns the i-th | ||
| 48 | element, method len() which returns the total number elements. | ||
| 49 | |||
| 50 | AI::MXNet::NDArray can be directly used as a dataset. | ||
| 51 | =cut | ||
| 52 | |||
| 53 | method at(Index $idx) { confess("Not Implemented") } | ||
| 54 | |||
| 55 | method len() { confess("Not Implemented") } | ||
| 56 | |||
| 57 | package AI::MXNet::Gluon::Data::ArrayDataset; | ||
| 58 | use AI::MXNet::Base; | ||
| 59 | use Mouse; | ||
| 60 | extends 'AI::MXNet::Gluon::Data::Set'; | ||
| 61 | |||
| 62 | =head1 NAME | ||
| 63 | |||
| 64 | AI::MXNet::Gluon::Data::ArrayDataset | ||
| 65 | =cut | ||
| 66 | |||
| 67 | =head1 DESCRIPTION | ||
| 68 | |||
| 69 | A dataset with a data array and a label array. | ||
| 70 | |||
| 71 | The i-th sample is `(data[i], label[i])`. | ||
| 72 | |||
| 73 | Parameters | ||
| 74 | ---------- | ||
| 75 | data : AI::MXNet::NDArray or PDL | ||
| 76 | The data array. | ||
| 77 | label : AI::MXNet::NDArray or PDL | ||
| 78 | The label array. | ||
| 79 | =cut | ||
| 80 | has [qw/data label/] => (is => 'rw', isa => 'PDL|AI::MXNet::NDArray', required => 1); | ||
| 81 | method python_constructor_arguments() { ['data', 'label'] } | ||
| 82 | |||
| 83 | sub BUILD | ||
| 84 | { | ||
| 85 | my $self = shift; | ||
| 86 | assert(($self->data->len == $self->label->len), "data and label lengths must be the same"); | ||
| 87 | if($self->label->isa('AI::MXNet::NDArray') and @{$self->label->shape} == 1) | ||
| 88 | { | ||
| 89 | $self->label($self->label->aspdl); | ||
| 90 | } | ||
| 91 | if($self->data->isa('PDL')) | ||
| 92 | { | ||
| 93 | $self->data(AI::MXNet::NDArray->array($self->data)); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | method at(Index $idx) | ||
| 98 | { | ||
| 99 | return [ | ||
| 100 | $self->data->at($idx), | ||
| 101 | $self->label->at($idx) | ||
| 102 | ]; | ||
| 103 | } | ||
| 104 | |||
| 105 | method len() | ||
| 106 | { | ||
| 107 | return $self->data->len | ||
| 108 | } | ||
| 109 | |||
| 110 | __PACKAGE__->register('AI::MXNet::Gluon::Data'); | ||
| 111 | |||
| 112 | package AI::MXNet::Gluon::Data::RecordFileSet; | ||
| 113 | use AI::MXNet::Base; | ||
| 114 | use Mouse; | ||
| 115 | extends 'AI::MXNet::Gluon::Data::Set'; | ||
| 116 | |||
| 117 | =head1 NAME | ||
| 118 | |||
| 119 | AI::MXNet::Gluon::Data::RecordFileSet | ||
| 120 | =cut | ||
| 121 | |||
| 122 | =head1 DESCRIPTION | ||
| 123 | |||
| 124 | A dataset wrapping over a RecordIO (.rec) file. | ||
| 125 | |||
| 126 | Each sample is a string representing the raw content of an record. | ||
| 127 | |||
| 128 | Parameters | ||
| 129 | ---------- | ||
| 130 | filename : str | ||
| 131 | Path to rec file. | ||
| 132 | =cut | ||
| 133 | has 'filename' => (is => 'ro', isa =>'Str', required => 1); | ||
| 134 | has '_record' => (is => 'rw', init_arg => undef); | ||
| 135 | method python_constructor_arguments() { ['filename'] } | ||
| 136 | |||
| 137 | sub BUILD | ||
| 138 | { | ||
| 139 | my $self = shift; | ||
| 140 | my $idx_file = $self->filename; | ||
| 141 | $idx_file =~ s/\.[^.]+$/.idx/; | ||
| 142 | $self->_record( | ||
| 143 | AI::MXNet::IndexedRecordIO->new( | ||
| 144 | idx_path => $idx_file, uri => $self->filename, flag => 'r' | ||
| 145 | ) | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | |||
| 149 | method at(Index $idx) { return $self->_record->read_idx($idx); } | ||
| 150 | |||
| 151 | method len() { return scalar(@{ $self->_record->keys }) } | ||
| 152 | |||
| 153 | __PACKAGE__->register('AI::MXNet::Gluon::Data'); | ||
| 154 | |||
| 155 | 1; |