Skip to content

Commit

Permalink
[CodeCamp2023-503] Add docs of Inferencer (#10784)
Browse files Browse the repository at this point in the history
  • Loading branch information
RangeKing committed Aug 15, 2023
1 parent 2dbf307 commit 884aad0
Show file tree
Hide file tree
Showing 8 changed files with 1,829 additions and 424 deletions.
1,667 changes: 1,408 additions & 259 deletions demo/inference_demo.ipynb

Large diffs are not rendered by default.

276 changes: 202 additions & 74 deletions docs/en/user_guides/inference.md

Large diffs are not rendered by default.

272 changes: 200 additions & 72 deletions docs/zh_cn/user_guides/inference.md

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions mmdet/apis/det_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DetInferencer(BaseInferencer):
postprocess_kwargs: set = {
'print_result',
'pred_out_dir',
'return_datasample',
'return_datasamples',
'no_save_pred',
}

Expand Down Expand Up @@ -303,7 +303,7 @@ def __call__(
no_save_vis: bool = False,
draw_pred: bool = True,
pred_score_thr: float = 0.3,
return_datasample: bool = False,
return_datasamples: bool = False,
print_result: bool = False,
no_save_pred: bool = True,
out_dir: str = '',
Expand All @@ -328,13 +328,13 @@ def __call__(
Defaults to True.
pred_score_thr (float): Minimum score of bboxes to draw.
Defaults to 0.3.
return_datasample (bool): Whether to return results as
return_datasamples (bool): Whether to return results as
:obj:`DetDataSample`. Defaults to False.
print_result (bool): Whether to print the inference result w/o
visualization to the console. Defaults to False.
no_save_pred (bool): Whether to force not to save prediction
results. Defaults to True.
out_file: Dir to save the inference results or
out_dir: Dir to save the inference results or
visualization. If left as empty, no file will be saved.
Defaults to ''.
texts (str | list[str]): Text prompts. Defaults to None.
Expand Down Expand Up @@ -405,7 +405,7 @@ def __call__(
results = self.postprocess(
preds,
visualization,
return_datasample=return_datasample,
return_datasamples=return_datasamples,
print_result=print_result,
no_save_pred=no_save_pred,
pred_out_dir=out_dir,
Expand Down Expand Up @@ -498,7 +498,7 @@ def postprocess(
self,
preds: PredType,
visualization: Optional[List[np.ndarray]] = None,
return_datasample: bool = False,
return_datasamples: bool = False,
print_result: bool = False,
no_save_pred: bool = False,
pred_out_dir: str = '',
Expand All @@ -516,7 +516,7 @@ def postprocess(
Args:
preds (List[:obj:`DetDataSample`]): Predictions of the model.
visualization (Optional[np.ndarray]): Visualized predictions.
return_datasample (bool): Whether to use Datasample to store
return_datasamples (bool): Whether to use Datasample to store
inference results. If False, dict will be used.
print_result (bool): Whether to print the inference result w/o
visualization to the console. Defaults to False.
Expand All @@ -533,7 +533,7 @@ def postprocess(
- ``visualization`` (Any): Returned by :meth:`visualize`.
- ``predictions`` (dict or DataSample): Returned by
:meth:`forward` and processed in :meth:`postprocess`.
If ``return_datasample=False``, it usually should be a
If ``return_datasamples=False``, it usually should be a
json-serializable dict containing only basic data elements such
as strings and numbers.
"""
Expand All @@ -542,14 +542,14 @@ def postprocess(

result_dict = {}
results = preds
if not return_datasample:
if not return_datasamples:
results = []
for pred in preds:
result = self.pred2dict(pred, pred_out_dir)
results.append(result)
elif pred_out_dir != '':
warnings.warn('Currently does not support saving datasample '
'when return_datasample is set to True. '
'when return_datasamples is set to True. '
'Prediction results are not saved!')
# Add img to the results after printing and dumping
result_dict['predictions'] = results
Expand Down
6 changes: 3 additions & 3 deletions projects/XDecoder/xdecoder/inference/image_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def __call__(
no_save_vis: bool = False,
draw_pred: bool = True,
pred_score_thr: float = 0.3,
return_datasample: bool = False,
return_datasamples: bool = False,
print_result: bool = False,
no_save_pred: bool = True,
out_dir: str = '',
Expand All @@ -211,7 +211,7 @@ def __call__(
Defaults to True.
pred_score_thr (float): Minimum score of bboxes to draw.
Defaults to 0.3.
return_datasample (bool): Whether to return results as
return_datasamples (bool): Whether to return results as
:obj:`DetDataSample`. Defaults to False.
print_result (bool): Whether to print the inference result w/o
visualization to the console. Defaults to False.
Expand Down Expand Up @@ -297,7 +297,7 @@ def __call__(
results = self.postprocess(
preds,
visualization,
return_datasample=return_datasample,
return_datasamples=return_datasamples,
print_result=print_result,
no_save_pred=no_save_pred,
pred_out_dir=out_dir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __call__(
no_save_vis: bool = False,
draw_pred: bool = True,
pred_score_thr: float = 0.3,
return_datasample: bool = False,
return_datasamples: bool = False,
print_result: bool = False,
no_save_pred: bool = True,
out_dir: str = '',
Expand All @@ -127,7 +127,7 @@ def __call__(
Defaults to True.
pred_score_thr (float): Minimum score of bboxes to draw.
Defaults to 0.3.
return_datasample (bool): Whether to return results as
return_datasamples (bool): Whether to return results as
:obj:`DetDataSample`. Defaults to False.
print_result (bool): Whether to print the inference result w/o
visualization to the console. Defaults to False.
Expand Down Expand Up @@ -216,7 +216,7 @@ def __call__(
results = self.postprocess(
preds,
visualization,
return_datasample=return_datasample,
return_datasamples=return_datasamples,
print_result=print_result,
no_save_pred=no_save_pred,
pred_out_dir=out_dir,
Expand Down
2 changes: 1 addition & 1 deletion projects/gradio_demo/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def inference(self, model, image):
ic_inferencer = ImageCaptionInferencer(
**self.model_info[model], scope='mmdet', device=get_free_device())
results_dict = ic_inferencer(
image, return_vis=False, no_save_vis=True, return_datasample=True)
image, return_vis=False, no_save_vis=True, return_datasamples=True)
return results_dict['predictions'][0].pred_caption


Expand Down
4 changes: 2 additions & 2 deletions tests/test_apis/test_det_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_visualize(self, model):
'rtmdet-t', 'mask-rcnn_r50_fpn_1x_coco', 'panoptic_fpn_r50_fpn_1x_coco'
])
def test_postprocess(self, model):
# return_datasample
# return_datasamples
img_path = 'tests/data/color.jpg'

mock_load = Mock(return_value=None)
Expand All @@ -135,7 +135,7 @@ def test_postprocess(self, model):
'palette': 'random'
}

res = inferencer(img_path, return_datasample=True)
res = inferencer(img_path, return_datasamples=True)
self.assertTrue(is_list_of(res['predictions'], DetDataSample))

with tempfile.TemporaryDirectory() as tmp_dir:
Expand Down

0 comments on commit 884aad0

Please sign in to comment.