function [boxDeltaTarget, objectnessTarget, classTarget, maskTarget, boxErrorScaleTarget] = generateTargets(YPredCellGathered, groundTruth, inputImageSize, anchorBoxes, anchorBoxMask, penaltyThreshold)
% 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;
for numPred = 1:size(YPredCellGathered,1)
% Select anchor boxes based on anchor box mask indices.
anchors = anchorBoxes(anchorBoxMask{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-anchorBoxes(:,2)/(2*inputImageSize(2)),0-anchorBoxes(:,1)/(2*inputImageSize(1)),anchorBoxes(:,2)/inputImageSize(2),anchorBoxes(:,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
function iou = getMaxIOUPredictedWithGroundTruth(predx,predy,predw,predh,truth)
% getMaxIOUPredictedWithGroundTruth computes the maximum intersection over
% union scores for every pair of predictions and ground-truth boxes.
[h,w,c,n] = size(predx);
iou = zeros([h w c n],'like',predx);
% For each batch prepare the predictions and ground-truth.
for batchSize = 1:n
truthBatch = truth(:,1:4,1,batchSize);
truthBatch = truthBatch(all(truthBatch,2),:);
predxb = predx(:,:,:,batchSize);
predyb = predy(:,:,:,batchSize);
predwb = predw(:,:,:,batchSize);
predhb = predh(:,:,:,batchSize);
predb = [predxb(:),predyb(:),predwb(:),predhb(:)];
% Convert from center xy coordinate to topleft xy coordinate.
predb = [predb(:,1)-predb(:,3)./2, predb(:,2)-predb(:,4)./2, predb(:,3), predb(:,4)];
% Compute and extract the maximum IOU of predictions with ground-truth.
overlap = bboxOverlapRatio(predb,truthBatch);
maxOverlap = max(overlap,[],2);
iou(:,:,:,batchSize) = reshape(maxOverlap,h,w,c);
end
end
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
YOLOV3车辆检测.rar (78个子文件)
YOLOV3车辆检测
gTruth.mat 18KB
generateTargets.m 6KB
generateYOLOv3Detections.m 2KB
测试集
Test (11).jpg 7KB
Test (10).jpg 7KB
Test (9).jpg 6KB
Test (18).jpg 7KB
Test (16).jpg 7KB
Test (1).jpg 7KB
Test (12).jpg 5KB
Test (13).jpg 7KB
Test (3).jpg 6KB
Test (14).jpg 7KB
Test (20).jpg 7KB
Test (17).jpg 5KB
Test (15).jpg 6KB
Test (2).jpg 5KB
Test (6).jpg 5KB
Test (4).jpg 5KB
Test (19).jpg 4KB
Test (7).jpg 6KB
Test (8).jpg 6KB
Test (5).jpg 5KB
训练集
image (3).jpg 6KB
image (26).jpg 7KB
image (31).jpg 5KB
image (29).jpg 9KB
image (13).jpg 5KB
image (43).jpg 7KB
image (22).jpg 7KB
image (1).jpg 5KB
image (28).jpg 8KB
image (11).jpg 6KB
image (25).jpg 6KB
image (18).jpg 6KB
image (9).jpg 6KB
image (27).jpg 7KB
image (21).jpg 7KB
image (23).jpg 8KB
image (20).jpg 6KB
image (10).jpg 7KB
image (19).jpg 7KB
image (8).jpg 6KB
image (44).jpg 7KB
src=http _pic95.nipic.com_file_20160413_11820783_124840098881_2.jpg&refer=http _pic95.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg.jpg 565KB
image (2).jpg 6KB
image (6).jpg 5KB
image (49).jpg 7KB
image (50).jpg 7KB
image (24).jpg 7KB
image (32).jpg 5KB
image (48).jpg 7KB
image (37).jpg 6KB
image (7).jpg 6KB
image (36).jpg 6KB
image (16).jpg 7KB
image (38).jpg 7KB
image (40).jpg 7KB
image (46).jpg 7KB
image (4).jpg 5KB
image (12).jpg 5KB
image (42).jpg 8KB
image (5).jpg 6KB
image (30).jpg 6KB
image (17).jpg 6KB
image (35).jpg 7KB
image (15).jpg 7KB
image (14).jpg 7KB
image (41).jpg 8KB
image (34).jpg 6KB
image (45).jpg 7KB
image (39).jpg 7KB
image (33).jpg 6KB
image (47).jpg 7KB
upsampleLayer.m 890B
ATrainYOLOV3.mlx 1.25MB
BTestmodel.mlx 145KB
YOLOv3.mat 8.9MB
共 78 条
- 1
爱钓鱼的小点点
- 粉丝: 30
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于串口通信的光通信上位机,包括运动控制和通信协议
- 串口与以太网文件传送协议(或自定义控制协议)
- Qt开发windows系统安装教程与代码实例.txt
- QT6实现的附带文件传输协议的串口终端
- 一个串口通讯类和调用Demo 通过设置串口、设置串口自定义协议,可方便对串口发送数据与接收数据
- 华为OD模拟题及参考答案.仅供学习和模拟考试使用
- stm32f103c8t6基于modbus协议和使用串口读取温湿度
- 英雄联盟LOL金克斯4K电脑壁纸
- Microbrain道闸产品上位机,以MahApps库为基础搭建界面,集成了串口(UART)、CAN、WIFI通信,十六进制协议
- Android串口通讯, 支持发送数据回调, 支持并发处理, 自定义协议, CRC校验, 自动粘包, 自动去除冗余的干扰数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3