Added README.md
[mtp.git] / mtp_example.cc
1
2 /*
3  *  mtp is the ``Multi Tracked Paths'', an implementation of the
4  *  k-shortest paths algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
11  *  mtp is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  mtp is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18  *  License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include <iostream>
26 #include <fstream>
27 #include <stdlib.h>
28 #include <string.h>
29
30 using namespace std;
31
32 #include "mtp_tracker.h"
33
34 //////////////////////////////////////////////////////////////////////
35
36 scalar_t noisy_score(scalar_t true_score, scalar_t erroneous_score,
37                      scalar_t score_noise, scalar_t flip_noise) {
38   if(drand48() < flip_noise) {
39     return erroneous_score + score_noise * (2.0f * scalar_t(drand48()) - 1.0f);
40   } else {
41     return true_score + score_noise * (2.0f * scalar_t(drand48()) - 1.0f);
42   }
43 }
44
45 void create_light_test(MTPTracker *tracker) {
46   int nb_locations = 7;
47   int nb_time_steps = 8;
48   int motion_amplitude = 1;
49
50   tracker->allocate(nb_time_steps, nb_locations);
51
52   // We define the spatial structure by stating what are the possible
53   // motions of targets, and what are the entrances and the exits.
54
55   // Here our example is a 1D space with motions from any location to
56   // any location less than motion_amplitude away, entrance at
57   // location 0 (or in the first time frame, i.e. targets can already
58   // be in the scene when the sequence starts) and exit at location
59   // nb_locations-1 (or from the last time frame, i.e. targets can
60   // still be present when the sequence finishes)
61
62   for(int l = 0; l < nb_locations; l++) {
63     for(int m = 0; m < nb_locations; m++) {
64       tracker->allowed_motions[l][m] = abs(l - m) <= motion_amplitude;
65     }
66   }
67
68   for(int t = 0; t < nb_time_steps; t++) {
69     for(int l = 0; l < nb_locations; l++) {
70       // We allow targets to enter in the first time frame, or in
71       // location 0
72       tracker->entrances[t][l] = (t == 0 || l == 0);
73       // We allow targets to leave from the last time frame, or from
74       // location nb_locations-1
75       tracker->exits[t][l] = (t == nb_time_steps - 1 || l == nb_locations-1);
76     }
77   }
78
79   // We construct the graph corresponding to this structure
80
81   tracker->build_graph();
82
83   // Then, we specify for every location and time step what is the
84   // detection score there.
85
86   scalar_t flip_noise = 0.05f;
87   scalar_t score_noise = 0.0f;
88
89   // We first put a background noise, with negative scores at every
90   // location.
91
92   for(int t = 0; t < nb_time_steps; t++) {
93     for(int l = 0; l < nb_locations; l++) {
94       tracker->detection_scores[t][l] = noisy_score(-1.0, 1.0, score_noise, flip_noise);
95     }
96   }
97
98   // Then we add two targets with a typical tracking local minimum
99   //
100   // * Target A moves from location 0 to the middle, stays there for a
101   //   while, and comes back. It is strongly detected on the first
102   //   half
103   //
104   // * Target B moves from location nb_locations-1 to the middle, stay
105   //   there for a while, and comes back. It is strongly detected on
106   //   the second half
107
108   int la, lb; // Target locations
109   scalar_t sa, sb; // Target detection scores
110   for(int t = 0; t < nb_time_steps; t++) {
111     if(t < nb_time_steps/2) {
112       la = t;
113       lb = nb_locations - 1 - t;
114       sa = noisy_score(10.0, -1.0, score_noise, flip_noise);
115       sb = noisy_score( 1.0, -1.0, score_noise, flip_noise);
116     } else {
117       la = nb_time_steps - 1 - t;
118       lb = t - nb_time_steps + nb_locations;
119       sa = noisy_score( 1.0, -1.0, score_noise, flip_noise);
120       sb = noisy_score(10.0, -1.0, score_noise, flip_noise);
121     }
122
123     if(la > nb_locations/2 - 1) la = nb_locations/2 - 1;
124     if(lb < nb_locations/2 + 1) lb = nb_locations/2 + 1;
125
126     tracker->detection_scores[t][la] = sa;
127     tracker->detection_scores[t][lb] = sb;
128   }
129 }
130
131 void create_heavy_test(MTPTracker *tracker) {
132   int nb_locations = 100;
133   int nb_time_steps = 1000;
134
135   tracker->allocate(nb_time_steps, nb_locations);
136
137   for(int l = 0; l < nb_locations; l++) {
138     for(int m = 0; m < nb_locations; m++) {
139       tracker->allowed_motions[l][m] = (drand48() < 0.1);
140     }
141   }
142
143   for(int t = 0; t < nb_time_steps; t++) {
144     for(int l = 0; l < nb_locations; l++) {
145       tracker->entrances[t][l] = drand48() < 0.01;
146       tracker->exits[t][l] = drand48() < 0.01;
147     }
148   }
149
150   tracker->build_graph();
151
152   for(int t = 0; t < nb_time_steps; t++) {
153     for(int l = 0; l < nb_locations; l++) {
154       tracker->detection_scores[t][l] = scalar_t(drand48()) - 0.95f;
155     }
156   }
157 }
158
159 int main(int argc, char **argv) {
160   int stress_test;
161
162   if(argc == 1) {
163     stress_test = 0;
164   } else if(argc == 2 && strcmp(argv[1], "stress") == 0) {
165     stress_test = 1;
166   } else {
167     cerr << "mtp_examples [stress]" << endl;
168     exit(EXIT_FAILURE);
169   }
170
171   MTPTracker *tracker = new MTPTracker();
172
173   if(stress_test) {
174     create_heavy_test(tracker);
175   } else {
176     create_light_test(tracker);
177   }
178
179   {
180     // Write down the tracker setting, so that we can use it as an
181     // example for the mtp command line
182     ofstream out_tracker("tracker.dat");
183     tracker->write(&out_tracker);
184   }
185
186   // Performs the tracking per se
187
188   tracker->track();
189
190   // Prints the detected trajectories
191
192   for(int t = 0; t < tracker->nb_trajectories(); t++) {
193     cout << "Trajectory "
194          << t
195          << " starting at " << tracker->trajectory_entrance_time(t)
196          << ", duration " << tracker->trajectory_duration(t)
197          << ", score " << tracker->trajectory_score(t)
198          << ", through locations";
199     for(int u = 0; u < tracker->trajectory_duration(t); u++) {
200       cout << " " << tracker->trajectory_location(t, u);
201     }
202     cout << endl;
203   }
204
205   delete tracker;
206
207   exit(EXIT_SUCCESS);
208 }