SegFormer feature extractor  `do_normalize=False`
Author: johnnv1
Pull request: no
Labels: name: bug

name: Feature extractors

### System Info

```shell
- `transformers` version: 4.18.0
- Platform: Linux-5.4.188+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.7.13
- Huggingface_hub version: 0.7.0
- PyTorch version (GPU?): 1.11.0+cu113 (True)
- Tensorflow version (GPU?): 2.8.2 (True)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No
```


### Who can help?

@NielsRogge 

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

```python
# [...]
feature_extractor = SegformerFeatureExtractor.from_pretrained('nvidia/mit-b1',
                                                              do_resize=False,
                                                              do_normalize=False)
# [...]
# call the extractor
# minimal example
img = Image.fromarray(np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8))
msk = Image.fromarray(np.random.randint(1, 10, (100, 100), dtype=np.uint8))

feature_extractor(images=img, segmentation_maps=msk, return_tensors="pt")
```

This will raise:
```pythonoutput
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/transformers/feature_extraction_utils.py in convert_to_tensors(self, tensor_type)
    167                 if not is_tensor(value):
--> 168                     tensor = as_tensor(value)
    169 

4 frames
/usr/local/lib/python3.7/dist-packages/transformers/feature_extraction_utils.py in as_tensor(value)
    149                     value = np.array(value)
--> 150                 return torch.tensor(value)
    151 

RuntimeError: Could not infer dtype of Image

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-70-9cd4a6ca6f4b> in <module>()
      2 msk = Image.fromarray(np.array((100,100,1), dtype=np.uint8))
      3 
----> 4 feature_extractor(images=img, segmentation_maps=msk, return_tensors="pt")

/usr/local/lib/python3.7/dist-packages/transformers/models/segformer/feature_extraction_segformer.py in __call__(self, images, segmentation_maps, return_tensors, **kwargs)
    208             data["labels"] = labels
    209 
--> 210         encoded_inputs = BatchFeature(data=data, tensor_type=return_tensors)
    211 
    212         return encoded_inputs

/usr/local/lib/python3.7/dist-packages/transformers/feature_extraction_utils.py in __init__(self, data, tensor_type)
     77     def __init__(self, data: Optional[Dict[str, Any]] = None, tensor_type: Union[None, str, TensorType] = None):
     78         super().__init__(data)
---> 79         self.convert_to_tensors(tensor_type=tensor_type)
     80 
     81     def __getitem__(self, item: str) -> Union[Any]:

/usr/local/lib/python3.7/dist-packages/transformers/feature_extraction_utils.py in convert_to_tensors(self, tensor_type)
    173                     raise ValueError("Unable to create tensor returning overflowing values of different lengths. ")
    174                 raise ValueError(
--> 175                     "Unable to create tensor, you should probably activate padding "
    176                     "with 'padding=True' to have batched tensors with the same length."
    177                 )

ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.
```

### Expected behavior
I was expecting that no error occurs in the conversion to tensor if I don't perform the normalization. 

This example is for SegFormer model, but I think the DETR have the same issue (#16715)

A workaround, is use:

```python
feature_extractor = SegformerFeatureExtractor.from_pretrained('nvidia/mit-b1',
                                                              do_resize=False,
                                                              do_normalize=True,
                                                              image_mean= [0., 0., 0.],
                                                              image_std = [1., 1., 1.])
                       )
```

I don't know if `convert_to_tensors` is to work with `PIL.Image`, maybe just need to add this conversion as a default step in the extractor: 

```python
images = [self.to_numpy_array(image) for image in images if isinstance(image, Image.Image)]
```



Comments:
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

This hasn't been fixed yet 

cc @alaradirik @amyeroberts as well :)

Hi @johnnv1,

thanks for reporting, we're aware of this issue with feature extractors (see #15055 for a detailed description) and are planning to take it into account when updating the preprocessing pipeline for our vision models.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.