cannot find the funcion "generateTargets"

12 views (last 30 days)
Hello there,
I am trying to replicate the yolov3 example here. However, I cannot find a utility function, generateTargets.
which is called in modelGradients, another utility function. Please help.
I am using matlab r2021a
Thanks
% Generate target for predictions from the ground truth data.
[boxTarget, objectnessTarget, classTarget, objectMaskTarget, boxErrorScale] = generateTargets(gatheredPredictions,...
YTrain, inputImageSize, detector.AnchorBoxes, penaltyThreshold);
  4 Comments
MirPooya Salehi Moharer
MirPooya Salehi Moharer on 23 May 2021
Thank you. I managed to fix it. Thanks for your time.
Kind regads.
Weiwei Luo
Weiwei Luo on 15 Nov 2021
I have exactly the same issue. I am using R2020B and R2021A. Open Example does not help. I still cannot find that function. I see it is called in line 216. It was mentined before line 198. But cannot find that function. Would you possible copy it here?

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 23 Apr 2021
Edited: Cris LaPierre on 23 Apr 2021
You can open this example in MATLAB using the following code
openExample('deeplearning_shared/ObjectDetectionUsingYOLOV3DeepLearningExample')
On my computer, this corresponds to the following location:
C:\Users\userName\Documents\MATLAB\Examples\R2021a\deeplearning_shared\ObjectDetectionUsingYOLOV3DeepLearningExample
When I navigate to that folder, generateTargets is there. Note that this folder is not added to your path automatically. You will need to either make that folder your current folder, or add it to your path before the example can be run. When you use the command above to open the example, it automatically changes the current folder.
  1 Comment
xinyi shen
xinyi shen on 23 Apr 2021
Thanks!
I indeed did not find this example file indeed. But in another app, I found the functions.
Social Distancing Monitoring System

Sign in to comment.

More Answers (1)

Weiwei Luo
Weiwei Luo on 23 Nov 2021
I wrote the the MATLAB Support team and get the code.
function [boxDeltaTarget, objectnessTarget, classTarget, maskTarget, boxErrorScaleTarget] = generateTargets(YPredCellGathered, groundTruth,...
inputImageSize, anchorBoxes, penaltyThreshold)
% originally at the back of the mlx file as utility function
% generateTargets creates target array for every prediction element
% x, y, width, height, confidence scores and class probabilities.
boxDeltaTarget = cell(size(YPredCellGathered,1),4);
objectnessTarget = cell(size(YPredCellGathered,1),1);
classTarget = cell(size(YPredCellGathered,1),1);
maskTarget = cell(size(YPredCellGathered,1),3);
boxErrorScaleTarget = cell(size(YPredCellGathered,1),1);
% Normalize the ground truth boxes w.r.t image input size.
gtScale = [inputImageSize(2) inputImageSize(1) inputImageSize(2) inputImageSize(1)];
groundTruth(:,1:4,:,:) = groundTruth(:,1:4,:,:)./gtScale;
anchorBoxesSet = cell2mat(anchorBoxes);
maskIdx = 1:size(anchorBoxesSet,1);
cellsz = cellfun(@size,anchorBoxes,'uni',false);
convMask = cellfun(@(v)v(1),cellsz);
anchorBoxMask = mat2cell(maskIdx,1,convMask)';
for numPred = 1:size(YPredCellGathered,1)
% Select anchor boxes based on anchor box mask indices.
anchors = anchorBoxes{numPred, :};
bx = YPredCellGathered{numPred,2};
by = YPredCellGathered{numPred,3};
bw = YPredCellGathered{numPred,4};
bh = YPredCellGathered{numPred,5};
predClasses = YPredCellGathered{numPred,6};
gridSize = size(bx);
if numel(gridSize)== 3
gridSize(4) = 1;
end
numClasses = size(predClasses,3)./size(anchors,1);
% Initialize the required variables.
mask = single(zeros(size(bx)));
confMask = single(ones(size(bx)));
classMask = single(zeros(size(predClasses)));
tx = single(zeros(size(bx)));
ty = single(zeros(size(by)));
tw = single(zeros(size(bw)));
th = single(zeros(size(bh)));
tconf = single(zeros(size(bx)));
tclass = single(zeros(size(predClasses)));
boxErrorScale = single(ones(size(bx)));
% Get the IOU of predictions with groundtruth.
iou = getMaxIOUPredictedWithGroundTruth(bx,by,bw,bh,groundTruth);
% Donot penalize the predictions which has iou greater than penalty
% threshold.
confMask(iou > penaltyThreshold) = 0;
for batch = 1:gridSize(4)
truthBatch = groundTruth(:,1:5,:,batch);
truthBatch = truthBatch(all(truthBatch,2),:);
% Get boxes with center as 0.
gtPred = [0-truthBatch(:,3)/2,0-truthBatch(:,4)/2,truthBatch(:,3),truthBatch(:,4)];
anchorPrior = [0-anchorBoxesSet(:,2)/(2*inputImageSize(2)),0-anchorBoxesSet(:,1)/(2*inputImageSize(1)),anchorBoxesSet(:,2)/inputImageSize(2),anchorBoxesSet(:,1)/inputImageSize(1)];
% Get the iou of best matching anchor box.
overLap = bboxOverlapRatio(gtPred,anchorPrior);
[~,bestAnchorIdx] = max(overLap,[],2);
% Select gt that are within the mask.
index = ismember(bestAnchorIdx,anchorBoxMask{numPred});
truthBatch = truthBatch(index,:);
bestAnchorIdx = bestAnchorIdx(index,:);
bestAnchorIdx = bestAnchorIdx - anchorBoxMask{numPred}(1,1) + 1;
if ~isempty(truthBatch)
% Convert top left position of ground-truth to centre coordinates.
truthBatch = [truthBatch(:,1)+truthBatch(:,3)./2,truthBatch(:,2)+truthBatch(:,4)./2,truthBatch(:,3),truthBatch(:,4),truthBatch(:,5)];
errorScale = 2 - truthBatch(:,3).*truthBatch(:,4);
truthBatch = [truthBatch(:,1)*gridSize(2),truthBatch(:,2)*gridSize(1),truthBatch(:,3)*inputImageSize(2),truthBatch(:,4)*inputImageSize(1),truthBatch(:,5)];
for t = 1:size(truthBatch,1)
% Get the position of ground-truth box in the grid.
colIdx = ceil(truthBatch(t,1));
colIdx(colIdx<1) = 1;
colIdx(colIdx>gridSize(2)) = gridSize(2);
rowIdx = ceil(truthBatch(t,2));
rowIdx(rowIdx<1) = 1;
rowIdx(rowIdx>gridSize(1)) = gridSize(1);
pos = [rowIdx,colIdx];
anchorIdx = bestAnchorIdx(t,1);
mask(pos(1,1),pos(1,2),anchorIdx,batch) = 1;
confMask(pos(1,1),pos(1,2),anchorIdx,batch) = 1;
% Calculate the shift in ground-truth boxes.
tShiftX = truthBatch(t,1)-pos(1,2)+1;
tShiftY = truthBatch(t,2)-pos(1,1)+1;
tShiftW = log(truthBatch(t,3)/anchors(anchorIdx,2));
tShiftH = log(truthBatch(t,4)/anchors(anchorIdx,1));
% Update the target box.
tx(pos(1,1),pos(1,2),anchorIdx,batch) = tShiftX;
ty(pos(1,1),pos(1,2),anchorIdx,batch) = tShiftY;
tw(pos(1,1),pos(1,2),anchorIdx,batch) = tShiftW;
th(pos(1,1),pos(1,2),anchorIdx,batch) = tShiftH;
boxErrorScale(pos(1,1),pos(1,2),anchorIdx,batch) = errorScale(t);
tconf(rowIdx,colIdx,anchorIdx,batch) = 1;
classIdx = (numClasses*(anchorIdx-1))+truthBatch(t,5);
tclass(rowIdx,colIdx,classIdx,batch) = 1;
classMask(rowIdx,colIdx,(numClasses*(anchorIdx-1))+(1:numClasses),batch) = 1;
end
end
end
boxDeltaTarget(numPred,:) = [{tx} {ty} {tw} {th}];
objectnessTarget{numPred,1} = tconf;
classTarget{numPred,1} = tclass;
maskTarget(numPred,:) = [{mask} {confMask} {classMask}];
boxErrorScaleTarget{numPred,:} = boxErrorScale;
end
end
  1 Comment
arindam mondal
arindam mondal on 22 Feb 2022
I cannot find the function 'getMaxIOUPredictedWithGroundTruth'. please help

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!