Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
c14a04ab
M
Models
项目概览
曾经的那一瞬间
/
Models
12 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c14a04ab
编写于
5月 12, 2022
作者:
F
Fan Yang
提交者:
A. Unique TensorFlower
5月 12, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add precision and recall metrics at predefined thresholds for image classification task.
PiperOrigin-RevId: 448320923
上级
13642b0f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
31 addition
and
0 deletion
+31
-0
official/vision/configs/image_classification.py
official/vision/configs/image_classification.py
+3
-0
official/vision/tasks/image_classification.py
official/vision/tasks/image_classification.py
+28
-0
未找到文件。
official/vision/configs/image_classification.py
浏览文件 @
c14a04ab
...
...
@@ -65,6 +65,8 @@ class ImageClassificationModel(hyperparams.Config):
# Adds a BatchNormalization layer pre-GlobalAveragePooling in classification
add_head_batch_norm
:
bool
=
False
kernel_initializer
:
str
=
'random_uniform'
# Whether to output softmax results instead of logits.
output_softmax
:
bool
=
False
@
dataclasses
.
dataclass
...
...
@@ -79,6 +81,7 @@ class Losses(hyperparams.Config):
@
dataclasses
.
dataclass
class
Evaluation
(
hyperparams
.
Config
):
top_k
:
int
=
5
precision_and_recall_thresholds
:
Optional
[
List
[
float
]]
=
None
@
dataclasses
.
dataclass
...
...
official/vision/tasks/image_classification.py
浏览文件 @
c14a04ab
...
...
@@ -184,6 +184,24 @@ class ImageClassificationTask(base_task.Task):
tf
.
keras
.
metrics
.
CategoricalAccuracy
(
name
=
'accuracy'
),
tf
.
keras
.
metrics
.
TopKCategoricalAccuracy
(
k
=
k
,
name
=
'top_{}_accuracy'
.
format
(
k
))]
if
hasattr
(
self
.
task_config
.
evaluation
,
'precision_and_recall_thresholds'
)
and
self
.
task_config
.
evaluation
.
precision_and_recall_thresholds
:
thresholds
=
self
.
task_config
.
evaluation
.
precision_and_recall_thresholds
# pylint:disable=g-complex-comprehension
metrics
+=
[
tf
.
keras
.
metrics
.
Precision
(
thresholds
=
th
,
name
=
'precision_at_threshold_{}'
.
format
(
th
),
top_k
=
1
)
for
th
in
thresholds
]
metrics
+=
[
tf
.
keras
.
metrics
.
Recall
(
thresholds
=
th
,
name
=
'recall_at_threshold_{}'
.
format
(
th
),
top_k
=
1
)
for
th
in
thresholds
]
# pylint:enable=g-complex-comprehension
else
:
metrics
=
[
tf
.
keras
.
metrics
.
SparseCategoricalAccuracy
(
name
=
'accuracy'
),
...
...
@@ -234,6 +252,7 @@ class ImageClassificationTask(base_task.Task):
num_replicas
=
tf
.
distribute
.
get_strategy
().
num_replicas_in_sync
with
tf
.
GradientTape
()
as
tape
:
outputs
=
model
(
features
,
training
=
True
)
# Casting output layer as float32 is necessary when mixed_precision is
# mixed_float16 or mixed_bfloat16 to ensure output is casted as float32.
outputs
=
tf
.
nest
.
map_structure
(
...
...
@@ -264,6 +283,11 @@ class ImageClassificationTask(base_task.Task):
optimizer
.
apply_gradients
(
list
(
zip
(
grads
,
tvars
)))
logs
=
{
self
.
loss
:
loss
}
# Convert logits to softmax for metric computation if needed.
if
hasattr
(
self
.
task_config
.
model
,
'output_softmax'
)
and
self
.
task_config
.
model
.
output_softmax
:
outputs
=
tf
.
nn
.
softmax
(
outputs
,
axis
=-
1
)
if
metrics
:
self
.
process_metrics
(
metrics
,
labels
,
outputs
)
elif
model
.
compiled_metrics
:
...
...
@@ -300,6 +324,10 @@ class ImageClassificationTask(base_task.Task):
aux_losses
=
model
.
losses
)
logs
=
{
self
.
loss
:
loss
}
# Convert logits to softmax for metric computation if needed.
if
hasattr
(
self
.
task_config
.
model
,
'output_softmax'
)
and
self
.
task_config
.
model
.
output_softmax
:
outputs
=
tf
.
nn
.
softmax
(
outputs
,
axis
=-
1
)
if
metrics
:
self
.
process_metrics
(
metrics
,
labels
,
outputs
)
elif
model
.
compiled_metrics
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录