FastSAM icon indicating copy to clipboard operation
FastSAM copied to clipboard

RuntimeError: expected scalar type long int but found float

Open syntherick opened this issue 1 year ago • 10 comments

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

syntherick avatar Jun 30 '23 00:06 syntherick

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.

berry-ding avatar Jun 30 '23 02:06 berry-ding

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,)

syntherick avatar Jun 30 '23 02:06 syntherick

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?

berry-ding avatar Jun 30 '23 03:06 berry-ding

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

jq-learning avatar Jun 30 '23 09:06 jq-learning

@berry-ding Here is the image which I used image

syntherick avatar Jun 30 '23 14:06 syntherick

Hi, I have the exactly same error.

ggsDing avatar Jul 01 '23 09:07 ggsDing

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

ggsDing avatar Jul 01 '23 09:07 ggsDing

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 everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,) File "E:\OroChiLab\FastSAM\fastsam\model.py", line 99, in call return self.predict(source, stream, **kwargs) File "C:\ProgramData\miniconda3\envs\torch-py37\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "E:\OroChiLab\FastSAM\fastsam\model.py", line 51, in predict return self.predictor(source, stream=stream) File "C:\ProgramData\miniconda3\envs\torch-py37\lib\site-packages\ultralytics\yolo\engine\predictor.py", line 184, in call return list(self.stream_inference(source, model)) # merge list of Result into one File "C:\ProgramData\miniconda3\envs\torch-py37\lib\site-packages\torch\autograd\grad_mode.py", line 43, in generator_context response = gen.send(None) File "C:\ProgramData\miniconda3\envs\torch-py37\lib\site-packages\ultralytics\yolo\engine\predictor.py", line 244, in stream_inference self.results = self.postprocess(preds, im, im0s) File "E:\OroChiLab\FastSAM\fastsam\predict.py", line 27, in postprocess critical_iou_index = bbox_iou(full_box[0][:4], p[0][:, :4], iou_thres=0.9, image_shape=img.shape[2:]) File "E:\OroChiLab\FastSAM\fastsam\utils.py", line 50, in bbox_iou boxes = adjust_bboxes_to_image_border(boxes, image_shape) File "E:\OroChiLab\FastSAM\fastsam\utils.py", line 19, in adjust_bboxes_to_image_border boxes[:, 0] = torch.where(boxes[:, 0] < threshold, 0, boxes[:, 0]) # x1 RuntimeError: expected scalar type __int64 but found float

OroChippw avatar Jul 03 '23 03:07 OroChippw

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

d710055071 avatar Jul 03 '23 03:07 d710055071

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😋

OroChippw avatar Jul 03 '23 03:07 OroChippw

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.

an-yongqi avatar Jul 06 '23 01:07 an-yongqi

the same problem:

Traceback (most recent call last): File "Inference.py", line 119, in main(args) File "Inference.py", line 79, in main everything_results = model( File "/home/rui.li/projects/FastSAM/fastsam/model.py", line 99, in call return self.predict(source, stream, **kwargs) File "/home/rui.li/projects/FastSAM/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context return func(*args, **kwargs) File "/home/rui.li/projects/FastSAM/fastsam/model.py", line 51, in predict return self.predictor(source, stream=stream) File "/home/rui.li/projects/FastSAM/venv/lib/python3.8/site-packages/ultralytics/yolo/engine/predictor.py", line 188, in call return list(self.stream_inference(source, model)) # merge list of Result into one File "/home/rui.li/projects/FastSAM/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 45, in generator_context response = gen.send(None) File "/home/rui.li/projects/FastSAM/venv/lib/python3.8/site-packages/ultralytics/yolo/engine/predictor.py", line 248, in stream_inference self.results = self.postprocess(preds, im, im0s) File "/home/rui.li/projects/FastSAM/fastsam/predict.py", line 31, in postprocess critical_iou_index = bbox_iou(full_box[0][:4], p[0][:, :4], iou_thres=0.9, image_shape=img.shape[2:]) File "/home/rui.li/projects/FastSAM/fastsam/utils.py", line 39, in bbox_iou boxes = adjust_bboxes_to_image_border(boxes, image_shape) File "/home/rui.li/projects/FastSAM/fastsam/utils.py", line 17, in adjust_bboxes_to_image_border boxes[:, 0] = torch.where(boxes[:, 0] < threshold, 0, boxes[:, 0]) # x1 RuntimeError: expected scalar type long int but found float

susufqx avatar Jul 06 '23 08:07 susufqx

我不知道为什么,但我更改了 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.

berry-ding avatar Jul 06 '23 08:07 berry-ding