From 1f5b1670cd73ebd948b57f6a83dc37cfb6f7da09 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 21 Oct 2020 17:46:36 +0200 Subject: [PATCH 1/7] style: Added annotation typing for googlenet --- torchvision/models/googlenet.py | 58 ++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index eb774d166ac..1751ae304c9 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -3,9 +3,10 @@ import torch import torch.nn as nn import torch.nn.functional as F -from torch.jit.annotations import Optional, Tuple +from torch.jit.annotations import Optional, Tuple, List from torch import Tensor from .utils import load_state_dict_from_url +from typing import Callable, Any __all__ = ['GoogLeNet', 'googlenet', "GoogLeNetOutputs", "_GoogLeNetOutputs"] @@ -23,7 +24,7 @@ _GoogLeNetOutputs = GoogLeNetOutputs -def googlenet(pretrained=False, progress=True, **kwargs): +def googlenet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> GoogLeNet: r"""GoogLeNet (Inception v1) model architecture from `"Going Deeper with Convolutions" `_. @@ -62,8 +63,14 @@ def googlenet(pretrained=False, progress=True, **kwargs): class GoogLeNet(nn.Module): __constants__ = ['aux_logits', 'transform_input'] - def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, init_weights=None, - blocks=None): + def __init__( + self, + num_classes: int = 1000, + aux_logits: bool = True, + transform_input: bool = False, + init_weights: Optional[bool] = None, + blocks: Optional[List[Callable[..., nn.Module]]] = None + ): super(GoogLeNet, self).__init__() if blocks is None: blocks = [BasicConv2d, Inception, InceptionAux] @@ -114,7 +121,7 @@ def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, ini if init_weights: self._initialize_weights() - def _initialize_weights(self): + def _initialize_weights(self) -> None: for m in self.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear): import scipy.stats as stats @@ -127,7 +134,7 @@ def _initialize_weights(self): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) - def _transform_input(self, x): + def _transform_input(self, x: Tensor) -> Tensor: # type: (Tensor) -> Tensor if self.transform_input: x_ch0 = torch.unsqueeze(x[:, 0], 1) * (0.229 / 0.5) + (0.485 - 0.5) / 0.5 @@ -136,7 +143,7 @@ def _transform_input(self, x): x = torch.cat((x_ch0, x_ch1, x_ch2), 1) return x - def _forward(self, x): + def _forward(self, x: Tensor) -> Tuple[Tensor, Optional[Tensor], Optional[Tensor]]: # type: (Tensor) -> Tuple[Tensor, Optional[Tensor], Optional[Tensor]] # N x 3 x 224 x 224 x = self.conv1(x) @@ -199,7 +206,7 @@ def eager_outputs(self, x: Tensor, aux2: Tensor, aux1: Optional[Tensor]) -> Goog else: return x # type: ignore[return-value] - def forward(self, x): + def forward(self, x: Tensor) -> GoogLeNetOutputs: # type: (Tensor) -> GoogLeNetOutputs x = self._transform_input(x) x, aux1, aux2 = self._forward(x) @@ -214,8 +221,17 @@ def forward(self, x): class Inception(nn.Module): - def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj, - conv_block=None): + def __init__( + self, + in_channels: int, + ch1x1: int, + ch3x3red: int, + ch3x3: int, + ch5x5red: int, + ch5x5: int, + pool_proj: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(Inception, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -238,7 +254,7 @@ def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_pr conv_block(in_channels, pool_proj, kernel_size=1) ) - def _forward(self, x): + def _forward(self, x: Tensor) -> List[Tensor]: branch1 = self.branch1(x) branch2 = self.branch2(x) branch3 = self.branch3(x) @@ -247,14 +263,19 @@ def _forward(self, x): outputs = [branch1, branch2, branch3, branch4] return outputs - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: outputs = self._forward(x) return torch.cat(outputs, 1) class InceptionAux(nn.Module): - def __init__(self, in_channels, num_classes, conv_block=None): + def __init__( + self, + in_channels: int, + num_classes: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(InceptionAux, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -263,7 +284,7 @@ def __init__(self, in_channels, num_classes, conv_block=None): self.fc1 = nn.Linear(2048, 1024) self.fc2 = nn.Linear(1024, num_classes) - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: # aux1: N x 512 x 14 x 14, aux2: N x 528 x 14 x 14 x = F.adaptive_avg_pool2d(x, (4, 4)) # aux1: N x 512 x 4 x 4, aux2: N x 528 x 4 x 4 @@ -283,12 +304,17 @@ def forward(self, x): class BasicConv2d(nn.Module): - def __init__(self, in_channels, out_channels, **kwargs): + def __init__( + self, + in_channels: int, + out_channels: int, + **kwargs + ): super(BasicConv2d, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) self.bn = nn.BatchNorm2d(out_channels, eps=0.001) - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: x = self.conv(x) x = self.bn(x) return F.relu(x, inplace=True) From 0413037ab4328016a046123834001341eb3cb715 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 21 Oct 2020 23:12:06 +0200 Subject: [PATCH 2/7] fix: Removed duplicate typing --- torchvision/models/googlenet.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index 1751ae304c9..710f8701149 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -135,7 +135,6 @@ def _initialize_weights(self) -> None: nn.init.constant_(m.bias, 0) def _transform_input(self, x: Tensor) -> Tensor: - # type: (Tensor) -> Tensor if self.transform_input: x_ch0 = torch.unsqueeze(x[:, 0], 1) * (0.229 / 0.5) + (0.485 - 0.5) / 0.5 x_ch1 = torch.unsqueeze(x[:, 1], 1) * (0.224 / 0.5) + (0.456 - 0.5) / 0.5 @@ -144,7 +143,6 @@ def _transform_input(self, x: Tensor) -> Tensor: return x def _forward(self, x: Tensor) -> Tuple[Tensor, Optional[Tensor], Optional[Tensor]]: - # type: (Tensor) -> Tuple[Tensor, Optional[Tensor], Optional[Tensor]] # N x 3 x 224 x 224 x = self.conv1(x) # N x 64 x 112 x 112 @@ -207,7 +205,6 @@ def eager_outputs(self, x: Tensor, aux2: Tensor, aux1: Optional[Tensor]) -> Goog return x # type: ignore[return-value] def forward(self, x: Tensor) -> GoogLeNetOutputs: - # type: (Tensor) -> GoogLeNetOutputs x = self._transform_input(x) x, aux1, aux2 = self._forward(x) aux_defined = self.training and self.aux_logits From d06c53e7f870ef3c93556ccf27ecf43d5f296f65 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 21 Oct 2020 23:15:10 +0200 Subject: [PATCH 3/7] refactor: Moved factory function after class definition to fix typing --- torchvision/models/googlenet.py | 72 ++++++++++++++++----------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index 710f8701149..fc03383b0de 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -24,42 +24,6 @@ _GoogLeNetOutputs = GoogLeNetOutputs -def googlenet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> GoogLeNet: - r"""GoogLeNet (Inception v1) model architecture from - `"Going Deeper with Convolutions" `_. - - 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 - aux_logits (bool): If True, adds two auxiliary branches that can improve training. - Default: *False* when pretrained is True otherwise *True* - transform_input (bool): If True, preprocesses the input according to the method with which it - was trained on ImageNet. Default: *False* - """ - if pretrained: - if 'transform_input' not in kwargs: - kwargs['transform_input'] = True - if 'aux_logits' not in kwargs: - kwargs['aux_logits'] = False - if kwargs['aux_logits']: - warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' - 'so make sure to train them') - original_aux_logits = kwargs['aux_logits'] - kwargs['aux_logits'] = True - kwargs['init_weights'] = False - model = GoogLeNet(**kwargs) - state_dict = load_state_dict_from_url(model_urls['googlenet'], - progress=progress) - model.load_state_dict(state_dict) - if not original_aux_logits: - model.aux_logits = False - model.aux1 = None - model.aux2 = None - return model - - return GoogLeNet(**kwargs) - - class GoogLeNet(nn.Module): __constants__ = ['aux_logits', 'transform_input'] @@ -315,3 +279,39 @@ def forward(self, x: Tensor) -> Tensor: x = self.conv(x) x = self.bn(x) return F.relu(x, inplace=True) + + +def googlenet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> GoogLeNet: + r"""GoogLeNet (Inception v1) model architecture from + `"Going Deeper with Convolutions" `_. + + 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 + aux_logits (bool): If True, adds two auxiliary branches that can improve training. + Default: *False* when pretrained is True otherwise *True* + transform_input (bool): If True, preprocesses the input according to the method with which it + was trained on ImageNet. Default: *False* + """ + if pretrained: + if 'transform_input' not in kwargs: + kwargs['transform_input'] = True + if 'aux_logits' not in kwargs: + kwargs['aux_logits'] = False + if kwargs['aux_logits']: + warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' + 'so make sure to train them') + original_aux_logits = kwargs['aux_logits'] + kwargs['aux_logits'] = True + kwargs['init_weights'] = False + model = GoogLeNet(**kwargs) + state_dict = load_state_dict_from_url(model_urls['googlenet'], + progress=progress) + model.load_state_dict(state_dict) + if not original_aux_logits: + model.aux_logits = False + model.aux1 = None + model.aux2 = None + return model + + return GoogLeNet(**kwargs) From 7c0a8460d5887ffdfd1be9ff17605b8d5cd1a52d Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 00:08:12 +0200 Subject: [PATCH 4/7] fix: Fixed annotation typing --- torchvision/models/googlenet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index fc03383b0de..74eac99164c 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -75,8 +75,8 @@ def __init__( self.aux1 = inception_aux_block(512, num_classes) self.aux2 = inception_aux_block(528, num_classes) else: - self.aux1 = None - self.aux2 = None + self.aux1 = None # type: ignore[assignment] + self.aux2 = None # type: ignore[assignment] self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.dropout = nn.Dropout(0.2) @@ -310,8 +310,8 @@ def googlenet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> model.load_state_dict(state_dict) if not original_aux_logits: model.aux_logits = False - model.aux1 = None - model.aux2 = None + model.aux1 = None # type: ignore[assignment] + model.aux2 = None # type: ignore[assignment] return model return GoogLeNet(**kwargs) From 553f08455508f744953e57c299eb469cd5498c25 Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 12:04:10 +0200 Subject: [PATCH 5/7] refactor: Removed un-necessary import --- torchvision/models/googlenet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index 74eac99164c..217cad4dde8 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -3,10 +3,9 @@ import torch import torch.nn as nn import torch.nn.functional as F -from torch.jit.annotations import Optional, Tuple, List from torch import Tensor from .utils import load_state_dict_from_url -from typing import Callable, Any +from typing import Optional, Tuple, List, Callable, Any __all__ = ['GoogLeNet', 'googlenet', "GoogLeNetOutputs", "_GoogLeNetOutputs"] From b689e2e20d9b0eef617629baaff9109f931c1914 Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 21:04:20 +0200 Subject: [PATCH 6/7] fix: Fixed typing --- torchvision/models/googlenet.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index 217cad4dde8..5ebc7a15566 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -33,7 +33,7 @@ def __init__( transform_input: bool = False, init_weights: Optional[bool] = None, blocks: Optional[List[Callable[..., nn.Module]]] = None - ): + ) -> None: super(GoogLeNet, self).__init__() if blocks is None: blocks = [BasicConv2d, Inception, InceptionAux] @@ -191,7 +191,7 @@ def __init__( ch5x5: int, pool_proj: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(Inception, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -235,7 +235,7 @@ def __init__( in_channels: int, num_classes: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(InceptionAux, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -268,8 +268,8 @@ def __init__( self, in_channels: int, out_channels: int, - **kwargs - ): + **kwargs: Any + ) -> None: super(BasicConv2d, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) self.bn = nn.BatchNorm2d(out_channels, eps=0.001) From 68b6925b6e8bc146aa32e7da507d288f93fcbb4a Mon Sep 17 00:00:00 2001 From: frgfm Date: Fri, 23 Oct 2020 11:41:44 +0200 Subject: [PATCH 7/7] refactor: Moved back up helper function and quote typed it --- torchvision/models/googlenet.py | 72 ++++++++++++++++----------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/torchvision/models/googlenet.py b/torchvision/models/googlenet.py index 5ebc7a15566..274ac74bc04 100644 --- a/torchvision/models/googlenet.py +++ b/torchvision/models/googlenet.py @@ -23,6 +23,42 @@ _GoogLeNetOutputs = GoogLeNetOutputs +def googlenet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> "GoogLeNet": + r"""GoogLeNet (Inception v1) model architecture from + `"Going Deeper with Convolutions" `_. + + 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 + aux_logits (bool): If True, adds two auxiliary branches that can improve training. + Default: *False* when pretrained is True otherwise *True* + transform_input (bool): If True, preprocesses the input according to the method with which it + was trained on ImageNet. Default: *False* + """ + if pretrained: + if 'transform_input' not in kwargs: + kwargs['transform_input'] = True + if 'aux_logits' not in kwargs: + kwargs['aux_logits'] = False + if kwargs['aux_logits']: + warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' + 'so make sure to train them') + original_aux_logits = kwargs['aux_logits'] + kwargs['aux_logits'] = True + kwargs['init_weights'] = False + model = GoogLeNet(**kwargs) + state_dict = load_state_dict_from_url(model_urls['googlenet'], + progress=progress) + model.load_state_dict(state_dict) + if not original_aux_logits: + model.aux_logits = False + model.aux1 = None # type: ignore[assignment] + model.aux2 = None # type: ignore[assignment] + return model + + return GoogLeNet(**kwargs) + + class GoogLeNet(nn.Module): __constants__ = ['aux_logits', 'transform_input'] @@ -278,39 +314,3 @@ def forward(self, x: Tensor) -> Tensor: x = self.conv(x) x = self.bn(x) return F.relu(x, inplace=True) - - -def googlenet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> GoogLeNet: - r"""GoogLeNet (Inception v1) model architecture from - `"Going Deeper with Convolutions" `_. - - 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 - aux_logits (bool): If True, adds two auxiliary branches that can improve training. - Default: *False* when pretrained is True otherwise *True* - transform_input (bool): If True, preprocesses the input according to the method with which it - was trained on ImageNet. Default: *False* - """ - if pretrained: - if 'transform_input' not in kwargs: - kwargs['transform_input'] = True - if 'aux_logits' not in kwargs: - kwargs['aux_logits'] = False - if kwargs['aux_logits']: - warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' - 'so make sure to train them') - original_aux_logits = kwargs['aux_logits'] - kwargs['aux_logits'] = True - kwargs['init_weights'] = False - model = GoogLeNet(**kwargs) - state_dict = load_state_dict_from_url(model_urls['googlenet'], - progress=progress) - model.load_state_dict(state_dict) - if not original_aux_logits: - model.aux_logits = False - model.aux1 = None # type: ignore[assignment] - model.aux2 = None # type: ignore[assignment] - return model - - return GoogLeNet(**kwargs)