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
bcf6bd9a
Commit
bcf6bd9a
authored
Feb 11, 2017
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaned up loss_metric_ code a little
parent
34cce6da
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
14 deletions
+66
-14
loss.h
dlib/dnn/loss.h
+43
-12
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
typedef
unsigned
long
training_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
<
typename
SUB_TYPE
,
typename
label_iterator
...
...
@@ -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
<
typename
const_label_iterator
,
...
...
@@ -927,8 +939,6 @@ namespace dlib
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
());
grad_mul
.
copy_size
(
temp
);
...
...
@@ -1043,31 +1053,52 @@ namespace dlib
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
;
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
);
}
}
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
;
}
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
:
float
margin
=
0
.
04
;
float
dist_thresh
=
0
.
6
;
// These variables are only here to avoid being reallocated over and over in
// compute_loss_value_and_gradient()
mutable
resizable_tensor
temp
,
grad_mul
;
...
...
dlib/dnn/loss_abstract.h
View file @
bcf6bd9a
...
...
@@ -559,6 +559,27 @@ namespace dlib
typedef
unsigned
long
training_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
<
typename
SUB_TYPE
,
typename
label_iterator
...
...
@@ -581,14 +602,14 @@ namespace dlib
given to this function, one for each sample in the input_tensor.
!*/
float
get_margin
()
const
{
return
0
.
1
;
}
float
get_margin
()
const
;
/*!
ensures
- returns the margin value used by the loss function. See the discussion
in WHAT THIS OBJECT REPRESENTS for details.
!*/
float
get_distance_threshold
()
const
{
return
0
.
75
;
}
float
get_distance_threshold
()
const
;
/*!
ensures
- 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