【问题标题】:Index exceeds matrix dimensions encountered when training a model训练模型时遇到的索引超出矩阵维度
【发布时间】:2013-01-07 17:43:15
【问题描述】:

我在使用 PASCAL 开发套件和由 Felzenszwalb、D. McAllester、D. Ramaman 和他的团队开发并在 Matlab 中实现的判别训练可变形零件模型系统来训练模型时遇到问题。

目前,当我尝试使用 10 个正图像和 10 个负图像为“猫”训练一个 1 分量模型时出现此输出错误。

Error:

??? Index exceeds matrix dimensions.

Error in ==> pascal_train at 48
models{i} = train(cls, models{i}, spos{i}, neg(1:maxneg),
0, 0, 4, 3, ...

Error in ==> pascal at 28
model = pascal_train(cls, n, note);

这是 pascal_train 文件

function model = pascal_train(cls, n, note)

% model = pascal_train(cls, n, note)
% Train a model with 2*n components using the PASCAL dataset.
% note allows you to save a note with the trained model
% example: note = 'testing FRHOG (FRobnicated HOG)

% At every "checkpoint" in the training process we reset the 
% RNG's seed to a fixed value so that experimental results are 
% reproducible.
initrand();

if nargin < 3
  note = '';
end

globals; 
[pos, neg] = pascal_data(cls, true, VOCyear);
% split data by aspect ratio into n groups
spos = split(cls, pos, n);

cachesize = 24000;
maxneg = 200;

% train root filters using warped positives & random negatives
try
  load([cachedir cls '_lrsplit1']);
catch
  initrand();
  for i = 1:n
    % split data into two groups: left vs. right facing instances
    models{i} = initmodel(cls, spos{i}, note, 'N');
    inds = lrsplit(models{i}, spos{i}, i);
    models{i} = train(cls, models{i}, spos{i}(inds), neg, i, 1, 1, 1, ...
                      cachesize, true, 0.7, false, ['lrsplit1_' num2str(i)]);
  end
  save([cachedir cls '_lrsplit1'], 'models');
end

% train root left vs. right facing root filters using latent detections
% and hard negatives
try
  load([cachedir cls '_lrsplit2']);
catch
  initrand();
  for i = 1:n
    models{i} = lrmodel(models{i});
    models{i} = train(cls, models{i}, spos{i}, neg(1:maxneg), 0, 0, 4, 3, ...
                      cachesize, true, 0.7, false, ['lrsplit2_' num2str(i)]);
  end
  save([cachedir cls '_lrsplit2'], 'models');
end

% merge models and train using latent detections & hard negatives
try 
  load([cachedir cls '_mix']);
catch
  initrand();
  model = mergemodels(models);
 48:   model = train(cls, model, pos, neg(1:maxneg), 0, 0, 1, 5, ...
                cachesize, true, 0.7, false, 'mix');


save([cachedir cls '_mix'], 'model');
end

% add parts and update models using latent detections & hard negatives.
try 
  load([cachedir cls '_parts']);
catch
  initrand();
  for i = 1:2:2*n
    model = model_addparts(model, model.start, i, i, 8, [6 6]);
  end
  model = train(cls, model, pos, neg(1:maxneg), 0, 0, 8, 10, ...
                cachesize, true, 0.7, false, 'parts_1');
  model = train(cls, model, pos, neg, 0, 0, 1, 5, ...
                cachesize, true, 0.7, true, 'parts_2');
  save([cachedir cls '_parts'], 'model');
end

save([cachedir cls '_final'], 'model');

我已经在第 48 行突出显示了发生错误的代码行。

我很确定系统正在读取正片和负片图像以进行正确训练。我不知道这个错误发生在哪里,因为 matlab 没有准确指出哪个索引超出了矩阵维度。

我已尝试尽可能多地整理代码,如果我在某处做错了请指导我。

我应该从哪里开始寻找任何建议?

好的,我尝试使用 display 来检查 pascal_train 使用的变量; 显示(一); 显示(尺寸(型号)); 显示(大小(spos)); 显示(长度(负)); disp(maxneg);

所以返回的结果是;

 1

 1     1

 1     1

10

200

【问题讨论】:

  • 我从您的问题中删除了 Pascal 标签,因为这里使用它专门指 Pascal 编程语言,而不是在 matlab 中使用 Pascal_data。标签的定义将为您提供它们在这里用来表示的意图。在这种情况下,这是一种误导,因为您的问题与 Pascal 语言没有任何关系。 :-)

标签: matlab machine-learning computer-vision object-detection


【解决方案1】:

只需替换:

models{i} = train(cls, models{i}, spos{i}, neg(1:maxneg),

作为

models{i} = train(cls, models{i}, spos{i}, neg(1:min(length(neg),maxneg)),

这个脚本的其他地方有几个类似的句子,你应该全部修改。

原因是你的训练样本集很小,所以你列出的“neg”比maxneg(200)

【讨论】:

    【解决方案2】:

    我没有你的问题的答案,但这里有一个建议可以帮助你自己调试这个问题。

    在 Matlab 菜单中,转到 Debug-> Stop if Errors/Warnings ... 并选择“Always stop if error (dbstop if error)”。现在再次运行您的脚本,这一次当您收到错误时,matlab 将停在发生错误的行,就好像您在那里放置了一个断点一样。此时,您可以使用整个工作区,您可以检查所有变量和矩阵大小,以查看哪个变量给了您所看到的错误。

    【讨论】:

    • 嗯,我做到了,是的,我确实得到了整个工作区,包括变量......我检查了我的结构并确保索引没有超出范围......所以这很令人费解,但又一次我是 matlab 的新手。我很想发布图片,但我需要 10 个代表点 =(
    • 您可能已经这样做了,但以防万一。由于您的代码在 models{i} = train(cls, models{i}, spos{i}, neg(1:maxneg), 0, 0, 4, 3, ... 处失败,请检查 size(models)、size(spos) 并确保当行失败时它们大于或等于 i 的值。还要检查length(neg) &gt;= maxneg.
    • 如果 length(neg) >= maxneg,为什么还需要检查?这意味着我必须使用超过负图像的限制?在这种情况下,我使用了 10,但限制为 200。
    • 当你使用neg(1:maxneg) 时,你假设neg 是一个至少有maxneg 点的向量,如果长度低于这个值,那么这是非法的,Matlab 会发出错误信号。从您发布到问题的编辑中,很明显 neg 的长度只有 10。我相信这是您的错误的根源。
    • 你帮我解决了,我增加了系统读取的负图像的数量,所以错误消失了,但我现在有新的错误。另一方面,我咨询了我的教授关于系统的问题,他说系统要求的负图像不应该有最低数量……所以我很困惑? =/谢谢你的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    相关资源
    最近更新 更多