Pytorch目标检测模型优化



我想减小对象检测模型的大小。同样,我尝试使用pytorch-mobile优化器优化Faster R-CNN模型用于目标检测,但生成的.ptzip文件与原始模型大小相同。

我使用了下面提到的代码

import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
script_model = torch.jit.script(model)
from torch.utils.mobile_optimizer import optimize_for_mobile
script_model_vulkan = optimize_for_mobile(script_model, backend='Vulkan')
torch.jit.save(script_model_vulkan, "frcnn.pth")

您必须首先量化您的模型
遵循以下步骤
&然后使用这些方法

from torch.utils.mobile_optimizer import optimize_for_mobile
script_model_vulkan = optimize_for_mobile(script_model, backend='Vulkan')
torch.jit.save(script_model_vulkan, "frcnn.pth")

编辑:

resnet50模型的量化过程

import torchvision
model = torchvision.models.resnet50(pretrained=True)
import os
import torch
def print_model_size(mdl):
torch.save(mdl.state_dict(), "tmp.pt")
print("%.2f MB" %(os.path.getsize("tmp.pt")/1e6))
os.remove('tmp.pt')
print_model_size(model) # will print original model size
backend = "qnnpack"
model.qconfig = torch.quantization.get_default_qconfig(backend)
torch.backends.quantized.engine = backend
model_static_quantized = torch.quantization.prepare(model, inplace=False)
model_static_quantized = torch.quantization.convert(model_static_quantized, inplace=False)

print_model_size(model_static_quantized) ## will print quantized model size

相关内容

  • 没有找到相关文章

最新更新