czg1225 / SlimSAM

SlimSAM: 0.1% Data Makes Segment Anything Slim
Apache License 2.0
248 stars 14 forks source link

AttributeError: 'GELU' object has no attribute 'approximate' #1

Closed camenduru closed 6 months ago

camenduru commented 6 months ago

Hi 👋 thanks for the project ❤ I got an error when I tried running inference.py (torch 2.1.0+cu118)

CUDA visible devices: 1
CUDA Device Name: Tesla T4
model_path: checkpoints/SlimSAM-77.pth
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-19-14955c5395e4>](https://localhost:8080/#) in <cell line: 165>()
    164 
    165 if __name__ == '__main__':
--> 166     test_model()

15 frames
[/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in __getattr__(self, name)
   1693             if name in modules:
   1694                 return modules[name]
-> 1695         raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
   1696 
   1697     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'GELU' object has no attribute 'approximate'

Is there a way to fix it?

czg1225 commented 6 months ago

Hello @camenduru, thanks for the issue.

This error is because the GELU function in the old version of pytorch does not have the parameter ‘approximate’. There are two ways can fix it:

  1. You can try lowering the version of pytorch to around 1.7.

  2. You can replace the GELU function which is at 'anconda/envs/SlimSAM/lib/python3.8/site-packages/torch/nn/modules/activation.py' as following function.

    class GELU(Module):
    r"""Applies the Gaussian Error Linear Units function:
    
    .. math:: \text{GELU}(x) = x * \Phi(x)
    
    where :math:`\Phi(x)` is the Cumulative Distribution Function for Gaussian Distribution.
    
    Shape:
        - Input: :math:`(N, *)` where `*` means, any number of additional
          dimensions
        - Output: :math:`(N, *)`, same shape as the input
    
    .. image:: ../scripts/activation_images/GELU.png
    
    Examples::
    
        >>> m = nn.GELU()
        >>> input = torch.randn(2)
        >>> output = m(input)
    """
    def forward(self, input: Tensor) -> Tensor:
        return F.gelu(input)

Thanks!

camenduru commented 6 months ago

Thanks ❤ solved 🎉

sed -i 's/input, approximate=self.approximate/input/g' /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py

I made a colab. 🥳 I hope you like it. https://github.com/camenduru/SlimSAM-colab