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
ModelZoo
ResNet50_tensorflow
Commits
dabcbc97
Commit
dabcbc97
authored
Oct 01, 2021
by
Vishnu Banna
Browse files
darknet loss functionality update
parent
1654fbea
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
25 deletions
+40
-25
official/vision/beta/projects/yolo/losses/yolo_loss.py
official/vision/beta/projects/yolo/losses/yolo_loss.py
+40
-25
No files found.
official/vision/beta/projects/yolo/losses/yolo_loss.py
View file @
dabcbc97
...
@@ -349,16 +349,16 @@ class DarknetLoss(YoloLossBase):
...
@@ -349,16 +349,16 @@ class DarknetLoss(YoloLossBase):
tf
.
cast
(
true_class
,
tf
.
int32
),
tf
.
cast
(
true_class
,
tf
.
int32
),
depth
=
tf
.
shape
(
pred_class
)[
-
1
],
depth
=
tf
.
shape
(
pred_class
)[
-
1
],
dtype
=
pred_class
.
dtype
)
dtype
=
pred_class
.
dtype
)
true_class
es
=
tf
.
stop_gradient
(
loss_utils
.
apply_mask
(
ind_mask
,
true_class
))
true_class
=
tf
.
stop_gradient
(
loss_utils
.
apply_mask
(
ind_mask
,
true_class
))
# Reorganize the one hot class list as a grid.
# Reorganize the one hot class list as a grid.
true_class
=
loss_utils
.
build_grid
(
true_class
_grid
=
loss_utils
.
build_grid
(
inds
,
true_class
es
,
pred_class
,
ind_mask
,
update
=
False
)
inds
,
true_class
,
pred_class
,
ind_mask
,
update
=
False
)
true_class
=
tf
.
stop_gradient
(
true_class
)
true_class
_grid
=
tf
.
stop_gradient
(
true_class
_grid
)
# Use the class mask to find the number of objects located in
# Use the class mask to find the number of objects located in
# each predicted grid cell/pixel.
# each predicted grid cell/pixel.
counts
=
true_class
counts
=
true_class
_grid
counts
=
tf
.
reduce_sum
(
counts
,
axis
=-
1
,
keepdims
=
True
)
counts
=
tf
.
reduce_sum
(
counts
,
axis
=-
1
,
keepdims
=
True
)
reps
=
tf
.
gather_nd
(
counts
,
inds
,
batch_dims
=
1
)
reps
=
tf
.
gather_nd
(
counts
,
inds
,
batch_dims
=
1
)
reps
=
tf
.
squeeze
(
reps
,
axis
=-
1
)
reps
=
tf
.
squeeze
(
reps
,
axis
=-
1
)
...
@@ -372,26 +372,41 @@ class DarknetLoss(YoloLossBase):
...
@@ -372,26 +372,41 @@ class DarknetLoss(YoloLossBase):
box_loss
=
math_ops
.
divide_no_nan
(
box_loss
,
reps
)
box_loss
=
math_ops
.
divide_no_nan
(
box_loss
,
reps
)
box_loss
=
tf
.
cast
(
tf
.
reduce_sum
(
box_loss
,
axis
=
1
),
dtype
=
y_pred
.
dtype
)
box_loss
=
tf
.
cast
(
tf
.
reduce_sum
(
box_loss
,
axis
=
1
),
dtype
=
y_pred
.
dtype
)
# Compute the sigmoid binary cross entropy for the class maps.
if
self
.
_update_on_repeat
:
class_loss
=
tf
.
reduce_mean
(
# Compute the sigmoid binary cross entropy for the class maps.
loss_utils
.
sigmoid_bce
(
class_loss
=
tf
.
reduce_mean
(
tf
.
expand_dims
(
true_class
,
axis
=-
1
),
loss_utils
.
sigmoid_bce
(
tf
.
expand_dims
(
pred_class
,
axis
=-
1
),
self
.
_label_smoothing
),
tf
.
expand_dims
(
true_class_grid
,
axis
=-
1
),
axis
=-
1
)
tf
.
expand_dims
(
pred_class
,
axis
=-
1
),
self
.
_label_smoothing
),
axis
=-
1
)
# Apply normalization to the class losses.
if
self
.
_cls_normalizer
<
1.0
:
# Apply normalization to the class losses.
# Build a mask based on the true class locations.
if
self
.
_cls_normalizer
<
1.0
:
cls_norm_mask
=
true_class
# Build a mask based on the true class locations.
# Apply the classes weight to class indexes were one_hot is one.
cls_norm_mask
=
true_class_grid
class_loss
*=
((
1
-
cls_norm_mask
)
+
cls_norm_mask
*
self
.
_cls_normalizer
)
# Apply the classes weight to class indexes were one_hot is one.
class_loss
*=
(
# Mask to the class loss and compute the sum over all the objects.
(
1
-
cls_norm_mask
)
+
cls_norm_mask
*
self
.
_cls_normalizer
)
class_loss
=
tf
.
reduce_sum
(
class_loss
,
axis
=-
1
)
class_loss
=
loss_utils
.
apply_mask
(
grid_mask
,
class_loss
)
# Mask to the class loss and compute the sum over all the objects.
class_loss
=
math_ops
.
rm_nan_inf
(
class_loss
,
val
=
0.0
)
class_loss
=
tf
.
reduce_sum
(
class_loss
,
axis
=-
1
)
class_loss
=
tf
.
cast
(
class_loss
=
loss_utils
.
apply_mask
(
grid_mask
,
class_loss
)
tf
.
reduce_sum
(
class_loss
,
axis
=
(
1
,
2
,
3
)),
dtype
=
y_pred
.
dtype
)
class_loss
=
math_ops
.
rm_nan_inf
(
class_loss
,
val
=
0.0
)
class_loss
=
tf
.
cast
(
tf
.
reduce_sum
(
class_loss
,
axis
=
(
1
,
2
,
3
)),
dtype
=
y_pred
.
dtype
)
else
:
pred_class
=
loss_utils
.
apply_mask
(
ind_mask
,
tf
.
gather_nd
(
pred_class
,
inds
,
batch_dims
=
1
))
class_loss
=
tf
.
keras
.
losses
.
binary_crossentropy
(
tf
.
expand_dims
(
true_class
,
axis
=
-
1
),
tf
.
expand_dims
(
pred_class
,
axis
=
-
1
),
label_smoothing
=
self
.
_label_smoothing
,
from_logits
=
True
)
class_loss
=
loss_utils
.
apply_mask
(
ind_mask
,
class_loss
)
class_loss
=
math_ops
.
divide_no_nan
(
class_loss
,
tf
.
expand_dims
(
reps
,
axis
=
-
1
))
class_loss
=
tf
.
cast
(
tf
.
reduce_sum
(
class_loss
,
axis
=
(
1
,
2
)),
dtype
=
y_pred
.
dtype
)
# Compute the sigmoid binary cross entropy for the confidence maps.
# Compute the sigmoid binary cross entropy for the confidence maps.
bce
=
tf
.
reduce_mean
(
bce
=
tf
.
reduce_mean
(
...
...
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