From 93b3e844a18cd01e18e98be57282e93cc8714995 Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Jun 2022 00:15:26 +0900 Subject: [PATCH 1/9] To resolve issue #6155 Fix warnings in the tests --- test/test_datasets.py | 1 - test/test_models.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_datasets.py b/test/test_datasets.py index a108479aee3..10f3f4794d5 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -616,7 +616,6 @@ class VOCSegmentationTestCase(datasets_utils.ImageDatasetTestCase): year=[f"20{year:02d}" for year in range(7, 13)], image_set=("train", "val", "trainval") ), dict(year="2007", image_set="test"), - dict(year="2007-test", image_set="test"), ) def inject_fake_data(self, tmpdir, config): diff --git a/test/test_models.py b/test/test_models.py index 0acef4dcef6..2f86e3f0a42 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -870,6 +870,7 @@ def test_quantized_classification_model(model_fn): "num_classes": 5, "input_shape": (1, 3, 224, 224), "quantize": True, + "init_weights": True, } model_name = model_fn.__name__ kwargs = {**defaults, **_model_params.get(model_name, {})} From 5b2049bdf0dce0f4ebd3011f683e054ddaf3c86a Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Jun 2022 10:49:36 +0900 Subject: [PATCH 2/9] To fix CI error To fix CI error --- torchvision/models/quantization/googlenet.py | 2 ++ torchvision/models/quantization/inception.py | 1 + torchvision/models/quantization/mobilenetv2.py | 2 ++ torchvision/models/quantization/resnet.py | 2 ++ torchvision/models/quantization/shufflenetv2.py | 2 ++ 5 files changed, 9 insertions(+) diff --git a/torchvision/models/quantization/googlenet.py b/torchvision/models/quantization/googlenet.py index 644df8ae496..682ba183336 100644 --- a/torchvision/models/quantization/googlenet.py +++ b/torchvision/models/quantization/googlenet.py @@ -74,6 +74,8 @@ def forward(self, x: Tensor) -> Tensor: class QuantizableGoogLeNet(GoogLeNet): # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659 def __init__(self, *args: Any, **kwargs: Any) -> None: + if "init_weights" not in kwargs: + kwargs["init_weights"] = True super().__init__( # type: ignore[misc] blocks=[QuantizableBasicConv2d, QuantizableInception, QuantizableInceptionAux], *args, **kwargs ) diff --git a/torchvision/models/quantization/inception.py b/torchvision/models/quantization/inception.py index ba4b21d4112..647c0047216 100644 --- a/torchvision/models/quantization/inception.py +++ b/torchvision/models/quantization/inception.py @@ -142,6 +142,7 @@ def __init__( QuantizableInceptionE, QuantizableInceptionAux, ], + init_weights=True, ) self.quant = torch.ao.quantization.QuantStub() self.dequant = torch.ao.quantization.DeQuantStub() diff --git a/torchvision/models/quantization/mobilenetv2.py b/torchvision/models/quantization/mobilenetv2.py index 936e9bcc1b1..5e44dc26e3f 100644 --- a/torchvision/models/quantization/mobilenetv2.py +++ b/torchvision/models/quantization/mobilenetv2.py @@ -46,6 +46,8 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: Args: Inherits args from floating point MobileNetV2 """ + if "init_weights" not in kwargs: + kwargs["init_weights"] = True super().__init__(*args, **kwargs) self.quant = QuantStub() self.dequant = DeQuantStub() diff --git a/torchvision/models/quantization/resnet.py b/torchvision/models/quantization/resnet.py index 891b608ce01..804e9ec3fd4 100644 --- a/torchvision/models/quantization/resnet.py +++ b/torchvision/models/quantization/resnet.py @@ -97,6 +97,8 @@ def fuse_model(self, is_qat: Optional[bool] = None) -> None: class QuantizableResNet(ResNet): def __init__(self, *args: Any, **kwargs: Any) -> None: + if "init_weights" not in kwargs: + kwargs["init_weights"] = True super().__init__(*args, **kwargs) self.quant = torch.ao.quantization.QuantStub() diff --git a/torchvision/models/quantization/shufflenetv2.py b/torchvision/models/quantization/shufflenetv2.py index 781591ae118..7ed319a810a 100644 --- a/torchvision/models/quantization/shufflenetv2.py +++ b/torchvision/models/quantization/shufflenetv2.py @@ -52,6 +52,8 @@ def forward(self, x: Tensor) -> Tensor: class QuantizableShuffleNetV2(shufflenetv2.ShuffleNetV2): # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659 def __init__(self, *args: Any, **kwargs: Any) -> None: + if "init_weights" not in kwargs: + kwargs["init_weights"] = True super().__init__(*args, inverted_residual=QuantizableInvertedResidual, **kwargs) # type: ignore[misc] self.quant = torch.ao.quantization.QuantStub() self.dequant = torch.ao.quantization.DeQuantStub() From 5ca18fe223f7688b07ee049b3829db6cd0182ff7 Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Jun 2022 12:17:37 +0900 Subject: [PATCH 3/9] Update resnet.py --- torchvision/models/quantization/resnet.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/torchvision/models/quantization/resnet.py b/torchvision/models/quantization/resnet.py index 804e9ec3fd4..891b608ce01 100644 --- a/torchvision/models/quantization/resnet.py +++ b/torchvision/models/quantization/resnet.py @@ -97,8 +97,6 @@ def fuse_model(self, is_qat: Optional[bool] = None) -> None: class QuantizableResNet(ResNet): def __init__(self, *args: Any, **kwargs: Any) -> None: - if "init_weights" not in kwargs: - kwargs["init_weights"] = True super().__init__(*args, **kwargs) self.quant = torch.ao.quantization.QuantStub() From d8d5b84e2fb1a92c36a06c0b5d7ec6aaab842619 Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Jun 2022 23:22:06 +0900 Subject: [PATCH 4/9] Remove unrelevant init_weights Remove unrelevant init_weights from mobilenet2, shufflenet2 --- test/test_models.py | 1 - torchvision/models/quantization/mobilenetv2.py | 2 -- torchvision/models/quantization/shufflenetv2.py | 2 -- 3 files changed, 5 deletions(-) diff --git a/test/test_models.py b/test/test_models.py index 2f86e3f0a42..0acef4dcef6 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -870,7 +870,6 @@ def test_quantized_classification_model(model_fn): "num_classes": 5, "input_shape": (1, 3, 224, 224), "quantize": True, - "init_weights": True, } model_name = model_fn.__name__ kwargs = {**defaults, **_model_params.get(model_name, {})} diff --git a/torchvision/models/quantization/mobilenetv2.py b/torchvision/models/quantization/mobilenetv2.py index 5e44dc26e3f..936e9bcc1b1 100644 --- a/torchvision/models/quantization/mobilenetv2.py +++ b/torchvision/models/quantization/mobilenetv2.py @@ -46,8 +46,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: Args: Inherits args from floating point MobileNetV2 """ - if "init_weights" not in kwargs: - kwargs["init_weights"] = True super().__init__(*args, **kwargs) self.quant = QuantStub() self.dequant = DeQuantStub() diff --git a/torchvision/models/quantization/shufflenetv2.py b/torchvision/models/quantization/shufflenetv2.py index 7ed319a810a..781591ae118 100644 --- a/torchvision/models/quantization/shufflenetv2.py +++ b/torchvision/models/quantization/shufflenetv2.py @@ -52,8 +52,6 @@ def forward(self, x: Tensor) -> Tensor: class QuantizableShuffleNetV2(shufflenetv2.ShuffleNetV2): # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659 def __init__(self, *args: Any, **kwargs: Any) -> None: - if "init_weights" not in kwargs: - kwargs["init_weights"] = True super().__init__(*args, inverted_residual=QuantizableInvertedResidual, **kwargs) # type: ignore[misc] self.quant = torch.ao.quantization.QuantStub() self.dequant = torch.ao.quantization.DeQuantStub() From 48132415baad88ec61b3d355828a537fc2f680ab Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 15 Jun 2022 00:36:16 +0900 Subject: [PATCH 5/9] To resolve warnings in models To resolve warnings in models/googlenet, models/inception --- torchvision/models/googlenet.py | 2 +- torchvision/models/inception.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index 5b0a91d4791..b2136f51974 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -34,7 +34,7 @@ def __init__( num_classes: int = 1000, aux_logits: bool = True, transform_input: bool = False, - init_weights: Optional[bool] = None, + init_weights: bool = True, blocks: Optional[List[Callable[..., nn.Module]]] = None, dropout: float = 0.2, dropout_aux: float = 0.7, diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 9207485085f..f07542923ca 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -32,7 +32,7 @@ def __init__( aux_logits: bool = True, transform_input: bool = False, inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, - init_weights: Optional[bool] = None, + init_weights: bool = True, dropout: float = 0.5, ) -> None: super().__init__() From 16ddd6c0f23fe56e8029f336c5e3f3c69e9c534f Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 15 Jun 2022 01:14:58 +0900 Subject: [PATCH 6/9] To resolve warnings in models To resolve warnings in models/googlenet, models/inception --- test/test_models.py | 4 ++++ torchvision/models/googlenet.py | 2 +- torchvision/models/inception.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test_models.py b/test/test_models.py index 0acef4dcef6..0bc712c64f3 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -609,6 +609,10 @@ def test_classification_model(model_fn, dev): num_classes = kwargs.get("num_classes") input_shape = kwargs.pop("input_shape") + if model_name in ['inception_v3', 'googlenet']: + if "init_weights" not in kwargs: + kwargs["init_weights"] = True + model = model_fn(**kwargs) model.eval().to(device=dev) # RNG always on CPU, to ensure x in cuda tests is bitwise identical to x in cpu tests diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index b2136f51974..5b0a91d4791 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -34,7 +34,7 @@ def __init__( num_classes: int = 1000, aux_logits: bool = True, transform_input: bool = False, - init_weights: bool = True, + init_weights: Optional[bool] = None, blocks: Optional[List[Callable[..., nn.Module]]] = None, dropout: float = 0.2, dropout_aux: float = 0.7, diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index f07542923ca..9207485085f 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -32,7 +32,7 @@ def __init__( aux_logits: bool = True, transform_input: bool = False, inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, - init_weights: bool = True, + init_weights: Optional[bool] = None, dropout: float = 0.5, ) -> None: super().__init__() From 10595d8973eed4ee525b897191e1d88682538314 Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 15 Jun 2022 01:21:59 +0900 Subject: [PATCH 7/9] Update test_models.py --- test/test_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_models.py b/test/test_models.py index 0bc712c64f3..ad74835a9e1 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -609,7 +609,7 @@ def test_classification_model(model_fn, dev): num_classes = kwargs.get("num_classes") input_shape = kwargs.pop("input_shape") - if model_name in ['inception_v3', 'googlenet']: + if model_name in ["inception_v3", "googlenet"]: if "init_weights" not in kwargs: kwargs["init_weights"] = True From ae06b1b5c21d6f4cd363f3507a6f0d5ce6010e8b Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 15 Jun 2022 23:27:24 +0900 Subject: [PATCH 8/9] Update test_models.py _model_params Update _model_params to prevent future warning --- test/test_models.py | 12 +++++++----- torchvision/models/quantization/googlenet.py | 2 -- torchvision/models/quantization/inception.py | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/test/test_models.py b/test/test_models.py index ad74835a9e1..a96096392f3 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -244,7 +244,13 @@ def _check_input_backprop(model, inputs): # The following contains configuration parameters for all models which are used by # the _test_*_model methods. _model_params = { - "inception_v3": {"input_shape": (1, 3, 299, 299)}, + "inception_v3": { + "input_shape": (1, 3, 299, 299), + "init_weights": True, + }, + "googlenet":{ + "init_weights": True, + }, "retinanet_resnet50_fpn": { "num_classes": 20, "score_thresh": 0.01, @@ -609,10 +615,6 @@ def test_classification_model(model_fn, dev): num_classes = kwargs.get("num_classes") input_shape = kwargs.pop("input_shape") - if model_name in ["inception_v3", "googlenet"]: - if "init_weights" not in kwargs: - kwargs["init_weights"] = True - model = model_fn(**kwargs) model.eval().to(device=dev) # RNG always on CPU, to ensure x in cuda tests is bitwise identical to x in cpu tests diff --git a/torchvision/models/quantization/googlenet.py b/torchvision/models/quantization/googlenet.py index 682ba183336..644df8ae496 100644 --- a/torchvision/models/quantization/googlenet.py +++ b/torchvision/models/quantization/googlenet.py @@ -74,8 +74,6 @@ def forward(self, x: Tensor) -> Tensor: class QuantizableGoogLeNet(GoogLeNet): # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659 def __init__(self, *args: Any, **kwargs: Any) -> None: - if "init_weights" not in kwargs: - kwargs["init_weights"] = True super().__init__( # type: ignore[misc] blocks=[QuantizableBasicConv2d, QuantizableInception, QuantizableInceptionAux], *args, **kwargs ) diff --git a/torchvision/models/quantization/inception.py b/torchvision/models/quantization/inception.py index 647c0047216..ba4b21d4112 100644 --- a/torchvision/models/quantization/inception.py +++ b/torchvision/models/quantization/inception.py @@ -142,7 +142,6 @@ def __init__( QuantizableInceptionE, QuantizableInceptionAux, ], - init_weights=True, ) self.quant = torch.ao.quantization.QuantStub() self.dequant = torch.ao.quantization.DeQuantStub() From f18776043b29856232336406cb7b29f350d9f9c5 Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 15 Jun 2022 23:31:54 +0900 Subject: [PATCH 9/9] Update test_models.py To correct format --- test/test_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_models.py b/test/test_models.py index a96096392f3..69a89d900e2 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -248,7 +248,7 @@ def _check_input_backprop(model, inputs): "input_shape": (1, 3, 299, 299), "init_weights": True, }, - "googlenet":{ + "googlenet": { "init_weights": True, }, "retinanet_resnet50_fpn": {