NielsRogge / Transformers-Tutorials

This repository contains demos I made with the Transformers library by HuggingFace.
MIT License
9.06k stars 1.41k forks source link

Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length. #453

Open code-amalps opened 2 months ago

code-amalps commented 2 months ago

Hi Team,

Good day.

I am new to the Image object detection. I was trying to implement the Object detection using DetrFeatureExtractor. When I try I am getting an error like " Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length. ". Previously it was working fine and few days ago it is getting break with this error.

The code I have been using :

` from transformers import DetrFeatureExtractor, DetrModel from PIL import Image import requests

   Object_detection = 'model/ObjectDetection'
  url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
  image = Image.open(requests.get(url, stream=True).raw)

  feature_extractor = DetrFeatureExtractor.from_pretrained(Object_detection,  local_files_only=True)
  model = DetrForObjectDetection.from_pretrained(Object_detection )
  inputs = feature_extractor(images=image, return_tensors="pt")
  outputs = model(**inputs)`

To find the issue I tried various steps from the raised tickets, what I found was when the _returntensors="pt" it is raising the error. As suggested in the error, I tried with Padding = True, but the response was the same. When I try with None it is not throwing any error instead I got an error in the output as "'list' object has no attribute 'shape'".

the versions I am using for the solution:

torch==2.2.2
sentence-transformers==2.2.2
transformers==4.41.2

Is there any way to resolve the issue? any help would be appreciated.

koga1694 commented 1 week ago

Is the numpy version 2.0 or higher? I downgraded the numpy version to 1.26.4 and resolved the issue. Try pip install numpy==1.26.4 and restart.