是否有可能对densenet169进行量化,以及如何进行量化?



我一直在尝试在密度模型上执行量化,但没有成功。我一直在尝试实现pytorch后培训静态量化。Pytorch有其他模型的量子化版本,但没有densenet。是否有可能量化密集体系结构?

我已经搜索了关于如何在预训练模型上应用量化的教程,但我没有任何成功。

以下是如何从torchvision在DenseNet169上做到这一点:

from torch.ao.quantization import QuantStub, DeQuantStub
from torch import nn
from torchvision.models import densenet169, DenseNet169_Weights
from tqdm import tqdm
from torch.ao.quantization import HistogramObserver, PerChannelMinMaxObserver
import torch
# Wrap base model with quant/dequant stub
class QuantizedDenseNet169(nn.Module):
def __init__(self):
super().__init__()
self.dn = densenet169(weights=DenseNet169_Weights.IMAGENET1K_V1)
self.quant = QuantStub()
self.dequant = DeQuantStub()
def forward(self, x):
x = self.quant(x)
x = self.dn(x)
return self.dequant(x)
dn = QuantizedDenseNet169()
# move to gpu
dn.cuda()
# Propagate qconfig
dn.qconfig = torch.quantization.QConfig(
activation=HistogramObserver.with_args(),
weight=PerChannelMinMaxObserver.with_args(dtype=torch.qint8)
)
# fbgemm for x86 architecture
torch.backends.quantized.engine = 'fbgemm'
dn = torch.quantization.prepare(dn, inplace=False)
# calibrate with own dataset (I'm using random inputs to show process)
with torch.no_grad():
for _ in tqdm(range(5), desc="PTQ progess"):
input_ = torch.randn([1, 3, 128, 128], device='cuda')
dn.forward(input_)
# move to cpu before quantization
dn.cpu()
dn = torch.quantization.convert(dn, inplace=False)
# check if it's working
out = dn(torch.randn([1, 3, 128, 128]))

相关内容

  • 没有找到相关文章

最新更新