how to efficiently track large numbers of objects -凯发k8网页登录
this example shows how to use the trackergnn
to track large numbers of targets. similar techniques can be applied to the trackerjpda
and trackertomht
as well.
introduction
in many applications, trackers are required to track hundreds or thousands of objects. increasing the number of tracks maintained by a tracker is a challenge, caused by the computational complexity of the algorithm at the core of every tracker. in particular, two common stages in the tracker update step are not easily scalable: calculating the assignment cost and performing the assignment. assignment cost calculation is common to trackergnn
, trackerjpda
, and trackertomht
, and the techniques shown in this example can be applied when using any of these trackers. the way each tracker performs the assignment is unique to each tracker, and may require tailored solutions to improve the tracker performance, which are beyond the scope of this example.
scenario
for the purposes of this example, you define a scenario that contains 900 platforms, organized in a 15-by-15 grid, with each grid cell containing 4 platforms. the purpose of the grid cells is to demonstrate the benefit of coarse cost calculation, explained later in the example.
the following code arranges the 900 objects in the grid cell and creates the visualization. on the left, the entire scenario is shown. on the right, the visualization zooms in on 4 grid cells. note each cell contains 4 platforms.
[platforms,tp,zoomedtp] = createplatforms;
use the default assignment cost calculation
this section shows the results of tracking the platforms defined above using a trackergnn
with default assignmentthreshold
. the assignmentthreshold
property contains two values: [c1 c2]
, where c1
is the threshold used for the assignment and c2
is a threshold for coarse calculation explained in the next section.
when the tracker is updated with a new set of detections, it calculates the cost of assigning every detection to every track. the accurate cost calculation must take into account the measurement and uncertainty of each detection as well as the expected measurement and expected uncertainty from each track, as depicted below.
by default, c2
is set to inf, which requires that the costs of all combinations of track and detection are calculated. this leads to a more accurate assignment, but is more computationally intensive. you should start with the default setting to make sure the tracker assigns detections to tracks in the best way, and then consider lowering the value of c2
to reduce the time required for calculating the assignment cost.
during assignment cost calculation, elements of the cost matrix whose values are higher than c1
are replaced with inf. doing so helps the assignment algorithm to ignore impossible assignments.
define a tracker that can track up to 1000 tracks. the tracker uses the default constant velocity extended kalman filter, and its state is defined as [x;vx;y;vy;z;vz]
, which is used by the positionselector
below to get the position components.
tracker = trackergnn('maxnumtracks',1000, 'assignmentthreshold', [30 inf]); positionselector = [1 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 0 0 0];
on the first call to step, the tracker instantiates all tracks. to isolate the time required to instantiate the tracks from the processing time required for the first step, you can call setup
and reset
before stepping the tracker. see the supporting function runtracker
at the end of this example for more details.
[trksummary,trusummary,info] = runtracker(platforms,tracker,positionselector,tp,zoomedtp);
tracker set up time: 8.3108 step 1 time: 3.7554 step 2 time: 15.3029 step 3 time: 14.1099 step 4 time: 14.3506 step 5 time: 14.3963
all steps from now are without detections. step 6 time: 0.53103 step 7 time: 0.52582 step 8 time: 0.50639 step 9 time: 0.50909 step 10 time: 0.16034 scenario done. all tracks are now deleted.
you analyze the tracking results by examining the track assignment metrics values. for perfect tracking the total number of tracks should be equal to the number of platforms and there should be no false, swapped, or divergent tracks. similarly, there should be no missing truth or breaks in the truth summary.
assignmentmetricssummary(trksummary,trusummary)
track assignment metrics summary: totalnumtracks numfalsetracks maxswapcount maxdivergencecount maxdivergencelength ______________ ______________ ____________ __________________ ___________________ 900 0 0 0 0 truth assignment metrics summary: totalnumtruths nummissingtruths maxestablishmentlength maxbreakcount ______________ ________________ ______________________ _____________ 900 0 1 0
use coarse assignment cost calculation
in the previous section, you saw that the tracker is able to track all the platforms, but every update step takes a long time. most of the time was spent on calculating the assignment cost matrix.
examining the cost matrix, you can see that vast majority of its elements are, in fact, inf.
cm = info.costmatrix; disp("cost matrix has " numel(cm) " elements."); disp("but the number of finite values is " numel(cm(isfinite(cm))) newline)
cost matrix has 810000 elements. but the number of finite values is 2700
the above result shows that the cost calculation spends too much time on calculating the assignment cost of all the combinations of track and detection. however, most of these combinations are too far to be assigned, as the actual measurement is too far from the track expected measurement based on the sensor characteristics. to avoid the waste in calculating all the costs, you can use coarse cost calculation.
coarse calculation is done to verify which combinations of track and detection may require an accurate normalized distance calculation. only combinations whose coarse assignment cost is lower than c2
are calculated accurately. the coarse cost calculation is depicted in the image below. a detection is represented by its measurement and measurement noise . two tracks are predicted to the time of the detection and projected to the measurement space, depicted by the points and . note that the track uncertainty is not projected to the measurement space, which allows us to vectorize the coarse calculation. this is a rough estimate, because only the uncertainty around the detection is taken into account. in the depicted example, the first track falls outside of the coarse calculation gate while the second track falls inside it. thus, accurate cost calculation is only done for the combination of this detection and the second track.
to use coarse cost calculation, release the tracker and modify its assignmentthreshold
to a value of [30 200]. then, rerun the tracker.
release(tracker) tracker.assignmentthreshold = [30 200]; [trksummary,trusummary] = runtracker(platforms,tracker,positionselector,tp,zoomedtp);
tracker set up time: 6.5846 step 1 time: 3.5863 step 2 time: 3.4095 step 3 time: 2.9347 step 4 time: 2.8555 step 5 time: 2.9397
all steps from now are without detections. step 6 time: 0.51446 step 7 time: 0.52277 step 8 time: 0.54865 step 9 time: 0.50941 step 10 time: 0.19085 scenario done. all tracks are now deleted.
you observe that the steps 3-5 now require significantly less time to complete. step 2 is also faster than it used to be, but still slower than steps 3-5.
to understand why step 2 is slower, consider the track states after the first tracker update. the states contain position information, but the velocity is still zero. when the tracker calculates the assignment cost, it predicts the track states to the detection times, but since the tracks have zero velocity, they remain in the same position. this results in large distances between the detection measurements and the expected measurements from the predicted track states. these relatively large assignment costs make it harder for the assignment algorithm to find the best assignment, which causes step 2 to take more time than steps 3-5.
it's important to verify that the track assignment with coarse cost calculation remains the same as without it. if the track assignment metrics are not the same, you must increase the size of the coarse calculation gate. the following shows that the tracking is still perfect as it was in the previous section, but each processing step took less time.
assignmentmetricssummary(trksummary,trusummary)
track assignment metrics summary: totalnumtracks numfalsetracks maxswapcount maxdivergencecount maxdivergencelength ______________ ______________ ____________ __________________ ___________________ 900 0 0 0 0 truth assignment metrics summary: totalnumtruths nummissingtruths maxestablishmentlength maxbreakcount ______________ ________________ ______________________ _____________ 900 0 1 0
use an external cost calculation
another way to control the time it takes to calculate the cost assignment is by using your own assignment cost calculation instead of the default the tracker uses.
an external cost calculation can take into account attributes that are not part of the track state and expected measurement. it can also use different distance metrics, for example euclidean norm instead of normalized distance. the choice of which cost calculation to apply depends on the specifics of the problem, the measurement space, and how you define the state and measurement.
to use an external cost calculation, you release the tracker and set its hascostmatrixinput
property to true. you must pass your own cost matrix as an additional input with each update to the tracker. see the supporting function runtracker
for more details.
release(tracker); tracker.hascostmatrixinput = true; [trksummary,trusummary] = runtracker(platforms,tracker,positionselector,tp,zoomedtp); assignmentmetricssummary(trksummary,trusummary)
tracker set up time: 6.559 step 1 time: 3.4394 step 2 time: 1.7852 step 3 time: 1.474 step 4 time: 1.5312 step 5 time: 1.5152
all steps from now are without detections. step 6 time: 0.60809 step 7 time: 0.61374 step 8 time: 0.616 step 9 time: 0.63798 step 10 time: 0.22762 scenario done. all tracks are now deleted. track assignment metrics summary: totalnumtracks numfalsetracks maxswapcount maxdivergencecount maxdivergencelength ______________ ______________ ____________ __________________ ___________________ 900 0 0 0 0 truth assignment metrics summary: totalnumtruths nummissingtruths maxestablishmentlength maxbreakcount ______________ ________________ ______________________ _____________ 900 0 1 0
as expected, the processing time is even lower when using an external cost calculation function.
change the gnn assignment algorithm
another option to try is using a different gnn assignment algorithm that may be more efficient in finding the assignment by modifying the assignment
property of the tracker.
release(tracker)
tracker.assignment = 'jonker-volgenant';
tracker.hascostmatrixinput = true;
runtracker(platforms,tracker,positionselector,tp,zoomedtp);
tracker set up time: 6.494 step 1 time: 3.5346 step 2 time: 1.894 step 3 time: 3.1192 step 4 time: 3.1212 step 5 time: 3.1458
all steps from now are without detections. step 6 time: 0.61109 step 7 time: 0.62456 step 8 time: 0.61849 step 9 time: 0.60604 step 10 time: 0.22303 scenario done. all tracks are now deleted.
the jonker-volgenant algorithm performs the assignment in the second step faster relative to the default munkres algorithm.
monte-carlo simulation
if you want to run multiple scenarios without modifying the tracker settings, there is no need to call the release
method. instead, just call the reset
method to clear previous track information from the tracker. this way, you save the time required to instantiate all the tracks. note the "tracker set up time" below relative to previous runs.
reset(tracker) runtracker(platforms,tracker,positionselector,tp,zoomedtp);
tracker set up time: 0.097531 step 1 time: 3.4684 step 2 time: 1.6592 step 3 time: 3.1429 step 4 time: 3.1274 step 5 time: 3.0994
all steps from now are without detections. step 6 time: 0.63232 step 7 time: 0.61857 step 8 time: 0.61433 step 9 time: 0.60698 step 10 time: 0.25301 scenario done. all tracks are now deleted.
summary
this example showed how to track large numbers of objects. when tracking many objects, the tracker spends a large fraction of the processing time on computing the cost assignment for each combination of track and detection. you saw how to use the cost calculation threshold to improve the time spent on calculating the assignment cost. in addition, the example showed how to use an external cost calculation, which may be designed to be more computationally efficient for the particular tracking problem you have.
you can reduce the cost assignment threshold or use an external cost calculation to improve the speed of the trackerjpda
and the trackertomht
as well.
supporting functions
createplatforms
this function creates the platforms in a 20x20 grid with 2x2 platforms per grid cell.
function [platforms,tp,zoomedtp] = createplatforms % this is a helper function to run the tracker and display the results. it % may be removed in the future. nh = 15; % number of horizontal grid cells nv = 15; % number of vertical grid cells nsq = 2; % 2x2 platforms in a grid cell npl = nh*nv*nsq^2; % overall number of platforms xgv = sort(-50 repmat(100 * (1:nh), [1 nsq])); ygv = sort(-50 repmat(100 * (1:nv), [1 nsq])); [x,y] = meshgrid(xgv,ygv); npts = nsq/2; xshift = 10*((-npts 1):npts) -5; yshift = xshift; xadd = repmat(xshift, [1 nh]); yadd = repmat(yshift, [1 nv]); [xx, yy] = meshgrid(xadd,yadd); x = x xx; y = y yy; pos = [x(:),y(:),zeros(numel(x),1)]; % the following creates an array of struct for the platforms, which are % used later for track assignment metrics. vel = [3 1 0]; % platform velocity platforms = repmat(struct('platformid', 1, 'position', [0 0 0], 'velocity', vel),npl,1); for i = 1:npl platforms(i).platformid = i; platforms(i).position(:) = pos(i,:); end % visualization f = figure('position',[1 1 1425 700]); movegui center; h1 = uipanel(f,'fontsize',12,'position',[.01 .01 .48 .98],"title","scene view"); a1 = axes(h1,'position',[0.05 0.05 0.9 0.9]); tp = theaterplot('parent', a1, 'xlimits',[0 nh*100], 'ylimits',[0 nv*100]); set(a1,'xtick',0:100:nh*100) set(a1,'ytick',0:100:nv*100) grid on pp = trackplotter(tp,'tag','truth','marker','^','markeredgecolor','k','markersize',4,'historydepth',10); plottrack(pp,reshape([platforms.position],3,[])'); trackplotter(tp, 'tag','tracks','markeredgecolor','b','markersize',6,'historydepth',10); c = get(a1.parent,'children'); for i = 1:numel(c) if isa(c(i),'matlab.graphics.illustration.legend') set(c(i),'visible','off') end end h2 = uipanel(f,'fontsize',12,'position',[.51 .01 .48 .98],'title','zoomed view'); a2 = axes(h2,'position',[0.05 0.05 0.9 0.9]); zoomedtp = theaterplot('parent', a2, 'xlimits',[400 500], 'ylimits',[400 500]); set(a2,'xtick',400:100:500) set(a2,'ytick',400:100:500) grid on zoomedpp = trackplotter(zoomedtp,'displayname','truth','marker','^','markeredgecolor','k','markersize',6,'historydepth',10); plottrack(zoomedpp,reshape([platforms.position],3,[])'); trackplotter(zoomedtp, 'displayname','tracks','markeredgecolor','b','markersize',8,'historydepth',10,'connecthistory','on','fontsize',1); end
runtracker
this function runs the tracker, updates the plotters, and gathers track assignment metrics.
function [trksummary,trusummary,info] = runtracker(platforms,tracker,positionselector,tp,zoomedtp) % this is a helper function to run the tracker and display the results. it % may be removed in the future. pp = findplotter(tp,'tag','truth'); trp = findplotter(tp, 'tag','tracks'); zoomedpp = findplotter(zoomedtp,'displayname','truth'); zoomedtrp = findplotter(zoomedtp, 'displayname','tracks'); % to save time, pre-allocate all the detections and assign them on the fly. npl = numel(platforms); det = objectdetection(0,[0;0;0]); dets = repmat({det},[npl,1]); % define a track assignment metrics object. tam = trackassignmentmetrics; % bring the visualization back. set(tp.parent.parent.parent,'visible','on') hasexternalcostfunction = tracker.hascostmatrixinput; % measure the time it takes to set the tracker up. tic if ~islocked(tracker) if hasexternalcostfunction setup(tracker,dets,0,0); else setup(tracker,dets,0); end end reset(tracker) disp("tracker set up time: " toc); % run 5 steps with detections for all the platforms. for t = 1:5 for i = 1:npl dets{i}.time = t; dets{i}.measurement = platforms(i).position(:); end tic if hasexternalcostfunction if islocked(tracker) % use predicttrackstotime to get all the predicted tracks. alltracks = predicttrackstotime(tracker,'all',t); else alltracks = []; end costmatrix = predictedeuclidean(alltracks,dets,positionselector); [tracks,~,~,info] = tracker(dets,t,costmatrix); else [tracks,~,~,info] = tracker(dets,t); end trpos = gettrackpositions(tracks, positionselector); trids = string([tracks.trackid]'); disp("step " t " time: " toc) % update the plot. plottrack(pp,reshape([platforms.position],3,[])'); plottrack(trp,trpos); plottrack(zoomedpp,reshape([platforms.position],3,[])'); plottrack(zoomedtrp,trpos,trids); drawnow % update the track assignment metrics object. if nargout [trksummary, trusummary] = tam(tracks,platforms); end % update the platform positions. for i = 1:npl platforms(i).position = platforms(i).position platforms(i).velocity; end end snapnow % run steps with no detections until the tracker deletes all the tracks. disp("all steps from now are without detections.") while ~isempty(tracks) t = t 1; tic if hasexternalcostfunction alltracks = predicttrackstotime(tracker,'all',t); costmatrix = predictedeuclidean(alltracks,{},positionselector); tracks = tracker({},t,costmatrix); else tracks = tracker({},t); end disp("step " t " time: " toc) % update the position of the tracks to plot. trpos = gettrackpositions(tracks,positionselector); trids = string([tracks.trackid]'); % update the plot. plottrack(pp,reshape([platforms.position],3,[])'); plottrack(trp,trpos); plottrack(zoomedpp,reshape([platforms.position],3,[])'); plottrack(zoomedtrp,trpos,trids); drawnow % update the platform positions. for i = 1:npl platforms(i).position = platforms(i).position platforms(i).velocity; end end disp("scenario done. all tracks are now deleted." newline) cleardata(pp) cleardata(trp) cleardata(zoomedpp) cleardata(zoomedtrp) set(tp.parent.parent.parent,'visible','off') % prevent excessive snapshots drawnow end
predictedeuclidean
the function calculates the euclidean distance between measured positions from detections and predicted positions from tracks.
function eucliddist = predictedeuclidean(tracks,detections,positionselector) % this is a helper function to run the tracker and display the results. it % may be removed in the future. if isempty(tracks) || isempty(detections) eucliddist = zeros(numel(tracks),numel(detections)); return end predictedstates = [tracks.state]; predictedpositions = positionselector * predictedstates; dets = [detections{:}]; measuredpositions = [dets.measurement]; eucliddist = zeros(numel(tracks),numel(detections)); for i = 1:numel(detections) diffs = bsxfun(@minus, predictedpositions',measuredpositions(:,i)'); eucliddist(:,i) = sqrt(sum((diffs .* diffs),2)); end end
assignmentmetricssummary
the function displays the key assignment metrics in a table form.
function assignmentmetricssummary(trksummary,trusummary) trksummary = rmfield(trksummary, {'totalswapcount','totaldivergencecount',... 'totaldivergencelength','maxredundancycount','totalredundancycount',... 'maxredundancylength','totalredundancylength'}); trusummary = rmfield(trusummary, {'totalestablishmentlength','totalbreakcount',... 'maxbreaklength','totalbreaklength'}); trktable = struct2table(trksummary); trutable = struct2table(trusummary); disp("track assignment metrics summary:") disp(trktable) disp("truth assignment metrics summary:") disp(trutable) end