Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
bcf6bd9a
Commit
bcf6bd9a
authored
Feb 11, 2017
by
Davis King
Browse files
Cleaned up loss_metric_ code a little
parent
34cce6da
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
14 deletions
+66
-14
dlib/dnn/loss.h
dlib/dnn/loss.h
+43
-12
dlib/dnn/loss_abstract.h
dlib/dnn/loss_abstract.h
+23
-2
No files found.
dlib/dnn/loss.h
View file @
bcf6bd9a
...
@@ -872,6 +872,17 @@ namespace dlib
...
@@ -872,6 +872,17 @@ namespace dlib
typedef
unsigned
long
training_label_type
;
typedef
unsigned
long
training_label_type
;
typedef
matrix
<
float
,
0
,
1
>
output_label_type
;
typedef
matrix
<
float
,
0
,
1
>
output_label_type
;
loss_metric_
()
=
default
;
loss_metric_
(
float
margin_
,
float
dist_thresh_
)
:
margin
(
margin_
),
dist_thresh
(
dist_thresh_
)
{
DLIB_CASSERT
(
margin_
>
0
);
DLIB_CASSERT
(
dist_thresh_
>
0
);
}
template
<
template
<
typename
SUB_TYPE
,
typename
SUB_TYPE
,
typename
label_iterator
typename
label_iterator
...
@@ -900,8 +911,9 @@ namespace dlib
...
@@ -900,8 +911,9 @@ namespace dlib
}
}
}
}
float
get_margin
()
const
{
return
0.1
;
}
float
get_distance_threshold
()
const
{
return
0.75
;
}
float
get_margin
()
const
{
return
margin
;
}
float
get_distance_threshold
()
const
{
return
dist_thresh
;
}
template
<
template
<
typename
const_label_iterator
,
typename
const_label_iterator
,
...
@@ -927,8 +939,6 @@ namespace dlib
...
@@ -927,8 +939,6 @@ namespace dlib
grad
.
nc
()
==
1
);
grad
.
nc
()
==
1
);
const
float
margin
=
get_margin
();
const
float
dist_thresh
=
get_distance_threshold
();
temp
.
set_size
(
output_tensor
.
num_samples
(),
output_tensor
.
num_samples
());
temp
.
set_size
(
output_tensor
.
num_samples
(),
output_tensor
.
num_samples
());
grad_mul
.
copy_size
(
temp
);
grad_mul
.
copy_size
(
temp
);
...
@@ -1043,31 +1053,52 @@ namespace dlib
...
@@ -1043,31 +1053,52 @@ namespace dlib
return
loss
;
return
loss
;
}
}
friend
void
serialize
(
const
loss_metric_
&
,
std
::
ostream
&
out
)
friend
void
serialize
(
const
loss_metric_
&
item
,
std
::
ostream
&
out
)
{
{
serialize
(
"loss_metric_"
,
out
);
serialize
(
"loss_metric_2"
,
out
);
serialize
(
item
.
margin
,
out
);
serialize
(
item
.
dist_thresh
,
out
);
}
}
friend
void
deserialize
(
loss_metric_
&
,
std
::
istream
&
in
)
friend
void
deserialize
(
loss_metric_
&
item
,
std
::
istream
&
in
)
{
{
std
::
string
version
;
std
::
string
version
;
deserialize
(
version
,
in
);
deserialize
(
version
,
in
);
if
(
version
!=
"loss_metric_"
)
if
(
version
==
"loss_metric_"
)
{
// These values used to be hard coded, so for this version of the metric
// learning loss we just use these values.
item
.
margin
=
0.1
;
item
.
dist_thresh
=
0.75
;
return
;
}
else
if
(
version
==
"loss_metric_2"
)
{
deserialize
(
item
.
margin
,
in
);
deserialize
(
item
.
dist_thresh
,
in
);
}
else
{
throw
serialization_error
(
"Unexpected version found while deserializing dlib::loss_metric_. Instead found "
+
version
);
throw
serialization_error
(
"Unexpected version found while deserializing dlib::loss_metric_. Instead found "
+
version
);
}
}
}
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
loss_metric_
&
)
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
loss_metric_
&
item
)
{
{
out
<<
"loss_metric"
;
out
<<
"loss_metric
(margin="
<<
item
.
margin
<<
", distance_threshold="
<<
item
.
dist_thresh
<<
")
"
;
return
out
;
return
out
;
}
}
friend
void
to_xml
(
const
loss_metric_
&
/*
item
*/
,
std
::
ostream
&
out
)
friend
void
to_xml
(
const
loss_metric_
&
item
,
std
::
ostream
&
out
)
{
{
out
<<
"<loss_metric/>"
;
out
<<
"<loss_metric
margin='"
<<
item
.
margin
<<
"' distance_threshold='"
<<
item
.
dist_thresh
<<
"'
/>"
;
}
}
private:
private:
float
margin
=
0.04
;
float
dist_thresh
=
0.6
;
// These variables are only here to avoid being reallocated over and over in
// These variables are only here to avoid being reallocated over and over in
// compute_loss_value_and_gradient()
// compute_loss_value_and_gradient()
mutable
resizable_tensor
temp
,
grad_mul
;
mutable
resizable_tensor
temp
,
grad_mul
;
...
...
dlib/dnn/loss_abstract.h
View file @
bcf6bd9a
...
@@ -559,6 +559,27 @@ namespace dlib
...
@@ -559,6 +559,27 @@ namespace dlib
typedef
unsigned
long
training_label_type
;
typedef
unsigned
long
training_label_type
;
typedef
matrix
<
float
,
0
,
1
>
output_label_type
;
typedef
matrix
<
float
,
0
,
1
>
output_label_type
;
loss_metric_
(
);
/*!
ensures
- #get_margin() == 0.04
- #get_distance_threshold() == 0.6
!*/
loss_metric_
(
float
margin
,
float
dist_thresh
);
/*!
requires
- margin > 0
- dist_thresh > 0
ensures
- #get_margin() == margin
- #get_distance_threshold() == dist_thresh
!*/
template
<
template
<
typename
SUB_TYPE
,
typename
SUB_TYPE
,
typename
label_iterator
typename
label_iterator
...
@@ -581,14 +602,14 @@ namespace dlib
...
@@ -581,14 +602,14 @@ namespace dlib
given to this function, one for each sample in the input_tensor.
given to this function, one for each sample in the input_tensor.
!*/
!*/
float
get_margin
()
const
{
return
0.1
;
}
float
get_margin
()
const
;
/*!
/*!
ensures
ensures
- returns the margin value used by the loss function. See the discussion
- returns the margin value used by the loss function. See the discussion
in WHAT THIS OBJECT REPRESENTS for details.
in WHAT THIS OBJECT REPRESENTS for details.
!*/
!*/
float
get_distance_threshold
()
const
{
return
0.75
;
}
float
get_distance_threshold
()
const
;
/*!
/*!
ensures
ensures
- returns the distance threshold value used by the loss function. See the discussion
- returns the distance threshold value used by the loss function. See the discussion
...
...
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