luca-medeiros / lang-segment-anything

SAM with text prompt
Apache License 2.0
1.74k stars 192 forks source link

How to custom checkpoint correctly? #36

Closed forestbat closed 1 year ago

forestbat commented 1 year ago

I have read this PR, and I wrote this code:

# 'VIT_H SAM Model/sam_vit_h_4b8939.pth' is my directory of model file
sam = LangSAM('VIT_H SAM Model/sam_vit_h_4b8939.pth')

And it crashed with this error:

self = <samgeo.text_sam.LangSAM object at 0x00000210B416E860>
model_type = 'VIT_H SAM Model/sam_vit_h_4b8939.pth'

    def build_sam(self, model_type):
        """Build the SAM model.

        Args:
            model_type (str, optional): The model type. It can be one of the following: vit_h, vit_l, vit_b.
                Defaults to 'vit_h'. See https://bit.ly/3VrpxUh for more details.
        """
>       checkpoint_url = SAM_MODELS[model_type]
E       KeyError: 'VIT_H SAM Model/sam_vit_h_4b8939.pth'

And even if I use sam = LangSAM(ckpt_path='VIT_H SAM Model/sam_vit_h_4b8939.pth') or sam = LangSAM('vit_h', 'VIT_H SAM Model/sam_vit_h_4b8939.pth') , it isn't effective. So how to custom checkpoint correctly?

luca-medeiros commented 1 year ago

Based on the 'error' message you sent, seems you have modified the code. Please try using main branch's code and if error raises, upload the whole error traceback.

forestbat commented 1 year ago

I have re-installed lang-sam and its version in pip list is 0.1.0. However it still crashed and traceback is same as the last traceback:

Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertModel: ['cls.predictions.transform.dense.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias']
- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).

main.py:5 (test_prompt_samgeo)
def test_prompt_samgeo():
        image = "pics/hainanm3-sentinel-10.jpg"
>       sam = LangSAM('VIT_H SAM Model/sam_vit_h_4b8939.pth')

main.py:22: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\ProgramData\anaconda3\envs\graduate\lib\site-packages\samgeo\text_sam.py:122: in __init__
    self.build_sam(model_type)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <samgeo.text_sam.LangSAM object at 0x00000280D399F370>
model_type = 'VIT_H SAM Model/sam_vit_h_4b8939.pth'

    def build_sam(self, model_type):
        """Build the SAM model.

        Args:
            model_type (str, optional): The model type. It can be one of the following: vit_h, vit_l, vit_b.
                Defaults to 'vit_h'. See https://bit.ly/3VrpxUh for more details.
        """
>       checkpoint_url = SAM_MODELS[model_type]
E       KeyError: 'VIT_H SAM Model/sam_vit_h_4b8939.pth'

C:\ProgramData\anaconda3\envs\graduate\lib\site-packages\samgeo\text_sam.py:139: KeyError
luca-medeiros commented 1 year ago

this traceback seems to be from a file called text_sam.py which doesn't exist in the library. The build_sam method too seems to be modified. I cant help you if you are not using lang_sam from main branch.