I figured out what was the problem. After my step-by-step debugging, I found problem in this line of code.
preprocessedTrainingData = transform(augmentedTrainingData, @(data)preprocess(yolov3Detector, data));
data = read(preprocessedTrainingData);
This line of code points to this function: vision.internal.cnn.LetterBoxImage
function [Inew,bboxnew] = LetterBoxImage(I,targetSize,varargin):
[Irow,Icol,Ichannels] = size(I);
bboxnew = [];
% Compute aspect Ratio.
arI = Irow./Icol;
% Preserve the maximum dimension based on the aspect ratio.
if arI<1
IcolFin = targetSize(1,2);
IrowFin = floor(IcolFin.*arI);
else
IrowFin = targetSize(1,1);
IcolFin = floor(IrowFin./arI);
end
% Resize the input image.
Itmp = imresize(I,[IrowFin,IcolFin]);
% Initialize Inew with gray values.
Inew = ones([targetSize,Ichannels],'like',I).*0.5;
% Compute the offset.
if arI<1
buff = targetSize(1,1)-IrowFin;
else
buff = targetSize(1,2)-IcolFin;
end
% Place the resized image on the canvas image.
if (buff==0)
Inew = Itmp;
else
buffVal = floor(buff/2);
if arI<1
Inew(buffVal:buffVal+IrowFin-1,:,:) = Itmp;
if ~isempty(varargin)
% Resize bounding boxes.
bboxnew = iScaleBboxes(varargin{1,1},size(Itmp),size(I));
bboxnew(:,2) = bboxnew(:,2)+buffVal;
end
else
Inew(:,buffVal:buffVal+IcolFin-1,:) = Itmp;
if ~isempty(varargin)
% Resize bounding boxes.
bboxnew = iScaleBboxes(varargin{1,1},size(Itmp),size(I));
bboxnew(:,1) = bboxnew(:,1)+buffVal;
end
end
end
end
I find the picture which gets error has the size with 612*612*3, and the target size is 227*227*3. So the variable arI goes to 1, and the varible buff goes to 0. The calculation of bboxnew has been skipped, so it goes to [].