更换文档检测模型
This commit is contained in:
37
paddle_detection/ppdet/engine/__init__.py
Normal file
37
paddle_detection/ppdet/engine/__init__.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Copyright (c) 2020 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 . import trainer
|
||||
from .trainer import *
|
||||
|
||||
from . import trainer_cot
|
||||
from .trainer_cot import *
|
||||
|
||||
from . import callbacks
|
||||
from .callbacks import *
|
||||
|
||||
from . import env
|
||||
from .env import *
|
||||
|
||||
__all__ = trainer.__all__ \
|
||||
+ callbacks.__all__ \
|
||||
+ env.__all__
|
||||
|
||||
from . import tracker
|
||||
from .tracker import *
|
||||
__all__ = __all__ + tracker.__all__
|
||||
|
||||
from . import trainer_ssod
|
||||
from .trainer_ssod import *
|
||||
__all__ = __all__ + trainer_ssod.__all__
|
||||
693
paddle_detection/ppdet/engine/callbacks.py
Normal file
693
paddle_detection/ppdet/engine/callbacks.py
Normal file
@@ -0,0 +1,693 @@
|
||||
# Copyright (c) 2020 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 os
|
||||
import sys
|
||||
import datetime
|
||||
import six
|
||||
import copy
|
||||
import json
|
||||
|
||||
import paddle
|
||||
import paddle.distributed as dist
|
||||
|
||||
from ppdet.utils.checkpoint import save_model, save_semi_model
|
||||
from ppdet.metrics import get_infer_results
|
||||
|
||||
from ppdet.utils.logger import setup_logger
|
||||
logger = setup_logger('ppdet.engine')
|
||||
|
||||
__all__ = [
|
||||
'Callback', 'ComposeCallback', 'LogPrinter', 'Checkpointer',
|
||||
'VisualDLWriter', 'SniperProposalsGenerator'
|
||||
]
|
||||
|
||||
|
||||
class Callback(object):
|
||||
def __init__(self, model):
|
||||
self.model = model
|
||||
|
||||
def on_step_begin(self, status):
|
||||
pass
|
||||
|
||||
def on_step_end(self, status):
|
||||
pass
|
||||
|
||||
def on_epoch_begin(self, status):
|
||||
pass
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
pass
|
||||
|
||||
def on_train_begin(self, status):
|
||||
pass
|
||||
|
||||
def on_train_end(self, status):
|
||||
pass
|
||||
|
||||
|
||||
class ComposeCallback(object):
|
||||
def __init__(self, callbacks):
|
||||
callbacks = [c for c in list(callbacks) if c is not None]
|
||||
for c in callbacks:
|
||||
assert isinstance(
|
||||
c, Callback), "callback should be subclass of Callback"
|
||||
self._callbacks = callbacks
|
||||
|
||||
def on_step_begin(self, status):
|
||||
for c in self._callbacks:
|
||||
c.on_step_begin(status)
|
||||
|
||||
def on_step_end(self, status):
|
||||
for c in self._callbacks:
|
||||
c.on_step_end(status)
|
||||
|
||||
def on_epoch_begin(self, status):
|
||||
for c in self._callbacks:
|
||||
c.on_epoch_begin(status)
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
for c in self._callbacks:
|
||||
c.on_epoch_end(status)
|
||||
|
||||
def on_train_begin(self, status):
|
||||
for c in self._callbacks:
|
||||
c.on_train_begin(status)
|
||||
|
||||
def on_train_end(self, status):
|
||||
for c in self._callbacks:
|
||||
c.on_train_end(status)
|
||||
|
||||
|
||||
class LogPrinter(Callback):
|
||||
def __init__(self, model):
|
||||
super(LogPrinter, self).__init__(model)
|
||||
|
||||
def on_step_end(self, status):
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
mode = status['mode']
|
||||
if mode == 'train':
|
||||
epoch_id = status['epoch_id']
|
||||
step_id = status['step_id']
|
||||
steps_per_epoch = status['steps_per_epoch']
|
||||
training_staus = status['training_staus']
|
||||
batch_time = status['batch_time']
|
||||
data_time = status['data_time']
|
||||
|
||||
epoches = self.model.cfg.epoch
|
||||
batch_size = self.model.cfg['{}Reader'.format(mode.capitalize(
|
||||
))]['batch_size']
|
||||
|
||||
logs = training_staus.log()
|
||||
space_fmt = ':' + str(len(str(steps_per_epoch))) + 'd'
|
||||
if step_id % self.model.cfg.log_iter == 0:
|
||||
eta_steps = (epoches - epoch_id) * steps_per_epoch - step_id
|
||||
eta_sec = eta_steps * batch_time.global_avg
|
||||
eta_str = str(datetime.timedelta(seconds=int(eta_sec)))
|
||||
ips = float(batch_size) / batch_time.avg
|
||||
fmt = ' '.join([
|
||||
'Epoch: [{}]',
|
||||
'[{' + space_fmt + '}/{}]',
|
||||
'learning_rate: {lr:.6f}',
|
||||
'{meters}',
|
||||
'eta: {eta}',
|
||||
'batch_cost: {btime}',
|
||||
'data_cost: {dtime}',
|
||||
'ips: {ips:.4f} images/s',
|
||||
])
|
||||
fmt = fmt.format(
|
||||
epoch_id,
|
||||
step_id,
|
||||
steps_per_epoch,
|
||||
lr=status['learning_rate'],
|
||||
meters=logs,
|
||||
eta=eta_str,
|
||||
btime=str(batch_time),
|
||||
dtime=str(data_time),
|
||||
ips=ips)
|
||||
logger.info(fmt)
|
||||
if mode == 'eval':
|
||||
step_id = status['step_id']
|
||||
if step_id % 100 == 0:
|
||||
logger.info("Eval iter: {}".format(step_id))
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
mode = status['mode']
|
||||
if mode == 'eval':
|
||||
sample_num = status['sample_num']
|
||||
cost_time = status['cost_time']
|
||||
logger.info('Total sample number: {}, average FPS: {}'.format(
|
||||
sample_num, sample_num / cost_time))
|
||||
|
||||
|
||||
class Checkpointer(Callback):
|
||||
def __init__(self, model):
|
||||
super(Checkpointer, self).__init__(model)
|
||||
self.best_ap = -1000.
|
||||
self.save_dir = self.model.cfg.save_dir
|
||||
if hasattr(self.model.model, 'student_model'):
|
||||
self.weight = self.model.model.student_model
|
||||
else:
|
||||
self.weight = self.model.model
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
# Checkpointer only performed during training
|
||||
mode = status['mode']
|
||||
epoch_id = status['epoch_id']
|
||||
weight = None
|
||||
save_name = None
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if mode == 'train':
|
||||
end_epoch = self.model.cfg.epoch
|
||||
if (
|
||||
epoch_id + 1
|
||||
) % self.model.cfg.snapshot_epoch == 0 or epoch_id == end_epoch - 1:
|
||||
save_name = str(
|
||||
epoch_id) if epoch_id != end_epoch - 1 else "model_final"
|
||||
weight = self.weight.state_dict()
|
||||
elif mode == 'eval':
|
||||
if 'save_best_model' in status and status['save_best_model']:
|
||||
for metric in self.model._metrics:
|
||||
map_res = metric.get_results()
|
||||
eval_func = "ap"
|
||||
if 'pose3d' in map_res:
|
||||
key = 'pose3d'
|
||||
eval_func = "mpjpe"
|
||||
elif 'bbox' in map_res:
|
||||
key = 'bbox'
|
||||
elif 'keypoint' in map_res:
|
||||
key = 'keypoint'
|
||||
else:
|
||||
key = 'mask'
|
||||
if key not in map_res:
|
||||
logger.warning("Evaluation results empty, this may be due to " \
|
||||
"training iterations being too few or not " \
|
||||
"loading the correct weights.")
|
||||
return
|
||||
if map_res[key][0] >= self.best_ap:
|
||||
self.best_ap = map_res[key][0]
|
||||
save_name = 'best_model'
|
||||
weight = self.weight.state_dict()
|
||||
logger.info("Best test {} {} is {:0.3f}.".format(
|
||||
key, eval_func, abs(self.best_ap)))
|
||||
if weight:
|
||||
if self.model.use_ema:
|
||||
exchange_save_model = status.get('exchange_save_model',
|
||||
False)
|
||||
if not exchange_save_model:
|
||||
# save model and ema_model
|
||||
save_model(
|
||||
status['weight'],
|
||||
self.model.optimizer,
|
||||
self.save_dir,
|
||||
save_name,
|
||||
epoch_id + 1,
|
||||
ema_model=weight)
|
||||
else:
|
||||
# save model(student model) and ema_model(teacher model)
|
||||
# in DenseTeacher SSOD, the teacher model will be higher,
|
||||
# so exchange when saving pdparams
|
||||
student_model = status['weight'] # model
|
||||
teacher_model = weight # ema_model
|
||||
save_model(
|
||||
teacher_model,
|
||||
self.model.optimizer,
|
||||
self.save_dir,
|
||||
save_name,
|
||||
epoch_id + 1,
|
||||
ema_model=student_model)
|
||||
del teacher_model
|
||||
del student_model
|
||||
else:
|
||||
save_model(weight, self.model.optimizer, self.save_dir,
|
||||
save_name, epoch_id + 1)
|
||||
|
||||
|
||||
class WiferFaceEval(Callback):
|
||||
def __init__(self, model):
|
||||
super(WiferFaceEval, self).__init__(model)
|
||||
|
||||
def on_epoch_begin(self, status):
|
||||
assert self.model.mode == 'eval', \
|
||||
"WiferFaceEval can only be set during evaluation"
|
||||
for metric in self.model._metrics:
|
||||
metric.update(self.model.model)
|
||||
sys.exit()
|
||||
|
||||
|
||||
class VisualDLWriter(Callback):
|
||||
"""
|
||||
Use VisualDL to log data or image
|
||||
"""
|
||||
|
||||
def __init__(self, model):
|
||||
super(VisualDLWriter, self).__init__(model)
|
||||
|
||||
assert six.PY3, "VisualDL requires Python >= 3.5"
|
||||
try:
|
||||
from visualdl import LogWriter
|
||||
except Exception as e:
|
||||
logger.error('visualdl not found, plaese install visualdl. '
|
||||
'for example: `pip install visualdl`.')
|
||||
raise e
|
||||
self.vdl_writer = LogWriter(
|
||||
model.cfg.get('vdl_log_dir', 'vdl_log_dir/scalar'))
|
||||
self.vdl_loss_step = 0
|
||||
self.vdl_mAP_step = 0
|
||||
self.vdl_image_step = 0
|
||||
self.vdl_image_frame = 0
|
||||
|
||||
def on_step_end(self, status):
|
||||
mode = status['mode']
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if mode == 'train':
|
||||
training_staus = status['training_staus']
|
||||
for loss_name, loss_value in training_staus.get().items():
|
||||
self.vdl_writer.add_scalar(loss_name, loss_value,
|
||||
self.vdl_loss_step)
|
||||
self.vdl_loss_step += 1
|
||||
elif mode == 'test':
|
||||
ori_image = status['original_image']
|
||||
result_image = status['result_image']
|
||||
self.vdl_writer.add_image(
|
||||
"original/frame_{}".format(self.vdl_image_frame), ori_image,
|
||||
self.vdl_image_step)
|
||||
self.vdl_writer.add_image(
|
||||
"result/frame_{}".format(self.vdl_image_frame),
|
||||
result_image, self.vdl_image_step)
|
||||
self.vdl_image_step += 1
|
||||
# each frame can display ten pictures at most.
|
||||
if self.vdl_image_step % 10 == 0:
|
||||
self.vdl_image_step = 0
|
||||
self.vdl_image_frame += 1
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
mode = status['mode']
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if mode == 'eval':
|
||||
for metric in self.model._metrics:
|
||||
for key, map_value in metric.get_results().items():
|
||||
self.vdl_writer.add_scalar("{}-mAP".format(key),
|
||||
map_value[0],
|
||||
self.vdl_mAP_step)
|
||||
self.vdl_mAP_step += 1
|
||||
|
||||
|
||||
class WandbCallback(Callback):
|
||||
def __init__(self, model):
|
||||
super(WandbCallback, self).__init__(model)
|
||||
|
||||
try:
|
||||
import wandb
|
||||
self.wandb = wandb
|
||||
except Exception as e:
|
||||
logger.error('wandb not found, please install wandb. '
|
||||
'Use: `pip install wandb`.')
|
||||
raise e
|
||||
|
||||
self.wandb_params = model.cfg.get('wandb', None)
|
||||
self.save_dir = self.model.cfg.save_dir
|
||||
if self.wandb_params is None:
|
||||
self.wandb_params = {}
|
||||
for k, v in model.cfg.items():
|
||||
if k.startswith("wandb_"):
|
||||
self.wandb_params.update({k.lstrip("wandb_"): v})
|
||||
|
||||
self._run = None
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
_ = self.run
|
||||
self.run.config.update(self.model.cfg)
|
||||
self.run.define_metric("epoch")
|
||||
self.run.define_metric("eval/*", step_metric="epoch")
|
||||
|
||||
self.best_ap = -1000.
|
||||
self.fps = []
|
||||
|
||||
@property
|
||||
def run(self):
|
||||
if self._run is None:
|
||||
if self.wandb.run is not None:
|
||||
logger.info(
|
||||
"There is an ongoing wandb run which will be used"
|
||||
"for logging. Please use `wandb.finish()` to end that"
|
||||
"if the behaviour is not intended")
|
||||
self._run = self.wandb.run
|
||||
else:
|
||||
self._run = self.wandb.init(**self.wandb_params)
|
||||
return self._run
|
||||
|
||||
def save_model(self,
|
||||
optimizer,
|
||||
save_dir,
|
||||
save_name,
|
||||
last_epoch,
|
||||
ema_model=None,
|
||||
ap=None,
|
||||
fps=None,
|
||||
tags=None):
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
model_path = os.path.join(save_dir, save_name)
|
||||
metadata = {}
|
||||
metadata["last_epoch"] = last_epoch
|
||||
if ap:
|
||||
metadata["ap"] = ap
|
||||
|
||||
if fps:
|
||||
metadata["fps"] = fps
|
||||
|
||||
if ema_model is None:
|
||||
ema_artifact = self.wandb.Artifact(
|
||||
name="ema_model-{}".format(self.run.id),
|
||||
type="model",
|
||||
metadata=metadata)
|
||||
model_artifact = self.wandb.Artifact(
|
||||
name="model-{}".format(self.run.id),
|
||||
type="model",
|
||||
metadata=metadata)
|
||||
|
||||
ema_artifact.add_file(model_path + ".pdema", name="model_ema")
|
||||
model_artifact.add_file(model_path + ".pdparams", name="model")
|
||||
|
||||
self.run.log_artifact(ema_artifact, aliases=tags)
|
||||
self.run.log_artfact(model_artifact, aliases=tags)
|
||||
else:
|
||||
model_artifact = self.wandb.Artifact(
|
||||
name="model-{}".format(self.run.id),
|
||||
type="model",
|
||||
metadata=metadata)
|
||||
model_artifact.add_file(model_path + ".pdparams", name="model")
|
||||
self.run.log_artifact(model_artifact, aliases=tags)
|
||||
|
||||
def on_step_end(self, status):
|
||||
|
||||
mode = status['mode']
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if mode == 'train':
|
||||
training_status = status['training_staus'].get()
|
||||
for k, v in training_status.items():
|
||||
training_status[k] = float(v)
|
||||
|
||||
# calculate ips, data_cost, batch_cost
|
||||
batch_time = status['batch_time']
|
||||
data_time = status['data_time']
|
||||
batch_size = self.model.cfg['{}Reader'.format(mode.capitalize(
|
||||
))]['batch_size']
|
||||
|
||||
ips = float(batch_size) / float(batch_time.avg)
|
||||
data_cost = float(data_time.avg)
|
||||
batch_cost = float(batch_time.avg)
|
||||
|
||||
metrics = {"train/" + k: v for k, v in training_status.items()}
|
||||
|
||||
metrics["train/ips"] = ips
|
||||
metrics["train/data_cost"] = data_cost
|
||||
metrics["train/batch_cost"] = batch_cost
|
||||
|
||||
self.fps.append(ips)
|
||||
self.run.log(metrics)
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
mode = status['mode']
|
||||
epoch_id = status['epoch_id']
|
||||
save_name = None
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if mode == 'train':
|
||||
fps = sum(self.fps) / len(self.fps)
|
||||
self.fps = []
|
||||
|
||||
end_epoch = self.model.cfg.epoch
|
||||
if (
|
||||
epoch_id + 1
|
||||
) % self.model.cfg.snapshot_epoch == 0 or epoch_id == end_epoch - 1:
|
||||
save_name = str(
|
||||
epoch_id) if epoch_id != end_epoch - 1 else "model_final"
|
||||
tags = ["latest", "epoch_{}".format(epoch_id)]
|
||||
self.save_model(
|
||||
self.model.optimizer,
|
||||
self.save_dir,
|
||||
save_name,
|
||||
epoch_id + 1,
|
||||
self.model.use_ema,
|
||||
fps=fps,
|
||||
tags=tags)
|
||||
if mode == 'eval':
|
||||
sample_num = status['sample_num']
|
||||
cost_time = status['cost_time']
|
||||
|
||||
fps = sample_num / cost_time
|
||||
|
||||
merged_dict = {}
|
||||
for metric in self.model._metrics:
|
||||
for key, map_value in metric.get_results().items():
|
||||
merged_dict["eval/{}-mAP".format(key)] = map_value[0]
|
||||
merged_dict["epoch"] = status["epoch_id"]
|
||||
merged_dict["eval/fps"] = sample_num / cost_time
|
||||
|
||||
self.run.log(merged_dict)
|
||||
|
||||
if 'save_best_model' in status and status['save_best_model']:
|
||||
for metric in self.model._metrics:
|
||||
map_res = metric.get_results()
|
||||
if 'pose3d' in map_res:
|
||||
key = 'pose3d'
|
||||
elif 'bbox' in map_res:
|
||||
key = 'bbox'
|
||||
elif 'keypoint' in map_res:
|
||||
key = 'keypoint'
|
||||
else:
|
||||
key = 'mask'
|
||||
if key not in map_res:
|
||||
logger.warning("Evaluation results empty, this may be due to " \
|
||||
"training iterations being too few or not " \
|
||||
"loading the correct weights.")
|
||||
return
|
||||
if map_res[key][0] >= self.best_ap:
|
||||
self.best_ap = map_res[key][0]
|
||||
save_name = 'best_model'
|
||||
tags = ["best", "epoch_{}".format(epoch_id)]
|
||||
|
||||
self.save_model(
|
||||
self.model.optimizer,
|
||||
self.save_dir,
|
||||
save_name,
|
||||
last_epoch=epoch_id + 1,
|
||||
ema_model=self.model.use_ema,
|
||||
ap=abs(self.best_ap),
|
||||
fps=fps,
|
||||
tags=tags)
|
||||
|
||||
def on_train_end(self, status):
|
||||
self.run.finish()
|
||||
|
||||
|
||||
class SniperProposalsGenerator(Callback):
|
||||
def __init__(self, model):
|
||||
super(SniperProposalsGenerator, self).__init__(model)
|
||||
ori_dataset = self.model.dataset
|
||||
self.dataset = self._create_new_dataset(ori_dataset)
|
||||
self.loader = self.model.loader
|
||||
self.cfg = self.model.cfg
|
||||
self.infer_model = self.model.model
|
||||
|
||||
def _create_new_dataset(self, ori_dataset):
|
||||
dataset = copy.deepcopy(ori_dataset)
|
||||
# init anno_cropper
|
||||
dataset.init_anno_cropper()
|
||||
# generate infer roidbs
|
||||
ori_roidbs = dataset.get_ori_roidbs()
|
||||
roidbs = dataset.anno_cropper.crop_infer_anno_records(ori_roidbs)
|
||||
# set new roidbs
|
||||
dataset.set_roidbs(roidbs)
|
||||
|
||||
return dataset
|
||||
|
||||
def _eval_with_loader(self, loader):
|
||||
results = []
|
||||
with paddle.no_grad():
|
||||
self.infer_model.eval()
|
||||
for step_id, data in enumerate(loader):
|
||||
outs = self.infer_model(data)
|
||||
for key in ['im_shape', 'scale_factor', 'im_id']:
|
||||
outs[key] = data[key]
|
||||
for key, value in outs.items():
|
||||
if hasattr(value, 'numpy'):
|
||||
outs[key] = value.numpy()
|
||||
|
||||
results.append(outs)
|
||||
|
||||
return results
|
||||
|
||||
def on_train_end(self, status):
|
||||
self.loader.dataset = self.dataset
|
||||
results = self._eval_with_loader(self.loader)
|
||||
results = self.dataset.anno_cropper.aggregate_chips_detections(results)
|
||||
# sniper
|
||||
proposals = []
|
||||
clsid2catid = {v: k for k, v in self.dataset.catid2clsid.items()}
|
||||
for outs in results:
|
||||
batch_res = get_infer_results(outs, clsid2catid)
|
||||
start = 0
|
||||
for i, im_id in enumerate(outs['im_id']):
|
||||
bbox_num = outs['bbox_num']
|
||||
end = start + bbox_num[i]
|
||||
bbox_res = batch_res['bbox'][start:end] \
|
||||
if 'bbox' in batch_res else None
|
||||
if bbox_res:
|
||||
proposals += bbox_res
|
||||
logger.info("save proposals in {}".format(self.cfg.proposals_path))
|
||||
with open(self.cfg.proposals_path, 'w') as f:
|
||||
json.dump(proposals, f)
|
||||
|
||||
|
||||
class SemiLogPrinter(LogPrinter):
|
||||
def __init__(self, model):
|
||||
super(SemiLogPrinter, self).__init__(model)
|
||||
|
||||
def on_step_end(self, status):
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
mode = status['mode']
|
||||
if mode == 'train':
|
||||
epoch_id = status['epoch_id']
|
||||
step_id = status['step_id']
|
||||
iter_id = status['iter_id']
|
||||
steps_per_epoch = status['steps_per_epoch']
|
||||
training_staus = status['training_staus']
|
||||
batch_time = status['batch_time']
|
||||
data_time = status['data_time']
|
||||
|
||||
epoches = self.model.cfg.epoch
|
||||
batch_size = self.model.cfg['{}Reader'.format(mode.capitalize(
|
||||
))]['batch_size']
|
||||
iters = epoches * steps_per_epoch
|
||||
logs = training_staus.log()
|
||||
iter_space_fmt = ':' + str(len(str(iters))) + 'd'
|
||||
space_fmt = ':' + str(len(str(iters))) + 'd'
|
||||
if step_id % self.model.cfg.log_iter == 0:
|
||||
eta_steps = (epoches - epoch_id) * steps_per_epoch - step_id
|
||||
eta_sec = eta_steps * batch_time.global_avg
|
||||
eta_str = str(datetime.timedelta(seconds=int(eta_sec)))
|
||||
ips = float(batch_size) / batch_time.avg
|
||||
fmt = ' '.join([
|
||||
'{' + iter_space_fmt + '}/{} iters',
|
||||
'Epoch: [{}]',
|
||||
'[{' + space_fmt + '}/{}]',
|
||||
'learning_rate: {lr:.6f}',
|
||||
'{meters}',
|
||||
'eta: {eta}',
|
||||
'batch_cost: {btime}',
|
||||
'data_cost: {dtime}',
|
||||
'ips: {ips:.4f} images/s',
|
||||
])
|
||||
fmt = fmt.format(
|
||||
iter_id,
|
||||
iters,
|
||||
epoch_id,
|
||||
step_id,
|
||||
steps_per_epoch,
|
||||
lr=status['learning_rate'],
|
||||
meters=logs,
|
||||
eta=eta_str,
|
||||
btime=str(batch_time),
|
||||
dtime=str(data_time),
|
||||
ips=ips)
|
||||
logger.info(fmt)
|
||||
if mode == 'eval':
|
||||
step_id = status['step_id']
|
||||
if step_id % 100 == 0:
|
||||
logger.info("Eval iter: {}".format(step_id))
|
||||
|
||||
|
||||
class SemiCheckpointer(Checkpointer):
|
||||
def __init__(self, model):
|
||||
super(SemiCheckpointer, self).__init__(model)
|
||||
cfg = self.model.cfg
|
||||
self.best_ap = 0.
|
||||
self.save_dir = os.path.join(self.model.cfg.save_dir,
|
||||
self.model.cfg.filename)
|
||||
if hasattr(self.model.model, 'student') and hasattr(self.model.model,
|
||||
'teacher'):
|
||||
self.weight = (self.model.model.teacher, self.model.model.student)
|
||||
elif hasattr(self.model.model, 'student') or hasattr(self.model.model,
|
||||
'teacher'):
|
||||
raise AttributeError(
|
||||
"model has no attribute 'student' or 'teacher'")
|
||||
else:
|
||||
raise AttributeError(
|
||||
"model has no attribute 'student' and 'teacher'")
|
||||
|
||||
def every_n_iters(self, iter_id, n):
|
||||
return (iter_id + 1) % n == 0 if n > 0 else False
|
||||
|
||||
def on_step_end(self, status):
|
||||
# Checkpointer only performed during training
|
||||
mode = status['mode']
|
||||
eval_interval = status['eval_interval']
|
||||
save_interval = status['save_interval']
|
||||
iter_id = status['iter_id']
|
||||
epoch_id = status['epoch_id']
|
||||
t_weight = None
|
||||
s_weight = None
|
||||
save_name = None
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if self.every_n_iters(iter_id, save_interval) and mode == 'train':
|
||||
save_name = "last_epoch"
|
||||
# save_name = str(iter_id + 1)
|
||||
t_weight = self.weight[0].state_dict()
|
||||
s_weight = self.weight[1].state_dict()
|
||||
save_semi_model(t_weight, s_weight, self.model.optimizer,
|
||||
self.save_dir, save_name, epoch_id + 1,
|
||||
iter_id + 1)
|
||||
|
||||
def on_epoch_end(self, status):
|
||||
# Checkpointer only performed during training
|
||||
mode = status['mode']
|
||||
eval_interval = status['eval_interval']
|
||||
save_interval = status['save_interval']
|
||||
iter_id = status['iter_id']
|
||||
epoch_id = status['epoch_id']
|
||||
t_weight = None
|
||||
s_weight = None
|
||||
save_name = None
|
||||
if dist.get_world_size() < 2 or dist.get_rank() == 0:
|
||||
if self.every_n_iters(iter_id, eval_interval) and mode == 'eval':
|
||||
if 'save_best_model' in status and status['save_best_model']:
|
||||
for metric in self.model._metrics:
|
||||
map_res = metric.get_results()
|
||||
if 'bbox' in map_res:
|
||||
key = 'bbox'
|
||||
elif 'keypoint' in map_res:
|
||||
key = 'keypoint'
|
||||
else:
|
||||
key = 'mask'
|
||||
if key not in map_res:
|
||||
logger.warning("Evaluation results empty, this may be due to " \
|
||||
"training iterations being too few or not " \
|
||||
"loading the correct weights.")
|
||||
return
|
||||
if map_res[key][0] > self.best_ap:
|
||||
self.best_ap = map_res[key][0]
|
||||
save_name = 'best_model'
|
||||
t_weight = self.weight[0].state_dict()
|
||||
s_weight = self.weight[1].state_dict()
|
||||
logger.info("Best teacher test {} ap is {:0.3f}.".
|
||||
format(key, self.best_ap))
|
||||
if t_weight and s_weight:
|
||||
save_semi_model(t_weight, s_weight,
|
||||
self.model.optimizer, self.save_dir,
|
||||
save_name, epoch_id + 1, iter_id + 1)
|
||||
50
paddle_detection/ppdet/engine/env.py
Normal file
50
paddle_detection/ppdet/engine/env.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# Copyright (c) 2020 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 os
|
||||
import random
|
||||
import numpy as np
|
||||
|
||||
import paddle
|
||||
from paddle.distributed import fleet
|
||||
|
||||
__all__ = ['init_parallel_env', 'set_random_seed', 'init_fleet_env']
|
||||
|
||||
|
||||
def init_fleet_env(find_unused_parameters=False):
|
||||
strategy = fleet.DistributedStrategy()
|
||||
strategy.find_unused_parameters = find_unused_parameters
|
||||
fleet.init(is_collective=True, strategy=strategy)
|
||||
|
||||
|
||||
def init_parallel_env():
|
||||
env = os.environ
|
||||
dist = 'PADDLE_TRAINER_ID' in env and 'PADDLE_TRAINERS_NUM' in env
|
||||
if dist:
|
||||
trainer_id = int(env['PADDLE_TRAINER_ID'])
|
||||
local_seed = (99 + trainer_id)
|
||||
random.seed(local_seed)
|
||||
np.random.seed(local_seed)
|
||||
|
||||
paddle.distributed.init_parallel_env()
|
||||
|
||||
|
||||
def set_random_seed(seed):
|
||||
paddle.seed(seed)
|
||||
random.seed(seed)
|
||||
np.random.seed(seed)
|
||||
373
paddle_detection/ppdet/engine/export_utils.py
Normal file
373
paddle_detection/ppdet/engine/export_utils.py
Normal file
@@ -0,0 +1,373 @@
|
||||
# Copyright (c) 2020 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 os
|
||||
import yaml
|
||||
from collections import OrderedDict
|
||||
|
||||
import paddle
|
||||
from ppdet.data.source.category import get_categories
|
||||
|
||||
from ppdet.utils.logger import setup_logger
|
||||
logger = setup_logger('ppdet.engine')
|
||||
|
||||
# Global dictionary
|
||||
TRT_MIN_SUBGRAPH = {
|
||||
'YOLO': 3,
|
||||
'PPYOLOE': 3,
|
||||
'SSD': 60,
|
||||
'RCNN': 40,
|
||||
'RetinaNet': 40,
|
||||
'S2ANet': 80,
|
||||
'EfficientDet': 40,
|
||||
'Face': 3,
|
||||
'TTFNet': 60,
|
||||
'FCOS': 16,
|
||||
'SOLOv2': 60,
|
||||
'HigherHRNet': 3,
|
||||
'HRNet': 3,
|
||||
'DeepSORT': 3,
|
||||
'ByteTrack': 10,
|
||||
'CenterTrack': 5,
|
||||
'JDE': 10,
|
||||
'FairMOT': 5,
|
||||
'GFL': 16,
|
||||
'PicoDet': 3,
|
||||
'CenterNet': 5,
|
||||
'TOOD': 5,
|
||||
'YOLOX': 8,
|
||||
'YOLOF': 40,
|
||||
'METRO_Body': 3,
|
||||
'DETR': 3,
|
||||
'CLRNet': 3
|
||||
}
|
||||
|
||||
KEYPOINT_ARCH = ['HigherHRNet', 'TopDownHRNet']
|
||||
MOT_ARCH = ['JDE', 'FairMOT', 'DeepSORT', 'ByteTrack', 'CenterTrack']
|
||||
LANE_ARCH = ['CLRNet']
|
||||
|
||||
TO_STATIC_SPEC = {
|
||||
'yolov3_darknet53_270e_coco': [{
|
||||
'im_id': paddle.static.InputSpec(
|
||||
name='im_id', shape=[-1, 1], dtype='float32'),
|
||||
'is_crowd': paddle.static.InputSpec(
|
||||
name='is_crowd', shape=[-1, 50], dtype='float32'),
|
||||
'gt_bbox': paddle.static.InputSpec(
|
||||
name='gt_bbox', shape=[-1, 50, 4], dtype='float32'),
|
||||
'curr_iter': paddle.static.InputSpec(
|
||||
name='curr_iter', shape=[-1], dtype='float32'),
|
||||
'image': paddle.static.InputSpec(
|
||||
name='image', shape=[-1, 3, -1, -1], dtype='float32'),
|
||||
'im_shape': paddle.static.InputSpec(
|
||||
name='im_shape', shape=[-1, 2], dtype='float32'),
|
||||
'scale_factor': paddle.static.InputSpec(
|
||||
name='scale_factor', shape=[-1, 2], dtype='float32'),
|
||||
'target0': paddle.static.InputSpec(
|
||||
name='target0', shape=[-1, 3, 86, -1, -1], dtype='float32'),
|
||||
'target1': paddle.static.InputSpec(
|
||||
name='target1', shape=[-1, 3, 86, -1, -1], dtype='float32'),
|
||||
'target2': paddle.static.InputSpec(
|
||||
name='target2', shape=[-1, 3, 86, -1, -1], dtype='float32'),
|
||||
}],
|
||||
'tinypose_128x96': [{
|
||||
'center': paddle.static.InputSpec(
|
||||
name='center', shape=[-1, 2], dtype='float32'),
|
||||
'scale': paddle.static.InputSpec(
|
||||
name='scale', shape=[-1, 2], dtype='float32'),
|
||||
'im_id': paddle.static.InputSpec(
|
||||
name='im_id', shape=[-1, 1], dtype='float32'),
|
||||
'image': paddle.static.InputSpec(
|
||||
name='image', shape=[-1, 3, 128, 96], dtype='float32'),
|
||||
'score': paddle.static.InputSpec(
|
||||
name='score', shape=[-1], dtype='float32'),
|
||||
'rotate': paddle.static.InputSpec(
|
||||
name='rotate', shape=[-1], dtype='float32'),
|
||||
'target': paddle.static.InputSpec(
|
||||
name='target', shape=[-1, 17, 32, 24], dtype='float32'),
|
||||
'target_weight': paddle.static.InputSpec(
|
||||
name='target_weight', shape=[-1, 17, 1], dtype='float32'),
|
||||
}],
|
||||
'fcos_r50_fpn_1x_coco': [{
|
||||
'im_id': paddle.static.InputSpec(
|
||||
name='im_id', shape=[-1, 1], dtype='float32'),
|
||||
'curr_iter': paddle.static.InputSpec(
|
||||
name='curr_iter', shape=[-1], dtype='float32'),
|
||||
'image': paddle.static.InputSpec(
|
||||
name='image', shape=[-1, 3, -1, -1], dtype='float32'),
|
||||
'im_shape': paddle.static.InputSpec(
|
||||
name='im_shape', shape=[-1, 2], dtype='float32'),
|
||||
'scale_factor': paddle.static.InputSpec(
|
||||
name='scale_factor', shape=[-1, 2], dtype='float32'),
|
||||
'reg_target0': paddle.static.InputSpec(
|
||||
name='reg_target0', shape=[-1, 160, 160, 4], dtype='float32'),
|
||||
'labels0': paddle.static.InputSpec(
|
||||
name='labels0', shape=[-1, 160, 160, 1], dtype='int32'),
|
||||
'centerness0': paddle.static.InputSpec(
|
||||
name='centerness0', shape=[-1, 160, 160, 1], dtype='float32'),
|
||||
'reg_target1': paddle.static.InputSpec(
|
||||
name='reg_target1', shape=[-1, 80, 80, 4], dtype='float32'),
|
||||
'labels1': paddle.static.InputSpec(
|
||||
name='labels1', shape=[-1, 80, 80, 1], dtype='int32'),
|
||||
'centerness1': paddle.static.InputSpec(
|
||||
name='centerness1', shape=[-1, 80, 80, 1], dtype='float32'),
|
||||
'reg_target2': paddle.static.InputSpec(
|
||||
name='reg_target2', shape=[-1, 40, 40, 4], dtype='float32'),
|
||||
'labels2': paddle.static.InputSpec(
|
||||
name='labels2', shape=[-1, 40, 40, 1], dtype='int32'),
|
||||
'centerness2': paddle.static.InputSpec(
|
||||
name='centerness2', shape=[-1, 40, 40, 1], dtype='float32'),
|
||||
'reg_target3': paddle.static.InputSpec(
|
||||
name='reg_target3', shape=[-1, 20, 20, 4], dtype='float32'),
|
||||
'labels3': paddle.static.InputSpec(
|
||||
name='labels3', shape=[-1, 20, 20, 1], dtype='int32'),
|
||||
'centerness3': paddle.static.InputSpec(
|
||||
name='centerness3', shape=[-1, 20, 20, 1], dtype='float32'),
|
||||
'reg_target4': paddle.static.InputSpec(
|
||||
name='reg_target4', shape=[-1, 10, 10, 4], dtype='float32'),
|
||||
'labels4': paddle.static.InputSpec(
|
||||
name='labels4', shape=[-1, 10, 10, 1], dtype='int32'),
|
||||
'centerness4': paddle.static.InputSpec(
|
||||
name='centerness4', shape=[-1, 10, 10, 1], dtype='float32'),
|
||||
}],
|
||||
'picodet_s_320_coco_lcnet': [{
|
||||
'im_id': paddle.static.InputSpec(
|
||||
name='im_id', shape=[-1, 1], dtype='float32'),
|
||||
'is_crowd': paddle.static.InputSpec(
|
||||
name='is_crowd', shape=[-1, -1, 1], dtype='float32'),
|
||||
'gt_class': paddle.static.InputSpec(
|
||||
name='gt_class', shape=[-1, -1, 1], dtype='int32'),
|
||||
'gt_bbox': paddle.static.InputSpec(
|
||||
name='gt_bbox', shape=[-1, -1, 4], dtype='float32'),
|
||||
'curr_iter': paddle.static.InputSpec(
|
||||
name='curr_iter', shape=[-1], dtype='float32'),
|
||||
'image': paddle.static.InputSpec(
|
||||
name='image', shape=[-1, 3, -1, -1], dtype='float32'),
|
||||
'im_shape': paddle.static.InputSpec(
|
||||
name='im_shape', shape=[-1, 2], dtype='float32'),
|
||||
'scale_factor': paddle.static.InputSpec(
|
||||
name='scale_factor', shape=[-1, 2], dtype='float32'),
|
||||
'pad_gt_mask': paddle.static.InputSpec(
|
||||
name='pad_gt_mask', shape=[-1, -1, 1], dtype='float32'),
|
||||
}],
|
||||
'ppyoloe_crn_s_300e_coco': [{
|
||||
'im_id': paddle.static.InputSpec(
|
||||
name='im_id', shape=[-1, 1], dtype='float32'),
|
||||
'is_crowd': paddle.static.InputSpec(
|
||||
name='is_crowd', shape=[-1, -1, 1], dtype='float32'),
|
||||
'gt_class': paddle.static.InputSpec(
|
||||
name='gt_class', shape=[-1, -1, 1], dtype='int32'),
|
||||
'gt_bbox': paddle.static.InputSpec(
|
||||
name='gt_bbox', shape=[-1, -1, 4], dtype='float32'),
|
||||
'curr_iter': paddle.static.InputSpec(
|
||||
name='curr_iter', shape=[-1], dtype='float32'),
|
||||
'image': paddle.static.InputSpec(
|
||||
name='image', shape=[-1, 3, -1, -1], dtype='float32'),
|
||||
'im_shape': paddle.static.InputSpec(
|
||||
name='im_shape', shape=[-1, 2], dtype='float32'),
|
||||
'scale_factor': paddle.static.InputSpec(
|
||||
name='scale_factor', shape=[-1, 2], dtype='float32'),
|
||||
'pad_gt_mask': paddle.static.InputSpec(
|
||||
name='pad_gt_mask', shape=[-1, -1, 1], dtype='float32'),
|
||||
}],
|
||||
}
|
||||
|
||||
|
||||
def apply_to_static(config, model):
|
||||
filename = config.get('filename', None)
|
||||
spec = TO_STATIC_SPEC.get(filename, None)
|
||||
model = paddle.jit.to_static(model, input_spec=spec)
|
||||
logger.info("Successfully to apply @to_static with specs: {}".format(spec))
|
||||
return model
|
||||
|
||||
|
||||
def _prune_input_spec(input_spec, program, targets):
|
||||
# try to prune static program to figure out pruned input spec
|
||||
# so we perform following operations in static mode
|
||||
device = paddle.get_device()
|
||||
paddle.enable_static()
|
||||
paddle.set_device(device)
|
||||
pruned_input_spec = [{}]
|
||||
program = program.clone()
|
||||
program = program._prune(targets=targets)
|
||||
global_block = program.global_block()
|
||||
for name, spec in input_spec[0].items():
|
||||
try:
|
||||
v = global_block.var(name)
|
||||
pruned_input_spec[0][name] = spec
|
||||
except Exception:
|
||||
pass
|
||||
paddle.disable_static(place=device)
|
||||
return pruned_input_spec
|
||||
|
||||
|
||||
def _parse_reader(reader_cfg, dataset_cfg, metric, arch, image_shape):
|
||||
preprocess_list = []
|
||||
label_list = []
|
||||
if arch != "lane_arch":
|
||||
anno_file = dataset_cfg.get_anno()
|
||||
|
||||
clsid2catid, catid2name = get_categories(metric, anno_file, arch)
|
||||
|
||||
label_list = [str(cat) for cat in catid2name.values()]
|
||||
|
||||
fuse_normalize = reader_cfg.get('fuse_normalize', False)
|
||||
sample_transforms = reader_cfg['sample_transforms']
|
||||
for st in sample_transforms[1:]:
|
||||
for key, value in st.items():
|
||||
p = {'type': key}
|
||||
if key == 'Resize':
|
||||
if int(image_shape[1]) != -1:
|
||||
value['target_size'] = image_shape[1:]
|
||||
value['interp'] = value.get('interp', 1) # cv2.INTER_LINEAR
|
||||
if fuse_normalize and key == 'NormalizeImage':
|
||||
continue
|
||||
p.update(value)
|
||||
preprocess_list.append(p)
|
||||
batch_transforms = reader_cfg.get('batch_transforms', None)
|
||||
if batch_transforms:
|
||||
for bt in batch_transforms:
|
||||
for key, value in bt.items():
|
||||
# for deploy/infer, use PadStride(stride) instead PadBatch(pad_to_stride)
|
||||
if key == 'PadBatch':
|
||||
preprocess_list.append({
|
||||
'type': 'PadStride',
|
||||
'stride': value['pad_to_stride']
|
||||
})
|
||||
break
|
||||
elif key == "CULaneResize":
|
||||
# cut and resize
|
||||
p = {'type': key}
|
||||
p.update(value)
|
||||
p.update({"cut_height": dataset_cfg.cut_height})
|
||||
preprocess_list.append(p)
|
||||
break
|
||||
|
||||
return preprocess_list, label_list
|
||||
|
||||
|
||||
def _parse_tracker(tracker_cfg):
|
||||
tracker_params = {}
|
||||
for k, v in tracker_cfg.items():
|
||||
tracker_params.update({k: v})
|
||||
return tracker_params
|
||||
|
||||
|
||||
def _dump_infer_config(config, path, image_shape, model):
|
||||
arch_state = False
|
||||
from ppdet.core.config.yaml_helpers import setup_orderdict
|
||||
setup_orderdict()
|
||||
use_dynamic_shape = True if image_shape[2] == -1 else False
|
||||
infer_cfg = OrderedDict({
|
||||
'mode': 'paddle',
|
||||
'draw_threshold': 0.5,
|
||||
'metric': config['metric'],
|
||||
'use_dynamic_shape': use_dynamic_shape
|
||||
})
|
||||
export_onnx = config.get('export_onnx', False)
|
||||
export_eb = config.get('export_eb', False)
|
||||
|
||||
infer_arch = config['architecture']
|
||||
if 'RCNN' in infer_arch and export_onnx:
|
||||
logger.warning(
|
||||
"Exporting RCNN model to ONNX only support batch_size = 1")
|
||||
infer_cfg['export_onnx'] = True
|
||||
infer_cfg['export_eb'] = export_eb
|
||||
|
||||
if infer_arch in MOT_ARCH:
|
||||
if infer_arch == 'DeepSORT':
|
||||
tracker_cfg = config['DeepSORTTracker']
|
||||
elif infer_arch == 'CenterTrack':
|
||||
tracker_cfg = config['CenterTracker']
|
||||
else:
|
||||
tracker_cfg = config['JDETracker']
|
||||
infer_cfg['tracker'] = _parse_tracker(tracker_cfg)
|
||||
|
||||
for arch, min_subgraph_size in TRT_MIN_SUBGRAPH.items():
|
||||
if arch in infer_arch:
|
||||
infer_cfg['arch'] = arch
|
||||
infer_cfg['min_subgraph_size'] = min_subgraph_size
|
||||
arch_state = True
|
||||
break
|
||||
|
||||
if infer_arch == 'PPYOLOEWithAuxHead':
|
||||
infer_arch = 'PPYOLOE'
|
||||
|
||||
if infer_arch in ['PPYOLOE', 'YOLOX', 'YOLOF']:
|
||||
infer_cfg['arch'] = infer_arch
|
||||
infer_cfg['min_subgraph_size'] = TRT_MIN_SUBGRAPH[infer_arch]
|
||||
arch_state = True
|
||||
|
||||
if not arch_state:
|
||||
logger.error(
|
||||
'Architecture: {} is not supported for exporting model now.\n'.
|
||||
format(infer_arch) +
|
||||
'Please set TRT_MIN_SUBGRAPH in ppdet/engine/export_utils.py')
|
||||
os._exit(0)
|
||||
if 'mask_head' in config[config['architecture']] and config[config[
|
||||
'architecture']]['mask_head']:
|
||||
infer_cfg['mask'] = True
|
||||
label_arch = 'detection_arch'
|
||||
if infer_arch in KEYPOINT_ARCH:
|
||||
label_arch = 'keypoint_arch'
|
||||
|
||||
if infer_arch in LANE_ARCH:
|
||||
infer_cfg['arch'] = infer_arch
|
||||
infer_cfg['min_subgraph_size'] = TRT_MIN_SUBGRAPH[infer_arch]
|
||||
infer_cfg['img_w'] = config['img_w']
|
||||
infer_cfg['ori_img_h'] = config['ori_img_h']
|
||||
infer_cfg['cut_height'] = config['cut_height']
|
||||
label_arch = 'lane_arch'
|
||||
head_name = "CLRHead"
|
||||
infer_cfg['conf_threshold'] = config[head_name]['conf_threshold']
|
||||
infer_cfg['nms_thres'] = config[head_name]['nms_thres']
|
||||
infer_cfg['max_lanes'] = config[head_name]['max_lanes']
|
||||
infer_cfg['num_points'] = config[head_name]['num_points']
|
||||
arch_state = True
|
||||
|
||||
if infer_arch in MOT_ARCH:
|
||||
if config['metric'] in ['COCO', 'VOC']:
|
||||
# MOT model run as Detector
|
||||
reader_cfg = config['TestReader']
|
||||
dataset_cfg = config['TestDataset']
|
||||
else:
|
||||
# 'metric' in ['MOT', 'MCMOT', 'KITTI']
|
||||
label_arch = 'mot_arch'
|
||||
reader_cfg = config['TestMOTReader']
|
||||
dataset_cfg = config['TestMOTDataset']
|
||||
else:
|
||||
reader_cfg = config['TestReader']
|
||||
dataset_cfg = config['TestDataset']
|
||||
|
||||
infer_cfg['Preprocess'], infer_cfg['label_list'] = _parse_reader(
|
||||
reader_cfg, dataset_cfg, config['metric'], label_arch, image_shape[1:])
|
||||
|
||||
if infer_arch == 'PicoDet':
|
||||
if hasattr(config, 'export') and config['export'].get(
|
||||
'post_process',
|
||||
False) and not config['export'].get('benchmark', False):
|
||||
infer_cfg['arch'] = 'GFL'
|
||||
head_name = 'PicoHeadV2' if config['PicoHeadV2'] else 'PicoHead'
|
||||
infer_cfg['NMS'] = config[head_name]['nms']
|
||||
# In order to speed up the prediction, the threshold of nms
|
||||
# is adjusted here, which can be changed in infer_cfg.yml
|
||||
config[head_name]['nms']["score_threshold"] = 0.3
|
||||
config[head_name]['nms']["nms_threshold"] = 0.5
|
||||
infer_cfg['fpn_stride'] = config[head_name]['fpn_stride']
|
||||
|
||||
yaml.dump(infer_cfg, open(path, 'w'))
|
||||
logger.info("Export inference config file to {}".format(os.path.join(path)))
|
||||
731
paddle_detection/ppdet/engine/tracker.py
Normal file
731
paddle_detection/ppdet/engine/tracker.py
Normal file
@@ -0,0 +1,731 @@
|
||||
# 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 os
|
||||
import glob
|
||||
import re
|
||||
import paddle
|
||||
import paddle.nn as nn
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
from collections import defaultdict
|
||||
|
||||
from ppdet.core.workspace import create
|
||||
from ppdet.utils.checkpoint import load_weight, load_pretrain_weight
|
||||
from ppdet.modeling.mot.utils import Detection, get_crops, scale_coords, clip_box
|
||||
from ppdet.modeling.mot.utils import MOTTimer, load_det_results, write_mot_results, save_vis_results
|
||||
from ppdet.modeling.mot.tracker import JDETracker, CenterTracker
|
||||
from ppdet.modeling.mot.tracker import DeepSORTTracker, OCSORTTracker, BOTSORTTracker
|
||||
from ppdet.modeling.architectures import YOLOX
|
||||
from ppdet.metrics import Metric, MOTMetric, KITTIMOTMetric, MCMOTMetric
|
||||
from ppdet.data.source.category import get_categories
|
||||
import ppdet.utils.stats as stats
|
||||
|
||||
from .callbacks import Callback, ComposeCallback
|
||||
|
||||
from ppdet.utils.logger import setup_logger
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
MOT_ARCH = ['JDE', 'FairMOT', 'DeepSORT', 'ByteTrack', 'CenterTrack']
|
||||
MOT_ARCH_JDE = MOT_ARCH[:2]
|
||||
MOT_ARCH_SDE = MOT_ARCH[2:4]
|
||||
MOT_DATA_TYPE = ['mot', 'mcmot', 'kitti']
|
||||
|
||||
__all__ = ['Tracker']
|
||||
|
||||
|
||||
class Tracker(object):
|
||||
def __init__(self, cfg, mode='eval'):
|
||||
self.cfg = cfg
|
||||
assert mode.lower() in ['test', 'eval'], \
|
||||
"mode should be 'test' or 'eval'"
|
||||
self.mode = mode.lower()
|
||||
self.optimizer = None
|
||||
|
||||
# build MOT data loader
|
||||
self.dataset = cfg['{}MOTDataset'.format(self.mode.capitalize())]
|
||||
|
||||
# build model
|
||||
self.model = create(cfg.architecture)
|
||||
|
||||
if isinstance(self.model.detector, YOLOX):
|
||||
for k, m in self.model.named_sublayers():
|
||||
if isinstance(m, nn.BatchNorm2D):
|
||||
m._epsilon = 1e-3 # for amp(fp16)
|
||||
m._momentum = 0.97 # 0.03 in pytorch
|
||||
|
||||
anno_file = self.dataset.get_anno()
|
||||
clsid2catid, catid2name = get_categories(
|
||||
self.cfg.metric, anno_file=anno_file)
|
||||
self.ids2names = []
|
||||
for k, v in catid2name.items():
|
||||
self.ids2names.append(v)
|
||||
|
||||
self.status = {}
|
||||
self.start_epoch = 0
|
||||
|
||||
# initial default callbacks
|
||||
self._init_callbacks()
|
||||
|
||||
# initial default metrics
|
||||
self._init_metrics()
|
||||
self._reset_metrics()
|
||||
|
||||
def _init_callbacks(self):
|
||||
self._callbacks = []
|
||||
self._compose_callback = None
|
||||
|
||||
def _init_metrics(self):
|
||||
if self.mode in ['test']:
|
||||
self._metrics = []
|
||||
return
|
||||
|
||||
if self.cfg.metric == 'MOT':
|
||||
self._metrics = [MOTMetric(), ]
|
||||
elif self.cfg.metric == 'MCMOT':
|
||||
self._metrics = [MCMOTMetric(self.cfg.num_classes), ]
|
||||
elif self.cfg.metric == 'KITTI':
|
||||
self._metrics = [KITTIMOTMetric(), ]
|
||||
else:
|
||||
logger.warning("Metric not support for metric type {}".format(
|
||||
self.cfg.metric))
|
||||
self._metrics = []
|
||||
|
||||
def _reset_metrics(self):
|
||||
for metric in self._metrics:
|
||||
metric.reset()
|
||||
|
||||
def register_callbacks(self, callbacks):
|
||||
callbacks = [h for h in list(callbacks) if h is not None]
|
||||
for c in callbacks:
|
||||
assert isinstance(c, Callback), \
|
||||
"metrics shoule be instances of subclass of Metric"
|
||||
self._callbacks.extend(callbacks)
|
||||
self._compose_callback = ComposeCallback(self._callbacks)
|
||||
|
||||
def register_metrics(self, metrics):
|
||||
metrics = [m for m in list(metrics) if m is not None]
|
||||
for m in metrics:
|
||||
assert isinstance(m, Metric), \
|
||||
"metrics shoule be instances of subclass of Metric"
|
||||
self._metrics.extend(metrics)
|
||||
|
||||
def load_weights_jde(self, weights):
|
||||
load_weight(self.model, weights, self.optimizer)
|
||||
|
||||
def load_weights_sde(self, det_weights, reid_weights):
|
||||
with_detector = self.model.detector is not None
|
||||
with_reid = self.model.reid is not None
|
||||
|
||||
if with_detector:
|
||||
load_weight(self.model.detector, det_weights)
|
||||
if with_reid:
|
||||
load_weight(self.model.reid, reid_weights)
|
||||
else:
|
||||
load_weight(self.model.reid, reid_weights)
|
||||
|
||||
def _eval_seq_centertrack(self,
|
||||
dataloader,
|
||||
save_dir=None,
|
||||
show_image=False,
|
||||
frame_rate=30,
|
||||
draw_threshold=0):
|
||||
assert isinstance(self.model.tracker, CenterTracker)
|
||||
if save_dir:
|
||||
if not os.path.exists(save_dir): os.makedirs(save_dir)
|
||||
tracker = self.model.tracker
|
||||
|
||||
timer = MOTTimer()
|
||||
frame_id = 0
|
||||
self.status['mode'] = 'track'
|
||||
self.model.eval()
|
||||
results = defaultdict(list) # only support single class now
|
||||
|
||||
for step_id, data in enumerate(tqdm(dataloader)):
|
||||
self.status['step_id'] = step_id
|
||||
if step_id == 0:
|
||||
self.model.reset_tracking()
|
||||
|
||||
# forward
|
||||
timer.tic()
|
||||
pred_ret = self.model(data)
|
||||
|
||||
online_targets = tracker.update(pred_ret)
|
||||
online_tlwhs, online_scores, online_ids = [], [], []
|
||||
for t in online_targets:
|
||||
bbox = t['bbox']
|
||||
tlwh = [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]]
|
||||
tscore = float(t['score'])
|
||||
tid = int(t['tracking_id'])
|
||||
if tlwh[2] * tlwh[3] > 0:
|
||||
online_tlwhs.append(tlwh)
|
||||
online_ids.append(tid)
|
||||
online_scores.append(tscore)
|
||||
timer.toc()
|
||||
# save results
|
||||
results[0].append(
|
||||
(frame_id + 1, online_tlwhs, online_scores, online_ids))
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
frame_id += 1
|
||||
return results, frame_id, timer.average_time, timer.calls
|
||||
|
||||
def _eval_seq_jde(self,
|
||||
dataloader,
|
||||
save_dir=None,
|
||||
show_image=False,
|
||||
frame_rate=30,
|
||||
draw_threshold=0):
|
||||
if save_dir:
|
||||
if not os.path.exists(save_dir): os.makedirs(save_dir)
|
||||
tracker = self.model.tracker
|
||||
tracker.max_time_lost = int(frame_rate / 30.0 * tracker.track_buffer)
|
||||
|
||||
timer = MOTTimer()
|
||||
frame_id = 0
|
||||
self.status['mode'] = 'track'
|
||||
self.model.eval()
|
||||
results = defaultdict(list) # support single class and multi classes
|
||||
|
||||
for step_id, data in enumerate(tqdm(dataloader)):
|
||||
self.status['step_id'] = step_id
|
||||
# forward
|
||||
timer.tic()
|
||||
pred_dets, pred_embs = self.model(data)
|
||||
|
||||
pred_dets, pred_embs = pred_dets.numpy(), pred_embs.numpy()
|
||||
online_targets_dict = self.model.tracker.update(pred_dets,
|
||||
pred_embs)
|
||||
online_tlwhs = defaultdict(list)
|
||||
online_scores = defaultdict(list)
|
||||
online_ids = defaultdict(list)
|
||||
for cls_id in range(self.cfg.num_classes):
|
||||
online_targets = online_targets_dict[cls_id]
|
||||
for t in online_targets:
|
||||
tlwh = t.tlwh
|
||||
tid = t.track_id
|
||||
tscore = t.score
|
||||
if tlwh[2] * tlwh[3] <= tracker.min_box_area: continue
|
||||
if tracker.vertical_ratio > 0 and tlwh[2] / tlwh[
|
||||
3] > tracker.vertical_ratio:
|
||||
continue
|
||||
online_tlwhs[cls_id].append(tlwh)
|
||||
online_ids[cls_id].append(tid)
|
||||
online_scores[cls_id].append(tscore)
|
||||
# save results
|
||||
results[cls_id].append(
|
||||
(frame_id + 1, online_tlwhs[cls_id], online_scores[cls_id],
|
||||
online_ids[cls_id]))
|
||||
|
||||
timer.toc()
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
frame_id += 1
|
||||
|
||||
return results, frame_id, timer.average_time, timer.calls
|
||||
|
||||
def _eval_seq_sde(self,
|
||||
dataloader,
|
||||
save_dir=None,
|
||||
show_image=False,
|
||||
frame_rate=30,
|
||||
seq_name='',
|
||||
scaled=False,
|
||||
det_file='',
|
||||
draw_threshold=0):
|
||||
if save_dir:
|
||||
if not os.path.exists(save_dir): os.makedirs(save_dir)
|
||||
use_detector = False if not self.model.detector else True
|
||||
use_reid = hasattr(self.model, 'reid')
|
||||
if use_reid and self.model.reid is not None:
|
||||
use_reid = True
|
||||
else:
|
||||
use_reid = False
|
||||
|
||||
timer = MOTTimer()
|
||||
results = defaultdict(list)
|
||||
frame_id = 0
|
||||
self.status['mode'] = 'track'
|
||||
self.model.eval()
|
||||
if use_reid:
|
||||
self.model.reid.eval()
|
||||
if not use_detector:
|
||||
dets_list = load_det_results(det_file, len(dataloader))
|
||||
logger.info('Finish loading detection results file {}.'.format(
|
||||
det_file))
|
||||
|
||||
tracker = self.model.tracker
|
||||
for step_id, data in enumerate(tqdm(dataloader)):
|
||||
self.status['step_id'] = step_id
|
||||
ori_image = data['ori_image'] # [bs, H, W, 3]
|
||||
ori_image_shape = data['ori_image'].shape[1:3]
|
||||
# ori_image_shape: [H, W]
|
||||
|
||||
input_shape = data['image'].shape[2:]
|
||||
# input_shape: [h, w], before data transforms, set in model config
|
||||
|
||||
im_shape = data['im_shape'][0].numpy()
|
||||
# im_shape: [new_h, new_w], after data transforms
|
||||
scale_factor = data['scale_factor'][0].numpy()
|
||||
|
||||
empty_detections = False
|
||||
# when it has no detected bboxes, will not inference reid model
|
||||
# and if visualize, use original image instead
|
||||
|
||||
# forward
|
||||
timer.tic()
|
||||
if not use_detector:
|
||||
dets = dets_list[frame_id]
|
||||
bbox_tlwh = np.array(dets['bbox'], dtype='float32')
|
||||
if bbox_tlwh.shape[0] > 0:
|
||||
# detector outputs: pred_cls_ids, pred_scores, pred_bboxes
|
||||
pred_cls_ids = np.array(dets['cls_id'], dtype='float32')
|
||||
pred_scores = np.array(dets['score'], dtype='float32')
|
||||
pred_bboxes = np.concatenate(
|
||||
(bbox_tlwh[:, 0:2],
|
||||
bbox_tlwh[:, 2:4] + bbox_tlwh[:, 0:2]),
|
||||
axis=1)
|
||||
else:
|
||||
logger.warning(
|
||||
'Frame {} has not object, try to modify score threshold.'.
|
||||
format(frame_id))
|
||||
empty_detections = True
|
||||
else:
|
||||
outs = self.model.detector(data)
|
||||
outs['bbox'] = outs['bbox'].numpy()
|
||||
outs['bbox_num'] = outs['bbox_num'].numpy()
|
||||
|
||||
if len(outs['bbox']) > 0 and empty_detections == False:
|
||||
# detector outputs: pred_cls_ids, pred_scores, pred_bboxes
|
||||
pred_cls_ids = outs['bbox'][:, 0:1]
|
||||
pred_scores = outs['bbox'][:, 1:2]
|
||||
if not scaled:
|
||||
# Note: scaled=False only in JDE YOLOv3 or other detectors
|
||||
# with LetterBoxResize and JDEBBoxPostProcess.
|
||||
#
|
||||
# 'scaled' means whether the coords after detector outputs
|
||||
# have been scaled back to the original image, set True
|
||||
# in general detector, set False in JDE YOLOv3.
|
||||
pred_bboxes = scale_coords(outs['bbox'][:, 2:],
|
||||
input_shape, im_shape,
|
||||
scale_factor)
|
||||
else:
|
||||
pred_bboxes = outs['bbox'][:, 2:]
|
||||
pred_dets_old = np.concatenate(
|
||||
(pred_cls_ids, pred_scores, pred_bboxes), axis=1)
|
||||
else:
|
||||
logger.warning(
|
||||
'Frame {} has not detected object, try to modify score threshold.'.
|
||||
format(frame_id))
|
||||
empty_detections = True
|
||||
|
||||
if not empty_detections:
|
||||
pred_xyxys, keep_idx = clip_box(pred_bboxes, ori_image_shape)
|
||||
if len(keep_idx[0]) == 0:
|
||||
logger.warning(
|
||||
'Frame {} has not detected object left after clip_box.'.
|
||||
format(frame_id))
|
||||
empty_detections = True
|
||||
|
||||
if empty_detections:
|
||||
timer.toc()
|
||||
# if visualize, use original image instead
|
||||
online_ids, online_tlwhs, online_scores = None, None, None
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
frame_id += 1
|
||||
# thus will not inference reid model
|
||||
continue
|
||||
|
||||
pred_cls_ids = pred_cls_ids[keep_idx[0]]
|
||||
pred_scores = pred_scores[keep_idx[0]]
|
||||
pred_dets = np.concatenate(
|
||||
(pred_cls_ids, pred_scores, pred_xyxys), axis=1)
|
||||
|
||||
if use_reid:
|
||||
crops = get_crops(
|
||||
pred_xyxys,
|
||||
ori_image,
|
||||
w=tracker.input_size[0],
|
||||
h=tracker.input_size[1])
|
||||
crops = paddle.to_tensor(crops)
|
||||
|
||||
data.update({'crops': crops})
|
||||
pred_embs = self.model(data)['embeddings'].numpy()
|
||||
else:
|
||||
pred_embs = None
|
||||
|
||||
if isinstance(tracker, DeepSORTTracker):
|
||||
online_tlwhs, online_scores, online_ids = [], [], []
|
||||
tracker.predict()
|
||||
online_targets = tracker.update(pred_dets, pred_embs)
|
||||
for t in online_targets:
|
||||
if not t.is_confirmed() or t.time_since_update > 1:
|
||||
continue
|
||||
tlwh = t.to_tlwh()
|
||||
tscore = t.score
|
||||
tid = t.track_id
|
||||
if tscore < draw_threshold: continue
|
||||
if tlwh[2] * tlwh[3] <= tracker.min_box_area: continue
|
||||
if tracker.vertical_ratio > 0 and tlwh[2] / tlwh[
|
||||
3] > tracker.vertical_ratio:
|
||||
continue
|
||||
online_tlwhs.append(tlwh)
|
||||
online_scores.append(tscore)
|
||||
online_ids.append(tid)
|
||||
timer.toc()
|
||||
|
||||
# save results
|
||||
results[0].append(
|
||||
(frame_id + 1, online_tlwhs, online_scores, online_ids))
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
|
||||
elif isinstance(tracker, JDETracker):
|
||||
# trick hyperparams only used for MOTChallenge (MOT17, MOT20) Test-set
|
||||
tracker.track_buffer, tracker.conf_thres = get_trick_hyperparams(
|
||||
seq_name, tracker.track_buffer, tracker.conf_thres)
|
||||
|
||||
online_targets_dict = tracker.update(pred_dets_old, pred_embs)
|
||||
online_tlwhs = defaultdict(list)
|
||||
online_scores = defaultdict(list)
|
||||
online_ids = defaultdict(list)
|
||||
for cls_id in range(self.cfg.num_classes):
|
||||
online_targets = online_targets_dict[cls_id]
|
||||
for t in online_targets:
|
||||
tlwh = t.tlwh
|
||||
tid = t.track_id
|
||||
tscore = t.score
|
||||
if tlwh[2] * tlwh[3] <= tracker.min_box_area: continue
|
||||
if tracker.vertical_ratio > 0 and tlwh[2] / tlwh[
|
||||
3] > tracker.vertical_ratio:
|
||||
continue
|
||||
online_tlwhs[cls_id].append(tlwh)
|
||||
online_ids[cls_id].append(tid)
|
||||
online_scores[cls_id].append(tscore)
|
||||
# save results
|
||||
results[cls_id].append(
|
||||
(frame_id + 1, online_tlwhs[cls_id],
|
||||
online_scores[cls_id], online_ids[cls_id]))
|
||||
timer.toc()
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
|
||||
elif isinstance(tracker, OCSORTTracker):
|
||||
# OC_SORT Tracker
|
||||
online_targets = tracker.update(pred_dets_old, pred_embs)
|
||||
online_tlwhs = []
|
||||
online_ids = []
|
||||
online_scores = []
|
||||
for t in online_targets:
|
||||
tlwh = [t[0], t[1], t[2] - t[0], t[3] - t[1]]
|
||||
tscore = float(t[4])
|
||||
tid = int(t[5])
|
||||
if tlwh[2] * tlwh[3] > 0:
|
||||
online_tlwhs.append(tlwh)
|
||||
online_ids.append(tid)
|
||||
online_scores.append(tscore)
|
||||
timer.toc()
|
||||
# save results
|
||||
results[0].append(
|
||||
(frame_id + 1, online_tlwhs, online_scores, online_ids))
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
|
||||
elif isinstance(tracker, BOTSORTTracker):
|
||||
# BOTSORT Tracker
|
||||
online_targets = tracker.update(
|
||||
pred_dets_old, img=ori_image.numpy())
|
||||
online_tlwhs = []
|
||||
online_ids = []
|
||||
online_scores = []
|
||||
for t in online_targets:
|
||||
tlwh = t.tlwh
|
||||
tid = t.track_id
|
||||
tscore = t.score
|
||||
if tlwh[2] * tlwh[3] > 0:
|
||||
online_tlwhs.append(tlwh)
|
||||
online_ids.append(tid)
|
||||
online_scores.append(tscore)
|
||||
timer.toc()
|
||||
# save results
|
||||
results[0].append(
|
||||
(frame_id + 1, online_tlwhs, online_scores, online_ids))
|
||||
save_vis_results(data, frame_id, online_ids, online_tlwhs,
|
||||
online_scores, timer.average_time, show_image,
|
||||
save_dir, self.cfg.num_classes, self.ids2names)
|
||||
|
||||
else:
|
||||
raise ValueError(tracker)
|
||||
frame_id += 1
|
||||
|
||||
return results, frame_id, timer.average_time, timer.calls
|
||||
|
||||
def mot_evaluate(self,
|
||||
data_root,
|
||||
seqs,
|
||||
output_dir,
|
||||
data_type='mot',
|
||||
model_type='JDE',
|
||||
save_images=False,
|
||||
save_videos=False,
|
||||
show_image=False,
|
||||
scaled=False,
|
||||
det_results_dir=''):
|
||||
if not os.path.exists(output_dir): os.makedirs(output_dir)
|
||||
result_root = os.path.join(output_dir, 'mot_results')
|
||||
if not os.path.exists(result_root): os.makedirs(result_root)
|
||||
assert data_type in MOT_DATA_TYPE, \
|
||||
"data_type should be 'mot', 'mcmot' or 'kitti'"
|
||||
assert model_type in MOT_ARCH, \
|
||||
"model_type should be 'JDE', 'DeepSORT', 'FairMOT' or 'ByteTrack'"
|
||||
|
||||
# run tracking
|
||||
n_frame = 0
|
||||
timer_avgs, timer_calls = [], []
|
||||
for seq in seqs:
|
||||
infer_dir = os.path.join(data_root, seq)
|
||||
if not os.path.exists(infer_dir) or not os.path.isdir(infer_dir):
|
||||
logger.warning("Seq {} error, {} has no images.".format(
|
||||
seq, infer_dir))
|
||||
continue
|
||||
if os.path.exists(os.path.join(infer_dir, 'img1')):
|
||||
infer_dir = os.path.join(infer_dir, 'img1')
|
||||
|
||||
frame_rate = 30
|
||||
seqinfo = os.path.join(data_root, seq, 'seqinfo.ini')
|
||||
if os.path.exists(seqinfo):
|
||||
meta_info = open(seqinfo).read()
|
||||
frame_rate = int(meta_info[meta_info.find('frameRate') + 10:
|
||||
meta_info.find('\nseqLength')])
|
||||
|
||||
save_dir = os.path.join(output_dir, 'mot_outputs',
|
||||
seq) if save_images or save_videos else None
|
||||
logger.info('Evaluate seq: {}'.format(seq))
|
||||
|
||||
self.dataset.set_images(self.get_infer_images(infer_dir))
|
||||
dataloader = create('EvalMOTReader')(self.dataset, 0)
|
||||
|
||||
result_filename = os.path.join(result_root, '{}.txt'.format(seq))
|
||||
|
||||
with paddle.no_grad():
|
||||
if model_type in MOT_ARCH_JDE:
|
||||
results, nf, ta, tc = self._eval_seq_jde(
|
||||
dataloader,
|
||||
save_dir=save_dir,
|
||||
show_image=show_image,
|
||||
frame_rate=frame_rate)
|
||||
elif model_type in MOT_ARCH_SDE:
|
||||
results, nf, ta, tc = self._eval_seq_sde(
|
||||
dataloader,
|
||||
save_dir=save_dir,
|
||||
show_image=show_image,
|
||||
frame_rate=frame_rate,
|
||||
seq_name=seq,
|
||||
scaled=scaled,
|
||||
det_file=os.path.join(det_results_dir,
|
||||
'{}.txt'.format(seq)))
|
||||
elif model_type == 'CenterTrack':
|
||||
results, nf, ta, tc = self._eval_seq_centertrack(
|
||||
dataloader,
|
||||
save_dir=save_dir,
|
||||
show_image=show_image,
|
||||
frame_rate=frame_rate)
|
||||
else:
|
||||
raise ValueError(model_type)
|
||||
|
||||
write_mot_results(result_filename, results, data_type,
|
||||
self.cfg.num_classes)
|
||||
n_frame += nf
|
||||
timer_avgs.append(ta)
|
||||
timer_calls.append(tc)
|
||||
|
||||
if save_videos:
|
||||
output_video_path = os.path.join(save_dir, '..',
|
||||
'{}_vis.mp4'.format(seq))
|
||||
cmd_str = 'ffmpeg -f image2 -i {}/%05d.jpg {}'.format(
|
||||
save_dir, output_video_path)
|
||||
os.system(cmd_str)
|
||||
logger.info('Save video in {}.'.format(output_video_path))
|
||||
|
||||
# update metrics
|
||||
for metric in self._metrics:
|
||||
metric.update(data_root, seq, data_type, result_root,
|
||||
result_filename)
|
||||
|
||||
timer_avgs = np.asarray(timer_avgs)
|
||||
timer_calls = np.asarray(timer_calls)
|
||||
all_time = np.dot(timer_avgs, timer_calls)
|
||||
avg_time = all_time / np.sum(timer_calls)
|
||||
logger.info('Time elapsed: {:.2f} seconds, FPS: {:.2f}'.format(
|
||||
all_time, 1.0 / avg_time))
|
||||
|
||||
# accumulate metric to log out
|
||||
for metric in self._metrics:
|
||||
metric.accumulate()
|
||||
metric.log()
|
||||
# reset metric states for metric may performed multiple times
|
||||
self._reset_metrics()
|
||||
|
||||
def get_infer_images(self, infer_dir):
|
||||
assert infer_dir is None or os.path.isdir(infer_dir), \
|
||||
"{} is not a directory".format(infer_dir)
|
||||
images = set()
|
||||
assert os.path.isdir(infer_dir), \
|
||||
"infer_dir {} is not a directory".format(infer_dir)
|
||||
exts = ['jpg', 'jpeg', 'png', 'bmp']
|
||||
exts += [ext.upper() for ext in exts]
|
||||
for ext in exts:
|
||||
images.update(glob.glob('{}/*.{}'.format(infer_dir, ext)))
|
||||
images = list(images)
|
||||
images.sort()
|
||||
assert len(images) > 0, "no image found in {}".format(infer_dir)
|
||||
logger.info("Found {} inference images in total.".format(len(images)))
|
||||
return images
|
||||
|
||||
def mot_predict_seq(self,
|
||||
video_file,
|
||||
frame_rate,
|
||||
image_dir,
|
||||
output_dir,
|
||||
data_type='mot',
|
||||
model_type='JDE',
|
||||
save_images=False,
|
||||
save_videos=True,
|
||||
show_image=False,
|
||||
scaled=False,
|
||||
det_results_dir='',
|
||||
draw_threshold=0.5):
|
||||
assert video_file is not None or image_dir is not None, \
|
||||
"--video_file or --image_dir should be set."
|
||||
assert video_file is None or os.path.isfile(video_file), \
|
||||
"{} is not a file".format(video_file)
|
||||
assert image_dir is None or os.path.isdir(image_dir), \
|
||||
"{} is not a directory".format(image_dir)
|
||||
|
||||
if not os.path.exists(output_dir): os.makedirs(output_dir)
|
||||
result_root = os.path.join(output_dir, 'mot_results')
|
||||
if not os.path.exists(result_root): os.makedirs(result_root)
|
||||
assert data_type in MOT_DATA_TYPE, \
|
||||
"data_type should be 'mot', 'mcmot' or 'kitti'"
|
||||
assert model_type in MOT_ARCH, \
|
||||
"model_type should be 'JDE', 'DeepSORT', 'FairMOT' or 'ByteTrack'"
|
||||
|
||||
# run tracking
|
||||
if video_file:
|
||||
seq = video_file.split('/')[-1].split('.')[0]
|
||||
self.dataset.set_video(video_file, frame_rate)
|
||||
logger.info('Starting tracking video {}'.format(video_file))
|
||||
elif image_dir:
|
||||
seq = image_dir.split('/')[-1].split('.')[0]
|
||||
if os.path.exists(os.path.join(image_dir, 'img1')):
|
||||
image_dir = os.path.join(image_dir, 'img1')
|
||||
images = [
|
||||
'{}/{}'.format(image_dir, x) for x in os.listdir(image_dir)
|
||||
]
|
||||
images.sort()
|
||||
self.dataset.set_images(images)
|
||||
logger.info('Starting tracking folder {}, found {} images'.format(
|
||||
image_dir, len(images)))
|
||||
else:
|
||||
raise ValueError('--video_file or --image_dir should be set.')
|
||||
|
||||
save_dir = os.path.join(output_dir, 'mot_outputs',
|
||||
seq) if save_images or save_videos else None
|
||||
|
||||
dataloader = create('TestMOTReader')(self.dataset, 0)
|
||||
result_filename = os.path.join(result_root, '{}.txt'.format(seq))
|
||||
if frame_rate == -1:
|
||||
frame_rate = self.dataset.frame_rate
|
||||
|
||||
with paddle.no_grad():
|
||||
if model_type in MOT_ARCH_JDE:
|
||||
results, nf, ta, tc = self._eval_seq_jde(
|
||||
dataloader,
|
||||
save_dir=save_dir,
|
||||
show_image=show_image,
|
||||
frame_rate=frame_rate,
|
||||
draw_threshold=draw_threshold)
|
||||
elif model_type in MOT_ARCH_SDE:
|
||||
results, nf, ta, tc = self._eval_seq_sde(
|
||||
dataloader,
|
||||
save_dir=save_dir,
|
||||
show_image=show_image,
|
||||
frame_rate=frame_rate,
|
||||
seq_name=seq,
|
||||
scaled=scaled,
|
||||
det_file=os.path.join(det_results_dir,
|
||||
'{}.txt'.format(seq)),
|
||||
draw_threshold=draw_threshold)
|
||||
elif model_type == 'CenterTrack':
|
||||
results, nf, ta, tc = self._eval_seq_centertrack(
|
||||
dataloader,
|
||||
save_dir=save_dir,
|
||||
show_image=show_image,
|
||||
frame_rate=frame_rate)
|
||||
else:
|
||||
raise ValueError(model_type)
|
||||
|
||||
if save_videos:
|
||||
output_video_path = os.path.join(save_dir, '..',
|
||||
'{}_vis.mp4'.format(seq))
|
||||
cmd_str = 'ffmpeg -f image2 -i {}/%05d.jpg {}'.format(
|
||||
save_dir, output_video_path)
|
||||
os.system(cmd_str)
|
||||
logger.info('Save video in {}'.format(output_video_path))
|
||||
|
||||
write_mot_results(result_filename, results, data_type,
|
||||
self.cfg.num_classes)
|
||||
|
||||
|
||||
def get_trick_hyperparams(video_name, ori_buffer, ori_thresh):
|
||||
if video_name[:3] != 'MOT':
|
||||
# only used for MOTChallenge (MOT17, MOT20) Test-set
|
||||
return ori_buffer, ori_thresh
|
||||
|
||||
video_name = video_name[:8]
|
||||
if 'MOT17-05' in video_name:
|
||||
track_buffer = 14
|
||||
elif 'MOT17-13' in video_name:
|
||||
track_buffer = 25
|
||||
else:
|
||||
track_buffer = ori_buffer
|
||||
|
||||
if 'MOT17-01' in video_name:
|
||||
track_thresh = 0.65
|
||||
elif 'MOT17-06' in video_name:
|
||||
track_thresh = 0.65
|
||||
elif 'MOT17-12' in video_name:
|
||||
track_thresh = 0.7
|
||||
elif 'MOT17-14' in video_name:
|
||||
track_thresh = 0.67
|
||||
else:
|
||||
track_thresh = ori_thresh
|
||||
|
||||
if 'MOT20-06' in video_name or 'MOT20-08' in video_name:
|
||||
track_thresh = 0.3
|
||||
else:
|
||||
track_thresh = ori_thresh
|
||||
|
||||
return track_buffer, ori_thresh
|
||||
1424
paddle_detection/ppdet/engine/trainer.py
Normal file
1424
paddle_detection/ppdet/engine/trainer.py
Normal file
File diff suppressed because it is too large
Load Diff
42
paddle_detection/ppdet/engine/trainer_cot.py
Normal file
42
paddle_detection/ppdet/engine/trainer_cot.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# Copyright (c) 2022 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 ppdet.core.workspace import create
|
||||
from ppdet.utils.logger import setup_logger
|
||||
logger = setup_logger('ppdet.engine')
|
||||
|
||||
from . import Trainer
|
||||
__all__ = ['TrainerCot']
|
||||
|
||||
class TrainerCot(Trainer):
|
||||
"""
|
||||
Trainer for label-cotuning
|
||||
calculate the relationship between base_classes and novel_classes
|
||||
"""
|
||||
def __init__(self, cfg, mode='train'):
|
||||
super(TrainerCot, self).__init__(cfg, mode)
|
||||
self.cotuning_init()
|
||||
|
||||
def cotuning_init(self):
|
||||
num_classes_novel = self.cfg['num_classes']
|
||||
|
||||
self.load_weights(self.cfg.pretrain_weights)
|
||||
|
||||
self.model.eval()
|
||||
relationship = self.model.relationship_learning(self.loader, num_classes_novel)
|
||||
|
||||
self.model.init_cot_head(relationship)
|
||||
self.optimizer = create('OptimizerBuilder')(self.lr, self.model)
|
||||
|
||||
|
||||
1192
paddle_detection/ppdet/engine/trainer_ssod.py
Normal file
1192
paddle_detection/ppdet/engine/trainer_ssod.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user