neverbiasu / ComfyUI-SAM2

A ComfyUI extension for Segment-Anything 2
72 stars 13 forks source link

AttributeError: 'ImageEncoder' object has no attribute 'img_size' #22

Closed BenDes21 closed 1 month ago

BenDes21 commented 2 months ago
Untictled

Any solution ?

Celtmant commented 2 months ago

same problem

Celtmant commented 2 months ago
Untictled

Any solution ?

In general, the problem is in Sam 2, I switched to the Sam 1 loader and everything works on the model from 2 gig,

neverbiasu commented 1 month ago

Could u show me your fully traceback?

SaintLandline commented 1 month ago
!!! Exception during processing!!! 'ImageEncoder' object has no attribute 'img_size'
Traceback (most recent call last):
  File "C:\ComfyUI_windows_portable\ComfyUI\execution.py", line 152, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ComfyUI_windows_portable\ComfyUI\execution.py", line 82, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ComfyUI_windows_portable\ComfyUI\execution.py", line 75, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfyui_segment_anything\node.py", line 325, in main
    (images, masks) = sam_segment(
                      ^^^^^^^^^^^^
  File "C:\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfyui_segment_anything\node.py", line 240, in sam_segment
    predictor = SamPredictorHQ(sam_model, sam_is_hq)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfyui_segment_anything\sam_hq\predictor.py", line 22, in __init__
    super().__init__(sam_model=sam_model)
  File "C:\ComfyUI_windows_portable\python_embeded\Lib\site-packages\segment_anything\predictor.py", line 31, in __init__
    self.transform = ResizeLongestSide(sam_model.image_encoder.img_size)
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ComfyUI_windows_portable\python_embeded\Lib\site-packages\torch\nn\modules\module.py", line 1709, in __getattr__
    raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'ImageEncoder' object has no attribute 'img_size'
coollofty commented 1 month ago

same problem, who solved?

Maelstrom2014 commented 1 month ago

same problem

neverbiasu commented 1 month ago

U r using comfyui_segment_anything node, I'll change our nodes name to distinguish it from the former.

coollofty commented 1 month ago

U r using comfyui_segment_anything node, I'll change our nodes name to distinguish it from the former.

It's not a problem with the name. I changed the name in the code and confirmed that the wrong node was not used, but the error still persists

NODE_CLASS_MAPPINGS = {
    'SAM2ModelLoader (sam2)': SAM2ModelLoader,
    'GroundingDinoModelLoader (sam2)': GroundingDinoModelLoader,
    'GroundingDinoSAMSegment (sam2)': GroundingDinoSAMSegment,
    'InvertMask (sam2)': InvertMask,
    "IsMaskEmpty (sam2)": IsMaskEmptyNode,
}

NODE_DISPLAY_NAME_MAPPINGS = {
    'SAM2ModelLoader': 'SAM2 Model Loader',
    'GroundingDinoModelLoader': 'Grounding Dino Model Loader',
    'GroundingDinoSAMSegment': 'Grounding Dino SAM Segment',
    'InvertMask': 'Invert Mask',
    "IsMaskEmpty": "Is Mask Empty",
}

Error during runtime:

Traceback (most recent call last): File "F:\ComfyUI\ComfyUI\execution.py", line 323, in execute output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\ComfyUI\execution.py", line 198, in get_output_data return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\ComfyUI\execution.py", line 169, in _map_node_over_list process_inputs(input_dict, i) File "F:\ComfyUI\ComfyUI\execution.py", line 158, in process_inputs results.append(getattr(obj, func)(**inputs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\ComfyUI\custom_nodes\ComfyUI-SAM2\node.py", line 311, in main (images, masks) = sam_segment( ^^^^^^^^^^^^ File "F:\ComfyUI\ComfyUI\custom_nodes\ComfyUI-SAM2\node.py", line 224, in sam_segment predictor = SAM2ImagePredictor(sam_model) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\ComfyUI\custom_nodes\ComfyUI-SAM2\sam2\sam2_image_predictor.py", line 41, in init self._transforms = SAM2Transforms( ^^^^^^^^^^^^^^^ File "F:\ComfyUI\ComfyUI\custom_nodes\ComfyUI-SAM2\sam2\utils\transforms.py", line 28, in init self.transforms = torch.jit.script( ^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_script.py", line 1432, in script return _script_impl( ^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_script.py", line 1146, in _script_impl return torch.jit._recursive.create_script_module( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 556, in create_script_module concrete_type = get_module_concrete_type(nn_module, share_types) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 505, in get_module_concrete_type concrete_type = concrete_type_store.get_or_create_concrete_type(nn_module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 437, in get_or_create_concrete_type concrete_type_builder = infer_concrete_type_builder(nn_module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 286, in infer_concrete_type_builder sub_concrete_type = get_module_concrete_type(item, share_types) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 505, in get_module_concrete_type concrete_type = concrete_type_store.get_or_create_concrete_type(nn_module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 437, in get_or_create_concrete_type concrete_type_builder = infer_concrete_type_builder(nn_module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 397, in infer_concrete_type_builder attr_type, inferred = infer_type(name, value) ^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_recursive.py", line 236, in infer_type attr_type = torch._C._jit_try_infer_type(item) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit\annotations.py", line 492, in try_ann_to_type scripted_class = torch.jit._script._recursive_compile_class(ann, loc) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch\jit_script.py", line 1602, in _recursive_compile_class rcb = _jit_internal.createResolutionCallbackForClassMethods(obj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch_jit_internal.py", line 473, in createResolutionCallbackForClassMethods captures.update(get_type_hint_captures(fn)) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\ComfyUI\python_embeded\Lib\site-packages\torch_jit_internal.py", line 387, in get_type_hint_captures raise OSError( OSError: Failed to get source for <function Enum._generate_nextvalue at 0x00000128B33F9080> using inspect.getsource

drake7707 commented 1 month ago

Having the same problem:

2024-09-24 13:16:45.202028 #28 [GroundingDinoModelLoader (segment anything)]: 4.87s
!!! Exception during processing !!! 'ImageEncoder' object has no attribute 'img_size'
Traceback (most recent call last):
  File "/comfyui/execution.py", line 317, in execute
    output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/comfyui/execution.py", line 192, in get_output_data
    return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/comfyui/execution.py", line 169, in _map_node_over_list
    process_inputs(input_dict, i)
  File "/comfyui/execution.py", line 158, in process_inputs
    results.append(getattr(obj, func)(**inputs))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/comfyui/custom_nodes/comfyui_segment_anything/node.py", line 325, in main
    (images, masks) = sam_segment(
                      ^^^^^^^^^^^^
  File "/comfyui/custom_nodes/comfyui_segment_anything/node.py", line 240, in sam_segment
    predictor = SamPredictorHQ(sam_model, sam_is_hq)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/comfyui/custom_nodes/comfyui_segment_anything/sam_hq/predictor.py", line 22, in __init__
    super().__init__(sam_model=sam_model)
  File "/opt/conda/lib/python3.11/site-packages/segment_anything/predictor.py", line 31, in __init__
    self.transform = ResizeLongestSide(sam_model.image_encoder.img_size)
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1729, in __getattr__
    raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'ImageEncoder' object has no attribute 'img_size'

Diving deeper into the code it's because SAM2 uses sam2.modeling.backbones.image_encoder whereas the SAM1 uses ImageEncoderViTHQ and TinyViT which both have an img_size property, which is apparently set to 1024 by default

drake7707 commented 1 month ago

Oh ok I understand what's going on. I have to uninstall the segment anything custom nodes so it's a naming issue. Once the nodes use all the nodes from this repo it works.

coollofty commented 1 month ago

Oh ok I understand what's going on. I have to uninstall the segment anything custom nodes so it's a naming issue. Once the nodes use all the nodes from this repo it works.

screen-capture

No, it's not you understand..... Look at this workflow, it uses all the nodes from this repo as mentioned above, and I have changed the names of all the nodes, so the nodes won't be used incorrectly, but the errors same

drake7707 commented 1 month ago

I had both ComfyUI-segment-anything-2 and comfyui_segment_anything in my custom nodes. Both of them have naming collisions with this repo. When I moved them out of my custom_nodes and restarted everything worked