# ValueError: Unable to create tensor, activate padding with 'padding=True'

**URL:** <https://discuss.huggingface.co/t/valueerror-unable-to-create-tensor-activate-padding-with-padding-true/118310>\
**Category:** Beginners\
**Created:** [November 13, 2024, 9:00am UTC](https://discuss.huggingface.co/t/valueerror-unable-to-create-tensor-activate-padding-with-padding-true/118310 "2024-11-13T09:00:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ojdeboer](https://avatars.discourse-cdn.com/v4/letter/o/ba9def/32.png) [@ojdeboer](https://discuss.huggingface.co/u/ojdeboer)\
**Post date:** [November 13, 2024, 9:00am UTC](https://discuss.huggingface.co/t/valueerror-unable-to-create-tensor-activate-padding-with-padding-true/118310/1 "2024-11-13T09:00:50Z")

</div>

I’m working on training a model for image segmentation using mask2former, but I’m encountering an issue when using AutoImageProcessor. I’ve included my code and environment details below. My input images and masks are RGB 8 bit images resp, and 512\*512 pixels. Adding ‘padding=true’ to the processor does not solve the problem.

My code:

‘’’  
import os  
from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation, Trainer, TrainingArguments  
import torch  
from PIL import Image  
import numpy as np

local\_save\_path = “./saved\_models/m2f/”  
model\_name = “facebook/mask2former-swin-large-coco-instance”

model = Mask2FormerForUniversalSegmentation.from\_pretrained(model\_name)  
processor = AutoImageProcessor.from\_pretrained(model\_name)

class SegmentationDataset:  
def **init** (self, image\_dir, mask\_dir, processor, image\_size=(512, 512)):  
self.image\_dir = image\_dir  
self.mask\_dir = mask\_dir  
self.images = sorted(os.listdir(image\_dir))  
self.masks = sorted(os.listdir(mask\_dir))  
self.processor = processor  
self.image\_size = image\_size

```
def __len__ (self):
    return len(self.images)

def __getitem__ (self, idx):
    image_path = os.path.join(self.image_dir, self.images[idx])
    mask_path = os.path.join(self.mask_dir, self.masks[idx])

    image = Image.open(image_path).convert("RGB").resize(self.image_size)
    mask = Image.open(mask_path).convert("L").resize(self.image_size)

    image = np.array(image,dtype=np.float32)
    mask = np.array(mask, dtype=np.float32)

    print(f"Image shape: {image.shape}")
    print(f"Mask shape: {mask.shape}")

    encoded_inputs = self.processor(
        images=image,
        segmentation_maps=mask,
        return_tensors="pt"
    )

    print(f"Image shape after processing: {encoded_inputs['pixel_values'].shape}")
    print(f"Mask shape after processing: {encoded_inputs['labels'].shape}")

    return encoded_inputs

```

# Initialize datasets

train\_dataset = SegmentationDataset(  
image\_dir=“/mnt/data1/pancreas/exp3/debug\_dataset/images/train”,  
mask\_dir=“/mnt/data1/pancreas/exp3/debug\_dataset/masks/train”,  
processor=processor  
)

val\_dataset = SegmentationDataset(  
image\_dir=“/mnt/data1/pancreas/exp3/debug\_dataset/images/val”,  
mask\_dir=“/mnt/data1/pancreas/exp3/debug\_dataset/masks/val”,  
processor=processor  
)

# Training arguments

training\_args = TrainingArguments(  
output\_dir=local\_save\_path,  
eval\_strategy=“epoch”,  
per\_device\_train\_batch\_size=2,  
per\_device\_eval\_batch\_size=2,  
num\_train\_epochs=10,  
save\_steps=1000,  
save\_total\_limit=2,  
logging\_dir=‘./logs’,  
logging\_steps=100,  
dataloader\_drop\_last=True # Drops last incomplete batch if present  
)

# Initialize Trainer

trainer = Trainer(  
model=model,  
args=training\_args,  
train\_dataset=train\_dataset,  
eval\_dataset=val\_dataset,  
)

# Train the model

trainer.train()  
‘’’

And the output/error message:

/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/utils/deprecation.py:165: UserWarning: The following named arguments are not valid for `Mask2FormerImageProcessor. __init__ ` and were ignored: ‘\_max\_size’  
return func(\*args, \*\*kwargs)  
0%| | 0/2500 [00:00\<?, ?it/s]Image shape: (512, 512, 3)  
Mask shape: (512, 512)  
Traceback (most recent call last):  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/feature\_extraction\_utils.py”, line 193, in convert\_to\_tensors  
tensor = as\_tensor(value)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/feature\_extraction\_utils.py”, line 150, in as\_tensor  
return torch.from\_numpy(value)  
TypeError: expected np.ndarray (got numpy.ndarray)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):  
File “/mnt/home/pycharm/huggingface/train.py”, line 88, in   
trainer.train()  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/trainer.py”, line 2123, in train  
return inner\_training\_loop(  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/trainer.py”, line 2427, in \_inner\_training\_loop  
batch\_samples, num\_items\_in\_batch = self.get\_batch\_samples(epoch\_iterator, num\_batches)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/trainer.py”, line 5045, in get\_batch\_samples  
batch\_samples += [next(epoch\_iterator)]  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/accelerate/data\_loader.py”, line 552, in **iter**  
current\_batch = next(dataloader\_iter)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/torch/utils/data/dataloader.py”, line 701, in **next**  
data = self.\_next\_data()  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/torch/utils/data/dataloader.py”, line 757, in \_next\_data  
data = self.\_dataset\_fetcher.fetch(index) # may raise StopIteration  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/torch/utils/data/\_utils/fetch.py”, line 52, in fetch  
data = [self.dataset[idx] for idx in possibly\_batched\_index]  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/torch/utils/data/\_utils/fetch.py”, line 52, in   
data = [self.dataset[idx] for idx in possibly\_batched\_index]  
File “/mnt/home/pycharm/huggingface/train.py”, line 40, in **getitem**  
encoded\_inputs = self.processor(  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/models/mask2former/image\_processing\_mask2former.py”, line 573, in **call**  
return self.preprocess(images, segmentation\_maps=segmentation\_maps, \*\*kwargs)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/utils/deprecation.py”, line 165, in wrapped\_func  
return func(\*args, \*\*kwargs)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/utils/generic.py”, line 852, in wrapper  
return func(\*args, \*\*valid\_kwargs)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/models/mask2former/image\_processing\_mask2former.py”, line 768, in preprocess  
encoded\_inputs = self.encode\_inputs(  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/models/mask2former/image\_processing\_mask2former.py”, line 931, in encode\_inputs  
encoded\_inputs = self.pad(  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/models/mask2former/image\_processing\_mask2former.py”, line 861, in pad  
return BatchFeature(data=data, tensor\_type=return\_tensors)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/feature\_extraction\_utils.py”, line 79, in **init**  
self.convert\_to\_tensors(tensor\_type=tensor\_type)  
File “/home/ojdeboer/anaconda3/envs/hf/lib/python3.10/site-packages/transformers/feature\_extraction\_utils.py”, line 199, in convert\_to\_tensors  
raise ValueError(  
ValueError: Unable to create tensor, you should probably activate padding with ‘padding=True’ to have batched tensors with the same length.  
0%| | 0/2500 [00:00\<?, ?it/s]

Process finished with exit code 1

My transformers environment:

- `transformers` version: 4.46.2
- Platform: Linux-6.8.0-48-generic-x86\_64-with-glibc2.35
- Python version: 3.10.15
- Huggingface\_hub version: 0.24.6
- Safetensors version: 0.4.5
- Accelerate version: 1.1.1
- Accelerate config: not found
- PyTorch version (GPU?): 2.5.1 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: 
- Using GPU in script?: 
- GPU type: NVIDIA GeForce RTX 3090

I suspect this issue might be due to the shape of the processed data or the need for padding. Should I modify the processor or handle padding differently to enable batched tensors? Thank you very much in advance for any advice!

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [November 13, 2024, 11:55am UTC](https://discuss.huggingface.co/t/valueerror-unable-to-create-tensor-activate-padding-with-padding-true/118310/2 "2024-11-13T11:55:47Z")

</div>

It is known that there is a bug in the current Transformer image preprocessor, and as we are still investigating the details, there is no reliable workaround.  
If this error is due to the bug, there is a possibility that the following workaround can be used to avoid it.  
In other words, it is possible to avoid the error by performing as much of the preprocessing as possible here and passing it in **PIL.Image.Image** format.

```py
    #image = np.array(image,dtype=np.float32)
    #mask = np.array(mask, dtype=np.float32)

    #print(f"Image shape: {image.shape}")
    #print(f"Mask shape: {mask.shape}")

    encoded_inputs = self.processor(
        images=[image],
        segmentation_maps=[mask], # Maybe I shouldn't make a list. I don't know.
        return_tensors="pt"
    )

```

---

<div class="post-metadata">

**Author:** ![ojdeboer](https://avatars.discourse-cdn.com/v4/letter/o/ba9def/32.png) [@ojdeboer](https://discuss.huggingface.co/u/ojdeboer)\
**Post date:** [November 13, 2024, 2:03pm UTC](https://discuss.huggingface.co/t/valueerror-unable-to-create-tensor-activate-padding-with-padding-true/118310/3 "2024-11-13T14:03:19Z")

</div>

Thank you John,  
Problem is solved, it had to do with an old Numpy version…

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [November 13, 2024, 2:04pm UTC](https://discuss.huggingface.co/t/valueerror-unable-to-create-tensor-activate-padding-with-padding-true/118310/4 "2024-11-13T14:04:51Z")

</div>

Oh… numpy\<2 problem…
