The size of mobilenetv2 become much bigger when convert to torchscript model

When I convert mobilenet_v2 network to torchscript model, the size increased from 13.5MB to 43.1MB. It’s too big to deploy in Android.

How can I compress the torchscript model ?

My code:

from torchvision.models import mobilenet_v2
net = mobilenet_v2()
torch.save(net,"net.pth")
script_module = torch.jit.script(net)
torch.jit.save(script_module,"net.pt")

The size of models:
%5D%7D%60OQ1C_(8%40U_2%409%24JSCNWO

Are you on the latest version of PyTorch (1.3)? We made some changes to significantly decrease the size of TorchScript binaries. For mobilenet_v2 on the nightly version of PyTorch, I get very similar sizes between eager PyTorch and TorchScript:

$ ls -la --block-size=M
-rw-------.  1 user user 15M Nov 15 13:06 net.pt
-rw-------.  1 user user 14M Nov 15 13:06 net.pth

If you are on PyTorch 1.3 and still are getting this problem, could you try using the nightly PyTorch package and see if that fixes it?

1 Like

Thank you, the problem solved when I upgraded torch from 1.2 to 1.3.