[文档]classQuantizableMobileNetV2(MobileNetV2):def__init__(self,*args:Any,**kwargs:Any)->None:""" MobileNet V2 main class Args: Inherits args from floating point MobileNetV2 """super().__init__(*args,**kwargs)self.quant=QuantStub()self.dequant=DeQuantStub()defforward(self,x:Tensor)->Tensor:x=self.quant(x)x=self._forward_impl(x)x=self.dequant(x)returnxdeffuse_model(self,is_qat:Optional[bool]=None)->None:forminself.modules():iftype(m)isConvNormActivation:_fuse_modules(m,["0","1","2"],is_qat,inplace=True)iftype(m)isQuantizableInvertedResidual:m.fuse_model(is_qat)
defmobilenet_v2(pretrained:bool=False,progress:bool=True,quantize:bool=False,**kwargs:Any,)->QuantizableMobileNetV2:""" Constructs a MobileNetV2 architecture from `"MobileNetV2: Inverted Residuals and Linear Bottlenecks" <https://arxiv.org/abs/1801.04381>`_. Note that quantize = True returns a quantized model with 8 bit weights. Quantized models only support inference and run on CPUs. GPU inference is not yet supported Args: pretrained (bool): If True, returns a model pre-trained on ImageNet. progress (bool): If True, displays a progress bar of the download to stderr quantize(bool): If True, returns a quantized model, else returns a float model """model=QuantizableMobileNetV2(block=QuantizableInvertedResidual,**kwargs)_replace_relu(model)ifquantize:# TODO use pretrained as a string to specify the backendbackend="qnnpack"quantize_model(model,backend)else:assertpretrainedin[True,False]ifpretrained:ifquantize:model_url=quant_model_urls["mobilenet_v2_"+backend]else:model_url=model_urls["mobilenet_v2"]state_dict=load_state_dict_from_url(model_url,progress=progress)model.load_state_dict(state_dict)returnmodel