From 60babcda56fc6a549e9a3b10a4edb15d852fa3f8 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 21 Oct 2020 17:47:19 +0200 Subject: [PATCH 1/8] style: Added annotation typing for inception --- torchvision/models/inception.py | 93 +++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 27 deletions(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index a131c8c754f..42b0188355d 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -3,9 +3,10 @@ import torch import torch.nn as nn import torch.nn.functional as F -from torch.jit.annotations import Optional +from torch.jit.annotations import Optional, Tuple from torch import Tensor from .utils import load_state_dict_from_url +from typing import Callable, Any __all__ = ['Inception3', 'inception_v3', 'InceptionOutputs', '_InceptionOutputs'] @@ -24,7 +25,7 @@ _InceptionOutputs = InceptionOutputs -def inception_v3(pretrained=False, progress=True, **kwargs): +def inception_v3(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> Inception3: r"""Inception v3 model architecture from `"Rethinking the Inception Architecture for Computer Vision" `_. @@ -63,8 +64,14 @@ def inception_v3(pretrained=False, progress=True, **kwargs): class Inception3(nn.Module): - def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, - inception_blocks=None, init_weights=None): + def __init__( + self, + num_classes: int = 1000, + aux_logits: bool = True, + transform_input: bool = False, + inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, + init_weights: Optional[bool] = None + ): super(Inception3, self).__init__() if inception_blocks is None: inception_blocks = [ @@ -124,7 +131,7 @@ def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) - def _transform_input(self, x): + def _transform_input(self, x: 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 @@ -132,7 +139,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]]: # N x 3 x 299 x 299 x = self.Conv2d_1a_3x3(x) # N x 32 x 149 x 149 @@ -188,13 +195,13 @@ def _forward(self, x): return x, aux @torch.jit.unused - def eager_outputs(self, x: torch.Tensor, aux: Optional[Tensor]) -> InceptionOutputs: + def eager_outputs(self, x: Tensor, aux: Optional[Tensor]) -> InceptionOutputs: if self.training and self.aux_logits: return InceptionOutputs(x, aux) else: return x # type: ignore[return-value] - def forward(self, x): + def forward(self, x: Tensor) -> InceptionOutputs: x = self._transform_input(x) x, aux = self._forward(x) aux_defined = self.training and self.aux_logits @@ -208,7 +215,12 @@ def forward(self, x): class InceptionA(nn.Module): - def __init__(self, in_channels, pool_features, conv_block=None): + def __init__( + self, + in_channels: int, + pool_features: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(InceptionA, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -223,7 +235,7 @@ def __init__(self, in_channels, pool_features, conv_block=None): self.branch_pool = conv_block(in_channels, pool_features, kernel_size=1) - def _forward(self, x): + def _forward(self, x: Tensor) -> List[Tensor]: branch1x1 = self.branch1x1(x) branch5x5 = self.branch5x5_1(x) @@ -239,14 +251,18 @@ def _forward(self, x): outputs = [branch1x1, branch5x5, branch3x3dbl, branch_pool] return outputs - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: outputs = self._forward(x) return torch.cat(outputs, 1) class InceptionB(nn.Module): - def __init__(self, in_channels, conv_block=None): + def __init__( + self, + in_channels: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(InceptionB, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -256,7 +272,7 @@ def __init__(self, in_channels, conv_block=None): self.branch3x3dbl_2 = conv_block(64, 96, kernel_size=3, padding=1) self.branch3x3dbl_3 = conv_block(96, 96, kernel_size=3, stride=2) - def _forward(self, x): + def _forward(self, x: Tensor) -> List[Tensor]: branch3x3 = self.branch3x3(x) branch3x3dbl = self.branch3x3dbl_1(x) @@ -268,14 +284,19 @@ def _forward(self, x): outputs = [branch3x3, branch3x3dbl, branch_pool] return outputs - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: outputs = self._forward(x) return torch.cat(outputs, 1) class InceptionC(nn.Module): - def __init__(self, in_channels, channels_7x7, conv_block=None): + def __init__( + self, + in_channels: int, + channels_7x7: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(InceptionC, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -294,7 +315,7 @@ def __init__(self, in_channels, channels_7x7, conv_block=None): self.branch_pool = conv_block(in_channels, 192, kernel_size=1) - def _forward(self, x): + def _forward(self, x: Tensor) -> List[Tensor]: branch1x1 = self.branch1x1(x) branch7x7 = self.branch7x7_1(x) @@ -313,14 +334,18 @@ def _forward(self, x): outputs = [branch1x1, branch7x7, branch7x7dbl, branch_pool] return outputs - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: outputs = self._forward(x) return torch.cat(outputs, 1) class InceptionD(nn.Module): - def __init__(self, in_channels, conv_block=None): + def __init__( + self, + in_channels: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(InceptionD, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -332,7 +357,7 @@ def __init__(self, in_channels, conv_block=None): self.branch7x7x3_3 = conv_block(192, 192, kernel_size=(7, 1), padding=(3, 0)) self.branch7x7x3_4 = conv_block(192, 192, kernel_size=3, stride=2) - def _forward(self, x): + def _forward(self, x: Tensor) -> List[Tensor]: branch3x3 = self.branch3x3_1(x) branch3x3 = self.branch3x3_2(branch3x3) @@ -345,14 +370,18 @@ def _forward(self, x): outputs = [branch3x3, branch7x7x3, branch_pool] return outputs - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: outputs = self._forward(x) return torch.cat(outputs, 1) class InceptionE(nn.Module): - def __init__(self, in_channels, conv_block=None): + def __init__( + self, + in_channels: int, + conv_block: Optional[Callable[..., nn.Module]] = None + ): super(InceptionE, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -369,7 +398,7 @@ def __init__(self, in_channels, conv_block=None): self.branch_pool = conv_block(in_channels, 192, kernel_size=1) - def _forward(self, x): + def _forward(self, x: Tensor) -> List[Tensor]: branch1x1 = self.branch1x1(x) branch3x3 = self.branch3x3_1(x) @@ -393,14 +422,19 @@ def _forward(self, x): outputs = [branch1x1, branch3x3, branch3x3dbl, branch_pool] 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 @@ -410,7 +444,7 @@ def __init__(self, in_channels, num_classes, conv_block=None): self.fc = nn.Linear(768, num_classes) self.fc.stddev = 0.001 - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: # N x 768 x 17 x 17 x = F.avg_pool2d(x, kernel_size=5, stride=3) # N x 768 x 5 x 5 @@ -430,12 +464,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 c4f84192dc9e0a90a8f6f4a27e5d18cb263c576b Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 21 Oct 2020 23:21:54 +0200 Subject: [PATCH 2/8] refactor: Moved factory function after class definition --- torchvision/models/inception.py | 76 ++++++++++++++++----------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 42b0188355d..9e998f6108e 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -3,7 +3,7 @@ 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 @@ -25,43 +25,6 @@ _InceptionOutputs = InceptionOutputs -def inception_v3(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> Inception3: - r"""Inception v3 model architecture from - `"Rethinking the Inception Architecture for Computer Vision" `_. - - .. note:: - **Important**: In contrast to the other models the inception_v3 expects tensors with a size of - N x 3 x 299 x 299, so ensure your images are sized accordingly. - - 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, add an auxiliary branch that can improve training. - Default: *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' in kwargs: - original_aux_logits = kwargs['aux_logits'] - kwargs['aux_logits'] = True - else: - original_aux_logits = True - kwargs['init_weights'] = False # we are loading weights from a pretrained model - model = Inception3(**kwargs) - state_dict = load_state_dict_from_url(model_urls['inception_v3_google'], - progress=progress) - model.load_state_dict(state_dict) - if not original_aux_logits: - model.aux_logits = False - del model.AuxLogits - return model - - return Inception3(**kwargs) - - class Inception3(nn.Module): def __init__( @@ -478,3 +441,40 @@ def forward(self, x: Tensor) -> Tensor: x = self.conv(x) x = self.bn(x) return F.relu(x, inplace=True) + + +def inception_v3(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> Inception3: + r"""Inception v3 model architecture from + `"Rethinking the Inception Architecture for Computer Vision" `_. + + .. note:: + **Important**: In contrast to the other models the inception_v3 expects tensors with a size of + N x 3 x 299 x 299, so ensure your images are sized accordingly. + + 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, add an auxiliary branch that can improve training. + Default: *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' in kwargs: + original_aux_logits = kwargs['aux_logits'] + kwargs['aux_logits'] = True + else: + original_aux_logits = True + kwargs['init_weights'] = False # we are loading weights from a pretrained model + model = Inception3(**kwargs) + state_dict = load_state_dict_from_url(model_urls['inception_v3_google'], + progress=progress) + model.load_state_dict(state_dict) + if not original_aux_logits: + model.aux_logits = False + del model.AuxLogits + return model + + return Inception3(**kwargs) From 86ac382a5696c97fd3a442cbab610cde5899cde7 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 21 Oct 2020 23:36:33 +0200 Subject: [PATCH 3/8] style: Changed attribute setting for type hinting --- torchvision/models/inception.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 9e998f6108e..ebad2ad31e6 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -403,9 +403,9 @@ def __init__( conv_block = BasicConv2d self.conv0 = conv_block(in_channels, 128, kernel_size=1) self.conv1 = conv_block(128, 768, kernel_size=5) - self.conv1.stddev = 0.01 + setattr(self.conv1, 'stddev', 0.01) self.fc = nn.Linear(768, num_classes) - self.fc.stddev = 0.001 + setattr(self.fc, 'stddev', 0.001) def forward(self, x: Tensor) -> Tensor: # N x 768 x 17 x 17 From 064f948769b04782f9014d21fbe818282cfe624d Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 12:06:02 +0200 Subject: [PATCH 4/8] refactor: Removed un-necessary import --- torchvision/models/inception.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index ebad2ad31e6..7b698005306 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.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 Callable, Any, Optional, Tuple, List __all__ = ['Inception3', 'inception_v3', 'InceptionOutputs', '_InceptionOutputs'] From 70abb17e4178cbc20b40b8005e9d5f38bff327ee Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 20:59:02 +0200 Subject: [PATCH 5/8] fix: Fixed typing in constructors --- torchvision/models/inception.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 7b698005306..231f6d0c628 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -33,7 +33,7 @@ def __init__( transform_input: bool = False, inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, init_weights: Optional[bool] = None - ): + ) -> None: super(Inception3, self).__init__() if inception_blocks is None: inception_blocks = [ @@ -182,7 +182,7 @@ def __init__( in_channels: int, pool_features: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(InceptionA, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -224,7 +224,7 @@ def __init__( self, in_channels: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(InceptionB, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -258,7 +258,7 @@ def __init__( in_channels: int, channels_7x7: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(InceptionC, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -307,7 +307,7 @@ def __init__( self, in_channels: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(InceptionD, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -343,7 +343,7 @@ def __init__( self, in_channels: int, conv_block: Optional[Callable[..., nn.Module]] = None - ): + ) -> None: super(InceptionE, self).__init__() if conv_block is None: conv_block = BasicConv2d @@ -396,15 +396,15 @@ 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 self.conv0 = conv_block(in_channels, 128, kernel_size=1) self.conv1 = conv_block(128, 768, kernel_size=5) - setattr(self.conv1, 'stddev', 0.01) + self.conv1.stddev = 0.01 # type: ignore[assignment] self.fc = nn.Linear(768, num_classes) - setattr(self.fc, 'stddev', 0.001) + self.fc.stddev = 0.001 # type: ignore[assignment] def forward(self, x: Tensor) -> Tensor: # N x 768 x 17 x 17 @@ -431,7 +431,7 @@ def __init__( in_channels: int, out_channels: int, **kwargs - ): + ) -> 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 372383ef00904e0b80c7c05687d04e8b7c7fa1f0 Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 21:01:06 +0200 Subject: [PATCH 6/8] fix: Fixed kwargs typing --- torchvision/models/inception.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 231f6d0c628..ccb8e2a9f69 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -430,7 +430,7 @@ 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) From bcc75477636ad524f7157659b2cd34710cb8867a Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 22 Oct 2020 21:02:39 +0200 Subject: [PATCH 7/8] style: Fixed lint --- torchvision/models/inception.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index ccb8e2a9f69..1b2c2c31a47 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -404,7 +404,7 @@ def __init__( self.conv1 = conv_block(128, 768, kernel_size=5) self.conv1.stddev = 0.01 # type: ignore[assignment] self.fc = nn.Linear(768, num_classes) - self.fc.stddev = 0.001 # type: ignore[assignment] + self.fc.stddev = 0.001 # type: ignore[assignment] def forward(self, x: Tensor) -> Tensor: # N x 768 x 17 x 17 From 6887ebb15bdefb70dcfa352389e3328fdc109bff Mon Sep 17 00:00:00 2001 From: frgfm Date: Fri, 23 Oct 2020 11:29:29 +0200 Subject: [PATCH 8/8] refactor: Moved helpers function back and quote typed it --- torchvision/models/inception.py | 74 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 1b2c2c31a47..9e49e446849 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -24,6 +24,43 @@ _InceptionOutputs = InceptionOutputs +def inception_v3(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> "Inception3": + r"""Inception v3 model architecture from + `"Rethinking the Inception Architecture for Computer Vision" `_. + + .. note:: + **Important**: In contrast to the other models the inception_v3 expects tensors with a size of + N x 3 x 299 x 299, so ensure your images are sized accordingly. + + 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, add an auxiliary branch that can improve training. + Default: *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' in kwargs: + original_aux_logits = kwargs['aux_logits'] + kwargs['aux_logits'] = True + else: + original_aux_logits = True + kwargs['init_weights'] = False # we are loading weights from a pretrained model + model = Inception3(**kwargs) + state_dict = load_state_dict_from_url(model_urls['inception_v3_google'], + progress=progress) + model.load_state_dict(state_dict) + if not original_aux_logits: + model.aux_logits = False + del model.AuxLogits + return model + + return Inception3(**kwargs) + + class Inception3(nn.Module): def __init__( @@ -440,40 +477,3 @@ def forward(self, x: Tensor) -> Tensor: x = self.conv(x) x = self.bn(x) return F.relu(x, inplace=True) - - -def inception_v3(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> Inception3: - r"""Inception v3 model architecture from - `"Rethinking the Inception Architecture for Computer Vision" `_. - - .. note:: - **Important**: In contrast to the other models the inception_v3 expects tensors with a size of - N x 3 x 299 x 299, so ensure your images are sized accordingly. - - 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, add an auxiliary branch that can improve training. - Default: *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' in kwargs: - original_aux_logits = kwargs['aux_logits'] - kwargs['aux_logits'] = True - else: - original_aux_logits = True - kwargs['init_weights'] = False # we are loading weights from a pretrained model - model = Inception3(**kwargs) - state_dict = load_state_dict_from_url(model_urls['inception_v3_google'], - progress=progress) - model.load_state_dict(state_dict) - if not original_aux_logits: - model.aux_logits = False - del model.AuxLogits - return model - - return Inception3(**kwargs)