【求助帖】2406期-林希老师-课程20(LLM->多模态理论+案例讲解),BLIP2运行第三步执行image caption任务出错 #562

Open
opened 2025-02-19 15:17:16 +08:00 by ChenChen · 2 comments

BLIP2执行第三段代码inputs = processor(image, return_tensors="pt").to(device, torch.float16)会出如下错误。尝试import numpy错误保持一样。(前两步获取图片和模型导入都是成功的。)确定环境pytorch,Cuda等版本都跟课件一致。按提示尝试加入padding=Ture的参数,运行结果仍然出一样的错。

RuntimeError Traceback (most recent call last)
File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:193, in BatchFeature.convert_to_tensors(self, tensor_type)
192 if not is_tensor(value):
--> 193 tensor = as_tensor(value)
195 self[key] = tensor

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:150, in BatchFeature._get_is_as_tensor_fns..as_tensor(value)
149 if isinstance(value, np.ndarray):
--> 150 return torch.from_numpy(value)
151 else:

RuntimeError: Numpy is not available

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
Cell In[19], line 3
1 #import numpy as np
2 #print(np.version)
----> 3 inputs = processor(image, return_tensors="pt").to(device, torch.float16)
5 generated_ids = model.generate(**inputs, max_new_tokens=30,max_length=51)
6 generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/models/blip_2/processing_blip_2.py:165, in Blip2Processor.call(self, images, text, audio, videos, **kwargs)
161 # add pixel_values encoding. If we also have text_encoding, update image encoding and return it.
162 # else, return the text encoding.
164 if images is not None:
--> 165 image_encoding = self.image_processor(images, **output_kwargs["images_kwargs"])
166 encoding.update(image_encoding)
167 return encoding

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/image_processing_utils.py:42, in BaseImageProcessor.call(self, images, **kwargs)
40 def call(self, images, **kwargs) -> BatchFeature:
41 """Preprocess an image or a batch of images."""
---> 42 return self.preprocess(images, **kwargs)

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/utils/generic.py:854, in filter_out_non_signature_kwargs..decorator..wrapper(*args, **kwargs)
845 cls_prefix = ""
847 warnings.warn(
848 f"The following named arguments are not valid for {cls_prefix}{func.__name__}"
849 f" and were ignored: {invalid_kwargs_names}",
850 UserWarning,
851 stacklevel=2,
852 )
--> 854 return func(*args, **valid_kwargs)

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/models/blip/image_processing_blip.py:291, in BlipImageProcessor.preprocess(self, images, do_resize, size, resample, do_rescale, rescale_factor, do_normalize, image_mean, image_std, return_tensors, do_convert_rgb, data_format, input_data_format)
282 images = [
283 self.normalize(image=image, mean=image_mean, std=image_std, input_data_format=input_data_format)
284 for image in images
285 ]
287 images = [
288 to_channel_dimension_format(image, data_format, input_channel_dim=input_data_format) for image in images
289 ]
--> 291 encoded_outputs = BatchFeature(data={"pixel_values": images}, tensor_type=return_tensors)
293 return encoded_outputs

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:79, in BatchFeature.init(self, data, tensor_type)
77 def init(self, data: Optional[Dict[str, Any]] = None, tensor_type: Union[None, str, TensorType] = None):
78 super().init(data)
---> 79 self.convert_to_tensors(tensor_type=tensor_type)

File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:199, in BatchFeature.convert_to_tensors(self, tensor_type)
197 if key == "overflowing_values":
198 raise ValueError("Unable to create tensor returning overflowing values of different lengths. ")
--> 199 raise ValueError(
200 "Unable to create tensor, you should probably activate padding "
201 "with 'padding=True' to have batched tensors with the same length."
202 )
204 return self

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

**BLIP2**执行第三段代码inputs = processor(image, return_tensors="pt").to(device, torch.float16)会出如下错误。尝试import numpy错误保持一样。(前两步获取图片和模型导入都是成功的。)确定环境pytorch,Cuda等版本都跟课件一致。按提示尝试加入padding=Ture的参数,运行结果仍然出一样的错。 --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:193, in BatchFeature.convert_to_tensors(self, tensor_type) 192 if not is_tensor(value): --> 193 tensor = as_tensor(value) 195 self[key] = tensor File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:150, in BatchFeature._get_is_as_tensor_fns.<locals>.as_tensor(value) 149 if isinstance(value, np.ndarray): --> 150 return torch.from_numpy(value) 151 else: RuntimeError: Numpy is not available During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) Cell In[19], line 3 1 #import numpy as np 2 #print(np.__version__) ----> 3 inputs = processor(image, return_tensors="pt").to(device, torch.float16) 5 generated_ids = model.generate(**inputs, max_new_tokens=30,max_length=51) 6 generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip() File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/models/blip_2/processing_blip_2.py:165, in Blip2Processor.__call__(self, images, text, audio, videos, **kwargs) 161 # add pixel_values encoding. If we also have text_encoding, update image encoding and return it. 162 # else, return the text encoding. 164 if images is not None: --> 165 image_encoding = self.image_processor(images, **output_kwargs["images_kwargs"]) 166 encoding.update(image_encoding) 167 return encoding File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/image_processing_utils.py:42, in BaseImageProcessor.__call__(self, images, **kwargs) 40 def __call__(self, images, **kwargs) -> BatchFeature: 41 """Preprocess an image or a batch of images.""" ---> 42 return self.preprocess(images, **kwargs) File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/utils/generic.py:854, in filter_out_non_signature_kwargs.<locals>.decorator.<locals>.wrapper(*args, **kwargs) 845 cls_prefix = "" 847 warnings.warn( 848 f"The following named arguments are not valid for `{cls_prefix}{func.__name__}`" 849 f" and were ignored: {invalid_kwargs_names}", 850 UserWarning, 851 stacklevel=2, 852 ) --> 854 return func(*args, **valid_kwargs) File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/models/blip/image_processing_blip.py:291, in BlipImageProcessor.preprocess(self, images, do_resize, size, resample, do_rescale, rescale_factor, do_normalize, image_mean, image_std, return_tensors, do_convert_rgb, data_format, input_data_format) 282 images = [ 283 self.normalize(image=image, mean=image_mean, std=image_std, input_data_format=input_data_format) 284 for image in images 285 ] 287 images = [ 288 to_channel_dimension_format(image, data_format, input_channel_dim=input_data_format) for image in images 289 ] --> 291 encoded_outputs = BatchFeature(data={"pixel_values": images}, tensor_type=return_tensors) 293 return encoded_outputs File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:79, in BatchFeature.__init__(self, data, tensor_type) 77 def __init__(self, data: Optional[Dict[str, Any]] = None, tensor_type: Union[None, str, TensorType] = None): 78 super().__init__(data) ---> 79 self.convert_to_tensors(tensor_type=tensor_type) File ~/miniconda3/envs/blip2_me/lib/python3.10/site-packages/transformers/feature_extraction_utils.py:199, in BatchFeature.convert_to_tensors(self, tensor_type) 197 if key == "overflowing_values": 198 raise ValueError("Unable to create tensor returning overflowing values of different lengths. ") --> 199 raise ValueError( 200 "Unable to create tensor, you should probably activate padding " 201 "with 'padding=True' to have batched tensors with the same length." 202 ) 204 return self ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.
ChenChen changed title from 【求助帖】2406期-Ray老师-课程20(LLM->多模态理论+案例讲解),BLIP2运行第三步执行image caption任务出错 to 【求助帖】2406期-林希老师-课程20(LLM->多模态理论+案例讲解),BLIP2运行第三步执行image caption任务出错 2025-02-20 14:04:01 +08:00

RuntimeError: Numpy is not available 说明没有安装numpy,需要安装下numpy。
ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length. 虽然报的是批次的错,但我觉得可能是有些库的版本问题,比如transformers的版本,可以排查下。

RuntimeError: Numpy is not available 说明没有安装numpy,需要安装下numpy。 ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length. 虽然报的是批次的错,但我觉得可能是有些库的版本问题,比如transformers的版本,可以排查下。
Author

RuntimeError: Numpy is not available 说明没有安装numpy,需要安装下numpy。
ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length. 虽然报的是批次的错,但我觉得可能是有些库的版本问题,比如transformers的版本,可以排查下。

重装了一下,python必须升级到3.10.问题就都没有了。(numpy版本是1.24.x)

> RuntimeError: Numpy is not available 说明没有安装numpy,需要安装下numpy。 > ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length. 虽然报的是批次的错,但我觉得可能是有些库的版本问题,比如transformers的版本,可以排查下。 重装了一下,python必须升级到3.10.问题就都没有了。(numpy版本是1.24.x)
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: HswOAuth/llm_course#562
No description provided.