facebookresearch / segment-anything

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
46.78k stars 5.54k forks source link

AttributeError: 'NoneType' object has no attribute 'set_image' #516

Open andysingal opened 1 year ago

andysingal commented 1 year ago

Greetings, While working on creating mask using detected box, i got the following error:

  def segment(image, sam_model, boxes):
    sam_model.set_image(image)
    H, W, _ = image.shape
    boxes_xyxy = box_ops.box_cxcywh_to_xyxy(boxes) * torch.Tensor([W, H, W, H])

    transformed_boxes = sam_model.transform.apply_boxes_torch(boxes_xyxy.to(device), image.shape[:2])
    masks, _, _ = sam_model.predict_torch(
        point_coords = None,
        point_labels = None,
        boxes = transformed_boxes,
        multimask_output = False,
        )
    return masks.cpu()

  def draw_mask(mask, image, random_color=True):
      if random_color:
          color = np.concatenate([np.random.random(3), np.array([0.8])], axis=0)
      else:
          color = np.array([30/255, 144/255, 255/255, 0.6])
      h, w = mask.shape[-2:]
      mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)

      annotated_frame_pil = Image.fromarray(image).convert("RGBA")
      mask_image_pil = Image.fromarray((mask_image.cpu().numpy() * 255).astype(np.uint8)).convert("RGBA")

      return np.array(Image.alpha_composite(annotated_frame_pil, mask_image_pil))

  segmented_frame_masks = segment(image_source, sam_predictor, boxes=detected_boxes)
  annotated_frame_with_mask = draw_mask(segmented_frame_masks[0][0], annotated_frame)
  Image.fromarray(annotated_frame_with_mask)

i got the error:

  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  [<ipython-input-74-5a61b3b98c1f>](https://localhost:8080/#) in <cell line: 1>()
  ----> 1 segmented_frame_masks = segment(image_source, sam_predictor, boxes=detected_boxes)
        2 annotated_frame_with_mask = draw_mask(segmented_frame_masks[0][0], annotated_frame)
        3 Image.fromarray(annotated_frame_with_mask)

  [<ipython-input-73-2bdf8f780a7e>](https://localhost:8080/#) in segment(image, sam_model, boxes)
       49 
       50 def segment(image, sam_model, boxes):
  ---> 51   sam_model.set_image(image)
       52   H, W, _ = image.shape
       53   boxes_xyxy = box_ops.box_cxcywh_to_xyxy(boxes) * torch.Tensor([W, H, W, H])

  AttributeError: 'NoneType' object has no attribute 'set_image'

article: https://medium.com/@amir_shakiba/sam-grounding-dino-stable-diffusion-segment-detect-change-da7926947286 colab: https://colab.research.google.com/drive/1d1zWF4YFQWwaEwYXEHEdl4jhzomZqXMb?usp=sharing

Looking forward to hearing from you Thanks

heyoeyo commented 1 year ago

Based on the error message, it seems like sam_model inside the segment(...) function has a value of None, and that leads to an error once you try to call the set_image(...) function on it.

In the code you provided, the sam_model is coming from the sam_predictor passed to the segment function, from this line: segmented_frame_masks = segment(image_source, sam_predictor, boxes=detected_boxes)

So that means that the sam_predictor variable is itself also None. In the colab link you provided, that variable is supposed to be set from calling the load_sam_model() function, but that function doesn't actually return anything. So to fix the problem that load_sam_model() function should probably include an extra line: return sam_model at the end of the function.

andysingal commented 1 year ago

Based on the error message, it seems like sam_model inside the segment(...) function has a value of None, and that leads to an error once you try to call the set_image(...) function on it.

In the code you provided, the sam_model is coming from the sam_predictor passed to the segment function, from this line: segmented_frame_masks = segment(image_source, sam_predictor, boxes=detected_boxes)

So that means that the sam_predictor variable is itself also None. In the colab link you provided, that variable is supposed to be set from calling the load_sam_model() function, but that function doesn't actually return anything. So to fix the problem that load_sam_model() function should probably include an extra line: return sam_model at the end of the function.

Thanks , let me check and get back to you. Please keep the ticket open