【发布时间】:2020-02-10 09:42:47
【问题描述】:
我正在尝试使用 MXNet (AI::MXNet) 的 Perl API 从 ModelZoo (AI::MXNet::Gluon::ModelZoo::Vision) 获取标准模型并对其进行训练。
以下代码没有抱怨,但它不适合()。 fit() 立即返回。我的数据集是一个 RecordIO 格式的图像文件(使用 im2rec 构建),我很确定它是有效的。
我已经从https://codehex.hateblo.jp/entry/2017/09/12/160149 修改了代码,原作者在这里象征性地创建了模型(而不是来自 ModelZoo)。
原则上,这是从 ModelZoo 训练模型的正确方法吗?
第二个问题是从一组图像创建数据加载器的最佳做法是什么。我在做什么(使用mx->io->ImageRecordIter)可以吗?
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
);
【问题讨论】: