segment-anything icon indicating copy to clipboard operation
segment-anything copied to clipboard

How do I feed batches to SamAutomaticMaskGenerator

Open rishiswethan opened this issue 1 year ago • 5 comments

I am not sure if this is available, but if it is, I'd love to know how to feed images in batches for SamAutomaticMaskGenerator

rishiswethan avatar Apr 12 '23 14:04 rishiswethan

following

Zhangwenyao1 avatar Apr 13 '23 10:04 Zhangwenyao1

following

Shar-01 avatar Apr 15 '23 18:04 Shar-01

following

haoxiangzhaotju avatar Apr 16 '23 17:04 haoxiangzhaotju

hi, you can feed a batch like this to the SAM image encoder with onnx runtime:

encoder_inputs = {"x": [batch[0,...].cpu().numpy(),
                          batch[1,...].cpu().numpy(),
                          batch[2,...].cpu().numpy()]
                          }
        output = self.sam_encoder_session.run(None, encoder_inputs)
        image_embedding = output[0]
        
        decoder_inputs = {
    "image_embeddings": image_embedding,
    "point_coords": onnx_coord,
    "point_labels": onnx_label,
    "mask_input": onnx_mask_input,
    "has_mask_input": onnx_has_mask_input,
    "orig_im_size": np.array(image.shape[:2], dtype=np.float32)
}

I am using the onnx model for the encoder part taken from here

I just copy-pasted this code snippet from one of my projects but you can adjust it to your needs. Though I used this code to only get the image embeddings from the encoder and used my own decoder for something else. but it should work with the SAM default decoder as well to get the masks. The code's a bit messy but I was just playing around and haven't had the time to optimize it

snawarhussain avatar Apr 17 '23 13:04 snawarhussain

following

zjr2000 avatar Apr 18 '23 03:04 zjr2000

https://github.com/ByungKwanLee/Full-Segment-Anything addresses the ciritical issues of SAM, which supports batch-input on the full-grid prompt (automatic mask generation) with post-processing: removing duplicated or small regions and holes, under flexible input image size

ByungKwanLee avatar Oct 16 '23 07:10 ByungKwanLee