Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
c23ef609
Commit
c23ef609
authored
Nov 04, 2011
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more tests
parent
f18acdf8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
16 deletions
+76
-16
sequence_labeler.cpp
dlib/test/sequence_labeler.cpp
+76
-16
No files found.
dlib/test/sequence_labeler.cpp
View file @
c23ef609
...
@@ -62,8 +62,58 @@ namespace
...
@@ -62,8 +62,58 @@ namespace
}
}
};
};
bool
called_rejct_labeling
=
false
;
class
feature_extractor2
{
public
:
typedef
unsigned
long
sample_type
;
unsigned
long
num_features
()
const
{
return
num_label_states
*
num_label_states
+
num_label_states
*
num_sample_states
;
}
unsigned
long
order
()
const
{
return
1
;
}
unsigned
long
num_labels
()
const
{
return
num_label_states
;
}
template
<
typename
EXP
>
bool
reject_labeling
(
const
std
::
vector
<
sample_type
>&
x
,
const
matrix_exp
<
EXP
>&
y
,
unsigned
long
position
)
const
{
called_rejct_labeling
=
true
;
return
false
;
}
template
<
typename
feature_setter
,
typename
EXP
>
void
get_features
(
feature_setter
&
set_feature
,
const
std
::
vector
<
sample_type
>&
x
,
const
matrix_exp
<
EXP
>&
y
,
unsigned
long
position
)
const
{
if
(
y
.
size
()
>
1
)
set_feature
(
y
(
1
)
*
num_label_states
+
y
(
0
));
set_feature
(
num_label_states
*
num_label_states
+
y
(
0
)
*
num_sample_states
+
x
[
position
]);
}
};
void
serialize
(
const
feature_extractor
&
,
std
::
ostream
&
)
{}
void
serialize
(
const
feature_extractor
&
,
std
::
ostream
&
)
{}
void
deserialize
(
feature_extractor
&
,
std
::
istream
&
)
{}
void
deserialize
(
feature_extractor
&
,
std
::
istream
&
)
{}
void
serialize
(
const
feature_extractor2
&
,
std
::
ostream
&
)
{}
void
deserialize
(
feature_extractor2
&
,
std
::
istream
&
)
{}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
...
@@ -174,18 +224,8 @@ namespace
...
@@ -174,18 +224,8 @@ namespace
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
typename
fe_type
>
class
sequence_labeler_tester
:
public
tester
void
do_test
()
{
public
:
sequence_labeler_tester
(
)
:
tester
(
"test_sequence_labeler"
,
"Runs tests on the sequence labeling code."
)
{}
void
perform_test
(
)
{
{
matrix
<
double
>
transition_probabilities
(
num_label_states
,
num_label_states
);
matrix
<
double
>
transition_probabilities
(
num_label_states
,
num_label_states
);
transition_probabilities
=
0.05
,
0.90
,
0.05
,
transition_probabilities
=
0.05
,
0.90
,
0.05
,
...
@@ -216,7 +256,7 @@ namespace
...
@@ -216,7 +256,7 @@ namespace
}
}
print_spinner
();
print_spinner
();
structural_sequence_labeling_trainer
<
feature_extractor
>
trainer
;
structural_sequence_labeling_trainer
<
fe_type
>
trainer
;
trainer
.
set_c
(
4
);
trainer
.
set_c
(
4
);
DLIB_TEST
(
trainer
.
get_c
()
==
4
);
DLIB_TEST
(
trainer
.
get_c
()
==
4
);
trainer
.
set_num_threads
(
4
);
trainer
.
set_num_threads
(
4
);
...
@@ -225,7 +265,7 @@ namespace
...
@@ -225,7 +265,7 @@ namespace
// Learn to do sequence labeling from the dataset
// Learn to do sequence labeling from the dataset
sequence_labeler
<
feature_extractor
>
labeler
=
trainer
.
train
(
samples
,
labels
);
sequence_labeler
<
fe_type
>
labeler
=
trainer
.
train
(
samples
,
labels
);
std
::
vector
<
unsigned
long
>
predicted_labels
=
labeler
(
samples
[
0
]);
std
::
vector
<
unsigned
long
>
predicted_labels
=
labeler
(
samples
[
0
]);
dlog
<<
LINFO
<<
"true hidden states: "
<<
trans
(
vector_to_matrix
(
labels
[
0
]));
dlog
<<
LINFO
<<
"true hidden states: "
<<
trans
(
vector_to_matrix
(
labels
[
0
]));
...
@@ -252,7 +292,7 @@ namespace
...
@@ -252,7 +292,7 @@ namespace
matrix
<
double
,
0
,
1
>
true_hmm_model_weights
=
log
(
join_cols
(
reshape_to_column_vector
(
transition_probabilities
),
matrix
<
double
,
0
,
1
>
true_hmm_model_weights
=
log
(
join_cols
(
reshape_to_column_vector
(
transition_probabilities
),
reshape_to_column_vector
(
emission_probabilities
)));
reshape_to_column_vector
(
emission_probabilities
)));
sequence_labeler
<
feature_extractor
>
labeler_true
(
true_hmm_model_weights
);
sequence_labeler
<
fe_type
>
labeler_true
(
true_hmm_model_weights
);
confusion_matrix
=
test_sequence_labeler
(
labeler_true
,
samples
,
labels
);
confusion_matrix
=
test_sequence_labeler
(
labeler_true
,
samples
,
labels
);
dlog
<<
LINFO
<<
"True HMM model: "
;
dlog
<<
LINFO
<<
"True HMM model: "
;
...
@@ -272,7 +312,7 @@ namespace
...
@@ -272,7 +312,7 @@ namespace
ostringstream
sout
;
ostringstream
sout
;
serialize
(
labeler
,
sout
);
serialize
(
labeler
,
sout
);
sequence_labeler
<
feature_extractor
>
labeler2
;
sequence_labeler
<
fe_type
>
labeler2
;
// recall from disk
// recall from disk
istringstream
sin
(
sout
.
str
());
istringstream
sin
(
sout
.
str
());
deserialize
(
labeler2
,
sin
);
deserialize
(
labeler2
,
sin
);
...
@@ -283,6 +323,26 @@ namespace
...
@@ -283,6 +323,26 @@ namespace
dlog
<<
LINFO
<<
"label accuracy: "
<<
accuracy
;
dlog
<<
LINFO
<<
"label accuracy: "
<<
accuracy
;
DLIB_TEST
(
std
::
abs
(
accuracy
-
0.882
)
<
0.01
);
DLIB_TEST
(
std
::
abs
(
accuracy
-
0.882
)
<
0.01
);
}
}
// ----------------------------------------------------------------------------------------
class
sequence_labeler_tester
:
public
tester
{
public
:
sequence_labeler_tester
(
)
:
tester
(
"test_sequence_labeler"
,
"Runs tests on the sequence labeling code."
)
{}
void
perform_test
(
)
{
do_test
<
feature_extractor
>
();
DLIB_TEST
(
called_rejct_labeling
==
false
);
do_test
<
feature_extractor2
>
();
DLIB_TEST
(
called_rejct_labeling
==
true
);
}
}
a
;
}
a
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment