open-mmlab/mmdetection · warning

Currently does not support saving datasample when return_dat

Error message

Currently does not support saving datasample when return_datasamples is set to True. Prediction results are not saved!

What it means

Warning from DetInferencer.postprocess: when return_datasamples=True and pred_out_dir is set, raw datasamples cannot be serialized to disk, so nothing is saved — only the in-memory return value is available.

Source

Thrown at mmdet/apis/det_inferencer.py:561

            - ``visualization`` (Any): Returned by :meth:`visualize`.
            - ``predictions`` (dict or DataSample): Returned by
                :meth:`forward` and processed in :meth:`postprocess`.
                If ``return_datasamples=False``, it usually should be a
                json-serializable dict containing only basic data elements such
                as strings and numbers.
        """
        if no_save_pred is True:
            pred_out_dir = ''

        result_dict = {}
        results = preds
        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_datasamples is set to True. '
                          'Prediction results are not saved!')
        # Add img to the results after printing and dumping
        result_dict['predictions'] = results
        if print_result:
            print(result_dict)
        result_dict['visualization'] = visualization
        return result_dict

    # TODO: The data format and fields saved in json need further discussion.
    #  Maybe should include model name, timestamp, filename, image info etc.
    def pred2dict(self,
                  data_sample: DetDataSample,
                  pred_out_dir: str = '') -> Dict:
        """Extract elements necessary to represent a prediction into a
        dictionary.

        It's better to contain only basic data elements such as strings and

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Call twice or choose one: use return_datasamples=True with out_dir='' and serialize datasamples yourself
  2. Or use return_datasamples=False with out_dir to get the standard json/pickle predictions
  3. Convert datasamples to dict via pred2dict before saving

Example fix

// before
res = inferencer(imgs, return_datasamples=True, out_dir='out/')  # nothing saved
// after
samples = inferencer(imgs, return_datasamples=True)
# manually pickle 'samples' if persistence is needed
Defensive patterns

Strategy: validation

Validate before calling

if return_datasamples and out_dir:
    print('datasamples will NOT be saved; save them manually')

Prevention

When it happens

Trigger: Calling inferencer(..., return_datasamples=True, out_dir='results/') (pred_out_dir non-empty).

Common situations: Users wanting both structured dump files and full DetDataSamples; the JSON dump path only supports pred2dict output, not datasamples.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/232f8dd5e6c1f5c9. Report an issue: GitHub.