Getting an overflow error when running the code
I am getting the below error when I am trying to run the code for getting an output. I am not able to figure out why this is causing an error.
I am pre-processing the image to be compatible by following this logic:
def process_image_for_model(image, patch_size=14):
"""
Process image to ensure compatible dimensions with the vision transformer
Args:
image (PIL.Image): Input image
patch_size (int): Size of patches (default is 14 for this model)
"""
# Get current image size
width, height = image.size
# Calculate number of patches
patches_w = math.ceil(width / patch_size)
patches_h = math.ceil(height / patch_size)
# Calculate target size that's compatible with patch_size
target_width = patches_w * patch_size
target_height = patches_h * patch_size
# Create new image with padding if needed
new_image = Image.new('RGB', (target_width, target_height), (0, 0, 0))
new_image.paste(image, (0, 0))
return new_image
Error
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
Cell In[59], line 3
1 #question = 'Give the modality, organ, analysis, abnormalities (if any), treatment (if abnormalities are present)?'
2 msgs = [{'role': 'user', 'content': [image_processed, qs[0]]}]
----> 3 res = model.chat( image=image_processed, msgs=msgs, tokenizer=tokenizer, sampling=True, temperature=0.95, stream=True )
4 generated_text = ""
File ~/.cache/huggingface/modules/transformers_modules/openbmb/MiniCPM-Llama3-V-2_5/320a581d2195ad4a52140bb427a07f7207aeac6e/modeling_minicpmv.py:325, in MiniCPMV.chat(self, image, msgs, tokenizer, processor, vision_hidden_states, max_new_tokens, sampling, max_inp_length, system_prompt, stream, **kwargs)
322 copy_msgs = [sys_msg] + copy_msgs
324 prompt = processor.tokenizer.apply_chat_template(copy_msgs, tokenize=False, add_generation_prompt=True)
--> 325 inputs = processor(prompt, images, return_tensors="pt", max_length=max_inp_length).to(self.device)
327 if sampling:
328 generation_config = {
329 "top_p": 0.8,
330 "top_k": 100,
(...)
333 "repetition_penalty": 1.05
334 }
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/processing_minicpmv.py:109, in MiniCPMVProcessor.__call__(self, text, images, padding, truncation, max_length, do_pad, return_tensors)
107 if images is not None:
108 image_inputs = self.image_processor(images, do_pad=do_pad, return_tensors=return_tensors)
--> 109 return self._convert_images_texts_to_inputs(image_inputs, text, max_length=max_length)
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/processing_minicpmv.py:179, in MiniCPMVProcessor._convert_images_texts_to_inputs(self, images, texts, do_pad, truncation, max_length, return_tensors)
177 final_texts = ""
178 for i in range(len(image_tags)):
--> 179 final_texts = final_texts + text_chunks[i] + self.image_processor.get_slice_image_placeholder(image_sizes[0][i])
180 final_texts += text_chunks[-1]
181 input_ids, image_bounds = self._convert(final_texts, max_length)
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/image_processing_minicpmv.py:306, in MiniCPMVImageProcessor.get_slice_image_placeholder(self, image_size)
305 def get_slice_image_placeholder(self, image_size):
--> 306 grid = self.get_sliced_grid(image_size=image_size)
307 return (
308 self.im_start_token
309 + self.unk_token * self.image_feature_size
310 + self.im_end_token
311 ) + self.get_grid_placeholder(grid=grid)
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/image_processing_minicpmv.py:278, in MiniCPMVImageProcessor.get_sliced_grid(self, image_size)
276 log_ratio = math.log(original_width / original_height)
277 ratio = original_width * original_height / (self.scale_resolution * self.scale_resolution)
--> 278 multiple = min(math.ceil(ratio), self.max_slice_nums)
279 if multiple <= 1:
280 return None
OverflowError: cannot convert float infinity to integer
Try this for pre-processing your image
def resize_with_aspect_ratio(image, target_size):
# Get the original dimensions
width, height = image.size
# Calculate the aspect ratio
aspect_ratio = width / height
# Resize while maintaining aspect ratio
if width < height:
new_width = target_size
new_height = int(target_size / aspect_ratio)
else:
new_height = target_size
new_width = int(target_size * aspect_ratio)
# Resize the image
return image.resize((new_width, new_height))
##############################
def process_image(image_path, target_channels=3):
try:
# Open the image
image = Image.open(image_path).convert('RGB')
# Convert to numpy array (remains uint8)
width, height = image.size
if width > 1000 or height > 1000:
image_resized = resize_with_aspect_ratio(image, target_size=1000)
else:
image_resized = image
width, height = image_resized.size
return image_resized
except Exception as e:
print(f"Error in preprocess_image: {str(e)}")
return None
##############################
img_location="d:\img-001.jpg"
image=process_image(img_location)
Try this for pre-processing your image
def resize_with_aspect_ratio(image, target_size):
# Get the original dimensions
width, height = image.size# Calculate the aspect ratio aspect_ratio = width / height # Resize while maintaining aspect ratio if width < height: new_width = target_size new_height = int(target_size / aspect_ratio) else: new_height = target_size new_width = int(target_size * aspect_ratio) # Resize the image return image.resize((new_width, new_height))
##############################
def process_image(image_path, target_channels=3):
try:
# Open the image
image = Image.open(image_path).convert('RGB')
# Convert to numpy array (remains uint8)
width, height = image.size
if width > 1000 or height > 1000:
image_resized = resize_with_aspect_ratio(image, target_size=1000)
else:
image_resized = image
width, height = image_resized.size
return image_resizedexcept Exception as e: print(f"Error in preprocess_image: {str(e)}") return None
##############################
img_location="d:\img-001.jpg"
image=process_image(img_location)
I preprocessed my image using the following code, but it still throws an error。
def resize_with_aspect_ratio(image, target_size):
# Get the original dimensions
width, height = image.size
# Calculate the aspect ratio
aspect_ratio = width / height
# Resize while maintaining aspect ratio
if width < height:
new_width = target_size
new_height = int(target_size / aspect_ratio)
else:
new_height = target_size
new_width = int(target_size * aspect_ratio)
# Resize the image
return image.resize((new_width, new_height))
def process_image(image_path, target_channels=3):
try:
# Open the image
image = Image.open(image_path).convert('RGB')
# Convert to numpy array (remains uint8)
width, height = image.size
if width > 1000 or height > 1000:
image_resized = resize_with_aspect_ratio(image, target_size=1000)
else:
image_resized = image
width, height = image_resized.size
return image_resized
except Exception as e:
print(f"Error in preprocess_image: {str(e)}")
return None
#############################################
{
"name": "ValueError",
"message": "cannot convert float NaN to integer",
"stack": "---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[24], line 2
1 msgs = [{'role': 'user', 'content': [image, question]}]
----> 2 res = model.chat( image=image, msgs=msgs, tokenizer=tokenizer, sampling=True, temperature=0.95, stream=True )
3 generated_text = ""
5 for new_text in res:
File ~/.cache/huggingface/modules/transformers_modules/openbmb/MiniCPM-Llama3-V-2_5/320a581d2195ad4a52140bb427a07f7207aeac6e/modeling_minicpmv.py:325, in MiniCPMV.chat(self, image, msgs, tokenizer, processor, vision_hidden_states, max_new_tokens, sampling, max_inp_length, system_prompt, stream, **kwargs)
322 copy_msgs = [sys_msg] + copy_msgs
324 prompt = processor.tokenizer.apply_chat_template(copy_msgs, tokenize=False, add_generation_prompt=True)
--> 325 inputs = processor(prompt, images, return_tensors="pt", max_length=max_inp_length).to(self.device)
327 if sampling:
328 generation_config = {
329 "top_p": 0.8,
330 "top_k": 100,
(...)
333 "repetition_penalty": 1.05
334 }
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/processing_minicpmv.py:109, in MiniCPMVProcessor.call(self, text, images, padding, truncation, max_length, do_pad, return_tensors)
107 if images is not None:
108 image_inputs = self.image_processor(images, do_pad=do_pad, return_tensors=return_tensors)
--> 109 return self._convert_images_texts_to_inputs(image_inputs, text, max_length=max_length)
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/processing_minicpmv.py:179, in MiniCPMVProcessor._convert_images_texts_to_inputs(self, images, texts, do_pad, truncation, max_length, return_tensors)
177 final_texts = ""
178 for i in range(len(image_tags)):
--> 179 final_texts = final_texts + text_chunks[i] + self.image_processor.get_slice_image_placeholder(image_sizes[0][i])
180 final_texts += text_chunks[-1]
181 input_ids, image_bounds = self._convert(final_texts, max_length)
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/image_processing_minicpmv.py:306, in MiniCPMVImageProcessor.get_slice_image_placeholder(self, image_size)
305 def get_slice_image_placeholder(self, image_size):
--> 306 grid = self.get_sliced_grid(image_size=image_size)
307 return (
308 self.im_start_token
309 + self.unk_token * self.image_feature_size
310 + self.im_end_token
311 ) + self.get_grid_placeholder(grid=grid)
File ~/.cache/huggingface/modules/transformers_modules/ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1/b6c6d6bee35e94c4ca46175bf9a1c436ec74394f/image_processing_minicpmv.py:278, in MiniCPMVImageProcessor.get_sliced_grid(self, image_size)
276 log_ratio = math.log(original_width / original_height)
277 ratio = original_width * original_height / (self.scale_resolution * self.scale_resolution)
--> 278 multiple = min(math.ceil(ratio), self.max_slice_nums)
279 if multiple <= 1:
280 return None
ValueError: cannot convert float NaN to integer"
}