How to make vgg pytorch's ptl size smaller on android?

import torch
# import joblib
from torch.utils.mobile_optimizer import optimize_for_mobile
from torchvision.models.vgg import vgg16
import torch, torchvision.models

# lb = joblib.load('lb.pkl')
device = torch.device('cuda:0')
#device = torch.device('cpu')#'cuda:0')
torch.backends.cudnn.benchmark = True
model = vgg16().to(device)



# model = torchvision.models.vgg16()
path = 'model-22222.pth'
torch.save(model.state_dict(), path) # nothing else here
model.load_state_dict(torch.load(path))

#model.load_state_dict(torch.load('./model-76-0.7754.pth'))

scripted_module = torch.jit.script(model)
optimized_scripted_module = optimize_for_mobile(scripted_module)
optimized_scripted_module._save_for_lite_interpreter("model-76-0.7754.ptl")

The optimize_for_mobile seems does not make the ptl file smaller, it’s about 527M, too large on Android. How to make it smaller?

You first need to ** Quantized** the model (This will reduce the size) and then convert it into torch script for it to run on android.

https://pytorch.org/tutorials/intermediate/quantized_transfer_learning_tutorial.html#part-1-training-a-custom-classifier-based-on-a-quantized-feature-extractor
https://pytorch.org/tutorials/recipes/script_optimized.html

1 Like

Is there any other solutions? For example using onnx , ncnn or transform into tensorflow lite? Thanks. Because the size only smaller 50%, how to reduce to about 5%. The size is too big yet. Not fit for the mobile phone users.