PaddleClas/ppcls/engine/evaluation/classification.py

183 lines
6.9 KiB
Python
Raw Normal View History

2021-08-22 23:10:23 +08:00
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import platform
import paddle
2022-05-23 18:27:55 +08:00
from ppcls.utils.misc import AverageMeter
2021-08-22 23:10:23 +08:00
from ppcls.utils import logger
2021-09-23 11:22:25 +08:00
def classification_eval(engine, epoch_id=0):
2022-05-14 17:31:52 +08:00
if hasattr(engine.eval_metric_func, "reset"):
engine.eval_metric_func.reset()
2021-08-22 23:10:23 +08:00
output_info = dict()
time_info = {
"batch_cost": AverageMeter(
"batch_cost", '.5f', postfix=" s,"),
"reader_cost": AverageMeter(
"reader_cost", ".5f", postfix=" s,"),
}
2021-09-23 11:22:25 +08:00
print_batch_step = engine.config["Global"]["print_batch_step"]
2021-08-22 23:10:23 +08:00
metric_key = None
tic = time.time()
2021-10-20 19:22:37 +08:00
accum_samples = 0
total_samples = len(
engine.eval_dataloader.
dataset) if not engine.use_dali else engine.eval_dataloader.size
2021-09-23 11:22:25 +08:00
max_iter = len(engine.eval_dataloader) - 1 if platform.system(
) == "Windows" else len(engine.eval_dataloader)
for iter_id, batch in enumerate(engine.eval_dataloader):
2021-08-22 23:10:23 +08:00
if iter_id >= max_iter:
break
if iter_id == 5:
for key in time_info:
time_info[key].reset()
2021-09-23 11:22:25 +08:00
if engine.use_dali:
2021-08-22 23:10:23 +08:00
batch = [
paddle.to_tensor(batch[0]['data']),
paddle.to_tensor(batch[0]['label'])
]
time_info["reader_cost"].update(time.time() - tic)
batch_size = batch[0].shape[0]
batch[0] = paddle.to_tensor(batch[0])
2021-09-27 11:00:37 +08:00
if not engine.config["Global"].get("use_multilabel", False):
2021-09-26 15:05:13 +08:00
batch[1] = batch[1].reshape([-1, 1]).astype("int64")
2021-08-22 23:10:23 +08:00
# image input
if engine.amp and engine.amp_eval:
with paddle.amp.auto_cast(
custom_black_list={
"flatten_contiguous_range", "greater_than"
},
level=engine.amp_level):
2022-01-07 14:41:46 +08:00
out = engine.model(batch[0])
else:
out = engine.model(batch[0])
2021-10-20 19:22:37 +08:00
# just for DistributedBatchSampler issue: repeat sampling
current_samples = batch_size * paddle.distributed.get_world_size()
accum_samples += current_samples
if isinstance(out, dict) and "Student" in out:
out = out["Student"]
if isinstance(out, dict) and "logits" in out:
out = out["logits"]
2022-04-12 14:56:44 +08:00
# gather Tensor when distributed
if paddle.distributed.get_world_size() > 1:
label_list = []
2022-05-18 12:06:38 +08:00
2022-04-12 14:56:44 +08:00
paddle.distributed.all_gather(label_list, batch[1])
labels = paddle.concat(label_list, 0)
2021-10-26 19:56:30 +08:00
2022-04-12 14:56:44 +08:00
if isinstance(out, list):
preds = []
for x in out:
2021-10-26 19:56:30 +08:00
pred_list = []
2022-04-12 14:56:44 +08:00
paddle.distributed.all_gather(pred_list, x)
pred_x = paddle.concat(pred_list, 0)
preds.append(pred_x)
else:
pred_list = []
paddle.distributed.all_gather(pred_list, out)
preds = paddle.concat(pred_list, 0)
2021-10-26 19:56:30 +08:00
2022-04-12 14:56:44 +08:00
if accum_samples > total_samples and not engine.use_dali:
preds = preds[:total_samples + current_samples - accum_samples]
labels = labels[:total_samples + current_samples -
2021-10-20 19:22:37 +08:00
accum_samples]
2022-04-12 14:56:44 +08:00
current_samples = total_samples + current_samples - accum_samples
else:
labels = batch[1]
preds = out
# calc loss
if engine.eval_loss_func is not None:
if engine.amp and engine.amp_eval:
2022-04-12 14:56:44 +08:00
with paddle.amp.auto_cast(
custom_black_list={
"flatten_contiguous_range", "greater_than"
},
level=engine.amp_level):
2022-04-12 14:56:44 +08:00
loss_dict = engine.eval_loss_func(preds, labels)
2021-10-20 19:22:37 +08:00
else:
2022-04-12 14:56:44 +08:00
loss_dict = engine.eval_loss_func(preds, labels)
2022-04-12 14:56:44 +08:00
for key in loss_dict:
if key not in output_info:
output_info[key] = AverageMeter(key, '7.5f')
output_info[key].update(loss_dict[key].numpy()[0],
current_samples)
2022-05-11 15:01:26 +08:00
2022-04-12 14:56:44 +08:00
# calc metric
if engine.eval_metric_func is not None:
2022-05-14 17:31:52 +08:00
engine.eval_metric_func(preds, labels)
2021-08-22 23:10:23 +08:00
time_info["batch_cost"].update(time.time() - tic)
if iter_id % print_batch_step == 0:
time_msg = "s, ".join([
"{}: {:.5f}".format(key, time_info[key].avg)
for key in time_info
])
ips_msg = "ips: {:.5f} images/sec".format(
batch_size / time_info["batch_cost"].avg)
2022-05-12 20:49:21 +08:00
if "ATTRMetric" in engine.config["Metric"]["Eval"][0]:
2022-05-11 15:01:26 +08:00
metric_msg = ""
else:
metric_msg = ", ".join([
"{}: {:.5f}".format(key, output_info[key].val)
for key in output_info
])
metric_msg += ", {}".format(engine.eval_metric_func.avg_info)
2021-08-22 23:10:23 +08:00
logger.info("[Eval][Epoch {}][Iter: {}/{}]{}, {}, {}".format(
epoch_id, iter_id,
2021-09-23 11:22:25 +08:00
len(engine.eval_dataloader), metric_msg, time_msg, ips_msg))
2021-08-22 23:10:23 +08:00
tic = time.time()
2021-09-23 11:22:25 +08:00
if engine.use_dali:
engine.eval_dataloader.reset()
2022-05-11 15:01:26 +08:00
2022-05-12 20:49:21 +08:00
if "ATTRMetric" in engine.config["Metric"]["Eval"][0]:
2022-05-11 15:01:26 +08:00
metric_msg = ", ".join([
"evalres: ma: {:.5f} label_f1: {:.5f} label_pos_recall: {:.5f} label_neg_recall: {:.5f} instance_f1: {:.5f} instance_acc: {:.5f} instance_prec: {:.5f} instance_recall: {:.5f}".
2022-05-23 18:27:55 +08:00
format(*engine.eval_metric_func.attr_res())
2022-05-11 15:01:26 +08:00
])
logger.info("[Eval][Epoch {}][Avg]{}".format(epoch_id, metric_msg))
# do not try to save best eval.model
if engine.eval_metric_func is None:
return -1
# return 1st metric in the dict
2022-05-23 18:27:55 +08:00
return engine.eval_metric_func.attr_res()[0]
2022-05-11 15:01:26 +08:00
else:
metric_msg = ", ".join([
"{}: {:.5f}".format(key, output_info[key].avg)
for key in output_info
])
metric_msg += ", {}".format(engine.eval_metric_func.avg_info)
2022-05-11 15:01:26 +08:00
logger.info("[Eval][Epoch {}][Avg]{}".format(epoch_id, metric_msg))
# do not try to save best eval.model
if engine.eval_metric_func is None:
return -1
# return 1st metric in the dict
return engine.eval_metric_func.avg