I am trying to use the Perl API of MXNet (AI::MXNet) to fetch a standard model from ModelZoo (AI::MXNet::Gluon::ModelZoo::Vision) and train it.
The following code does not complain but it does not fit(). fit() just returns immediately. My dataset is a file of images in the RecordIO format (constructed using im2rec) and I am quite sure it is valid.
I have modified code from here https://codehex.hateblo.jp/entry/2017/09/12/160149 , where the original author creates the model symbolically (and not from ModelZoo).
Is this, in principle, the right way to train a model from ModelZoo?
A second question is what's best practice for creating a data loader from a set of images. Is what I am doing (using mx->io->ImageRecordIter
) OK?
use strict;
use warnings;
use AI::MXNet qw/mx/;
use AI::MXNet::Gluon qw/gluon/; # needed for nn-> (which must be replaced by gluon->nn->...)
use AI::MXNet::Gluon::NN qw(nn); # for nn->
use AI::MXNet::Gluon::ModelZoo::Vision::VGG;
use AI::MXNet::Monitor;
my $ctx = mx->cpu(0);
my $vgg = AI::MXNet::Gluon::ModelZoo::Vision->get_vgg(
11, # num layers
(
'classes' => 26, # latin alphabet recognition
'root' => 'abc', # where to save the models
'ctx' => $ctx # context
)
);
die "get the model" unless defined $vgg;
my $batch_size = 4;
# num channels, width, height our png training images of letters:
my $img_shape = [3, 256, 256];
my $training_file = 'training.bin';
my $train_dataiter = mx->io->ImageRecordIter({
'path_imgrec' => $training_file,
'path_imglist' => 'training.lst',
# num channels, width, height
'data_shape' => $img_shape,
'batch_size' => $batch_size,
'label_width' => 1, # dimensionality of labels, for us is 1 (i.e. just the letter name)
});
die "mx->io->ImageRecordIter()" unless defined $train_dataiter;
$vgg->init_params(initializer => mx->init->Xavier(magnitude => 2));
$vgg->init_optimizer(optimizer => 'sgd', optimizer_params => {learning_rate => 0.1});
$vgg->initialize();
print "$0 : fitting ...\n";
$vgg->fit(
$train_dataiter,
'num_epoch' => 1000
);