|
| 1 | +function [box, cls] = yolov8Transform(predictions,numClasses) |
| 2 | +% Transforms predictions from dlnetwork to |
| 3 | +% box [x_center y_center w h] and cls scores |
| 4 | + |
| 5 | +regMax = 16; |
| 6 | +outputsPerAnchor = numClasses + regMax*4; |
| 7 | +stride = [8,16,32]; |
| 8 | +batch = 1; |
| 9 | + |
| 10 | +% Extract feature maps from Matlab model |
| 11 | +% Apply this if dlarray output |
| 12 | +predictions = cellfun(@extractdata,predictions,'UniformOutput',false); |
| 13 | +predictions = cellfun(@gather,predictions,'UniformOutput',false); |
| 14 | + |
| 15 | +% Compute anchor grid and stride |
| 16 | +[anchorGrid, stride] = helper.make_anchors(predictions, stride); |
| 17 | +% anchor grid and stride transposed |
| 18 | +anchorGrid = anchorGrid'; |
| 19 | +stride = stride'; |
| 20 | + |
| 21 | +% Reshape predictions from model output |
| 22 | +pred = cellfun(@(p){permute(p,[2,1,3,4])}, predictions, 'UniformOutput',true); |
| 23 | +pred = cellfun(@(p){reshape(p,[],outputsPerAnchor, batch)}, pred, 'UniformOutput',true); |
| 24 | +pred = cellfun(@(p){permute(p,[2,1,3,4])}, pred, 'UniformOutput',true); |
| 25 | + |
| 26 | +% Concat all Predictions |
| 27 | +predCat = cat(2,pred{:}); |
| 28 | + |
| 29 | +% Split classes and boxes |
| 30 | +box = predCat(1:64,:,:); |
| 31 | +cls = predCat(65:end,:,:); |
| 32 | + |
| 33 | +box = helper.distributionFocalLoss(box); |
| 34 | +% Converting boxes to xywh format here |
| 35 | +box = helper.dist2bbox(box,anchorGrid); |
| 36 | +box = box .* stride; |
| 37 | +% Sigmoid of classes |
| 38 | +cls = sigmoid(dlarray(cls)); |
| 39 | +cls = extractdata(cls); |
| 40 | +end |
0 commit comments