EfficientNet-PyTorch icon indicating copy to clipboard operation
EfficientNet-PyTorch copied to clipboard

errer: tramsform Model to Torch Script

Open huangzhenjie opened this issue 5 years ago • 11 comments

when I want to save the trained Model as .pt. The error is (RuntimeError: Could not export Python function call 'SwishImplementation'. Remove calls to Python functions before export. Did you forget add @script or @script_method annotation? If this is a nn.ModuleList, add it to constants:) . How could I solve it? thanks!!!

huangzhenjie avatar Oct 15 '19 03:10 huangzhenjie

Hello! Yes, the latest update to the swish implementation saves memory but broke torchscript. An update to fix this is coming.

lukemelas avatar Oct 15 '19 03:10 lukemelas

Done -- check the latest update!

lukemelas avatar Oct 15 '19 05:10 lukemelas

thanks!! let me try.

huangzhenjie avatar Oct 15 '19 06:10 huangzhenjie

thanks!! let me try.

Do you get it done on this issue? I am precisely in the same trouble. My pytorch version is 1.3.1.

xiaoliumi avatar Jan 19 '20 13:01 xiaoliumi

Hi Luke! Thanks a ton for this wonderful repo!

I have problems with scripting your implementation: Getting attributes of tuples is not supported: Getting attributes of tuples is not supported: at /home/pavlovskaya/anaconda3/envs/antispoofing/lib/python3.7/site-packages/efficientnet_pytorch/model.py:76:11 def forward(self, inputs, drop_connect_rate=None): """ :param inputs: input tensor :param drop_connect_rate: drop connect rate (float, between 0 and 1) :return: output of block """

    # Expansion and Depthwise Convolution
    x = inputs
    if self._block_args.expand_ratio != 1:
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        x = self._swish(self._bn0(self._expand_conv(inputs)))
    x = self._swish(self._bn1(self._depthwise_conv(x)))

And with tracing: torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults:

So, unfortunately, can't use the EN((((

ne-bo avatar Jan 24 '20 12:01 ne-bo

I am having the same issue as @ne-bo, has there been any progress on this? Thanks so much!

jesford avatar Jun 24 '20 23:06 jesford

I am having the same issue too

Daonancai avatar Sep 11 '20 10:09 Daonancai

0.7.0 version has the same issue

Podidiving avatar Nov 02 '20 08:11 Podidiving

A workaround is to set model.set_swish(memory_efficient=False) before saving.

mzuzic avatar Nov 29 '20 12:11 mzuzic

Hi ! Thanks a lot for this repo !

I have encountered the same problem stated at the beginning of this issue. When I try to get the torch script of my efficientnet model I get this error

RuntimeError: Python builtin <built-in method apply of FunctionMeta object at 0x9145330> is currently not supported in Torchscript: File "/home/xxx/.local/lib/python3.8/site-packages/efficientnet_pytorch/utils.py", line 76 def forward(self, x): return SwishImplementation.apply(x) ~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE

This is my code

model = EfficientNet.from_pretrained('efficientnet-b2', num_classes=10)
torch.jit.script(model).save('model.ptc')

My pytorch version is 1.7.1 And my efficientnet_pytorch version is 0.7.0

Thanks

medric49 avatar Mar 24 '21 14:03 medric49

Than you @mzuzic for https://github.com/lukemelas/EfficientNet-PyTorch/issues/89#issuecomment-735387662 Any news regarding export with memory efficient swish?

mikel-brostrom avatar Aug 12 '22 08:08 mikel-brostrom