huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
135.34k stars 27.09k forks source link

Image Processor fails to process void segmentation maps #30064

Open antopost opened 7 months ago

antopost commented 7 months ago

System Info

Who can help?

No response

Information

Tasks

Reproduction

from transformers import Mask2FormerImageProcessor
import numpy as np

ignore_index = 255
image_processor = Mask2FormerImageProcessor(ignore_index=ignore_index,
                                                         do_resize=False,
                                                         do_rescale=False,
                                                         do_normalize=False)

image_norm = np.random.rand(4, 4, 3)

# Create void mask (all pixels have ignore_index)
semantic_mask = np.ones(image_norm.shape[:2], dtype=np.uint8)*255

semantic_mask = semantic_mask.astype(np.uint8)
print(semantic_mask)

inputs = image_processor(
    image_norm,
    segmentation_maps=semantic_mask,
    return_tensors='pt',
)

print(inputs)

===========================================================

[[255 255 255 255] [255 255 255 255] [255 255 255 255] [255 255 255 255]] Traceback (most recent call last): File "/home/anba/catkin_ws/src/tas_dev/dev/anba/Mask2Former/test.py", line 21, in inputs = image_processor( File "/home/anba/anaconda3/envs/SAM/lib/python3.10/site-packages/transformers/models/mask2former/image_processing_mask2former.py", line 566, in call return self.preprocess(images, segmentation_maps=segmentation_maps, **kwargs) File "/home/anba/anaconda3/envs/SAM/lib/python3.10/site-packages/transformers/models/mask2former/image_processing_mask2former.py", line 764, in preprocess encoded_inputs = self.encode_inputs( File "/home/anba/anaconda3/envs/SAM/lib/python3.10/site-packages/transformers/models/mask2former/image_processing_mask2former.py", line 943, in encode_inputs masks, classes = self.convert_segmentation_map_to_binary_masks( File "/home/anba/anaconda3/envs/SAM/lib/python3.10/site-packages/transformers/models/mask2former/image_processing_mask2former.py", line 558, in convert_segmentation_map_to_binary_masks return convert_segmentation_map_to_binary_masks( File "/home/anba/anaconda3/envs/SAM/lib/python3.10/site-packages/transformers/models/mask2former/image_processing_mask2former.py", line 284, in convert_segmentation_map_to_binary_masks binary_masks = np.stack(binary_masks, axis=0) # (num_labels, height, width) File "<__array_function__ internals>", line 180, in stack File "/home/anba/anaconda3/envs/SAM/lib/python3.10/site-packages/numpy/core/shape_base.py", line 422, in stack raise ValueError('need at least one array to stack') ValueError: need at least one array to stack

Process finished with exit code 1

Expected behavior

If this is intended that void masks should never be passed, then the result is fine.

However, when training segmentation models, shouldn't it be possible to include images with only background/void class?

ArthurZucker commented 7 months ago

cc @amyeroberts and @NielsRogge

NielsRogge commented 7 months ago

This looks like an edge case which isn't supported yet. Would require a PR to extend it for this use case

JINO-ROHIT commented 7 months ago

can i take this up? @NielsRogge

amyeroberts commented 7 months ago

@JINO-ROHIT Sure!

shriyashchougule commented 5 months ago

Any work around for this issue ? I am getting this error often for my use case.

amyeroberts commented 5 months ago

@shriyashchougule The other PR closed due to inactivity - feel free to open open if you'd like to tackle the issue!