FastSAM
FastSAM copied to clipboard
RuntimeError: expected scalar type long int but found float
I get following error when I run the model
File /root/FastSAM/fastsam/utils.py:17, in adjust_bboxes_to_image_border(boxes, image_shape, threshold)
14 h, w = image_shape
16 # Adjust boxes
---> 17 boxes[:, 0] = torch.where(boxes[:, 0] < threshold, 0, boxes[:, 0]) # x1
18 boxes[:, 1] = torch.where(boxes[:, 1] < threshold, 0, boxes[:, 1]) # y1
19 boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, w, boxes[:, 2]) # x2
RuntimeError: expected scalar type long int but found float
Hi @syntherick , this appears to be a bug in the code, and we will resolve it as soon as possible. Could you please provide the inference code or command you used? This information would be very helpful for us to investigate the issue.
Hi @berry-ding, I get the error for either of following:
!python ./Inference.py --model_path ../FastSAM-x.pt --img_path ./image.jpg --imgsz 1024
and
from fastsam import FastSAM, FastSAMPrompt
model = FastSAM('../FastSAM-x.pt')
IMAGE_PATH = './image.jpg'
DEVICE = '0'
everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,)
Hello @syntherick , we have retested and were unable to reproduce the issue. Could you please git pull the latest version of the code and try again? If the problem persists, it might be a bug triggered by specific images. Could you please share the image that caused the problem with us?
I don't why, but I changed the code in utils.py 17-20, then it works well.
Adjust boxes
boxes[:, 0] = torch.where(boxes[:, 0] < threshold, torch.tensor(0,dtype=torch.float), boxes[:, 0]) # x1
boxes[:, 1] = torch.where(boxes[:, 1] < threshold, torch.tensor(0,dtype=torch.float), boxes[:, 1]) # y1
boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, torch.tensor(w,dtype=torch.float), boxes[:, 2]) # x2
boxes[:, 3] = torch.where(boxes[:, 3] > h - threshold, torch.tensor(h,dtype=torch.float), boxes[:, 3]) # y2
@berry-ding Here is the image which I used
Hi, I have the exactly same error.
I don't why, but I changed the code in utils.py 17-20, then it works well.
Adjust boxes
boxes[:, 0] = torch.where(boxes[:, 0] < threshold, torch.tensor(0,dtype=torch.float), boxes[:, 0]) # x1 boxes[:, 1] = torch.where(boxes[:, 1] < threshold, torch.tensor(0,dtype=torch.float), boxes[:, 1]) # y1 boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, torch.tensor(w,dtype=torch.float), boxes[:, 2]) # x2 boxes[:, 3] = torch.where(boxes[:, 3] > h - threshold, torch.tensor(h,dtype=torch.float), boxes[:, 3]) # y2
There will be bugs again again running on cuda devices
I meet the same proble , when use the inference demo you provide
from fastsam import FastSAM, FastSAMPrompt
model = FastSAM('./weights/FastSAM.pt')
IMAGE_PATH = './images/dogs.jpg'
DEVICE = 'cpu'
everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,)
prompt_process = FastSAMPrompt(IMAGE_PATH, everything_results, device=DEVICE)
# everything prompt
ann = prompt_process.everything_prompt()
# bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
ann = prompt_process.box_prompt(bbox=[200, 200, 300, 300])
# text prompt
ann = prompt_process.text_prompt(text='a photo of a dog')
# point prompt
# points default [[0,0]] [[x1,y1],[x2,y2]]
# point_label default [0] [1,0] 0:background, 1:foreground
ann = prompt_process.point_prompt(points=[[620, 360]], pointlabel=[1])
prompt_process.plot(annotations=ann,output='./output/',)
And it show runtimeerror like this
File "inference_demo.py", line 6, in
boxes[:, 0] = torch.where(boxes[:, 0] < threshold, torch.tensor(0,dtype=torch.float,device=boxes.device), boxes[:, 0]) # x1
boxes[:, 1] = torch.where(boxes[:, 1] < threshold, torch.tensor(0,dtype=torch.float,device=boxes.device), boxes[:, 1]) # y1
boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, torch.tensor(w,dtype=torch.float,device=boxes.device), boxes[:, 2]) # x2
boxes[:, 3] = torch.where(boxes[:, 3] > h - threshold, torch.tensor(h,dtype=torch.float,device=boxes.device), boxes[:, 3]) # y2
is ok
boxes[:, 0] = torch.where(boxes[:, 0] < threshold, torch.tensor(0,dtype=torch.float,device=boxes.device), boxes[:, 0]) # x1 boxes[:, 1] = torch.where(boxes[:, 1] < threshold, torch.tensor(0,dtype=torch.float,device=boxes.device), boxes[:, 1]) # y1 boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, torch.tensor(w,dtype=torch.float,device=boxes.device), boxes[:, 2]) # x2 boxes[:, 3] = torch.where(boxes[:, 3] > h - threshold, torch.tensor(h,dtype=torch.float,device=boxes.device), boxes[:, 3]) # y2
is ok
It worked thanks for your answer😋
We would close this issue for now to keep the issue tracker organized. However, if the problem persists or if you have any further questions, please feel free to comment here or open a new issue. We value your input and are happy to assist further.
the same problem:
Traceback (most recent call last):
File "Inference.py", line 119, in
我不知道为什么,但我更改了 utils.py 17-20 中的代码,然后它就可以正常工作了。
调整盒
boxes[:, 0] = torch.where(boxes[:, 0] < threshold, torch.tensor(0,dtype=torch.float), boxes[:, 0]) # x1 boxes[:, 1] = torch.where(boxes[:, 1] < threshold, torch.tensor(0,dtype=torch.float), boxes[:, 1]) # y1 boxes[:, 2] = torch.where(boxes[:, 2] > w - threshold, torch.tensor(w,dtype=torch.float), boxes[:, 2]) # x2 boxes[:, 3] = torch.where(boxes[:, 3] > h - threshold, torch.tensor(h,dtype=torch.float), boxes[:, 3]) # y2
Thank you! we have fixed our codes.