GitHub issue #6229: Apply inference on all images in the dataset
State: closed | Created: 2023-09-10T08:36:12Z

### Describe the bug

```
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[14], line 11
      9 for idx, example in enumerate(dataset['train']):
     10     image_path = example['image']
---> 11     mask_image = process_image(image_path)
     12     mask_image.save(f"mask_{idx}.png")

Cell In[14], line 4, in process_image(image_path)
      2 def process_image(image_path):
      3     print("Processing image:", image_path)
----> 4     result = inferencer(image_path)['predictions']
      5     mask = np.where(result == 12, 255, 0).astype('uint8')
      6     return Image.fromarray(mask)

File /usr/local/lib/python3.10/dist-packages/mmseg/apis/mmseg_inferencer.py:183, in MMSegInferencer.__call__(self, inputs, return_datasamples, batch_size, show, wait_time, out_dir, img_out_dir, pred_out_dir, **kwargs)
    180     pred_out_dir = ''
    181     img_out_dir = ''
--> 183 return super().__call__(
    184     inputs=inputs,
    185     return_datasamples=return_datasamples,
    186     batch_size=batch_size,
    187     show=show,
    188     wait_time=wait_time,
    189     img_out_dir=img_out_dir,
    190     pred_out_dir=pred_out_dir,
    191     **kwargs)

File /usr/local/lib/python3.10/dist-packages/mmengine/infer/infer.py:221, in BaseInferencer.__call__(self, inputs, return_datasamples, batch_size, **kwargs)
    218 inputs = self.preprocess(
    219     ori_inputs, batch_size=batch_size, **preprocess_kwargs)
    220 preds = []
--> 221 for data in (track(inputs, description='Inference')
    222              if self.show_progress else inputs):
    223     preds.extend(self.forward(data, **forward_kwargs))
    224 visualization = self.visualize(
    225     ori_inputs, preds,
    226     **visualize_kwargs)  # type: ignore  # noqa: E501

File /usr/local/lib/python3.10/dist-packages/rich/progress.py:168, in track(sequence, description, total, auto_refresh, console, transient, get_time, refresh_per_second, style, complete_style, finished_style, pulse_style, update_period, disable, show_speed)
    157 progress = Progress(
    158     *columns,
    159     auto_refresh=auto_refresh,
   (...)
    164     disable=disable,
    165 )
    167 with progress:
--> 168     yield from progress.track(
    169         sequence, total=total, description=description, update_period=update_period
    170     )

File /usr/local/lib/python3.10/dist-packages/rich/progress.py:1210, in Progress.track(self, sequence, total, task_id, description, update_period)
   1208 if self.live.auto_refresh:
   1209     with _TrackThread(self, task_id, update_period) as track_thread:
-> 1210         for value in sequence:
   1211             yield value
   1212             track_thread.completed += 1

File /usr/local/lib/python3.10/dist-packages/mmengine/infer/infer.py:291, in BaseInferencer.preprocess(self, inputs, batch_size, **kwargs)
    266 """Process the inputs into a model-feedable format.
    267 
    268 Customize your preprocess by overriding this method. Preprocess should
   (...)
    287     Any: Data processed by the ``pipeline`` and ``collate_fn``.
    288 """
    289 chunked_data = self._get_chunk_data(
    290     map(self.pipeline, inputs), batch_size)
--> 291 yield from map(self.collate_fn, chunked_data)

File /usr/local/lib/python3.10/dist-packages/mmengine/infer/infer.py:588, in BaseInferencer._get_chunk_data(self, inputs, chunk_size)
    586 chunk_data = []
    587 for _ in range(chunk_size):
--> 588     processed_data = next(inputs_iter)
    589     chunk_data.append(processed_data)
    590 yield chunk_data

File /usr/local/lib/python3.10/dist-packages/mmcv/transforms/base.py:12, in BaseTransform.__call__(self, results)
      9 def __call__(self,
     10              results: Dict) -> Optional[Union[Dict, Tuple[List, List]]]:
---> 12     return self.transform(results)

File /usr/local/lib/python3.10/dist-packages/mmcv/transforms/wrappers.py:88, in Compose.transform(self, results)
     79 """Call function to apply transforms sequentially.
     80 
     81 Args:
   (...)
     85     dict or None: Transformed results.
     86 """
     87 for t in self.transforms:
---> 88     results = t(results)  # type: ignore
     89     if results is None:
     90         return None

File /usr/local/lib/python3.10/dist-packages/mmcv/transforms/base.py:12, in BaseTransform.__call__(self, results)
      9 def __call__(self,
     10              results: Dict) -> Optional[Union[Dict, Tuple[List, List]]]:
---> 12     return self.transform(results)

File /usr/local/lib/python3.10/dist-packages/mmseg/datasets/transforms/loading.py:496, in InferencerLoader.transform(self, single_input)
    494     inputs = single_input
    495 else:
--> 496     raise NotImplementedError
    498 if 'img' in inputs:
    499     return self.from_ndarray(inputs)

NotImplementedError: 
````

### Steps to reproduce the bug

```
from datasets import load_dataset
dataset = load_dataset('Andyrasika/cat_kingdom')
dataset

from mmseg.apis import MMSegInferencer

checkpoint_name = 'segformer_mit-b5_8xb2-160k_ade20k-640x640'

inferencer = MMSegInferencer(model=checkpoint_name)

# Define a function to apply the code to each image in the dataset
def process_image(image_path):
    print("Processing image:", image_path)
    result = inferencer(image_path)['predictions']
    mask = np.where(result == 12, 255, 0).astype('uint8')
    return Image.fromarray(mask)

# Process and save masks for each image in the dataset
for idx, example in enumerate(dataset['train']):
    image_path = example['image']
    mask_image = process_image(image_path)
    mask_image.save(f"mask_{idx}.png")
```

### Expected behavior

create a separate column with masks in the dataset and further shows as a separate column in hub

### Environment info

jupyter notebook RTX 3090