•
config/textdet/_base_/default_runtime.py, config/textrecog/_base_/default_runtime.py
# Visualization
vis_backends = [
#dict(type='LocalVisBackend'),
dict(type='WandbVisBackend')]
visualizer = dict(
type='TextRecogLocalVisualizer',
name='visualizer',
vis_backends=vis_backends)
Python
복사
•
mmengine/visualization/vis_backend.py
@VISBACKENDS.register_module()
class WandbVisBackend(BaseVisBackend):
"""Wandb visualization backend class.
Examples:
>>> from mmengine.visualization import WandbVisBackend
>>> import numpy as np
>>> wandb_vis_backend = WandbVisBackend()
>>> img=np.random.randint(0, 256, size=(10, 10, 3))
>>> wandb_vis_backend.add_image('img', img)
>>> wandb_vis_backend.add_scaler('mAP', 0.6)
>>> wandb_vis_backend.add_scalars({'loss': [1, 2, 3],'acc': 0.8})
>>> cfg = Config(dict(a=1, b=dict(b1=[0, 1])))
>>> wandb_vis_backend.add_config(cfg)
Args:
save_dir (str, optional): The root directory to save the files
produced by the visualizer.
init_kwargs (dict, optional): wandb initialization
input parameters. Default to None.
define_metric_cfg (dict, optional):
A dict of metrics and summary for wandb.define_metric.
The key is metric and the value is summary.
When ``define_metric_cfg={'coco/bbox_mAP': 'max'}``,
The maximum value of``coco/bbox_mAP`` is logged on wandb UI.
See
`wandb docs <https://docs.wandb.ai/ref/python/run#define_metric>`_
for details.
Default: None
commit: (bool, optional) Save the metrics dict to the wandb server
and increment the step. If false `wandb.log` just
updates the current metrics dict with the row argument
and metrics won't be saved until `wandb.log` is called
with `commit=True`. Default to True.
log_code_name: (str, optional) The name of code artifact.
By default, the artifact will be named
source-$PROJECT_ID-$ENTRYPOINT_RELPATH. See
`wandb docs <https://docs.wandb.ai/ref/python/run#log_code>`_
for details. Defaults to None.
New in version 0.3.0.
"""
def __init__(self,
save_dir: str,
init_kwargs: Optional[dict] = None,
define_metric_cfg: Optional[dict] = None,
commit: Optional[bool] = True,
log_code_name: Optional[str] = None):
super().__init__(save_dir)
self._init_kwargs = init_kwargs
self._define_metric_cfg = define_metric_cfg
self._commit = commit
self._log_code_name = log_code_name
def _init_env(self):
"""Setup env for wandb."""
if not os.path.exists(self._save_dir):
os.makedirs(self._save_dir, exist_ok=True) # type: ignore
if self._init_kwargs is None:
self._init_kwargs = {'dir': self._save_dir}
else:
self._init_kwargs.setdefault('dir', self._save_dir)
try:
import wandb
except ImportError:
raise ImportError(
'Please run "pip install wandb" to install wandb')
wandb.init(**self._init_kwargs)
if self._define_metric_cfg is not None:
for metric, summary in self._define_metric_cfg.items():
wandb.define_metric(metric, summary=summary)
self._wandb = wandb
@property # type: ignore
@force_init_env
def experiment(self):
"""Return wandb object.
The experiment attribute can get the wandb backend, If you want to
write other data, such as writing a table, you can directly get the
wandb backend through experiment.
"""
return self._wandb
@force_init_env
def add_config(self, config: Config, **kwargs) -> None:
"""Record the config to wandb.
Args:
config (Config): The Config object
"""
self._wandb.config.update(dict(config))
self._wandb.run.log_code(name=self._log_code_name)
@force_init_env
def add_image(self,
name: str,
image: np.ndarray,
step: int = 0,
**kwargs) -> None:
"""Record the image to wandb.
Args:
name (str): The image identifier.
image (np.ndarray): The image to be saved. The format
should be RGB.
step (int): Useless parameter. Wandb does not
need this parameter. Default to 0.
"""
image = self._wandb.Image(image)
self._wandb.log({name: image}, commit=self._commit)
@force_init_env
def add_scalar(self,
name: str,
value: Union[int, float, torch.Tensor, np.ndarray],
step: int = 0,
**kwargs) -> None:
"""Record the scalar data to wandb.
Args:
name (str): The scalar identifier.
value (int, float, torch.Tensor, np.ndarray): Value to save.
step (int): Useless parameter. Wandb does not
need this parameter. Default to 0.
"""
self._wandb.log({name: value}, commit=self._commit)
@force_init_env
def add_scalars(self,
scalar_dict: dict,
step: int = 0,
file_path: Optional[str] = None,
**kwargs) -> None:
"""Record the scalar's data to wandb.
Args:
scalar_dict (dict): Key-value pair storing the tag and
corresponding values.
step (int): Useless parameter. Wandb does not
need this parameter. Default to 0.
file_path (str, optional): Useless parameter. Just for
interface unification. Default to None.
"""
self._wandb.log(scalar_dict, commit=self._commit)
def close(self) -> None:
"""close an opened wandb object."""
if hasattr(self, '_wandb'):
self._wandb.join()
Python
복사
•
tools/test.py
# ...
def trigger_visualization_hook(cfg, args):
default_hooks = cfg.default_hooks
if 'visualization' in default_hooks:
visualization_hook = default_hooks['visualization']
# Turn on visualization
visualization_hook['enable'] = True
visualization_hook['draw_gt'] = True
visualization_hook['draw_pred'] = True
if args.show:
visualization_hook['show'] = True
visualization_hook['wait_time'] = args.wait_time
if args.show_dir:
cfg.visualizer['save_dir'] = args.show_dir
cfg.visualizer['vis_backends'] = [dict(type='LocalVisBackend')]
# ...
return cfg
# ...
def main():
# ...
if args.show or args.show_dir:
cfg = trigger_visualization_hook(cfg, args)
# ...
# ...
if __name__ == '__main__':
main()
Python
복사