Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GenVQA
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vision
GenVQA
Commits
4d563c2d
Commit
4d563c2d
authored
8 years ago
by
tgupta6
Browse files
Options
Downloads
Patches
Plain Diff
margin based loss for object training
parent
5a257b12
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
losses.py
+25
-12
25 additions, 12 deletions
losses.py
with
25 additions
and
12 deletions
losses.py
+
25
−
12
View file @
4d563c2d
...
@@ -57,21 +57,33 @@ def margin_loss(y, y_pred, margin):
...
@@ -57,21 +57,33 @@ def margin_loss(y, y_pred, margin):
keep_dims
=
True
,
name
=
'
correct_score
'
)
keep_dims
=
True
,
name
=
'
correct_score
'
)
return
tf
.
reduce_mean
(
tf
.
maximum
(
0.0
,
y_pred
+
margin
-
correct_score
))
return
tf
.
reduce_mean
(
tf
.
maximum
(
0.0
,
y_pred
+
margin
-
correct_score
))
def
multilabel_margin_loss
(
y
,
y_pred
,
margin
):
y_list
=
tf
.
unpack
(
y
)
def
multilabel_margin_loss
(
y
,
y_pred
,
margin
,
num_samples
):
y_pred_list
=
tf
.
unpack
(
y
)
y_list
=
tf
.
unpack
(
y
,
num_samples
)
y_pred_list
=
tf
.
unpack
(
y_pred
,
num_samples
)
loss
=
0.0
loss
=
0.0
for
y_
,
y_pred_
in
zip
(
y_list
,
y_pred_list
):
for
i
in
xrange
(
num_samples
):
partition
=
tf
.
dynamic_partition
(
y_pred_
,
y_
,
2
)
y_
=
y_list
[
i
]
pos_labels_scores
=
tf
.
expand_dims
(
tf
.
transpose
(
partition
[
1
]),
1
)
y_pred_
=
y_pred_list
[
i
]
neg_labels_scores
=
partition
[
0
]
k
=
tf
.
reduce_sum
(
y_
)
margin_violation
=
tf
.
maximum
(
loss
+=
tf
.
cond
(
0
,
neg
-
labels_scores
+
margin
-
pos_labels_scores
)
k
>
0.5
,
loss
+=
tf
.
reduce_mean
(
margin_violatio
n
)
lambda
:
multilabel_margin_loss_inner
(
y_
,
y_pred_
,
margi
n
)
,
lambda
:
tf
.
constant
(
0.0
))
loss
/=
len
(
y_list
)
loss
/=
float
(
num_samples
)
return
loss
return
loss
def
multilabel_margin_loss_inner
(
y_
,
y_pred_
,
margin
):
partition_ids
=
tf
.
cast
(
y_
>
0.5
,
tf
.
int32
)
partition
=
tf
.
dynamic_partition
(
y_pred_
,
partition_ids
,
2
)
pos_labels_scores
=
tf
.
expand_dims
(
partition
[
1
],
1
)
neg_labels_scores
=
partition
[
0
]
margin_violation
=
tf
.
maximum
(
0.0
,
neg_labels_scores
+
margin
-
pos_labels_scores
)
return
tf
.
reduce_mean
(
margin_violation
)
def
mil_loss
(
scores
,
y
,
type
=
'
obj
'
,
epsilon
=
1e-5
):
def
mil_loss
(
scores
,
y
,
type
=
'
obj
'
,
epsilon
=
1e-5
):
if
type
==
'
obj
'
:
if
type
==
'
obj
'
:
log_prob
=
tf
.
nn
.
log_softmax
(
scores
)
log_prob
=
tf
.
nn
.
log_softmax
(
scores
)
...
@@ -82,6 +94,7 @@ def mil_loss(scores, y, type='obj', epsilon=1e-5):
...
@@ -82,6 +94,7 @@ def mil_loss(scores, y, type='obj', epsilon=1e-5):
loss
=
-
tf
.
reduce_sum
(
max_region_scores
)
/
tf
.
maximum
(
tf
.
reduce_sum
(
y
),
epsilon
)
loss
=
-
tf
.
reduce_sum
(
max_region_scores
)
/
tf
.
maximum
(
tf
.
reduce_sum
(
y
),
epsilon
)
return
loss
return
loss
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
scores
=
tf
.
constant
([[
0.2
,
0.3
,
0.7
],[
0.8
,
0.2
,
0.9
]])
scores
=
tf
.
constant
([[
0.2
,
0.3
,
0.7
],[
0.8
,
0.2
,
0.9
]])
labels
=
tf
.
constant
([[
1.0
,
0.0
,
0.0
],[
0.0
,
1.0
,
0.0
]])
labels
=
tf
.
constant
([[
1.0
,
0.0
,
0.0
],[
0.0
,
1.0
,
0.0
]])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment