You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I would like to retrieve the image embeddings produced by the encoder just like we can when running SAM2ImagePredictor as mentioned in this issue.
Issue
After looking at the code, it seems like even if we we use crop_n_layers=0, there is a call to reset_predictor which will erase the imag embeddings making it impossible to re-use them for other downstream use cases.
Workaround
The current workaround I found is a bit of a hack, I override the method reset_predictor of the predictor to be my custom function that saves the embeddings before executing the normal reset_predictor function which will set them to None.
classMaskGeneratorWrapper:
def__init__(self, mask_generator):
original_reset_predictor=mask_generator.predictor.__class__.reset_predictorself.img_embed= {}
defcustom_reset_predictor(cls):
self.img_embed=cls._featuresoriginal_reset_predictor(cls)
self.mask_generator=mask_generatorself.mask_generator.predictor.reset_predictor=custom_reset_predictor.__get__(mask_generator.predictor)
defpredict(self, image):
returnself.mask_generator.generate(image)
mask_generator_wrapper=MaskGeneratorWrapper(mask_generator)
predictions=mask_generator_wrapper.predict(rearrange(image, "c h w -> h w c").cpu().numpy())
Proposal
Would it be possible to add a flag to SAM2AutomaticMaskGenerator or to its generate method so that the image embeddings are preserved ?
The text was updated successfully, but these errors were encountered:
Hi,
I would like to retrieve the image embeddings produced by the encoder just like we can when running
SAM2ImagePredictor
as mentioned in this issue.Issue
After looking at the code, it seems like even if we we use
![image](https://private-user-images.githubusercontent.com/26939775/386379443-6a1297b1-71e9-4b06-8600-c81873a739d9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk3ODg5MDAsIm5iZiI6MTczOTc4ODYwMCwicGF0aCI6Ii8yNjkzOTc3NS8zODYzNzk0NDMtNmExMjk3YjEtNzFlOS00YjA2LTg2MDAtYzgxODczYTczOWQ5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE3VDEwMzY0MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWE0YzczNWJjZmEwN2E5ZDI1NDY4NWM1ZWFiN2ZmNTAyNTNlMzE4ZjQxZTVhMTJmNmNjMTEyYWExNDNmN2EyNWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.AgkZYDOjxvsGIi3sTXe4-rviuBqpNYpnNzgrPW1H0hc)
crop_n_layers=0
, there is a call toreset_predictor
which will erase the imag embeddings making it impossible to re-use them for other downstream use cases.Workaround
The current workaround I found is a bit of a hack, I override the method
reset_predictor
of the predictor to be my custom function that saves the embeddings before executing the normalreset_predictor
function which will set them toNone
.Proposal
Would it be possible to add a flag to
SAM2AutomaticMaskGenerator
or to itsgenerate
method so that the image embeddings are preserved ?The text was updated successfully, but these errors were encountered: