| 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 | =head1 NAME | ||
| 19 | |||
| 20 | AI::MXNet::Gluon::Data::Loader::DataLoader - Dataset generator. | ||
| 21 | =cut | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Loads data from a dataset and returns mini-batches of data. | ||
| 26 | |||
| 27 | Parameters | ||
| 28 | ---------- | ||
| 29 | dataset : Dataset | ||
| 30 | Source dataset. Note that numpy and mxnet arrays can be directly used | ||
| 31 | as a Dataset. | ||
| 32 | batch_size : int | ||
| 33 | Size of mini-batch. | ||
| 34 | shuffle : bool | ||
| 35 | Whether to shuffle the samples. | ||
| 36 | sampler : Sampler | ||
| 37 | The sampler to use. Either specify sampler or shuffle, not both. | ||
| 38 | last_batch : {'keep', 'discard', 'rollover'} | ||
| 39 | How to handle the last batch if batch_size does not evenly divide | ||
| 40 | `len(dataset)`. | ||
| 41 | |||
| 42 | keep - A batch with less samples than previous batches is returned. | ||
| 43 | discard - The last batch is discarded if its incomplete. | ||
| 44 | rollover - The remaining samples are rolled over to the next epoch. | ||
| 45 | batch_sampler : Sampler | ||
| 46 | A sampler that returns mini-batches. Do not specify batch_size, | ||
| 47 | shuffle, sampler, and last_batch if batch_sampler is specified. | ||
| 48 | =cut | ||
| 49 | |||
| 50 | use strict; | ||
| 51 | use warnings; | ||
| 52 | package AI::MXNet::Gluon::Data::Loader::DataLoader; | ||
| 53 | use AI::MXNet::Function::Parameters; | ||
| 54 | use Mouse; | ||
| 55 | |||
| 56 | method _class_name() | ||
| 57 | { | ||
| 58 | my $class = ref $self || $self; | ||
| 59 | $class =~ s/^.+:://; | ||
| 60 | $class; | ||
| 61 | } | ||
| 62 | |||
| 63 | method register(Str $container) | ||
| 64 | { | ||
| 65 | my $sub_name = $self->_class_name; | ||
| 66 | no strict 'refs'; | ||
| 67 | *{$container.'::'.$sub_name} = sub { shift; $self->new(@_) }; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Collate data into batch. | ||
| 71 | func _batchify($data, $dtype) | ||
| 72 | { | ||
| 73 | if(blessed $data->[0] and $data->[0]->isa('AI::MXNet::NDArray')) | ||
| 74 | { | ||
| 75 | return AI::MXNet::NDArray->stack(@{ $data }); | ||
| 76 | } | ||
| 77 | elsif(ref $data->[0] eq 'ARRAY') | ||
| 78 | { | ||
| 79 | my (@data, @label); | ||
| 80 | for my $i (@$data) | ||
| 81 | { | ||
| 82 | my ($d, $l) = @$i; | ||
| 83 | push @data, $d; | ||
| 84 | push @label, $l; | ||
| 85 | } | ||
| 86 | return [_batchify(\@data, $dtype), _batchify(\@label, $dtype)]; | ||
| 87 | } | ||
| 88 | else | ||
| 89 | { | ||
| 90 | return AI::MXNet::NDArray->array($data, dtype => $dtype); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | has 'dataset' => (is => 'rw', isa => 'AI::MXNet::Gluon::Data::Set|AI::MXNet::NDArray|PDL', required => 1); | ||
| 95 | has 'batch_size' => (is => 'ro', isa => 'Int'); | ||
| 96 | has 'shuffle' => (is => 'ro', isa => 'Bool', default => 0); | ||
| 97 | has 'sampler' => (is => 'rw', isa => 'AI::MXNet::Gluon::Data::Sampler'); | ||
| 98 | has 'batch_sampler' => (is => 'rw', isa => 'AI::MXNet::Gluon::Data::Sampler'); | ||
| 99 | has 'last_batch' => (is => 'rw', isa => 'Str', default => 'keep'); | ||
| 100 | |||
| 101 | around BUILDARGS => \&AI::MXNet::Base::process_arguments; | ||
| 102 | method python_constructor_arguments() { ['dataset', 'batch_size'] } | ||
| 103 | |||
| 104 | sub BUILD | ||
| 105 | { | ||
| 106 | my $self = shift; | ||
| 107 | if($self->dataset->isa('PDL')) | ||
| 108 | { | ||
| 109 | $self->dataset(AI::MXNet::NDArray->array($self->dataset)); | ||
| 110 | } | ||
| 111 | if(not defined $self->batch_sampler) | ||
| 112 | { | ||
| 113 | if(not defined $self->batch_size) | ||
| 114 | { | ||
| 115 | confess("batch_size must be specified unless batch_sampler is specified"); | ||
| 116 | } | ||
| 117 | if(not defined $self->sampler) | ||
| 118 | { | ||
| 119 | if($self->shuffle) | ||
| 120 | { | ||
| 121 | $self->sampler( | ||
| 122 | AI::MXNet::Gluon::Data::Sampler::RandomSampler->new( | ||
| 123 | length => $self->dataset->len | ||
| 124 | ) | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | else | ||
| 128 | { | ||
| 129 | $self->sampler( | ||
| 130 | AI::MXNet::Gluon::Data::Sampler::SequentialSampler->new( | ||
| 131 | length => $self->dataset->len, | ||
| 132 | ) | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | elsif($self->shuffle) | ||
| 137 | { | ||
| 138 | confess("shuffle must not be specified if sampler is specified"); | ||
| 139 | } | ||
| 140 | $self->batch_sampler( | ||
| 141 | AI::MXNet::Gluon::Data::Sampler::BatchSampler->new( | ||
| 142 | sampler => $self->sampler, | ||
| 143 | batch_size => $self->batch_size, | ||
| 144 | last_batch => $self->last_batch | ||
| 145 | ) | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | elsif(defined $self->batch_size or $self->shuffle or defined $self->sampler or defined $self->last_batch) | ||
| 149 | { | ||
| 150 | confess("batch_size, shuffle, sampler and last_batch must ". | ||
| 151 | "not be specified if batch_sampler is specified."); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | use overload | ||
| 156 | '<>' => sub { | ||
| 157 | my $self = shift; | ||
| 158 | my $sampler = $self->batch_sampler; | ||
| 159 | my $batch = <$sampler>; | ||
| 160 | if(not defined $batch) | ||
| 161 | { | ||
| 162 | return undef; | ||
| 163 | }; | ||
| 164 | return _batchify([map { $self->dataset->at($_) } @{ $batch }], eval { $self->dataset->label->dtype }//'int32'); | ||
| 165 | }; | ||
| 166 | |||
| 167 | method len() | ||
| 168 | { | ||
| 169 | $self->batch_sampler->len; | ||
| 170 | } | ||
| 171 | |||
| 172 | use overload '@{}' => sub { shift->list }; | ||
| 173 | |||
| 174 | method list() | ||
| 175 | { | ||
| 176 | my @ret; | ||
| 177 | while(defined(my $data = <$self>)) | ||
| 178 | { | ||
| 179 | push @ret, $data; | ||
| 180 | } | ||
| 181 | return \@ret; | ||
| 182 | } | ||
| 183 | |||
| 184 | __PACKAGE__->register('AI::MXNet::Gluon::Data'); | ||
| 185 | |||
| 186 | 1; |