danielgatis / rembg

Rembg is a tool to remove images background
MIT License
16.48k stars 1.84k forks source link

[BUG] sam model doesn't work #500

Closed mkabatek closed 1 year ago

mkabatek commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

Attempting to run the following command rembg i -m sam test.png sam.png

lol@lols-MacBook-Pro rembg-testing % rembg i -m sam test.png sam.png
Traceback (most recent call last):
  File "/opt/homebrew/bin/rembg", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/rembg/cli.py", line 33, in main
    _main()  # type: ignore
    ^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/rembg/commands/i_command.py", line 93, in i_command
    output.write(remove(input.read(), session=new_session(model, **kwargs), **kwargs))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/rembg/bg.py", line 164, in remove
    masks = session.predict(img, *args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/rembg/sessions/sam.py", line 89, in predict
    image = self.normalize(image)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/rembg/sessions/sam.py", line 77, in normalize
    x = (img - pixel_mean) / pixel_std
         ~~~~^~~~~~~~~~~~
ValueError: operands could not be broadcast together with shapes (1024,1024) (1,1,3)

Expected behavior I expect that the program doesn't crash.

OS Version: macOS 13.0

Rembg version: version 2.0.50

vbarbarosh commented 1 year ago

I faced with the same error.

$ rembg i -m sam -x '{"input_labels": [1], "input_points": [[10,10]]}' input.png out.png
/home/vb/.local/lib/python3.10/site-packages/numba/np/ufunc/parallel.py:371: NumbaWarning: The TBB threading layer requires TBB version 2021 update 6 or later i.e., TBB_INTERFACE_VERSION >= 12060. Found TBB_INTERFACE_VERSION = 12050. The TBB threading layer is disabled.
  warnings.warn(problem)
Traceback (most recent call last):
  File "/home/vb/.local/bin/rembg", line 8, in <module>
    sys.exit(main())
  File "/home/vb/.local/lib/python3.10/site-packages/rembg/cli.py", line 33, in main
    _main()  # type: ignore
  File "/home/vb/.local/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/home/vb/.local/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/home/vb/.local/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/vb/.local/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/vb/.local/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/home/vb/.local/lib/python3.10/site-packages/rembg/commands/i_command.py", line 93, in i_command
    output.write(remove(input.read(), session=new_session(model, **kwargs), **kwargs))
  File "/home/vb/.local/lib/python3.10/site-packages/rembg/bg.py", line 164, in remove
    masks = session.predict(img, *args, **kwargs)
  File "/home/vb/.local/lib/python3.10/site-packages/rembg/sessions/sam.py", line 89, in predict
    image = self.normalize(image)
  File "/home/vb/.local/lib/python3.10/site-packages/rembg/sessions/sam.py", line 77, in normalize
    x = (img - pixel_mean) / pixel_std
ValueError: operands could not be broadcast together with shapes (1024,1024,4) (1,1,3) 
$ rembg --version
/home/vb/.local/lib/python3.10/site-packages/numba/np/ufunc/parallel.py:371: NumbaWarning: The TBB threading layer requires TBB version 2021 update 6 or later i.e., TBB_INTERFACE_VERSION >= 12060. Found TBB_INTERFACE_VERSION = 12050. The TBB threading layer is disabled.
  warnings.warn(problem)
rembg, version 2.0.50
github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.