From af2cd761c69c1c0df87a4972d70b66915e1fe27d Mon Sep 17 00:00:00 2001 From: ekka Date: Sat, 9 Mar 2019 00:29:27 +0530 Subject: [PATCH 1/3] Internal imagenet normalisation for pretrained models like InceptionV3 implementation Added the argument transform_input during the calling of densenet models for automatic normalisation of inputs with imagenet mean and std for easier use. This is consistent with the inceptionV3 pytorch implementation. --- torchvision/models/densenet.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/torchvision/models/densenet.py b/torchvision/models/densenet.py index 43f8e7c3eba..b97253bde52 100644 --- a/torchvision/models/densenet.py +++ b/torchvision/models/densenet.py @@ -69,9 +69,11 @@ class DenseNet(nn.Module): """ def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), - num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000): + num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000, transform_input=False): super(DenseNet, self).__init__() + + self.transform_input = transform_input # First convolution self.features = nn.Sequential(OrderedDict([ @@ -110,6 +112,14 @@ def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), nn.init.constant_(m.bias, 0) def forward(self, x): + + #imagenet normalisation + if self.transform_input: + x_ch0 = (torch.unsqueeze(x[:, 0], 1) - 0.485) / 0.229 + x_ch1 = (torch.unsqueeze(x[:, 1], 1) - 0.456) / 0.224 + x_ch2 = (torch.unsqueeze(x[:, 2], 1) - 0.406) / 0.225 + x = torch.cat((x_ch0, x_ch1, x_ch2), 1) + features = self.features(x) out = F.relu(features, inplace=True) out = F.adaptive_avg_pool2d(out, (1, 1)).view(features.size(0), -1) From 4c262fd416bc1dad3324ef5f9a78ad7abc2f654f Mon Sep 17 00:00:00 2001 From: ekka Date: Sat, 9 Mar 2019 01:23:16 +0530 Subject: [PATCH 2/3] fixed flake8 --- torchvision/models/densenet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/models/densenet.py b/torchvision/models/densenet.py index b97253bde52..d4ca9fff689 100644 --- a/torchvision/models/densenet.py +++ b/torchvision/models/densenet.py @@ -72,7 +72,7 @@ def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000, transform_input=False): super(DenseNet, self).__init__() - + self.transform_input = transform_input # First convolution @@ -112,14 +112,14 @@ def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), nn.init.constant_(m.bias, 0) def forward(self, x): - - #imagenet normalisation + + # imagenet normalisation if self.transform_input: x_ch0 = (torch.unsqueeze(x[:, 0], 1) - 0.485) / 0.229 x_ch1 = (torch.unsqueeze(x[:, 1], 1) - 0.456) / 0.224 x_ch2 = (torch.unsqueeze(x[:, 2], 1) - 0.406) / 0.225 x = torch.cat((x_ch0, x_ch1, x_ch2), 1) - + features = self.features(x) out = F.relu(features, inplace=True) out = F.adaptive_avg_pool2d(out, (1, 1)).view(features.size(0), -1) From 9f3140dfa36ae90534cd12529f61118dfac57eca Mon Sep 17 00:00:00 2001 From: ekka Date: Sat, 9 Mar 2019 22:00:12 +0530 Subject: [PATCH 3/3] Updated docs of densnets with transform_input argument Included the argument `transform_input` in the docs of densenets --- torchvision/models/densenet.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/torchvision/models/densenet.py b/torchvision/models/densenet.py index d4ca9fff689..beb5857bbe8 100644 --- a/torchvision/models/densenet.py +++ b/torchvision/models/densenet.py @@ -133,6 +133,8 @@ def densenet121(pretrained=False, **kwargs): Args: pretrained (bool): If True, returns a model pre-trained on ImageNet + transform_input (bool): If True, preprocesses the input according to the method with which it + was trained on ImageNet. Default: *False* """ model = DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 24, 16), **kwargs) @@ -160,6 +162,8 @@ def densenet169(pretrained=False, **kwargs): Args: pretrained (bool): If True, returns a model pre-trained on ImageNet + transform_input (bool): If True, preprocesses the input according to the method with which it + was trained on ImageNet. Default: *False* """ model = DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 32, 32), **kwargs) @@ -187,6 +191,8 @@ def densenet201(pretrained=False, **kwargs): Args: pretrained (bool): If True, returns a model pre-trained on ImageNet + transform_input (bool): If True, preprocesses the input according to the method with which it + was trained on ImageNet. Default: *False* """ model = DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32), **kwargs) @@ -214,6 +220,8 @@ def densenet161(pretrained=False, **kwargs): Args: pretrained (bool): If True, returns a model pre-trained on ImageNet + transform_input (bool): If True, preprocesses the input according to the method with which it + was trained on ImageNet. Default: *False* """ model = DenseNet(num_init_features=96, growth_rate=48, block_config=(6, 12, 36, 24), **kwargs)