4

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
);
bliako
  • 977
  • 1
  • 5
  • 16

1 Answers1

2

It's not the right wait to train. The ModelZoo returns a Gluon Hybrid block. The 'fit' is used in Module interface. See how you can train with the Gluon:

https://metacpan.org/source/SKOLYCHEV/AI-MXNet-1.4/examples/gluon/mnist.pl

The same with Module: https://metacpan.org/source/SKOLYCHEV/AI-MXNet-1.4/examples/mnist.pl

You can call ->export method of hybrid block like $vgg->export(); https://metacpan.org/source/SKOLYCHEV/AI-MXNet-1.4/lib/AI/MXNet/Gluon/Block.pm#L1242

This will save json structure of the network and current parameters into a file. Then you can initialize the Module interface from these.

Hito Yume
  • 21
  • 3
  • Hito Yume thanks! I need to use my custom data which are images of alphabet letters. I am flexible about model or if Gluon or not. I do not know how to use the 1st example you cited with my dataiter and I do not know how to use the 2nd example with some ready-made solution of optical character recognition. Do you have any more information about how to use a dataiter with a Gluon from ModelZoo? Do you have any information about exporting and importing parameters? In ```export()``` method it says to move forwartd once? How do you do that? Summary: I need lots of examples please if you have any. – bliako Feb 10 '20 at 13:17
  • The gluon example I posted will work as is I think with any iterator, because all of them have the same interface. The forward operation is simply calling gluon block as a subroutine with NDArray (converted image) as $vgg->($image_nd_array); . See dog breed classification example on this page http://blogs.perl.org/users/sergey_kolychev/2018/07/machine-learning-in-perl-kyuubi-goes-to-a-modelzoo-during-the-starry-night.html – Hito Yume Feb 10 '20 at 23:52
  • If you are willing to post some code with custom training data, perhaps similarly to the code I posted, it would be much helpful. I have read and tried the links you posted many times, they are tied to some use-cases which are not what I have. I tried to adapt to my use-case but ... So post some skeletal code if you can please. – bliako Feb 11 '20 at 10:28
  • thank you @Hito Yume. I did not mean that you must get my code working but I would appreciate it if you already have some code working and you can share it with me. Please don't spend any time on this unless you already have some personal examples you want to share. I am sorry if I sounded a bit pressing. It's just that I tried already the links. So, thank you for your input, if you have already some working code then please share, otherwise don't spend more time on it if you have something better to do... thanks Hito Yume – bliako Feb 17 '20 at 18:56