Index in position 2 exceeds array bounds (must not exceed 68160).

4 views (last 30 days)
Hello all, I trying to run a program, but I get this error. Could you please help me? THANKS!!!!
This is the code
demoSBD.m
% This scripts train and test CHM on Stanford Background Dataset.
% Change the trainpath, labelpath to point to the SBD.
% I have a bash script, trainparallel.sh, to run CHM for 8 classes in
% paralle using trainparalle.m. If you have enough resources you can use that script instead of
% this one.
% I also have testparallel.sh and testparallel.m scripts to run testing in parallel.
trainpath = '/home/smhf/Documents/matlab/code/iccv09Data/images/'; %path to the training Images
labelpath = '/home/smhf/Documents/matlab/code/iccv09Data/labels/'; %path to the training Labels
addpath(genpath('FilterStuff')); %add path to functions required for feature extraction.
% parameters
Nstage = 2;
Nlevel = 5;
load CV1.mat % This is the list of training and testing for the first cross validation.
% Only for preallocation purpose
filestr = dir([trainpath '*.jpg']);
filesla = dir([labelpath '*.txt']);
filestr = filestr(rTrain);
filesla = filesla(rTrain);
ntr = length(filestr);
for class = 0:7
savingpath = ['./temp/' num2str(class) '/']; %path to save results and temporary files
mkdir(savingpath);
PixN = zeros(Nlevel+1,1);
Nfeat = zeros(Nlevel+1,1);
for l = 0:Nlevel
for i = 1:ntr
temp = load([labelpath filesla(i).name]);
[M,~,indSu] = extractBinary(temp,class,l-1);
PixN(l+1) = PixN(l+1) + numel(indSu);
end
end
for l = 0:Nlevel
tempfeat = Filterbank(imread([trainpath filestr(1).name]),l);
Nfeat(l+1) = size(tempfeat,1);
end
tempim = imread([trainpath filestr(1).name]);
tempfeat = ConstructNeighborhoodsS(tempim(:,:,1));
Nfeatcontext = size(tempfeat,1);
param.ntr = ntr;
param.PixN = PixN;
param.Nfeat = Nfeat;
param.Nfeatcontext = Nfeatcontext;
param.Nlevel = Nlevel;
param.class = class;
param.rTrain = rTrain;
% Train the CHM
for s = 1:Nstage
param.stage = s;
for l = 0:Nlevel
param.level = l;
param.PixN = PixN(l+1);
param.Nfeat = Nfeat(l+1);
model = trainCHM(trainpath,labelpath,savingpath,param);
if s==Nstage, break; end
end
end
end
% Test CHM
for class = 0:7
savingpath = ['./temp/' num2str(class) '/'];
param.Nstage = Nstage;
testpath = './training/'; %path to the test Images
fileste = dir([testpath '*.jpg']);
fileste = fileste(rTest);
str = [savingpath 'output_testImages/'];
mkdir(str);
outputs{length(fileste)} = [];
parfor i = 1:length(fileste)
img = imread([testpath fileste(i).name]);
clabels = testCHM(img,savingpath,param);
outputs{i} = clabels;
end
for i = 1:length(fileste)
clabels = outputs{i};
save([str 'slice' num2str(i)],'clabels','-v7.3');
end
end
trainCHM.m
function model = trainCHM(trainpath,labelpath,savingpath,param)
ntr = param.ntr;
PixN = param.PixN;
Nfeat = param.Nfeat;
level = param.level;
stage = param.stage;
Nfeatcontext = param.Nfeatcontext;
Nlevel = param.Nlevel;
class = param.class;
rTrain = param.rTrain;
filestr = dir([trainpath '*.jpg']);
filesla = dir([labelpath '*.txt']);
filestr = filestr(rTrain);
filesla = filesla(rTrain);
if ~exist([savingpath 'MODEL_level' num2str(level) '_stage' num2str(stage) '.mat'],'file')
% Feature Extraction
fprintf('Extracting features ... stage %d level %d \n',stage,level);
if stage==1 || level~=0
X = zeros(Nfeat + level*Nfeatcontext,PixN);
DD = zeros(1,PixN);
COUNTER = 0;
for i = 1:ntr
img = imread([trainpath filestr(i).name]);
img = MyDownSample(img,level-1);
fv = Filterbank(img,level);
fvcontext = zeros(level*Nfeatcontext,size(img,1)*size(img,2));
for j = 0:level-1
Contextpath = [savingpath 'output_level' num2str(j) '_stage' num2str(stage) '/'];
temp = load([Contextpath 'slice' num2str(i)]);
if j>=1
temp = MyDownSample(temp.clabels,level-j);
elseif j==0
temp = MyDownSample(temp.clabels,level-j-1);
end
fvcontext(j*Nfeatcontext+1:(j+1)*Nfeatcontext,:) = ConstructNeighborhoodsS(temp);
end
temp = load([labelpath filesla(i).name]);
[clabels,~,indSu] = extractBinary(temp,class,level-1);
D = reshape(clabels,1,numel(clabels));
D = D(indSu);
fv = fv(:,indSu);
fvcontext = fvcontext(:,indSu);
X(:,COUNTER+1:COUNTER+size(fv,2)) = [fv;fvcontext];
DD(1,COUNTER+1:COUNTER+size(fv,2)) = D;
COUNTER = COUNTER + size(fv,2);
end
else
X = zeros(Nfeat + (Nlevel+1)*Nfeatcontext,PixN);
DD = zeros(1,PixN);
COUNTER = 0;
for i = 1:ntr
img = imread([trainpath filestr(i).name]);
img = MyDownSample(img,level-1);
fv = Filterbank(img,level);
fvcontext = zeros((Nlevel+1)*Nfeatcontext,size(img,1)*size(img,2));
for j = 0:Nlevel
Contextpath = [savingpath 'output_level' num2str(j) '_stage' num2str(stage-1) '/'];
temp = load([Contextpath 'slice' num2str(i)]);
temp = MyUpSample(temp.clabels,j-1);
temp = temp(1:size(img,1),1:size(img,2),:);
fvcontext(j*Nfeatcontext+1:(j+1)*Nfeatcontext,:) = ConstructNeighborhoodsS(temp);
end
temp = load([labelpath filesla(i).name]);
[clabels,~,indSu] = extractBinary(temp,class,level-1);
D = reshape(clabels,1,numel(clabels));
D = D(indSu);
fv = fv(:,indSu);
fvcontext = fvcontext(:,indSu);
X(:,COUNTER+1:COUNTER+size(fv,2)) = [fv;fvcontext];
DD(1,COUNTER+1:COUNTER+size(fv,2)) = D;
COUNTER = COUNTER + size(fv,2);
end
end
% Subsampling
% if PixN > 2000000 % incrrease this for real problems
ind = [];
maxs =min([histc(DD(:),0),histc(DD(:),1),1500000]);
for i=0:max(DD)
if histc(DD,i)>maxs
a = find(DD==i);
rpm = randperm(numel(a));
index = a(rpm(1:maxs));
else
index = find(DD==i);
end
ind = [ind index];
end
X = X(:,ind);
DD = DD(:,ind);
% end
% Learning the Classifier
fprintf('start learning LDNN ... stage %d level %d \n',stage,level);
opt.level = level;
opt.stage = stage;
tic;model = LearnAndOrNetMEX( X,DD,opt);Time = toc;
save([savingpath 'MODEL_level' num2str(level) '_stage' num2str(stage)] , 'model','Time', '-v7.3');
else
fprintf('Model_level %d _stage %d exists...proceed to generate output \n',level,stage);
load([savingpath 'MODEL_level' num2str(level) '_stage' num2str(stage) '.mat']);
end
% Write the outputs
fprintf('Generating outputs ... stage %d level %d \n',stage,level);
str = [savingpath 'output_level' num2str(level) '_stage' num2str(stage) '/'];
mkdir(str);
if ~exist([str 'slice' num2str(ntr) '.mat'],'file')
% matlabpool open 12
trOutput{ntr} = [];
if stage==1 || level~=0
parfor i = 1:ntr
img = imread([trainpath filestr(i).name]);
img = MyDownSample(img,level-1);
fv = Filterbank(img,level);
fvcontext = zeros(level*Nfeatcontext,size(img,1)*size(img,2));
for j = 0:level-1
Contextpath = [savingpath 'output_level' num2str(j) '_stage' num2str(stage) '/'];
temp = load([Contextpath 'slice' num2str(i)]);
if j>=1
temp = MyDownSample(temp.clabels,level-j);
elseif j==0
temp = MyDownSample(temp.clabels,level-j-1);
end
fvcontext(j*Nfeatcontext+1:(j+1)*Nfeatcontext,:) = ConstructNeighborhoodsS(temp);
end
X = [fv;fvcontext];
[~, Y_floats] = EvaluateAndOrNetMX(X,model);
clabels = reshape(Y_floats,[size(img,1) size(img,2)]);
trOutput{i} = clabels;
end
else
parfor i = 1:ntr
img = imread([trainpath filestr(i).name]);
img = MyDownSample(img,level-1);
fv = Filterbank(img,level);
fvcontext = zeros((Nlevel+1)*Nfeatcontext,size(img,1)*size(img,2));
for j = 0:Nlevel
Contextpath = [savingpath 'output_level' num2str(j) '_stage' num2str(stage-1) '/'];
temp = load([Contextpath 'slice' num2str(i)]);
temp = MyUpSample(temp.clabels,j-1);
temp = temp(1:size(img,1),1:size(img,2),:);
fvcontext(j*Nfeatcontext+1:(j+1)*Nfeatcontext,:) = ConstructNeighborhoodsS(temp);
end
X = [fv;fvcontext];
[~, Y_floats] = EvaluateAndOrNetMX(X,model);
clabels = reshape(Y_floats,[size(img,1) size(img,2)]);
trOutput{i} = clabels;
end
end
for i = 1:ntr
clabels = trOutput{i};
save([str 'slice' num2str(i)],'clabels','-v7.3');
end
% matlabpool close;
else
fprintf('Outputs of stage %d level %d already generated...skip \n',stage,level);
end

Answers (1)

Fangjun Jiang
Fangjun Jiang on 11 Jul 2020
Here is a simple way to duplicate your error. Hope you will get it. When you have a matrix of say 2x3 elements, you can't access the (2,4) element because it does not exist.
>> clear all
>> fv=ones(2,68160);
>> indSu=68161;
>> fv=fv(:,indSu)
Index in position 2 exceeds array bounds (must not exceed 68160).

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!