Reformat multi-line logs and docstrings (#1489)

This commit is contained in:
q.yao 2022-12-06 19:50:58 +08:00 committed by GitHub
parent 7b3c3bc223
commit 4046e13146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 97 additions and 94 deletions

View File

@ -30,7 +30,7 @@ def visualize_model(model_cfg: Union[str, mmcv.Config],
>>> img = 'demo.jpg'
>>> device = 'cpu'
>>> visualize_model(model_cfg, deploy_cfg, model, \
img, device, show_result=True)
>>> img, device, show_result=True)
Args:
model_cfg (str | mmcv.Config): Model config file or Config object.
@ -59,8 +59,8 @@ def visualize_model(model_cfg: Union[str, mmcv.Config],
if isinstance(model, (list, tuple)):
assert len(model) > 0, 'Model should have at least one element.'
assert all([isinstance(m, str) for m in model]), 'All elements in the \
list should be str'
assert all([isinstance(m, str) for m in model]), \
'All elements in the list should be str'
if backend == Backend.PYTORCH:
model = task_processor.init_pytorch_model(model[0])

View File

@ -54,9 +54,8 @@ class BaseWrapper(torch.nn.Module, metaclass=ABCMeta):
"""Set the output names."""
self._output_names = value
def output_to_list(self, output_dict: Dict[str,
torch.Tensor]) -> \
List[torch.Tensor]:
def output_to_list(
self, output_dict: Dict[str, torch.Tensor]) -> List[torch.Tensor]:
"""Convert the output dict of forward() to a tensor list.
Args:

View File

@ -44,11 +44,11 @@ class ORTWrapper(BaseWrapper):
logger = get_root_logger()
if osp.exists(ort_custom_op_path):
session_options.register_custom_ops_library(ort_custom_op_path)
logger.info(f'Successfully loaded onnxruntime custom ops from \
{ort_custom_op_path}')
logger.info('Successfully loaded onnxruntime custom ops from '
f'{ort_custom_op_path}')
else:
logger.warning(f'The library of onnxruntime custom ops does \
not exist: {ort_custom_op_path}')
logger.warning('The library of onnxruntime custom ops does '
f'not exist: {ort_custom_op_path}')
device_id = parse_device_id(device)
providers = ['CPUExecutionProvider'] \
if device == 'cpu' else \

View File

@ -34,6 +34,6 @@ def load_tensorrt_plugin() -> bool:
logger.info(f'Successfully loaded tensorrt plugins from {lib_path}')
success = True
else:
logger.warning(f'Could not load the library of tensorrt plugins. \
Because the file does not exist: {lib_path}')
logger.warning(f'Could not load the library of tensorrt plugins.'
f'Because the file does not exist: {lib_path}')
return success

View File

@ -68,8 +68,8 @@ def onnx2tensorrt(work_dir: str,
if not isinstance(input_shapes, Dict):
input_shapes = dict(zip(input_names, input_shapes))
assert device.startswith('cuda'), f'TensorRT requires cuda device, \
but given: {device}'
assert device.startswith('cuda'), 'TensorRT requires cuda device,' \
f'but given: {device}'
device_id = parse_device_id(device)
assert save_file.endswith(

View File

@ -84,8 +84,8 @@ class TRTWrapper(BaseWrapper):
self.engine = load(engine)
if not isinstance(self.engine, trt.ICudaEngine):
raise TypeError(f'`engine` should be str or trt.ICudaEngine, \
but given: {type(self.engine)}')
raise TypeError('`engine` should be str or trt.ICudaEngine,'
f'but given: {type(self.engine)}')
self._register_state_dict_hook(TRTWrapper.__on_state_dict)
self.context = self.engine.create_execution_context()

View File

@ -300,8 +300,8 @@ class Classification(BaseTask):
if 'topk' not in postprocess:
topk = (1, )
logger = get_root_logger()
logger.warning('no topk in postprocess config, using default \
topk value.')
logger.warning('no topk in postprocess config, using default'
'topk value.')
else:
topk = postprocess.topk
postprocess.topk = max(topk)

View File

@ -178,8 +178,8 @@ def get_classes_from_config(model_cfg: Union[str, mmcv.Config]):
if class_names is None:
logger = get_root_logger()
logger.warning(f'Use generated class names, because \
it failed to parse CLASSES from config: {data_cfg}')
logger.warning(f'Use generated class names, because '
f'it failed to parse CLASSES from config: {data_cfg}')
num_classes = model_cfg.model.head.num_classes
class_names = [str(i) for i in range(num_classes)]
return class_names

View File

@ -583,8 +583,8 @@ class NCNNEnd2EndModel(End2EndModel):
device: str, class_names: Sequence[str],
model_cfg: Union[str, mmcv.Config],
deploy_cfg: Union[str, mmcv.Config], **kwargs):
assert backend == Backend.NCNN, f'only supported ncnn, but give \
{backend.value}'
assert backend == Backend.NCNN, 'only supported ncnn, but give' \
f'{backend.value}'
super(NCNNEnd2EndModel,
self).__init__(backend, backend_files, device, class_names,
@ -669,8 +669,8 @@ class RKNNModel(End2EndModel):
device: str, class_names: Sequence[str],
model_cfg: Union[str, mmcv.Config],
deploy_cfg: Union[str, mmcv.Config], **kwargs):
assert backend == Backend.RKNN, f'only supported RKNN, but give \
{backend.value}'
assert backend == Backend.RKNN, 'only supported RKNN, but give' \
f'{backend.value}'
super(RKNNModel, self).__init__(backend, backend_files, device,
class_names, deploy_cfg, **kwargs)

View File

@ -41,8 +41,8 @@ def focus__forward__ncnn(ctx, self, x):
x (Tensor): The calculated tensor with shape (N, 4*C, H//2, W//2).
"""
batch_size, c, h, w = x.shape
assert h % 2 == 0 and w % 2 == 0, f'focus for yolox needs even feature\
height and width, got {(h, w)}.'
assert h % 2 == 0 and w % 2 == 0, 'focus for yolox needs even feature' \
f'height and width, got {(h, w)}.'
x = x.reshape(batch_size, c * h, 1, w)
_b, _c, _h, _w = x.shape

View File

@ -356,8 +356,8 @@ def base_dense_head__get_bboxes__ncnn(ctx,
"""
assert len(cls_scores) == len(bbox_preds)
deploy_cfg = ctx.cfg
assert not is_dynamic_shape(deploy_cfg), 'base_dense_head for ncnn\
only supports static shape.'
assert not is_dynamic_shape(deploy_cfg), 'base_dense_head for ncnn' \
'only supports static shape.'
if score_factors is None:
# e.g. Retina, FreeAnchor, Foveabox, etc.
@ -367,8 +367,8 @@ def base_dense_head__get_bboxes__ncnn(ctx,
with_score_factors = True
assert len(cls_scores) == len(score_factors)
batch_size = cls_scores[0].shape[0]
assert batch_size == 1, f'ncnn deployment requires batch size 1, \
got {batch_size}.'
assert batch_size == 1, 'ncnn deployment requires batch size 1,' \
f'got {batch_size}.'
num_levels = len(cls_scores)
if with_score_factors:
@ -384,13 +384,15 @@ def base_dense_head__get_bboxes__ncnn(ctx,
vars = torch.tensor([normalizer, normalizer, 1, 1],
dtype=torch.float32)
else:
assert len(normalizer) == 4, f'normalizer of tblr must be 4,\
got {len(normalizer)}'
assert len(normalizer) == 4, 'normalizer of tblr must be 4,' \
f' got {len(normalizer)}'
assert (normalizer[0] == normalizer[1] and normalizer[2]
== normalizer[3]), 'normalizer between top \
and bottom, left and right must be the same value, or \
we can not transform it to delta_xywh format.'
assert (
normalizer[0] == normalizer[1]
and normalizer[2] == normalizer[3]
), 'normalizer between top and bottom,' \
'left and right must be the same value, or' \
'we can not transform it to delta_xywh format.'
vars = torch.tensor([normalizer[0], normalizer[2], 1, 1],
dtype=torch.float32)

View File

@ -80,7 +80,7 @@ def yolov3_head__get_bboxes(ctx,
pred_map = pred_map.permute(0, 2, 3,
1).reshape(batch_size, -1, self.num_attrib)
# Inplace operation like
# ```pred_map[..., :2] = \torch.sigmoid(pred_map[..., :2])```
# ```pred_map[..., :2] = torch.sigmoid(pred_map[..., :2])```
# would create constant tensor when exporting to onnx
pred_map_conf = torch.sigmoid(pred_map[..., :2])
pred_map_rest = pred_map[..., 2:]

View File

@ -188,8 +188,9 @@ def yolox_head__get_bboxes__ncnn(ctx,
batch_mlvl_scores = torch.cat([dummy_cls_scores, cls_scores], dim=2)
score_factor = torch.cat(flatten_objectness, dim=1).sigmoid()
flatten_bbox_preds = torch.cat(flatten_bbox_preds, dim=1)
assert flatten_priors.shape[-1] == 4, f'yolox needs (B, N, 4) priors, got\
(B, N, {flatten_priors.shape[-1]})'
assert flatten_priors.shape[-1] == 4, \
'yolox needs (B, N, 4) priors, got ' \
f'(B, N, {flatten_priors.shape[-1]})'
prior_box_x1 = (flatten_priors[:, :, 0:1] - flatten_priors[:, :, 2:3] / 2)\
/ img_width
prior_box_y1 = (flatten_priors[:, :, 1:2] - flatten_priors[:, :, 3:4] / 2)\

View File

@ -53,8 +53,8 @@ def process_model_config(model_cfg: mmcv.Config,
test_pipeline[1].transforms[0].height = input_shape[1]
test_pipeline[1].transforms[0].max_width = input_shape[0]
else:
raise ValueError(f'Transforms[0] should be ResizeOCR, but got\
{test_pipeline[1].transforms[0].type}')
raise ValueError('Transforms[0] should be ResizeOCR, but got'
f'{test_pipeline[1].transforms[0].type}')
else:
test_pipeline[1].update(resize)
model_cfg.data.test.pipeline = test_pipeline

View File

@ -25,11 +25,11 @@ def gv_ratio_roi_head__simple_test_bboxes(ctx,
Default: False.
Returns:
tuple[list[Tensor], list[Tensor]]: The first list contains \
the boxes of the corresponding image in a batch, each \
tensor has the shape (num_boxes, 6) and last dimension \
6 represent (x, y, w, h, theta, score). Each Tensor \
in the second list is the labels with shape (num_boxes, ). \
tuple[list[Tensor], list[Tensor]]: The first list contains
the boxes of the corresponding image in a batch, each
tensor has the shape (num_boxes, 6) and last dimension
6 represent (x, y, w, h, theta, score). Each Tensor
in the second list is the labels with shape (num_boxes, ).
The length of both lists should be equal to batch_size.
"""

View File

@ -57,11 +57,11 @@ def oriented_standard_roi_head__simple_test_bboxes(ctx,
Default: False.
Returns:
tuple[list[Tensor], list[Tensor]]: The first list contains \
the boxes of the corresponding image in a batch, each \
tensor has the shape (num_boxes, 6) and last dimension \
6 represent (x, y, w, h, theta, score). Each Tensor \
in the second list is the labels with shape (num_boxes, ). \
tuple[list[Tensor], list[Tensor]]: The first list contains
the boxes of the corresponding image in a batch, each
tensor has the shape (num_boxes, 6) and last dimension
6 represent (x, y, w, h, theta, score). Each Tensor
in the second list is the labels with shape (num_boxes, ).
The length of both lists should be equal to batch_size.
"""

View File

@ -204,9 +204,9 @@ def mark(func_name: Optional[str] = None,
Args:
func_name (str): The name of the function where marks come from.
inputs (Sequence[str]): The input names of the marks. The final name \
inputs (Sequence[str]): The input names of the marks. The final name
might have suffix if inputs is list or dictionary.
outputs (Sequence[str]): The output names of the marks. The final \
outputs (Sequence[str]): The output names of the marks. The final
name might have suffix if outputs is list or dictionary.
Returns:

View File

@ -19,8 +19,7 @@ class SymbolicRewriter:
recover after exiting the RewriteContext.
Examples:
>>> @SYMBOLIC_REWRITER.register_symbolic('squeeze', \
>>> is_pytorch=True)
>>> @SYMBOLIC_REWRITER.register_symbolic('squeeze', is_pytorch=True)
>>> def squeeze_default(ctx, g, self, dim=None):
>>> if dim is None:
>>> dims = []
@ -49,9 +48,9 @@ class SymbolicRewriter:
Args:
func_name (str): The function name/path to override the symbolic.
backend (str): The rewriter will be activated on which backend.
is_pytorch (bool): Enable this flag if func_name is the name of \
is_pytorch (bool): Enable this flag if func_name is the name of
a pytorch builtin function.
arg_descriptors (Sequence[str]): The argument descriptors of the \
arg_descriptors (Sequence[str]): The argument descriptors of the
symbol.
ir (IR): The rewriter will be activated on which IR.
extra_checkers (Checker | List[Checker] | None): Other requirements

View File

@ -584,8 +584,8 @@ def test_topk(backend,
input = torch.rand(1, 8, 12, 17)
else:
input = input_list[0]
assert input.shape[0] == 1, (f'ncnn batch must be 1, \
but got {input.shape[0]}')
assert input.shape[0] == 1, ('ncnn batch must be 1, '
f'but got {input.shape[0]}')
def topk_function(inputs):
return torch.Tensor.topk(inputs, k, dim, largest, sorted)
@ -635,11 +635,11 @@ def test_shape(backend,
else:
input = input_list[0]
assert input.dim() == dim + 1, 'input.dim() must equal to dim + 1'
assert tuple(input.shape) == orig_shape, 'input.shape must the \
same as orig_shape'
assert tuple(input.shape) == orig_shape, ('input.shape must the '
'same as orig_shape')
assert input.shape[0] == 1, (f'ncnn batch must be 1, \
but got {input.shape[0]}')
assert input.shape[0] == 1, ('ncnn batch must be 1, '
f'but got {input.shape[0]}')
shape_node = make_node('Shape', input_names, output_names)
assert len(input_names) == 1, 'length of input_names must be 1'
@ -688,13 +688,13 @@ def test_constantofshape(backend,
input = input_list[0]
assert input.dim() == dim + 1, 'input.dim() must equal to dim + 1'
assert tuple(input.shape) == (n, c, h,
w)[-dim - 1:], 'input.shape must the \
same as orig_shape'
w)[-dim - 1:], ('input.shape must the '
'same as orig_shape')
assert input.shape[0] == 1, (f'ncnn input batch must be 1, \
got {input.shape[0]}')
assert input[0][0] == 1, (f'ncnn output mat batch must be 1, \
got {input[0][0]}')
assert input.shape[0] == 1, ('ncnn input batch must be 1, '
f'got {input.shape[0]}')
assert input[0][0] == 1, ('ncnn output mat batch must be 1, '
f'got {input[0][0]}')
constantofshape_node = make_node(
'ConstantOfShape', input_names, output_names, value=float(val))
@ -745,10 +745,10 @@ def test_gather(backend,
else:
data = input_list[0]
indice = input_list[1]
assert data.shape[0] == 1, (f'ncnn batch must be 1, \
but got {data.shape[0]}')
assert indice.shape[0] == 1, (f'ncnn batch must be 1, \
but got {indice.shape[0]}')
assert data.shape[0] == 1, ('ncnn batch must be 1, '
f'but got {data.shape[0]}')
assert indice.shape[0] == 1, ('ncnn batch must be 1, '
f'but got {indice.shape[0]}')
gather_node = make_node('Gather', input_names, output_names, axis=axis + 1)
gather_graph = make_graph([gather_node], 'gather_graph', [
@ -766,8 +766,8 @@ def test_gather(backend,
import importlib
import onnxruntime
assert importlib.util.find_spec('onnxruntime') is not None, 'onnxruntime \
not installed.'
assert importlib.util.find_spec('onnxruntime') is not None, \
'onnxruntime not installed.'
import numpy as np
session = onnxruntime.InferenceSession(gather_model.SerializeToString())
@ -795,11 +795,12 @@ def test_tensorslice(backend, dim, input_list=None, save_dir=None):
input = torch.rand((8, 12, 17)[-dim:]).unsqueeze(0)
else:
input = input_list[0]
assert input.dim() == dim + 1, f'input.dim() must equal to \
dim + 1, expected: {dim + 1}, got: {input.dim()}'
assert input.dim() == dim + 1, ('input.dim() must equal to '
f'dim + 1, expected: {dim + 1}, '
f'got: {input.dim()}')
assert input.shape[0] == 1, (f'ncnn batch must be 1, \
but got {input.shape[0]}')
assert input.shape[0] == 1, ('ncnn batch must be 1, '
f'but got {input.shape[0]}')
def tensorslice_function(inputs):
if dim == 1:
@ -835,10 +836,10 @@ def test_expand(backend,
else:
input = input_list[0]
target = input_list[1]
assert input.shape[0] == 1, (f'ncnn batch must be 1, \
but not {input.shape[0]}')
assert target.shape[0] == 1, (f'ncnn batch must be 1, \
but not {target.shape[0]}')
assert input.shape[0] == 1, (f'ncnn batch must be 1, '
f'but not {input.shape[0]}')
assert target.shape[0] == 1, (f'ncnn batch must be 1, '
f'but not {target.shape[0]}')
def expand_function(input, target):
return input.expand_as(target)

View File

@ -34,8 +34,8 @@ def parse_args():
help='the dir to save logs and models')
parser.add_argument(
'--calib-dataset-cfg',
help='dataset config path used to calibrate in int8 mode. If not \
specified, it will use "val" dataset in model config instead.',
help=('dataset config path used to calibrate in int8 mode. If not '
'specified, it will use "val" dataset in model config instead.'),
default=None)
parser.add_argument(
'--device', help='device used for conversion', default='cpu')
@ -198,9 +198,9 @@ def main():
assert len(model_params) == len(ir_files)
from mmdeploy.apis.tensorrt import is_available as trt_is_available
assert trt_is_available(
), 'TensorRT is not available,' \
+ ' please install TensorRT and build TensorRT custom ops first.'
assert trt_is_available(), (
'TensorRT is not available,'
' please install TensorRT and build TensorRT custom ops first.')
from mmdeploy.apis.tensorrt import onnx2tensorrt
PIPELINE_MANAGER.enable_multiprocess(True, [onnx2tensorrt])
@ -229,9 +229,9 @@ def main():
from mmdeploy.apis.ncnn import is_available as is_available_ncnn
if not is_available_ncnn():
logger.error('ncnn support is not available, please make sure \
1) `mmdeploy_onnx2ncnn` existed in `PATH` \
2) python import ncnn success')
logger.error('ncnn support is not available, please make sure:\n'
'1) `mmdeploy_onnx2ncnn` existed in `PATH`\n'
'2) python import ncnn success')
exit(1)
import mmdeploy.apis.ncnn as ncnn_api
@ -279,9 +279,10 @@ def main():
from mmdeploy.apis.snpe import is_available as is_available
if not is_available():
logger.error('snpe support is not available, please check \
1) `snpe-onnx-to-dlc` existed in `PATH` 2) snpe only support \
ubuntu18.04')
logger.error('snpe support is not available, please check\n'
'1) `snpe-onnx-to-dlc` existed in `PATH`\n'
'2) snpe only support\n'
'ubuntu18.04')
exit(1)
import mmdeploy.apis.snpe as snpe_api