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
fcd09603
"tools/vscode:/vscode.git/clone" did not exist on "9fe352d69679e45b3379efc07c0f92be032d8026"
Commit
fcd09603
authored
Oct 27, 2021
by
Abdullah Rashwan
Committed by
A. Unique TensorFlower
Oct 27, 2021
Browse files
Internal change
PiperOrigin-RevId: 406022769
parent
4b06b4b3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
32 deletions
+40
-32
official/vision/beta/evaluation/segmentation_metrics.py
official/vision/beta/evaluation/segmentation_metrics.py
+28
-29
official/vision/beta/tasks/semantic_segmentation.py
official/vision/beta/tasks/semantic_segmentation.py
+12
-3
No files found.
official/vision/beta/evaluation/segmentation_metrics.py
View file @
fcd09603
...
@@ -13,7 +13,6 @@
...
@@ -13,7 +13,6 @@
# limitations under the License.
# limitations under the License.
"""Metrics for segmentation."""
"""Metrics for segmentation."""
import
tensorflow
as
tf
import
tensorflow
as
tf
from
official.vision
import
keras_cv
from
official.vision
import
keras_cv
...
@@ -77,11 +76,11 @@ class MeanIoU(tf.keras.metrics.MeanIoU):
...
@@ -77,11 +76,11 @@ class MeanIoU(tf.keras.metrics.MeanIoU):
if
self
.
_rescale_predictions
:
if
self
.
_rescale_predictions
:
# This part can only run on cpu/gpu due to dynamic image resizing.
# This part can only run on cpu/gpu due to dynamic image resizing.
f
latten_
predictions
=
[]
f
or
i
in
range
(
tf
.
shape
(
predictions
)[
0
]):
flatten_
mask
s
=
[
]
mask
=
masks
[
i
]
flatten_
valid_masks
=
[]
valid_mask
=
valid_masks
[
i
]
for
mask
,
valid_mask
,
predicted_mask
,
image_info
in
zip
(
predicted_mask
=
predictions
[
i
]
masks
,
valid_masks
,
predictions
,
images_info
):
image_info
=
images_info
[
i
]
rescale_size
=
tf
.
cast
(
rescale_size
=
tf
.
cast
(
tf
.
math
.
ceil
(
image_info
[
1
,
:]
/
image_info
[
2
,
:]),
tf
.
int32
)
tf
.
math
.
ceil
(
image_info
[
1
,
:]
/
image_info
[
2
,
:]),
tf
.
int32
)
...
@@ -104,12 +103,12 @@ class MeanIoU(tf.keras.metrics.MeanIoU):
...
@@ -104,12 +103,12 @@ class MeanIoU(tf.keras.metrics.MeanIoU):
image_shape
[
1
])
image_shape
[
1
])
predicted_mask
=
tf
.
argmax
(
predicted_mask
,
axis
=
2
)
predicted_mask
=
tf
.
argmax
(
predicted_mask
,
axis
=
2
)
flatten_predictions
.
append
(
tf
.
reshape
(
predicted_mask
,
shape
=
[
1
,
-
1
])
)
flatten_predictions
=
tf
.
reshape
(
predicted_mask
,
shape
=
[
1
,
-
1
])
flatten_masks
.
append
(
tf
.
reshape
(
mask
,
shape
=
[
1
,
-
1
])
)
flatten_masks
=
tf
.
reshape
(
mask
,
shape
=
[
1
,
-
1
])
flatten_valid_masks
.
append
(
tf
.
reshape
(
valid_mask
,
shape
=
[
1
,
-
1
])
)
flatten_valid_masks
=
tf
.
reshape
(
valid_mask
,
shape
=
[
1
,
-
1
])
flatten_predictions
=
tf
.
concat
(
flatten_predictions
,
axis
=
1
)
super
(
MeanIoU
,
self
).
update_state
(
flatten_masks
=
tf
.
concat
(
flatten_masks
,
axis
=
1
)
flatten_masks
,
flatten_predictions
,
flatten_valid_masks
=
tf
.
c
onc
at
(
flatten_valid_masks
,
axis
=
1
)
tf
.
ca
s
t
(
flatten_valid_masks
,
tf
.
float32
)
)
else
:
else
:
predictions
=
tf
.
image
.
resize
(
predictions
=
tf
.
image
.
resize
(
...
@@ -121,9 +120,9 @@ class MeanIoU(tf.keras.metrics.MeanIoU):
...
@@ -121,9 +120,9 @@ class MeanIoU(tf.keras.metrics.MeanIoU):
flatten_masks
=
tf
.
reshape
(
masks
,
shape
=
[
-
1
])
flatten_masks
=
tf
.
reshape
(
masks
,
shape
=
[
-
1
])
flatten_valid_masks
=
tf
.
reshape
(
valid_masks
,
shape
=
[
-
1
])
flatten_valid_masks
=
tf
.
reshape
(
valid_masks
,
shape
=
[
-
1
])
super
(
MeanIoU
,
self
).
update_state
(
super
(
MeanIoU
,
self
).
update_state
(
flatten_masks
,
flatten_predictions
,
flatten_masks
,
flatten_predictions
,
tf
.
cast
(
flatten_valid_masks
,
tf
.
float32
))
tf
.
cast
(
flatten_valid_masks
,
tf
.
float32
))
class
PerClassIoU
(
keras_cv
.
metrics
.
PerClassIoU
):
class
PerClassIoU
(
keras_cv
.
metrics
.
PerClassIoU
):
...
@@ -184,11 +183,11 @@ class PerClassIoU(keras_cv.metrics.PerClassIoU):
...
@@ -184,11 +183,11 @@ class PerClassIoU(keras_cv.metrics.PerClassIoU):
if
self
.
_rescale_predictions
:
if
self
.
_rescale_predictions
:
# This part can only run on cpu/gpu due to dynamic image resizing.
# This part can only run on cpu/gpu due to dynamic image resizing.
f
latten_
predictions
=
[]
f
or
i
in
range
(
tf
.
shape
(
predictions
)[
0
]):
flatten_
mask
s
=
[
]
mask
=
masks
[
i
]
flatten_
valid_masks
=
[]
valid_mask
=
valid_masks
[
i
]
for
mask
,
valid_mask
,
predicted_mask
,
image_info
in
zip
(
predicted_mask
=
predictions
[
i
]
masks
,
valid_masks
,
predictions
,
images_info
):
image_info
=
images_info
[
i
]
rescale_size
=
tf
.
cast
(
rescale_size
=
tf
.
cast
(
tf
.
math
.
ceil
(
image_info
[
1
,
:]
/
image_info
[
2
,
:]),
tf
.
int32
)
tf
.
math
.
ceil
(
image_info
[
1
,
:]
/
image_info
[
2
,
:]),
tf
.
int32
)
...
@@ -211,12 +210,12 @@ class PerClassIoU(keras_cv.metrics.PerClassIoU):
...
@@ -211,12 +210,12 @@ class PerClassIoU(keras_cv.metrics.PerClassIoU):
image_shape
[
1
])
image_shape
[
1
])
predicted_mask
=
tf
.
argmax
(
predicted_mask
,
axis
=
2
)
predicted_mask
=
tf
.
argmax
(
predicted_mask
,
axis
=
2
)
flatten_predictions
.
append
(
tf
.
reshape
(
predicted_mask
,
shape
=
[
1
,
-
1
])
)
flatten_predictions
=
tf
.
reshape
(
predicted_mask
,
shape
=
[
1
,
-
1
])
flatten_masks
.
append
(
tf
.
reshape
(
mask
,
shape
=
[
1
,
-
1
])
)
flatten_masks
=
tf
.
reshape
(
mask
,
shape
=
[
1
,
-
1
])
flatten_valid_masks
.
append
(
tf
.
reshape
(
valid_mask
,
shape
=
[
1
,
-
1
])
)
flatten_valid_masks
=
tf
.
reshape
(
valid_mask
,
shape
=
[
1
,
-
1
])
flatten_predictions
=
tf
.
concat
(
flatten_predictions
,
axis
=
1
)
super
(
PerClassIoU
,
self
).
update_state
(
flatten_masks
=
tf
.
concat
(
flatten_masks
,
axis
=
1
)
flatten_masks
,
flatten_predictions
,
flatten_valid_masks
=
tf
.
c
onc
at
(
flatten_valid_masks
,
axis
=
1
)
tf
.
ca
s
t
(
flatten_valid_masks
,
tf
.
float32
)
)
else
:
else
:
predictions
=
tf
.
image
.
resize
(
predictions
=
tf
.
image
.
resize
(
...
@@ -228,6 +227,6 @@ class PerClassIoU(keras_cv.metrics.PerClassIoU):
...
@@ -228,6 +227,6 @@ class PerClassIoU(keras_cv.metrics.PerClassIoU):
flatten_masks
=
tf
.
reshape
(
masks
,
shape
=
[
-
1
])
flatten_masks
=
tf
.
reshape
(
masks
,
shape
=
[
-
1
])
flatten_valid_masks
=
tf
.
reshape
(
valid_masks
,
shape
=
[
-
1
])
flatten_valid_masks
=
tf
.
reshape
(
valid_masks
,
shape
=
[
-
1
])
super
(
PerClassIoU
,
self
).
update_state
(
super
(
PerClassIoU
,
self
).
update_state
(
flatten_masks
,
flatten_predictions
,
flatten_masks
,
flatten_predictions
,
tf
.
cast
(
flatten_valid_masks
,
tf
.
float32
))
tf
.
cast
(
flatten_valid_masks
,
tf
.
float32
))
official/vision/beta/tasks/semantic_segmentation.py
View file @
fcd09603
...
@@ -158,6 +158,10 @@ class SemanticSegmentationTask(base_task.Task):
...
@@ -158,6 +158,10 @@ class SemanticSegmentationTask(base_task.Task):
.
resize_eval_groundtruth
,
.
resize_eval_groundtruth
,
dtype
=
tf
.
float32
)
dtype
=
tf
.
float32
)
# Update state on CPU if TPUStrategy due to dynamic resizing.
self
.
_process_iou_metric_on_cpu
=
isinstance
(
tf
.
distribute
.
get_strategy
(),
tf
.
distribute
.
TPUStrategy
)
return
metrics
return
metrics
def
train_step
(
self
,
def
train_step
(
self
,
...
@@ -251,7 +255,11 @@ class SemanticSegmentationTask(base_task.Task):
...
@@ -251,7 +255,11 @@ class SemanticSegmentationTask(base_task.Task):
loss
=
0
loss
=
0
logs
=
{
self
.
loss
:
loss
}
logs
=
{
self
.
loss
:
loss
}
logs
.
update
({
self
.
iou_metric
.
name
:
(
labels
,
outputs
)})
if
self
.
_process_iou_metric_on_cpu
:
logs
.
update
({
self
.
iou_metric
.
name
:
(
labels
,
outputs
)})
else
:
self
.
iou_metric
.
update_state
(
labels
,
outputs
)
if
metrics
:
if
metrics
:
self
.
process_metrics
(
metrics
,
labels
,
outputs
)
self
.
process_metrics
(
metrics
,
labels
,
outputs
)
...
@@ -267,8 +275,9 @@ class SemanticSegmentationTask(base_task.Task):
...
@@ -267,8 +275,9 @@ class SemanticSegmentationTask(base_task.Task):
if
state
is
None
:
if
state
is
None
:
self
.
iou_metric
.
reset_states
()
self
.
iou_metric
.
reset_states
()
state
=
self
.
iou_metric
state
=
self
.
iou_metric
self
.
iou_metric
.
update_state
(
step_outputs
[
self
.
iou_metric
.
name
][
0
],
if
self
.
_process_iou_metric_on_cpu
:
step_outputs
[
self
.
iou_metric
.
name
][
1
])
self
.
iou_metric
.
update_state
(
step_outputs
[
self
.
iou_metric
.
name
][
0
],
step_outputs
[
self
.
iou_metric
.
name
][
1
])
return
state
return
state
def
reduce_aggregated_logs
(
self
,
aggregated_logs
,
global_step
=
None
):
def
reduce_aggregated_logs
(
self
,
aggregated_logs
,
global_step
=
None
):
...
...
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