storyicon / comfyui_segment_anything

Based on GroundingDino and SAM, use semantic strings to segment any element in an image. The comfyui version of sd-webui-segment-anything.
Apache License 2.0
529 stars 67 forks source link

'SAMWrapper' object has no attribute 'image_encoder' #61

Closed v4r4rth closed 2 months ago

v4r4rth commented 2 months ago

Updated to the latest ComfyUI and SAM + Grounding Dino seems to have broke, getting the following error in the UI:

Error occurred when executing GroundingDinoSAMSegment (segment anything):

'SAMWrapper' object has no attribute 'image_encoder'

File "/home/sid/ComfyUI/execution.py", line 151, in recursive_execute
output_data, output_ui = get_output_data(obj, input_data_all)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sid/ComfyUI/execution.py", line 81, in get_output_data
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sid/ComfyUI/execution.py", line 74, in map_node_over_list
results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sid/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 325, in main
(images, masks) = sam_segment(
^^^^^^^^^^^^
File "/home/sid/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 240, in sam_segment
predictor = SamPredictorHQ(sam_model, sam_is_hq)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sid/ComfyUI/custom_nodes/comfyui_segment_anything/sam_hq/predictor.py", line 22, in __init__
super().__init__(sam_model=sam_model)
File "/home/sid/miniconda3/envs/comfyui/lib/python3.11/site-packages/segment_anything/predictor.py", line 31, in __init__
self.transform = ResizeLongestSide(sam_model.image_encoder.img_size)
^^^^^^^^^^^^^^^^^^^^^^^

And the following in the terminal:

Traceback (most recent call last):
  File "/home/sid/ComfyUI/execution.py", line 151, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sid/ComfyUI/execution.py", line 81, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sid/ComfyUI/execution.py", line 74, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sid/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 325, in main
    (images, masks) = sam_segment(
                      ^^^^^^^^^^^^
  File "/home/sid/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 240, in sam_segment
    predictor = SamPredictorHQ(sam_model, sam_is_hq)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sid/ComfyUI/custom_nodes/comfyui_segment_anything/sam_hq/predictor.py", line 22, in __init__
    super().__init__(sam_model=sam_model)
  File "/home/sid/miniconda3/envs/comfyui/lib/python3.11/site-packages/segment_anything/predictor.py", line 31, in __init__
    self.transform = ResizeLongestSide(sam_model.image_encoder.img_size)
                                       ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'SAMWrapper' object has no attribute 'image_encoder'

Prompt executed in 14.11 seconds

Please suggest what I can try to fix//troubleshoot - the same workflow was working fine till yesterday...

mr-asa commented 2 months ago

I have the same problem

Traceback (most recent call last):
  File "F:\ComfyUI\execution.py", line 151, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
  File "F:\ComfyUI\execution.py", line 81, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
  File "F:\ComfyUI\custom_nodes\ComfyUI-0246\utils.py", line 381, in new_func
    res_value = old_func(*final_args, **kwargs)
  File "F:\ComfyUI\execution.py", line 74, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
  File "F:\ComfyUI\custom_nodes\comfyui_segment_anything\node.py", line 325, in main
    (images, masks) = sam_segment(
  File "F:\ComfyUI\custom_nodes\comfyui_segment_anything\node.py", line 240, in sam_segment
    predictor = SamPredictorHQ(sam_model, sam_is_hq)
  File "F:\ComfyUI\custom_nodes\comfyui_segment_anything\sam_hq\predictor.py", line 22, in __init__
    super().__init__(sam_model=sam_model)
  File "f:\ComfyUI\venv\lib\site-packages\segment_anything\predictor.py", line 31, in __init__
    self.transform = ResizeLongestSide(sam_model.image_encoder.img_size)
AttributeError: 'SAMWrapper' object has no attribute 'image_encoder'

Packages are:

addict            2.4.0
segment_anything  1.0
timm              0.9.12
yapf              0.40.2
MattMosquito commented 2 months ago

I have the same issue as well

stride-srl commented 2 months ago

The problem is with a naming duplication in ComfyUI-Impact-Pack node. Uninstall and retry ( if you want to fix this one you can change the name of this library with another one, the issue is on "SAMLoader" )

Donggukim commented 2 months ago

Same issue :( 스크린샷 2024-04-09 103924 스크린샷 2024-04-09 104017

v4r4rth commented 2 months ago

The problem is with a naming duplication in ComfyUI-Impact-Pack node. Uninstall and retry ( if you want to fix this one you can change the name of this library with another one, the issue is on "SAMLoader" )

You are right, I switched the Impact-Pack node with the node provided in this extension and my bug went away! I have three nodes which load the SAM models lol - next time just need to be more consistent with how I set up the workflow I guess, so for GroundingDino I will use the related SAM node, ofr other functions, maybe not.