UX-Decoder / Semantic-SAM

[ECCV 2024] Official implementation of the paper "Semantic-SAM: Segment and Recognize Anything at Any Granularity"
2.28k stars 109 forks source link

wrong with multi GPU for demo code #88

Open WeiminLee opened 7 months ago

WeiminLee commented 7 months ago

self._stats[k] = torch.cat([self._stats[k], v], dim=0) RuntimeError: Expected all tensors to be on the same device, but found

from semantic_sam import prepare_image, plot_results, build_semantic_sam, SemanticSamAutomaticMaskGenerator original_image, input_image = prepare_image(image_pth=img_path) # change the image path to your image cptk_path = "/data/model_hub/SAM/seem_focall_v1.pt" mask_generator = SemanticSamAutomaticMaskGenerator(build_semantic_sam(model_type='L', ckpt=cptk_path)) # model_type: 'L' / 'T', depends on your checkpint masks = mask_generator.generate(input_image) plot_results(masks, original_image, save_path='./docs/daping/semantic_sam_uportal.jpg') # results and original images will be saved at save_path

FengLi-ust commented 6 months ago

Hi, demo is supposed to run on a single GPU.

vojtajina commented 4 months ago

I'm getting the same error on single GPU:

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_34181/1495446942.py in <module>
      3 original_image, input_image = prepare_image(image_pth=test_img_path)  # change the image path to your image
      4 mask_generator = SemanticSamAutomaticMaskGenerator(build_semantic_sam(model_type='L', ckpt='swinl_only_sam_many2many.pth')) # model_type: 'L' / 'T', depends on your checkpint
----> 5 masks = mask_generator.generate(input_image)
      6 plot_results(masks, original_image, save_path='./vis')  # results and original images will be saved at save_path

~/.local/lib/python3.10/site-packages/torch/utils/_contextlib.py in decorate_context(*args, **kwargs)
    113     def decorate_context(*args, **kwargs):
    114         with ctx_factory():
--> 115             return func(*args, **kwargs)
    116 
    117     return decorate_context

~/Semantic-SAM/tasks/automatic_mask_generator.py in generate(self, image)
    180 
    181         # Generate masks
--> 182         mask_data = self._generate_masks(image)
    183 
    184         # Filter small disconnected regions and holes in masks

~/Semantic-SAM/tasks/automatic_mask_generator.py in _generate_masks(self, image)
    223         data = MaskData()
    224         for crop_box, layer_idx in zip(crop_boxes, layer_idxs):
--> 225             crop_data = self._process_crop(image, crop_box, layer_idx, orig_size)
    226 
    227             data.cat(crop_data)

~/Semantic-SAM/tasks/automatic_mask_generator.py in _process_crop(self, image, crop_box, crop_layer_idx, orig_size)
    265         for (points,) in batch_iterator(self.points_per_batch, points_for_image):
    266             batch_data = self._process_batch(cropped_im,points, cropped_im_size, crop_box, orig_size)
--> 267             data.cat(batch_data)
    268             del batch_data
    269 

~/Semantic-SAM/utils/sam_utils/amg.py in cat(self, new_stats)
     62                 self._stats[k] = deepcopy(v)
     63             elif isinstance(v, torch.Tensor):
---> 64                 self._stats[k] = torch.cat([self._stats[k], v], dim=0)
     65             elif isinstance(v, np.ndarray):
     66                 self._stats[k] = np.concatenate([self._stats[k], v], axis=0)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument tensors in method wrapper_CUDA_cat)

Any ideas how to force everything on GPU?

vojtajina commented 4 months ago

This fixed the problem for me: https://github.com/UX-Decoder/Semantic-SAM/issues/66#issue-1898905876