BeitFeatureExtractor no longer works with grayscale images "unsupported number of image dimensions"
Author: grantdelozier
Pull request: no

### System Info

```
Python 3.7.13 (default, Mar 29 2022, 02:18:16) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import transformers
>>> transformers.__version__
'4.27.4'
```


### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [x] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

I recently had to update my transformers version due to a cross dependency issue and my image preprocesssing code stopped working. 

```
from PIL import Image
from transformers import BeitFeatureExtractor

pil_image = Image.open('sample_rgb_image.png').convert('L') #RGB image and convert to grayscale

image_feature_extractor = BeitFeatureExtractor.from_pretrained('/opt/ml/configs/beit-config.json')

pixel_input_ids = image_feature_extractor(pil_image, return_tensors="pt")['pixel_values']
```
Produces this error:

```
/opt/conda/lib/python3.7/site-packages/transformers/models/beit/feature_extraction_beit.py:31: FutureWarning: The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use BeitImageProcessor instead.
  FutureWarning,
Traceback (most recent call last):
  File "test_beit.py", line 8, in <module>
    pixel_input_ids = image_feature_extractor(pil_image, return_tensors="pt")['pixel_values']
  File "/opt/conda/lib/python3.7/site-packages/transformers/models/beit/image_processing_beit.py", line 359, in __call__
    return super().__call__(images, segmentation_maps=segmentation_maps, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/transformers/image_processing_utils.py", line 458, in __call__
    return self.preprocess(images, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/transformers/models/beit/image_processing_beit.py", line 481, in preprocess
    for img in images
  File "/opt/conda/lib/python3.7/site-packages/transformers/models/beit/image_processing_beit.py", line 481, in <listcomp>
    for img in images
  File "/opt/conda/lib/python3.7/site-packages/transformers/models/beit/image_processing_beit.py", line 314, in _preprocess_image
    image_std=image_std,
  File "/opt/conda/lib/python3.7/site-packages/transformers/models/beit/image_processing_beit.py", line 271, in _preprocess
    image = self.resize(image=image, size=size, resample=resample)
  File "/opt/conda/lib/python3.7/site-packages/transformers/models/beit/image_processing_beit.py", line 176, in resize
    image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs
  File "/opt/conda/lib/python3.7/site-packages/transformers/image_transforms.py", line 290, in resize
    data_format = infer_channel_dimension_format(image) if data_format is None else data_format
  File "/opt/conda/lib/python3.7/site-packages/transformers/image_utils.py", line 159, in infer_channel_dimension_format
    raise ValueError(f"Unsupported number of image dimensions: {image.ndim}")
ValueError: Unsupported number of image dimensions: 2
```


### Expected behavior

In the past I was always able to give a grayscale PIL image as input to feature extractor. Is this input type no longer supported?

Comments:
cc @amyeroberts 

Hi @grantdelozier, thanks for reporting this. 

Unfortunately for some of the image transformations, grayscale images aren't currently compatible. Handling the different input formats in a more robust way is something I'm currently working on. Having these issues reported is really useful to know how to prioritise and which test cases that should pass.  

At the moment, grayscale images / masks are handled in a bit of a hacky way by adding an axis e.g. [here in mask2former](https://github.com/huggingface/transformers/blob/48706c7178127e7bcd6cccd90d941801e071a4a2/src/transformers/models/mask2former/image_processing_mask2former.py#L611). 

To understand the previous behaviour, could you share the feature extractor config and version of transformers which was working? The reason I ask is that when testing on commit `83e5a1060` - which added the BeiT model, the feature extractor also failed with a grayscale image input. 

Hi Amy,

First, thanks for all the awesome work in the transformers project!

My last known version transformers where grayscale worked is `transformers==4.24`

Here is my beit config:
```

{
    "architectures": [
      "BeitForMaskedImageModeling"
    ],
    "attention_probs_dropout_prob": 0.0,
    "drop_path_rate": 0.1,
    "hidden_act": "gelu",
    "hidden_dropout_prob": 0.0,
    "hidden_size": 768,
    "image_size": [512, 512],
    "size": [512, 512],
    "initializer_range": 0.02,
    "intermediate_size": 3072,
    "layer_norm_eps": 1e-12,
    "layer_scale_init_value": 0.1,
    "model_type": "beit",
    "num_attention_heads": 12,
    "num_channels": 1,
    "num_hidden_layers": 12,
    "patch_size": 16,
    "semantic_loss_ignore_index": 255,
    "torch_dtype": "float32",
    "use_absolute_position_embeddings": true,
    "use_auxiliary_head": true,
    "use_mask_token": true,
    "use_mean_pooling": true,
    "use_relative_position_bias": true,
    "use_shared_relative_position_bias": false,
    "do_center_crop": false,
    "vocab_size": 8192,
    "image_mean": [0.5],
    "image_std": [0.5]
}
```


@grantdelozier Thanks for sharing the config :) I'll use this as reference grayscale config to make sure everything works as expected in the fixes for accepting grayscale images. 

Just ran into this issue today as well, with both `ViTFeatureExtractor` and `MobileViTFeatureExtractor` (but I believe this is an issue with the base class anyway).

@amyeroberts Is there a problem with just expanding the dimensions of the image after converting to a numpy array / tensor?

e.g.,:
```bash
>>> example['image']
<PIL.PngImagePlugin.PngImageFile image mode=L size=28x28 at 0x7F0480FA54B0>
>>> feature_extractor(example['image'], return_tensors='pt')
...
ValueError: Unsupported number of image dimensions: 2
>>> feature_extractor(np.expand_dims(np.array(example['image']), 0), return_tensors='pt')
{'pixel_values': tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          ...,
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.]]]])}

```

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.