Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update batch evaluate #154

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions paddlers/tasks/change_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import paddlers.utils.logging as logging
from paddlers.models import seg_losses
from paddlers.transforms import Resize, decode_image, construct_sample
from paddlers.utils import get_single_card_bs
from paddlers.utils import to_data_parallel
from paddlers.utils.checkpoint import cd_pretrain_weights_dict
from .base import BaseModel
from .utils import seg_metrics as metrics
Expand Down Expand Up @@ -447,25 +447,22 @@ def evaluate(self, eval_dataset, batch_size=1, return_details=False):
"""

self._check_transforms(eval_dataset.transforms)
net = self.net
net.eval()

self.net.eval()
# XXX: Hard-coding
nranks = paddle.distributed.get_world_size()
local_rank = paddle.distributed.get_rank()
if nranks > 1:
# Initialize parallel environment if not done.
if not (paddle.distributed.parallel.parallel_helper.
_is_parallel_ctx_initialized()):
if not paddle.distributed.parallel.parallel_helper._is_parallel_ctx_initialized(
):
paddle.distributed.init_parallel_env()
net = to_data_parallel(
net, find_unused_parameters=self.find_unused_parameters)
else:
net = to_data_parallel(
net, find_unused_parameters=self.find_unused_parameters)

batch_size_each_card = get_single_card_bs(batch_size)
if batch_size_each_card > 1:
batch_size_each_card = 1
batch_size = batch_size_each_card * paddlers.env_info['num']
logging.warning(
"ChangeDetector only supports batch_size=1 for each gpu/cpu card " \
"during evaluation, so batch_size " \
"is forcibly set to {}.".format(batch_size)
)
self.eval_data_loader = self.build_data_loader(
eval_dataset, batch_size=batch_size, mode='eval')

Expand All @@ -485,9 +482,9 @@ def evaluate(self, eval_dataset, batch_size=1, return_details=False):
enable=True,
custom_white_list=self.custom_white_list,
custom_black_list=self.custom_black_list):
outputs = self.run(self.net, data, 'eval')
outputs = self.run(net, data, 'eval')
else:
outputs = self.run(self.net, data, 'eval')
outputs = self.run(net, data, 'eval')
pred_area = outputs['pred_area']
label_area = outputs['label_area']
intersect_area = outputs['intersect_area']
Expand Down Expand Up @@ -694,6 +691,8 @@ def postprocess(self, batch_pred, batch_restore_list):
else:
raise RuntimeError
results.append(pred)
if len(results) > 1:
results = [paddle.concat(results, axis=0)]
return results

def _infer_postprocess(self, batch_label_map, batch_score_map,
Expand Down
94 changes: 54 additions & 40 deletions paddlers/tasks/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import paddlers.rs_models.clas as cmcls
import paddlers.utils.logging as logging
from paddlers.models.ppcls.metric import build_metrics
from paddlers.utils import to_data_parallel
from paddlers.models import clas_losses
from paddlers.models.ppcls.data.postprocess import build_postprocess
from paddlers.utils.checkpoint import cls_pretrain_weights_dict
Expand Down Expand Up @@ -402,54 +403,67 @@ def evaluate(self, eval_dataset, batch_size=1, return_details=False):
"""

self._check_transforms(eval_dataset.transforms)
net = self.net
net.eval()

self.net.eval()
# XXX: Hard-coding
nranks = paddle.distributed.get_world_size()
local_rank = paddle.distributed.get_rank()
if nranks > 1:
# Initialize parallel environment if not done.
if not paddle.distributed.parallel.parallel_helper._is_parallel_ctx_initialized(
):
paddle.distributed.init_parallel_env()
net = to_data_parallel(
net, find_unused_parameters=self.find_unused_parameters)
else:
net = to_data_parallel(
net, find_unused_parameters=self.find_unused_parameters)

self.eval_data_loader = self.build_data_loader(
eval_dataset, batch_size=batch_size, mode='eval')
logging.info("Start to evaluate (total_samples={}, total_steps={})...".
format(eval_dataset.num_samples, eval_dataset.num_samples))

top1s = []
top5s = []
with paddle.no_grad():
for step, data in enumerate(self.eval_data_loader):
if self.precision == 'fp16':
with paddle.amp.auto_cast(
level=self.amp_level,
enable=True,
custom_white_list=self.custom_white_list,
custom_black_list=self.custom_black_list):
outputs = self.run(net, data, 'eval')
else:
outputs = self.run(net, data, 'eval')
if nranks > 1:
t1 = outputs["top1"]
t5 = outputs["top5"]
t1s = []
t5s = []
paddle.distributed.all_gather(t1s, t1)
paddle.distributed.all_gather(t5s, t5)
for rank_id in range(nranks):
top1 = t1s[rank_id]
top5 = t5s[rank_id]
for i in range(data['image'].shape[0]):
top1s.append(top1)
top5s.append(top5)
else:
for i in range(data['image'].shape[0]):
top1s.append(outputs["top1"])
top5s.append(outputs["top5"])

top1 = np.mean(top1s)
top5 = np.mean(top5s)
eval_metrics = OrderedDict(zip(['top1', 'top5'], [top1, top5]))

if return_details:
# TODO: Add details
return eval_metrics, None

if batch_size > 1:
logging.warning(
"Classifier only supports single card evaluation with batch_size=1 "
"during evaluation, so batch_size is forcibly set to 1.")
batch_size = 1

if nranks < 2 or local_rank == 0:
self.eval_data_loader = self.build_data_loader(
eval_dataset, batch_size=batch_size, mode='eval')
logging.info(
"Start to evaluate (total_samples={}, total_steps={})...".
format(eval_dataset.num_samples, eval_dataset.num_samples))

top1s = []
top5s = []
with paddle.no_grad():
for step, data in enumerate(self.eval_data_loader):
if self.precision == 'fp16':
with paddle.amp.auto_cast(
level=self.amp_level,
enable=True,
custom_white_list=self.custom_white_list,
custom_black_list=self.custom_black_list):
outputs = self.run(self.net, data, 'eval')
else:
outputs = self.run(self.net, data, 'eval')
top1s.append(outputs["top1"])
top5s.append(outputs["top5"])

top1 = np.mean(top1s)
top5 = np.mean(top5s)
eval_metrics = OrderedDict(zip(['top1', 'top5'], [top1, top5]))

if return_details:
# TODO: Add details
return eval_metrics, None

return eval_metrics
return eval_metrics

@paddle.no_grad()
def predict(self, img_file, transforms=None):
Expand Down
Loading
Loading