|
| 1 | +import warnings |
| 2 | +from functools import partial |
| 3 | +from typing import Any, List, Optional, Union |
| 4 | + |
| 5 | +import torch |
| 6 | +from torchvision.transforms.functional import InterpolationMode |
| 7 | + |
| 8 | +from ....models.quantization.mobilenetv3 import ( |
| 9 | + InvertedResidualConfig, |
| 10 | + QuantizableInvertedResidual, |
| 11 | + QuantizableMobileNetV3, |
| 12 | + _replace_relu, |
| 13 | +) |
| 14 | +from ...transforms.presets import ImageNetEval |
| 15 | +from .._api import Weights, WeightEntry |
| 16 | +from .._meta import _IMAGENET_CATEGORIES |
| 17 | +from ..mobilenetv3 import MobileNetV3LargeWeights, _mobilenet_v3_conf |
| 18 | + |
| 19 | + |
| 20 | +__all__ = [ |
| 21 | + "QuantizableMobileNetV3", |
| 22 | + "QuantizedMobileNetV3LargeWeights", |
| 23 | + "mobilenet_v3_large", |
| 24 | +] |
| 25 | + |
| 26 | + |
| 27 | +def _mobilenet_v3_model( |
| 28 | + inverted_residual_setting: List[InvertedResidualConfig], |
| 29 | + last_channel: int, |
| 30 | + weights: Optional[Weights], |
| 31 | + progress: bool, |
| 32 | + quantize: bool, |
| 33 | + **kwargs: Any, |
| 34 | +) -> QuantizableMobileNetV3: |
| 35 | + if weights is not None: |
| 36 | + kwargs["num_classes"] = len(weights.meta["categories"]) |
| 37 | + if "backend" in weights.meta: |
| 38 | + kwargs["backend"] = weights.meta["backend"] |
| 39 | + backend = kwargs.pop("backend", "qnnpack") |
| 40 | + |
| 41 | + model = QuantizableMobileNetV3(inverted_residual_setting, last_channel, block=QuantizableInvertedResidual, **kwargs) |
| 42 | + _replace_relu(model) |
| 43 | + |
| 44 | + if quantize: |
| 45 | + model.fuse_model() |
| 46 | + model.qconfig = torch.quantization.get_default_qat_qconfig(backend) |
| 47 | + torch.quantization.prepare_qat(model, inplace=True) |
| 48 | + |
| 49 | + if weights is not None: |
| 50 | + model.load_state_dict(weights.state_dict(progress=progress)) |
| 51 | + |
| 52 | + if quantize: |
| 53 | + torch.quantization.convert(model, inplace=True) |
| 54 | + model.eval() |
| 55 | + |
| 56 | + return model |
| 57 | + |
| 58 | + |
| 59 | +class QuantizedMobileNetV3LargeWeights(Weights): |
| 60 | + ImageNet1K_QNNPACK_RefV1 = WeightEntry( |
| 61 | + url="https://download.pytorch.org/models/quantized/mobilenet_v3_large_qnnpack-5bcacf28.pth", |
| 62 | + transforms=partial(ImageNetEval, crop_size=224), |
| 63 | + meta={ |
| 64 | + "size": (224, 224), |
| 65 | + "categories": _IMAGENET_CATEGORIES, |
| 66 | + "interpolation": InterpolationMode.BILINEAR, |
| 67 | + "backend": "qnnpack", |
| 68 | + "quantization": "qat", |
| 69 | + "recipe": "https://github.com/pytorch/vision/tree/main/references/classification#qat-mobilenetv3", |
| 70 | + "unquantized": MobileNetV3LargeWeights.ImageNet1K_RefV1, |
| 71 | + "acc@1": 73.004, |
| 72 | + "acc@5": 90.858, |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def mobilenet_v3_large( |
| 78 | + weights: Optional[Union[QuantizedMobileNetV3LargeWeights, MobileNetV3LargeWeights]] = None, |
| 79 | + progress: bool = True, |
| 80 | + quantize: bool = False, |
| 81 | + **kwargs: Any, |
| 82 | +) -> QuantizableMobileNetV3: |
| 83 | + if "pretrained" in kwargs: |
| 84 | + warnings.warn("The argument pretrained is deprecated, please use weights instead.") |
| 85 | + if kwargs.pop("pretrained"): |
| 86 | + weights = ( |
| 87 | + QuantizedMobileNetV3LargeWeights.ImageNet1K_QNNPACK_RefV1 |
| 88 | + if quantize |
| 89 | + else MobileNetV3LargeWeights.ImageNet1K_RefV1 |
| 90 | + ) |
| 91 | + else: |
| 92 | + weights = None |
| 93 | + |
| 94 | + if quantize: |
| 95 | + weights = QuantizedMobileNetV3LargeWeights.verify(weights) |
| 96 | + else: |
| 97 | + weights = MobileNetV3LargeWeights.verify(weights) |
| 98 | + |
| 99 | + inverted_residual_setting, last_channel = _mobilenet_v3_conf("mobilenet_v3_large", **kwargs) |
| 100 | + return _mobilenet_v3_model(inverted_residual_setting, last_channel, weights, progress, quantize, **kwargs) |
0 commit comments