MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/sequence_segmenter_ex.cpp:169–235  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

167// ----------------------------------------------------------------------------------------
168
169int main()
170{
171 // Finally we make it into the main program body. So the first thing we do is get our
172 // training data.
173 std::vector<std::vector<std::string> > samples;
174 std::vector<std::vector<std::pair<unsigned long, unsigned long> > > segments;
175 make_training_examples(samples, segments);
176
177
178 // Next we use the structural_sequence_segmentation_trainer to learn our segmentation
179 // model based on just the samples and segments. But first we setup some of its
180 // parameters.
181 structural_sequence_segmentation_trainer<feature_extractor> trainer;
182 // This is the common SVM C parameter. Larger values encourage the trainer to attempt
183 // to fit the data exactly but might overfit. In general, you determine this parameter
184 // by cross-validation.
185 trainer.set_c(10);
186 // This trainer can use multiple CPU cores to speed up the training. So set this to
187 // the number of available CPU cores.
188 trainer.set_num_threads(4);
189
190
191 // Learn to do sequence segmentation from the dataset
192 sequence_segmenter<feature_extractor> segmenter = trainer.train(samples, segments);
193
194
195 // Let's print out all the segments our segmenter detects.
196 for (unsigned long i = 0; i < samples.size(); ++i)
197 {
198 // get all the detected segments in samples[i]
199 std::vector<std::pair<unsigned long,unsigned long> > seg = segmenter(samples[i]);
200 // Print each of them
201 for (unsigned long j = 0; j < seg.size(); ++j)
202 {
203 print_segment(samples[i], seg[j]);
204 }
205 }
206
207
208 // Now let's test it on a new sentence and see what it detects.
209 std::vector<std::string> sentence(split("There once was a man from Nantucket whose name rhymed with Bob Bucket"));
210 std::vector<std::pair<unsigned long,unsigned long> > seg = segmenter(sentence);
211 for (unsigned long j = 0; j < seg.size(); ++j)
212 {
213 print_segment(sentence, seg[j]);
214 }
215
216
217
218 // We can also test the accuracy of the segmenter on a dataset. This statement simply
219 // tests on the training data. In this case we will see that it predicts everything
220 // correctly.
221 cout << "\nprecision, recall, f1-score: " << test_sequence_segmenter(segmenter, samples, segments);
222 // Similarly, we can do 5-fold cross-validation and print the results. Just as before,
223 // we see everything is predicted correctly.
224 cout << "precision, recall, f1-score: " << cross_validate_sequence_segmenter(trainer, samples, segments, 5);
225
226

Callers

nothing calls this directly

Calls 11

test_sequence_segmenterFunction · 0.85
make_training_examplesFunction · 0.70
print_segmentFunction · 0.70
serializeFunction · 0.70
deserializeFunction · 0.70
splitFunction · 0.50
set_cMethod · 0.45
set_num_threadsMethod · 0.45
trainMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected