| 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 | package AI::MXNet::IO; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::NS; | ||
| 22 | use AI::MXNet::Base; | ||
| 23 | use AI::MXNet::Function::Parameters; | ||
| 24 | use Scalar::Util qw/blessed/; | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | AI::MXNet::IO - Data loading interface of MXNet | ||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This document summarizes supported data formats and iterator APIs to read the data including | ||
| 34 | mx->io Data iterators for common data formats. | ||
| 35 | mx->recordio Data iterators for the RecordIO data format. | ||
| 36 | mx->image Image Iterators and image augmentation functions. | ||
| 37 | =cut | ||
| 38 | |||
| 39 | # Convert data into canonical form. | ||
| 40 | method init_data( | ||
| 41 | Maybe[AcceptableInput|HashRef[AcceptableInput]|ArrayRef[AcceptableInput]|Hash::Ordered] $data, | ||
| 42 | Undef|Int :$allow_empty=, | ||
| 43 | Str :$default_name | ||
| 44 | ) | ||
| 45 | { | ||
| 46 | Carp::confess("data must be defined or allow_empty set to true value") | ||
| 47 | if(not defined $data and not $allow_empty); | ||
| 48 | $data //= []; | ||
| 49 | if(blessed $data and not $data->isa('Hash::Ordered')) | ||
| 50 | { | ||
| 51 | $data = [$data]; | ||
| 52 | } | ||
| 53 | |||
| 54 | Carp::confess("data must not be empty or allow_empty set to true value") | ||
| 55 | if(ref($data) eq 'ARRAY' and not @{ $data } and not $allow_empty); | ||
| 56 | |||
| 57 | my @ret; | ||
| 58 | if(ref($data) eq 'ARRAY') | ||
| 59 | { | ||
| 60 | if(@{ $data } == 1) | ||
| 61 | { | ||
| 62 | @ret = ([$default_name, $data->[0]]); | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { | ||
| 66 | my $i = -1; | ||
| 67 | @ret = map { $i++; ["_${i}_$default_name", $_] } @{ $data }; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | elsif(ref($data) eq 'HASH') | ||
| 71 | { | ||
| 72 | AI::MXNet::Logging->warning( | ||
| 73 | "Use of a raw perl hash as input is obsolete and the behaviour of the iterator is undefined.\n". | ||
| 74 | "Please use Hash::Ordered object instead." | ||
| 75 | ); | ||
| 76 | while(my ($k, $v) = each %{ $data }) | ||
| 77 | { | ||
| 78 | push @ret, [$k, $v]; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | elsif(blessed $data and $data->isa('Hash::Ordered')) | ||
| 82 | { | ||
| 83 | for my $k ($data->keys) | ||
| 84 | { | ||
| 85 | push @ret, [$k, $data->get($k)]; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | for my $d (@ret) | ||
| 89 | { | ||
| 90 | if(not (blessed $d->[1] and $d->[1]->isa('AI::MXNet::NDArray'))) | ||
| 91 | { | ||
| 92 | $d->[1] = AI::MXNet::NDArray->array($d->[1]); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return \@ret; | ||
| 96 | } | ||
| 97 | |||
| 98 | method DataDesc(@args) { AI::MXNet::DataDesc->new(@args) } | ||
| 99 | method DataBatch(@args) { AI::MXNet::DataBatch->new(@args) } | ||
| 100 | |||
| 101 | package AI::MXNet::DataDesc; | ||
| 102 | use Mouse; | ||
| 103 | use overload '""' => \&stringify, | ||
| 104 | '@{}' => \&to_nameshape; | ||
| 105 | has 'name' => (is => 'ro', isa => "Str", required => 1); | ||
| 106 | has 'shape' => (is => 'ro', isa => "Shape", required => 1); | ||
| 107 | has 'dtype' => (is => 'ro', isa => "Dtype", default => 'float32'); | ||
| 108 | has 'layout' => (is => 'ro', isa => "Str", default => 'NCHW'); | ||
| 109 | |||
| 110 | around BUILDARGS => sub { | ||
| 111 | my $orig = shift; | ||
| 112 | my $class = shift; | ||
| 113 | if(@_ >= 2 and ref $_[1] eq 'ARRAY') | ||
| 114 | { | ||
| 115 | my $name = shift; | ||
| 116 | my $shape = shift; | ||
| 117 | return $class->$orig(name => $name, shape => $shape, @_); | ||
| 118 | } | ||
| 119 | return $class->$orig(@_); | ||
| 120 | }; | ||
| 121 | |||
| 122 | method stringify($other=, $reverse=) | ||
| 123 | { | ||
| 124 | sprintf( | ||
| 125 | "DataDesc[%s,%s,%s,%s]", | ||
| 126 | $self->name, | ||
| 127 | join('x', @{ $self->shape }), | ||
| 128 | $self->dtype, | ||
| 129 | $self->layout | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | |||
| 133 | method to_nameshape($other=, $reverse=) | ||
| 134 | { | ||
| 135 | [$self->name, $self->shape]; | ||
| 136 | } | ||
| 137 | |||
| 138 | =head1 NAME | ||
| 139 | |||
| 140 | AI::MXNet::DataDesc - A container class for describing the data layout. | ||
| 141 | =cut | ||
| 142 | |||
| 143 | =head2 get_batch_axis | ||
| 144 | |||
| 145 | Get the dimension that corresponds to the batch size. | ||
| 146 | |||
| 147 | Parameters | ||
| 148 | ---------- | ||
| 149 | layout : str | ||
| 150 | layout string. For example, "NCHW". | ||
| 151 | |||
| 152 | Returns | ||
| 153 | ------- | ||
| 154 | An axis indicating the batch_size dimension. When data-parallelism is | ||
| 155 | used, the data will be automatically split and concatenate along the batch_size | ||
| 156 | dimension. Axis can be -1, which means the whole array will be copied for each | ||
| 157 | data-parallelism device. | ||
| 158 | =cut | ||
| 159 | |||
| 160 | method get_batch_axis(Str|Undef $layout) | ||
| 161 | { | ||
| 162 | return 0 unless defined $layout; | ||
| 163 | return index($layout, 'N'); | ||
| 164 | } | ||
| 165 | |||
| 166 | =head2 get_list | ||
| 167 | |||
| 168 | Coverts the input to an array ref AI::MXNet::DataDesc objects. | ||
| 169 | |||
| 170 | Parameters | ||
| 171 | ---------- | ||
| 172 | $shapes : HashRef[Shape] | ||
| 173 | $types= : Maybe[HashRef[Dtype]] | ||
| 174 | =cut | ||
| 175 | |||
| 176 | method get_list(HashRef[Shape] $shapes, Maybe[HashRef[Dtype]] $types=) | ||
| 177 | { | ||
| 178 | $types //= {}; | ||
| 179 | return [ | ||
| 180 | map { | ||
| 181 | AI::MXNet::DataDesc->new( | ||
| 182 | name => $_, | ||
| 183 | shape => $shapes->{$_}, | ||
| 184 | (exists $types->{$_} ? (type => $types->{$_}) : ()) | ||
| 185 | ) | ||
| 186 | } keys %{ $shapes } | ||
| 187 | ]; | ||
| 188 | } | ||
| 189 | |||
| 190 | package AI::MXNet::DataBatch; | ||
| 191 | use Mouse; | ||
| 192 | |||
| 193 | =head1 NAME | ||
| 194 | |||
| 195 | AI::MXNet::DataBatch - A container for a mini-batch of the data and related information. | ||
| 196 | =cut | ||
| 197 | |||
| 198 | =head1 DESCRIPTION | ||
| 199 | |||
| 200 | Default object for holding a mini-batch of data and related information. | ||
| 201 | =cut | ||
| 202 | |||
| 203 | has 'data' => (is => 'rw', isa => 'Maybe[ArrayRef[AI::MXNet::NDArray]]', required => 1); | ||
| 204 | has 'label' => (is => 'rw', isa => 'Maybe[ArrayRef[AI::MXNet::NDArray]]'); | ||
| 205 | has 'pad' => (is => 'rw'); | ||
| 206 | has 'index' => (is => 'rw'); | ||
| 207 | has 'bucket_key' => (is => 'rw'); | ||
| 208 | has 'provide_data' => (is => 'rw'); | ||
| 209 | has 'provide_label' => (is => 'rw'); | ||
| 210 | |||
| 211 | package AI::MXNet::DataIter; | ||
| 212 | use Mouse; | ||
| 213 | use overload '<>' => sub { shift->next }, | ||
| 214 | '@{}' => sub { shift->list }; | ||
| 215 | |||
| 216 | =head1 NAME | ||
| 217 | |||
| 218 | AI::MXNet::DataIter - A parent class for MXNet data iterators. | ||
| 219 | =cut | ||
| 220 | |||
| 221 | has 'batch_size' => (is => 'rw', isa => 'Int', default => 0); | ||
| 222 | |||
| 223 | =head2 reset | ||
| 224 | |||
| 225 | Reset the iterator. | ||
| 226 | =cut | ||
| 227 | |||
| 228 | method reset(){} | ||
| 229 | |||
| 230 | =head2 list | ||
| 231 | |||
| 232 | Returns remaining iterator items as an array ref. | ||
| 233 | =cut | ||
| 234 | |||
| 235 | method list() | ||
| 236 | { | ||
| 237 | my @ret; | ||
| 238 | while(my $data = <$self>) | ||
| 239 | { | ||
| 240 | $data->label([map { $_->copy } @{ $data->label }]); | ||
| 241 | $data->data([map { $_->copy } @{ $data->data }]); | ||
| 242 | push @ret, $data; | ||
| 243 | } | ||
| 244 | return \@ret; | ||
| 245 | } | ||
| 246 | |||
| 247 | =head2 next | ||
| 248 | |||
| 249 | Returns the next data batch from the iterator. | ||
| 250 | |||
| 251 | Returns | ||
| 252 | ------- | ||
| 253 | $data : AI::MXNet::DataBatch | ||
| 254 | The data of next batch. | ||
| 255 | =cut | ||
| 256 | |||
| 257 | method next() | ||
| 258 | { | ||
| 259 | if($self->iter_next()) | ||
| 260 | { | ||
| 261 | return AI::MXNet::DataBatch->new( | ||
| 262 | data => $self->getdata, | ||
| 263 | label => $self->getlabel, | ||
| 264 | pad => $self->getpad, | ||
| 265 | index => $self->getindex | ||
| 266 | ); | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | return undef; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 iter_next | ||
| 275 | |||
| 276 | Iterate to next batch. | ||
| 277 | |||
| 278 | Returns | ||
| 279 | ------- | ||
| 280 | $has_next : Bool | ||
| 281 | =cut | ||
| 282 | |||
| 283 | method iter_next(){} | ||
| 284 | |||
| 285 | =head2 get_data | ||
| 286 | |||
| 287 | The data of current batch. | ||
| 288 | |||
| 289 | Returns | ||
| 290 | ------- | ||
| 291 | data : AI::MXNet::NDArray | ||
| 292 | =cut | ||
| 293 | |||
| 294 | method get_data(){} | ||
| 295 | |||
| 296 | =head2 getlabel | ||
| 297 | |||
| 298 | The label of the current batch. | ||
| 299 | |||
| 300 | Returns | ||
| 301 | ------- | ||
| 302 | label : AI::MXNet::NDArray | ||
| 303 | =cut | ||
| 304 | |||
| 305 | method getlabel(){} | ||
| 306 | |||
| 307 | =head2 getindex | ||
| 308 | |||
| 309 | The index of the current batch. | ||
| 310 | |||
| 311 | Returns | ||
| 312 | ------- | ||
| 313 | $index : PDL | ||
| 314 | =cut | ||
| 315 | |||
| 316 | method getindex(){} | ||
| 317 | |||
| 318 | =head2 getpad | ||
| 319 | |||
| 320 | The number of padding examples in the current batch. | ||
| 321 | |||
| 322 | Returns | ||
| 323 | ------- | ||
| 324 | $pad : Int | ||
| 325 | =cut | ||
| 326 | |||
| 327 | method getpad(){} | ||
| 328 | |||
| 329 | package AI::MXNet::ResizeIter; | ||
| 330 | use Mouse; | ||
| 331 | |||
| 332 | extends 'AI::MXNet::DataIter'; | ||
| 333 | |||
| 334 | =head1 NAME | ||
| 335 | |||
| 336 | AI::MXNet::ResizeIter | ||
| 337 | =cut | ||
| 338 | |||
| 339 | =head1 DESCRIPTION | ||
| 340 | |||
| 341 | Resize a DataIter to a given number of batches per epoch. | ||
| 342 | May produce incomplete batch in the middle of an epoch due | ||
| 343 | to the padding from internal iterator. | ||
| 344 | |||
| 345 | Parameters | ||
| 346 | ---------- | ||
| 347 | data_iter : DataIter | ||
| 348 | Internal data iterator. | ||
| 349 | size : number of batches per epoch to resize to. | ||
| 350 | reset_internal : whether to reset internal iterator on ResizeIter.reset | ||
| 351 | =cut | ||
| 352 | |||
| 353 | has 'data_iter' => (is => 'ro', isa => 'AI::MXnet::DataIter', required => 1); | ||
| 354 | has 'size' => (is => 'ro', isa => 'Int', required => 1); | ||
| 355 | has 'reset_internal' => (is => 'rw', isa => 'Int', default => 1); | ||
| 356 | has 'cur' => (is => 'rw', isa => 'Int', default => 0); | ||
| 357 | has 'current_batch' => (is => 'rw', isa => 'Maybe[AI::MXNet::DataBatch]'); | ||
| 358 | has [qw/provide_data | ||
| 359 | default_bucket_key | ||
| 360 | provide_label | ||
| 361 | batch_size/] => (is => 'rw', init_arg => undef); | ||
| 362 | |||
| 363 | sub BUILD | ||
| 364 | { | ||
| 365 | my $self = shift; | ||
| 366 | $self->provide_data($self->data_iter->provide_data); | ||
| 367 | $self->provide_label($self->data_iter->provide_label); | ||
| 368 | $self->batch_size($self->data_iter->batch_size); | ||
| 369 | if($self->data_iter->can('default_bucket_key')) | ||
| 370 | { | ||
| 371 | $self->default_bucket_key($self->data_iter->default_bucket_key); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | method reset() | ||
| 376 | { | ||
| 377 | $self->cur(0); | ||
| 378 | if($self->reset_internal) | ||
| 379 | { | ||
| 380 | $self->data_iter->reset; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | method iter_next() | ||
| 385 | { | ||
| 386 | return 0 if($self->cur == $self->size); | ||
| 387 | $self->current_batch($self->data_iter->next); | ||
| 388 | if(not defined $self->current_batch) | ||
| 389 | { | ||
| 390 | $self->data_iter->reset; | ||
| 391 | $self->current_batch($self->data_iter->next); | ||
| 392 | } | ||
| 393 | $self->cur($self->cur + 1); | ||
| 394 | return 1; | ||
| 395 | } | ||
| 396 | |||
| 397 | method get_data() | ||
| 398 | { | ||
| 399 | return $self->current_batch->data; | ||
| 400 | } | ||
| 401 | |||
| 402 | method getlabel() | ||
| 403 | { | ||
| 404 | return $self->current_batch->label; | ||
| 405 | } | ||
| 406 | |||
| 407 | method getindex() | ||
| 408 | { | ||
| 409 | return $self->current_batch->index; | ||
| 410 | } | ||
| 411 | |||
| 412 | method getpad() | ||
| 413 | { | ||
| 414 | return $self->current_batch->pad; | ||
| 415 | } | ||
| 416 | |||
| 417 | package AI::MXNet::NDArrayIter; | ||
| 418 | use Mouse; | ||
| 419 | use AI::MXNet::Base; | ||
| 420 | use List::Util; | ||
| 421 | extends 'AI::MXNet::DataIter'; | ||
| 422 | |||
| 423 | =head1 NAME | ||
| 424 | |||
| 425 | AI::MXNet::NDArrayIter - Predefined NDArray iterator. | ||
| 426 | =cut | ||
| 427 | |||
| 428 | =head1 DESCRIPTION | ||
| 429 | |||
| 430 | Predefined NDArray iterator. Accepts PDL or AI::MXNet::NDArray object as an input. | ||
| 431 | |||
| 432 | Parameters | ||
| 433 | ---------- | ||
| 434 | data: Maybe[AcceptableInput|HashRef[AcceptableInput]|ArrayRef[AcceptableInput]]. | ||
| 435 | NDArrayIter supports single or multiple data and label. | ||
| 436 | label: Maybe[AcceptableInput|HashRef[AcceptableInput]|ArrayRef[AcceptableInput]]. | ||
| 437 | Same as data, but is not given to the model during testing. | ||
| 438 | batch_size=1: Int | ||
| 439 | Batch Size | ||
| 440 | shuffle=0: Bool | ||
| 441 | Whether to shuffle the data | ||
| 442 | last_batch_handle='pad': 'pad', 'discard' or 'roll_over' | ||
| 443 | How to handle the last batch | ||
| 444 | |||
| 445 | Note | ||
| 446 | ---- | ||
| 447 | This iterator will pad, discard or roll over the last batch if | ||
| 448 | the size of data does not match batch_size. Roll over is intended | ||
| 449 | for training and can cause problems if used for prediction. | ||
| 450 | =cut | ||
| 451 | |||
| 452 | has 'data' => (is => 'rw', isa => 'Maybe[AcceptableInput|HashRef[AcceptableInput]|ArrayRef[AcceptableInput]|Hash::Ordered]'); | ||
| 453 | has 'data_list' => (is => 'rw', isa => 'ArrayRef[AI::MXNet::NDArray]'); | ||
| 454 | has 'label' => (is => 'rw', isa => 'Maybe[AcceptableInput|HashRef[AcceptableInput]|ArrayRef[AcceptableInput]|Hash::Ordered]'); | ||
| 455 | has 'batch_size' => (is => 'rw', isa => 'Int', default => 1); | ||
| 456 | has 'shuffle' => (is => 'rw', isa => 'Bool', default => 0); | ||
| 457 | has 'last_batch_handle' => (is => 'rw', isa => 'Str', default => 'pad'); | ||
| 458 | has 'label_name' => (is => 'rw', isa => 'Str', default => 'softmax_label'); | ||
| 459 | has 'num_source' => (is => 'rw', isa => 'Int'); | ||
| 460 | has 'cursor' => (is => 'rw', isa => 'Int'); | ||
| 461 | has 'num_data' => (is => 'rw', isa => 'Int'); | ||
| 462 | |||
| 463 | around BUILDARGS => \&AI::MXNet::Base::process_arguments; | ||
| 464 | method python_constructor_arguments() { ['data', 'label'] }; | ||
| 465 | |||
| 466 | sub BUILD | ||
| 467 | { | ||
| 468 | my $self = shift; | ||
| 469 | my $data = AI::MXNet::IO->init_data($self->data, allow_empty => 0, default_name => 'data'); | ||
| 470 | my $label = AI::MXNet::IO->init_data($self->label, allow_empty => 1, default_name => $self->label_name); | ||
| 471 | if( | ||
| 472 | ( | ||
| 473 | (blessed $data and $data->isa('AI::MXNet::NDArray::CSR')) | ||
| 474 | or | ||
| 475 | (blessed $label and $label->isa('AI::MXNet::NDArray::CSR')) | ||
| 476 | ) | ||
| 477 | and | ||
| 478 | ($self->last_batch_handle != 'discard') | ||
| 479 | ) | ||
| 480 | { | ||
| 481 | confess("`NDArrayIter` only supports AI::MXNet::NDArray::CSR with `last_batch_handle` set to discard."); | ||
| 482 | } | ||
| 483 | my $num_data = $data->[0][1]->shape->[0]; | ||
| 484 | confess("size of data dimension 0 $num_data < batch_size ${\ $self->batch_size }") | ||
| 485 | unless($num_data >= $self->batch_size); | ||
| 486 | if($self->shuffle) | ||
| 487 | { | ||
| 488 | my @idx = List::Util::shuffle(0..$num_data-1); | ||
| 489 | $_->[1] = AI::MXNet::NDArray->array( | ||
| 490 | pdl_shuffle($_->[1]->stype eq 'csr' ? $_->[1]->aspdlccs : $_->[1]->aspdl, \@idx), | ||
| 491 | ctx => $_->[1]->context | ||
| 492 | ) for (@$data, @$label); | ||
| 493 | } | ||
| 494 | if($self->last_batch_handle eq 'discard') | ||
| 495 | { | ||
| 496 | my $new_n = $num_data - $num_data % $self->batch_size - 1; | ||
| 497 | $_->[1] = $_->[1]->slice([0, $new_n]) for @$data; | ||
| 498 | $_->[1] = $_->[1]->slice([0, $new_n]) for @$label; | ||
| 499 | $num_data = $new_n + 1; | ||
| 500 | } | ||
| 501 | my $data_list = [map { $_->[1] } (@{ $data }, @{ $label })]; | ||
| 502 | my $num_source = @{ $data_list }; | ||
| 503 | my $cursor = -$self->batch_size; | ||
| 504 | $self->data($data); | ||
| 505 | $self->data_list($data_list); | ||
| 506 | $self->label($label); | ||
| 507 | $self->num_source($num_source); | ||
| 508 | $self->cursor($cursor); | ||
| 509 | $self->num_data($num_data); | ||
| 510 | } | ||
| 511 | |||
| 512 | # The name and shape of data provided by this iterator | ||
| 513 | method provide_data() | ||
| 514 | { | ||
| 515 | return [map { | ||
| 516 | my ($k, $v) = @{ $_ }; | ||
| 517 | my $shape = $v->shape; | ||
| 518 | $shape->[0] = $self->batch_size; | ||
| 519 | AI::MXNet::DataDesc->new(name => $k, shape => $shape, dtype => $v->dtype) | ||
| 520 | } @{ $self->data }]; | ||
| 521 | } | ||
| 522 | |||
| 523 | # The name and shape of label provided by this iterator | ||
| 524 | method provide_label() | ||
| 525 | { | ||
| 526 | return [map { | ||
| 527 | my ($k, $v) = @{ $_ }; | ||
| 528 | my $shape = $v->shape; | ||
| 529 | $shape->[0] = $self->batch_size; | ||
| 530 | AI::MXNet::DataDesc->new(name => $k, shape => $shape, dtype => $v->dtype) | ||
| 531 | } @{ $self->label }]; | ||
| 532 | } | ||
| 533 | |||
| 534 | # Ignore roll over data and set to start | ||
| 535 | method hard_reset() | ||
| 536 | { | ||
| 537 | $self->cursor(-$self->batch_size); | ||
| 538 | } | ||
| 539 | |||
| 540 | method reset() | ||
| 541 | { | ||
| 542 | if($self->last_batch_handle eq 'roll_over' and $self->cursor > $self->num_data) | ||
| 543 | { | ||
| 544 | $self->cursor(-$self->batch_size + ($self->cursor%$self->num_data)%$self->batch_size); | ||
| 545 | } | ||
| 546 | else | ||
| 547 | { | ||
| 548 | $self->cursor(-$self->batch_size); | ||
| 549 | } | ||
| 550 | } | ||
| 551 | |||
| 552 | method iter_next() | ||
| 553 | { | ||
| 554 | $self->cursor($self->batch_size + $self->cursor); | ||
| 555 | return $self->cursor < $self->num_data; | ||
| 556 | } | ||
| 557 | |||
| 558 | method next() | ||
| 559 | { | ||
| 560 | if($self->iter_next) | ||
| 561 | { | ||
| 562 | return AI::MXNet::DataBatch->new( | ||
| 563 | data => $self->getdata, | ||
| 564 | label => $self->getlabel, | ||
| 565 | pad => $self->getpad, | ||
| 566 | index => undef | ||
| 567 | ); | ||
| 568 | } | ||
| 569 | else | ||
| 570 | { | ||
| 571 | return undef; | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | # Load data from underlying arrays, internal use only | ||
| 576 | method _getdata($data_source) | ||
| 577 | { | ||
| 578 | confess("DataIter needs reset.") unless $self->cursor < $self->num_data; | ||
| 579 | if(($self->cursor + $self->batch_size) <= $self->num_data) | ||
| 580 | { | ||
| 581 | return [ | ||
| 582 | map { | ||
| 583 | $_->[1]->slice([$self->cursor,$self->cursor+$self->batch_size-1]) | ||
| 584 | } @{ $data_source } | ||
| 585 | ]; | ||
| 586 | } | ||
| 587 | else | ||
| 588 | { | ||
| 589 | my $pad = $self->batch_size - $self->num_data + $self->cursor - 1; | ||
| 590 | return [ | ||
| 591 | map { | ||
| 592 | AI::MXNet::NDArray->concatenate( | ||
| 593 | [ | ||
| 594 | $_->[1]->slice([$self->cursor, -1]), | ||
| 595 | $_->[1]->slice([0, $pad]) | ||
| 596 | ] | ||
| 597 | ) | ||
| 598 | } @{ $data_source } | ||
| 599 | ]; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | method getdata() | ||
| 604 | { | ||
| 605 | return $self->_getdata($self->data); | ||
| 606 | } | ||
| 607 | |||
| 608 | method getlabel() | ||
| 609 | { | ||
| 610 | return $self->_getdata($self->label); | ||
| 611 | } | ||
| 612 | |||
| 613 | method getpad() | ||
| 614 | { | ||
| 615 | if( $self->last_batch_handle eq 'pad' | ||
| 616 | and | ||
| 617 | ($self->cursor + $self->batch_size) > $self->num_data | ||
| 618 | ) | ||
| 619 | { | ||
| 620 | return $self->cursor + $self->batch_size - $self->num_data; | ||
| 621 | } | ||
| 622 | else | ||
| 623 | { | ||
| 624 | return 0; | ||
| 625 | } | ||
| 626 | } | ||
| 627 | package AI::MXNet::MXDataIter; | ||
| 628 | use Mouse; | ||
| 629 | use AI::MXNet::Base; | ||
| 630 | |||
| 631 | extends 'AI::MXNet::DataIter'; | ||
| 632 | |||
| 633 | =head1 NAME | ||
| 634 | |||
| 635 | AI::MXNet::MXDataIter - A data iterator pre-built in C++ layer of MXNet. | ||
| 636 | =cut | ||
| 637 | |||
| 638 | =head1 DESCRIPTION | ||
| 639 | |||
| 640 | Here are the list of currently available predefined iterators, for more custom iterators | ||
| 641 | please check out the examples directory. | ||
| 642 | Also please refer to the L<Python docs|https://mxnet.apache.org/api/python/io/io.html> | ||
| 643 | mx->io->CSVIter Returns the CSV file iterator. | ||
| 644 | mx->io->LibSVMIter Returns the LibSVM iterator which returns data with csr storage type. | ||
| 645 | mx->io->ImageRecordIter Iterates on image RecordIO files | ||
| 646 | mx->io->ImageRecordInt8Iter Iterating on image RecordIO files | ||
| 647 | mx->io->ImageRecordUInt8Iter Iterating on image RecordIO files | ||
| 648 | mx->io->MNISTIter Iterating on the MNIST dataset. | ||
| 649 | mx->recordio->MXRecordIO Reads/writes RecordIO data format, supporting sequential read and write. | ||
| 650 | mx->recordio->MXIndexedRecordIO Reads/writes RecordIO data format, supporting random access. | ||
| 651 | mx->image->ImageIter Image data iterator with a large number of augmentation choices. | ||
| 652 | =cut | ||
| 653 | |||
| 654 | has 'handle' => (is => 'ro', isa => 'DataIterHandle', required => 1); | ||
| 655 | has '_debug_skip_load' => (is => 'rw', isa => 'Int', default => 0); | ||
| 656 | has '_debug_at_begin' => (is => 'rw', isa => 'Int', default => 0); | ||
| 657 | has 'data_name' => (is => 'ro', isa => 'Str', default => 'data'); | ||
| 658 | has 'label_name' => (is => 'ro', isa => 'Str', default => 'softmax_label'); | ||
| 659 | has [qw/first_batch | ||
| 660 | provide_data | ||
| 661 | provide_label | ||
| 662 | batch_size/] => (is => 'rw', init_arg => undef); | ||
| 663 | |||
| 664 | sub BUILD | ||
| 665 | { | ||
| 666 | my $self = shift; | ||
| 667 | $self->first_batch($self->next); | ||
| 668 | my $data = $self->first_batch->data->[0]; | ||
| 669 | $self->provide_data([ | ||
| 670 | AI::MXNet::DataDesc->new( | ||
| 671 | name => $self->data_name, | ||
| 672 | shape => $data->shape, | ||
| 673 | dtype => $data->dtype | ||
| 674 | ) | ||
| 675 | ]); | ||
| 676 | my $label = $self->first_batch->label->[0]; | ||
| 677 | $self->provide_label([ | ||
| 678 | AI::MXNet::DataDesc->new( | ||
| 679 | name => $self->label_name, | ||
| 680 | shape => $label->shape, | ||
| 681 | dtype => $label->dtype | ||
| 682 | ) | ||
| 683 | ]); | ||
| 684 | $self->batch_size($data->shape->[0]); | ||
| 685 | } | ||
| 686 | |||
| 687 | sub DEMOLISH | ||
| 688 | { | ||
| 689 | check_call(AI::MXNetCAPI::DataIterFree(shift->handle)); | ||
| 690 | } | ||
| 691 | |||
| 692 | =head2 debug_skip_load | ||
| 693 | |||
| 694 | Set the iterator to simply return always first batch. | ||
| 695 | Notes | ||
| 696 | ----- | ||
| 697 | This can be used to test the speed of network without taking | ||
| 698 | the loading delay into account. | ||
| 699 | =cut | ||
| 700 | |||
| 701 | method debug_skip_load() | ||
| 702 | { | ||
| 703 | $self->_debug_skip_load(1); | ||
| 704 | AI::MXNet::Logging->info('Set debug_skip_load to be true, will simply return first batch'); | ||
| 705 | } | ||
| 706 | |||
| 707 | method reset() | ||
| 708 | { | ||
| 709 | $self->_debug_at_begin(1); | ||
| 710 | $self->first_batch(undef); | ||
| 711 | check_call(AI::MXNetCAPI::DataIterBeforeFirst($self->handle)); | ||
| 712 | } | ||
| 713 | |||
| 714 | |||
| 715 | method next() | ||
| 716 | { | ||
| 717 | if($self->_debug_skip_load and not $self->_debug_at_begin) | ||
| 718 | { | ||
| 719 | return AI::MXNet::DataBatch->new( | ||
| 720 | data => [$self->getdata], | ||
| 721 | label => [$self->getlabel], | ||
| 722 | pad => $self->getpad, | ||
| 723 | index => $self->getindex | ||
| 724 | ); | ||
| 725 | } | ||
| 726 | if(defined $self->first_batch) | ||
| 727 | { | ||
| 728 | my $batch = $self->first_batch; | ||
| 729 | $self->first_batch(undef); | ||
| 730 | return $batch | ||
| 731 | } | ||
| 732 | $self->_debug_at_begin(0); | ||
| 733 | my $next_res = check_call(AI::MXNetCAPI::DataIterNext($self->handle)); | ||
| 734 | if($next_res) | ||
| 735 | { | ||
| 736 | return AI::MXNet::DataBatch->new( | ||
| 737 | data => [$self->getdata], | ||
| 738 | label => [$self->getlabel], | ||
| 739 | pad => $self->getpad, | ||
| 740 | index => $self->getindex | ||
| 741 | ); | ||
| 742 | } | ||
| 743 | else | ||
| 744 | { | ||
| 745 | return undef; | ||
| 746 | } | ||
| 747 | } | ||
| 748 | |||
| 749 | method iter_next() | ||
| 750 | { | ||
| 751 | if(defined $self->first_batch) | ||
| 752 | { | ||
| 753 | return 1; | ||
| 754 | } | ||
| 755 | else | ||
| 756 | { | ||
| 757 | return scalar(check_call(AI::MXNetCAPI::DataIterNext($self->handle))); | ||
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | method getdata() | ||
| 762 | { | ||
| 763 | my $handle = check_call(AI::MXNetCAPI::DataIterGetData($self->handle)); | ||
| 764 | return AI::MXNet::NDArray->_ndarray_cls($handle); | ||
| 765 | } | ||
| 766 | |||
| 767 | method getlabel() | ||
| 768 | { | ||
| 769 | my $handle = check_call(AI::MXNetCAPI::DataIterGetLabel($self->handle)); | ||
| 770 | return AI::MXNet::NDArray->_ndarray_cls($handle); | ||
| 771 | } | ||
| 772 | |||
| 773 | method getindex() | ||
| 774 | { | ||
| 775 | return pdl(check_call(AI::MXNetCAPI::DataIterGetIndex($self->handle))); | ||
| 776 | } | ||
| 777 | |||
| 778 | method getpad() | ||
| 779 | { | ||
| 780 | return scalar(check_call(AI::MXNetCAPI::DataIterGetPadNum($self->handle))); | ||
| 781 | } | ||
| 782 | |||
| 783 | package AI::MXNet::IO; | ||
| 784 | |||
| 785 | sub NDArrayIter { shift; return AI::MXNet::NDArrayIter->new(@_); } | ||
| 786 | |||
| 787 | my %iter_meta; | ||
| 788 | method get_iter_meta() | ||
| 789 | { | ||
| 790 | return \%iter_meta; | ||
| 791 | } | ||
| 792 | |||
| 793 | # Create an io iterator by handle. | ||
| 794 | func _make_io_iterator($handle) | ||
| 795 | { | ||
| 796 | my ($iter_name, $desc, | ||
| 797 | $arg_names, $arg_types, $arg_descs | ||
| 798 | ) = @{ check_call(AI::MXNetCAPI::DataIterGetIterInfo($handle)) }; | ||
| 799 | my $param_str = build_param_doc($arg_names, $arg_types, $arg_descs); | ||
| 800 | my $doc_str = "$desc\n\n" | ||
| 801 | ."$param_str\n" | ||
| 802 | ."name : string, required.\n" | ||
| 803 | ." Name of the resulting data iterator.\n\n" | ||
| 804 | ."Returns\n" | ||
| 805 | ."-------\n" | ||
| 806 | ."iterator: DataIter\n" | ||
| 807 | ." The result iterator."; | ||
| 808 | my $iter = sub { | ||
| 809 | my $class = shift; | ||
| 810 | my (@args, %kwargs); | ||
| 811 | if(@_ and ref $_[-1] eq 'HASH') | ||
| 812 | { | ||
| 813 | %kwargs = %{ pop(@_) }; | ||
| 814 | } | ||
| 815 | @args = @_; | ||
| 816 | Carp::confess("$iter_name can only accept keyword arguments") | ||
| 817 | if @args; | ||
| 818 | for my $key (keys %kwargs) | ||
| 819 | { | ||
| 820 | $kwargs{ $key } = "(" .join(",", @{ $kwargs{ $key } }) .")" | ||
| 821 | if ref $kwargs{ $key } eq 'ARRAY'; | ||
| 822 | } | ||
| 823 | my $handle = check_call( | ||
| 824 | AI::MXNetCAPI::DataIterCreateIter( | ||
| 825 | $handle, | ||
| 826 | scalar(keys %kwargs), | ||
| 827 | \%kwargs | ||
| 828 | ) | ||
| 829 | ); | ||
| 830 | return AI::MXNet::MXDataIter->new(handle => $handle, %kwargs); | ||
| 831 | }; | ||
| 832 | $iter_meta{$iter}{__name__} = $iter_name; | ||
| 833 | $iter_meta{$iter}{__doc__} = $doc_str; | ||
| 834 | return $iter; | ||
| 835 | } | ||
| 836 | |||
| 837 | # List and add all the data iterators to current module. | ||
| 838 | method _init_io_module() | ||
| 839 | { | ||
| 840 | for my $creator (@{ check_call(AI::MXNetCAPI::ListDataIters()) }) | ||
| 841 | { | ||
| 842 | my $data_iter = _make_io_iterator($creator); | ||
| 843 | { | ||
| 844 | my $name = $iter_meta{ $data_iter }{__name__}; | ||
| 845 | no strict 'refs'; | ||
| 846 | { | ||
| 847 | *{__PACKAGE__."::$name"} = $data_iter; | ||
| 848 | } | ||
| 849 | } | ||
| 850 | } | ||
| 851 | } | ||
| 852 | |||
| 853 | # Initialize the io in startups | ||
| 854 | __PACKAGE__->_init_io_module; | ||
| 855 | |||
| 856 | 1; |