Function infer_channel_dimension_format has a bug
Author: aleksmirosh
Pull request: no

### System Info

Hi, I am working on my own DataLoader.
So my input to class transformers.SegformerImageProcessor has shape (6, 512, 512).
So channel first.

As I realized the problem is **image.shape[first_dim]** for me is 0, 
but your if construction said channel dimension should be always 1 or 3, 
which does not make sense for 3 dim images, because above you assigned first_dim, last_dim = 0, 2. 

@amyeroberts
not sure I tagged right person, sorry 

### Who can help?

_No response_

### 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

```

def infer_channel_dimension_format(image: np.ndarray) -> ChannelDimension:
    if image.ndim == 3:
        first_dim, last_dim = 0, 2
    elif image.ndim == 4:
        first_dim, last_dim = 1, 3
    else:
        raise ValueError(f"Unsupported number of image dimensions: {image.ndim}")

    if image.shape[first_dim] in (1, 3):
        return ChannelDimension.FIRST
    elif image.shape[last_dim] in (1, 3):
        return ChannelDimension.LAST
    raise ValueError("Unable to infer channel dimension format")
```
any image wish a shape (ch, w, h)
infer_channel_dimension_format(image)

### Expected behavior

I expected function will return ChannelDimension.FIRST for image in shape (CH, W, H)

Comments:
Hi @aleksmirosh - you've tagged the right person :) Thanks for raising this issue. 

The functions in the image transforms library currently only support grayscale (1 channel) and RGB (3 channel) images. What format are the images you're trying to use? 

@amyeroberts 
Thank you for your quick response and sorry for not complet describing of the issue.
So I use 6 channels. 
I used version 4.24.0 before and tried to update it to the latest.
Do you plan to update to any channel number in the future? 


There aren't any immediate plans to accept an arbitrary number of channels. I agree it would be useful and will add it to the potential extensions of the library. 

For the 6 channel images - are these concatenations of 2, 3-channel images or a single image with 6 channels? If the former, the simplest way to get this working quickly would be to pass each of 3 RBG images to the image processor and then concatenate. However, this will likely be quite inefficient. 

Hello! Just wanted to add that I've come across this issue as well, but using the CLIPProcessor which uses https://github.com/huggingface/transformers/blob/2f320661f364557c821c285729dab3881e977363/src/transformers/image_transforms.py#L304

Basically I pass in images that always have the dimension (H,W,C), but occasionally I'll get images that are (1,1,3) or (3,*,3), but in both cases the first dimension is inferred as the channel dimension, which is not what I intended. The (3,1,3) case will not error, but silently proceed, but the (1,1,3) case errors for me b/c the mean is of length 3 but the inferred num_channels of the image is 1

> For the 6 channel images - are these concatenations of 2, 3-channel images or a single image with 6 channels? If the former, the simplest way to get this working quickly would be to pass each of 3 RBG images to the image processor and then concatenate. However, this will likely be quite inefficient.

this is 6 channels single image, but I will try to use concatenation. thank you for the advice.
Also if using the parameter 'data_format=None' could it help?

@terrykong maybe data_format=None could help with your case?

Thanks for the suggestion @aleksmirosh. Unfortunately, that kwarg only affects the output format: https://github.com/huggingface/transformers/blob/2f320661f364557c821c285729dab3881e977363/src/transformers/image_transforms.py#L322-L323

The input format is not configurable and is inferred: https://github.com/huggingface/transformers/blob/2f320661f364557c821c285729dab3881e977363/src/transformers/image_transforms.py#L340

It would be nice if the input format could be specified.

Hi @aleksmirosh , `data_format` specifies the desired output format for the images. I'm working on a PR to add the option to specify for the input format @terrykong 

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.

Quick fix for 6 channels images: (Can be used for any number of channels that does not collide with image height or width). Change (1, 6) as required.



def infer_channel_dimension_format(image: np.ndarray) -> ChannelDimension:
    if image.ndim == 3:
        first_dim, last_dim = 0, 2
    elif image.ndim == 4:
        first_dim, last_dim = 1, 3
    else:
        raise ValueError(f"Unsupported number of image dimensions: {image.ndim}")

    if image.shape[first_dim] in (1, 6):
        return ChannelDimension.FIRST
    elif image.shape[last_dim] in (1, 6):
        return ChannelDimension.LAST
    raise ValueError("Unable to infer channel dimension format")