r NASNetALarge model architecture from the NASNet <https : // arxiv . org / abs / 1707 . 07012 > _ paper .

def nasnetalarge(num_classes=1001, pretrained='imagenet'):
    r"""NASNetALarge model architecture from the
    `"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
    """
    if pretrained:
        settings = pretrained_settings['nasnetalarge'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)

        # both 'imagenet'&'imagenet+background' are loaded from same parameters
        model = NASNetALarge(num_classes=1001)
        model.load_state_dict(model_zoo.load_url(settings['url']))

        if pretrained == 'imagenet':
            new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
            new_last_linear.weight.data = model.last_linear.weight.data[1:]
            new_last_linear.bias.data = model.last_linear.bias.data[1:]
            model.last_linear = new_last_linear

        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']

        model.mean = settings['mean']
        model.std = settings['std']
    else:
        model = NASNetALarge(num_classes=num_classes)
    return model


r BNInception model architecture from <https : // arxiv . org / pdf / 1502 . 03167 . pdf > _ paper .

def bninception(num_classes=1000, pretrained='imagenet'):
    r"""BNInception model architecture from <https://arxiv.org/pdf/1502.03167.pdf>`_ paper.
    """
    model = BNInception(num_classes=num_classes)
    if pretrained is not None:
        settings = pretrained_settings['bninception'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        model.mean = settings['mean']
        model.std = settings['std']
    return model

def
bninception
(
num_classes
=
1000
,
pretrained
=
'imagenet'
)
:
model
=
BNInception
(
num_classes
=
num_classes
)
if
pretrained
is
not
:
settings
=
pretrained_settings
[
'bninception'
]
[
pretrained
]
assert
num_classes
==
settings
[
'num_classes'
]
,
"num_classes should be {}, but is {}"
.
format
(
settings
[
'num_classes'
]
,
num_classes
)
model
.
load_state_dict
(
model_zoo
.
load_url
(
settings
[
'url'
]
)
)
model
.
input_space
=
settings
[
'input_space'
]
model
.
input_size
=
settings
[
'input_size'
]
model
.
input_range
=
settings
[
'input_range'
]
model
.
mean
=
settings
[
'mean'
]
model
.
std
=
settings
[
'std'
]
return
model


r AlexNet model architecture from the One weird trick ... <https : // arxiv . org / abs / 1404 . 5997 > _ paper .

def alexnet(num_classes=1000, pretrained='imagenet'):
    r"""AlexNet model architecture from the
    `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
    """
    # https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py
    model = models.alexnet(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['alexnet'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_alexnet(model)
    return model


r Densenet - 121 model from Densely Connected Convolutional Networks <https : // arxiv . org / pdf / 1608 . 06993 . pdf >

def densenet121(num_classes=1000, pretrained='imagenet'):
    r"""Densenet-121 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
    """
    model = models.densenet121(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['densenet121'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_densenets(model)
    return model

def densenet121 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . densenet121 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'densenet121' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_densenets ( model ) return model


r Densenet - 169 model from Densely Connected Convolutional Networks <https : // arxiv . org / pdf / 1608 . 06993 . pdf >

def densenet169(num_classes=1000, pretrained='imagenet'):
    r"""Densenet-169 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
    """
    model = models.densenet169(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['densenet169'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_densenets(model)
    return model

def densenet169 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . densenet169 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'densenet169' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_densenets ( model ) return model


r Densenet - 201 model from Densely Connected Convolutional Networks <https : // arxiv . org / pdf / 1608 . 06993 . pdf >

def densenet201(num_classes=1000, pretrained='imagenet'):
    r"""Densenet-201 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
    """
    model = models.densenet201(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['densenet201'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_densenets(model)
    return model

def densenet201 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . densenet201 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'densenet201' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_densenets ( model ) return model


r Densenet - 161 model from Densely Connected Convolutional Networks <https : // arxiv . org / pdf / 1608 . 06993 . pdf >

def densenet161(num_classes=1000, pretrained='imagenet'):
    r"""Densenet-161 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
    """
    model = models.densenet161(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['densenet161'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_densenets(model)
    return model

def densenet161 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . densenet161 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'densenet161' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_densenets ( model ) return model


r Inception v3 model architecture from Rethinking the Inception Architecture for Computer Vision <http : // arxiv . org / abs / 1512 . 00567 > _ .

def inceptionv3(num_classes=1000, pretrained='imagenet'):
    r"""Inception v3 model architecture from
    `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_.
    """
    model = models.inception_v3(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['inceptionv3'][pretrained]
        model = load_pretrained(model, num_classes, settings)

    # Modify attributs
    model.last_linear = model.fc
    del model.fc

    def features(self, input):
        # 299 x 299 x 3
        x = self.Conv2d_1a_3x3(input) # 149 x 149 x 32
        x = self.Conv2d_2a_3x3(x) # 147 x 147 x 32
        x = self.Conv2d_2b_3x3(x) # 147 x 147 x 64
        x = F.max_pool2d(x, kernel_size=3, stride=2) # 73 x 73 x 64
        x = self.Conv2d_3b_1x1(x) # 73 x 73 x 80
        x = self.Conv2d_4a_3x3(x) # 71 x 71 x 192
        x = F.max_pool2d(x, kernel_size=3, stride=2) # 35 x 35 x 192
        x = self.Mixed_5b(x) # 35 x 35 x 256
        x = self.Mixed_5c(x) # 35 x 35 x 288
        x = self.Mixed_5d(x) # 35 x 35 x 288
        x = self.Mixed_6a(x) # 17 x 17 x 768
        x = self.Mixed_6b(x) # 17 x 17 x 768
        x = self.Mixed_6c(x) # 17 x 17 x 768
        x = self.Mixed_6d(x) # 17 x 17 x 768
        x = self.Mixed_6e(x) # 17 x 17 x 768
        if self.training and self.aux_logits:
            self._out_aux = self.AuxLogits(x) # 17 x 17 x 768
        x = self.Mixed_7a(x) # 8 x 8 x 1280
        x = self.Mixed_7b(x) # 8 x 8 x 2048
        x = self.Mixed_7c(x) # 8 x 8 x 2048
        return x

    def logits(self, features):
        x = F.avg_pool2d(features, kernel_size=8) # 1 x 1 x 2048
        x = F.dropout(x, training=self.training) # 1 x 1 x 2048
        x = x.view(x.size(0), -1) # 2048
        x = self.last_linear(x) # 1000 (num_classes)
        if self.training and self.aux_logits:
            aux = self._out_aux
            self._out_aux = None
            return x, aux
        return x

    def forward(self, input):
        x = self.features(input)
        x = self.logits(x)
        return x

    # Modify methods
    model.features = types.MethodType(features, model)
    model.logits = types.MethodType(logits, model)
    model.forward = types.MethodType(forward, model)
    return model

def
inceptionv3
(
num_classes
=
1000
,
pretrained
=
'imagenet'
)
:
model
=
models
.
inception_v3
(
pretrained
=
False
)
if
pretrained
is
not
:
settings
=
pretrained_settings
[
'inceptionv3'
]
[
pretrained
]
model
=
load_pretrained
(
model
,
num_classes
,
settings
)
# Modify attributs
model
.
last_linear
=
model
.
fc
del
model
.
fc
def
features
(
self
,
input
)
:
# 299 x 299 x 3
x
=
self
.
Conv2d_1a_3x3
(
input
)
# 149 x 149 x 32
x
=
self
.
Conv2d_2a_3x3
(
x
)
# 147 x 147 x 32
x
=
self
.
Conv2d_2b_3x3
(
x
)
# 147 x 147 x 64
x
=
F
.
max_pool2d
(
x
,
kernel_size
=
3
,
stride
=
2
)
# 73 x 73 x 64
x
=
self
.
Conv2d_3b_1x1
(
x
)
# 73 x 73 x 80
x
=
self
.
Conv2d_4a_3x3
(
x
)
# 71 x 71 x 192
x
=
F
.
max_pool2d
(
x
,
kernel_size
=
3
,
stride
=
2
)
# 35 x 35 x 192
x
=
self
.
Mixed_5b
(
x
)
# 35 x 35 x 256
x
=
self
.
Mixed_5c
(
x
)
# 35 x 35 x 288
x
=
self
.
Mixed_5d
(
x
)
# 35 x 35 x 288
x
=
self
.
Mixed_6a
(
x
)
# 17 x 17 x 768
x
=
self
.
Mixed_6b
(
x
)
# 17 x 17 x 768
x
=
self
.
Mixed_6c
(
x
)
# 17 x 17 x 768
x
=
self
.
Mixed_6d
(
x
)
# 17 x 17 x 768
x
=
self
.
Mixed_6e
(
x
)
# 17 x 17 x 768
if
self
.
training
and
self
.
aux_logits
:
self
.
_out_aux
=
self
.
AuxLogits
(
x
)
# 17 x 17 x 768
x
=
self
.
Mixed_7a
(
x
)
# 8 x 8 x 1280
x
=
self
.
Mixed_7b
(
x
)
# 8 x 8 x 2048
x
=
self
.
Mixed_7c
(
x
)
# 8 x 8 x 2048
return
x
def
logits
(
self
,
features
)
:
x
=
F
.
avg_pool2d
(
features
,
kernel_size
=
8
)
# 1 x 1 x 2048
x
=
F
.
dropout
(
x
,
training
=
self
.
training
)
# 1 x 1 x 2048
x
=
x
.
view
(
x
.
size
(
0
)
,
1
)
# 2048
x
=
self
.
last_linear
(
x
)
# 1000 (num_classes)
if
self
.
training
and
self
.
aux_logits
:
aux
=
self
.
_out_aux
self
.
_out_aux
=
return
x
,
aux
return
x
def
forward
(
self
,
input
)
:
x
=
self
.
features
(
input
)
x
=
self
.
logits
(
x
)
return
x
# Modify methods
model
.
features
=
types
.
MethodType
(
features
,
model
)
model
.
logits
=
types
.
MethodType
(
logits
,
model
)
model
.
forward
=
types
.
MethodType
(
forward
,
model
)
return
model


def resnet18(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-18 model.
    """
    model = models.resnet18(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet18'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_resnets(model)
    return model

def resnet18 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . resnet18 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'resnet18' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_resnets ( model ) return model


def resnet34(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-34 model.
    """
    model = models.resnet34(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet34'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_resnets(model)
    return model

def resnet34 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . resnet34 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'resnet34' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_resnets ( model ) return model


def resnet50(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-50 model.
    """
    model = models.resnet50(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet50'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_resnets(model)
    return model

def resnet50 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . resnet50 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'resnet50' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_resnets ( model ) return model


def resnet101(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-101 model.
    """
    model = models.resnet101(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet101'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_resnets(model)
    return model

def resnet101 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . resnet101 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'resnet101' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_resnets ( model ) return model


def resnet152(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-152 model.
    """
    model = models.resnet152(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet152'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_resnets(model)
    return model

def resnet152 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . resnet152 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'resnet152' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_resnets ( model ) return model


r SqueezeNet model architecture from the SqueezeNet : AlexNet - level accuracy with 50x fewer parameters and <0 . 5MB model size <https : // arxiv . org / abs / 1602 . 07360 > _ paper .

def squeezenet1_0(num_classes=1000, pretrained='imagenet'):
    r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
    accuracy with 50x fewer parameters and <0.5MB model size"
    <https://arxiv.org/abs/1602.07360>`_ paper.
    """
    model = models.squeezenet1_0(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['squeezenet1_0'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_squeezenets(model)
    return model

def squeezenet1_0 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . squeezenet1_0 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'squeezenet1_0' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_squeezenets ( model ) return model


r SqueezeNet 1 . 1 model from the official SqueezeNet repo <https : // github . com / DeepScale / SqueezeNet / tree / master / SqueezeNet_v1 . 1 > _ . SqueezeNet 1 . 1 has 2 . 4x less computation and slightly fewer parameters than SqueezeNet 1 . 0 without sacrificing accuracy .

def squeezenet1_1(num_classes=1000, pretrained='imagenet'):
    r"""SqueezeNet 1.1 model from the `official SqueezeNet repo
    <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_.
    SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters
    than SqueezeNet 1.0, without sacrificing accuracy.
    """
    model = models.squeezenet1_1(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['squeezenet1_1'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_squeezenets(model)
    return model

def squeezenet1_1 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . squeezenet1_1 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'squeezenet1_1' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_squeezenets ( model ) return model


VGG 11 - layer model ( configuration A )

def vgg11(num_classes=1000, pretrained='imagenet'):
    """VGG 11-layer model (configuration "A")
    """
    model = models.vgg11(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg11'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg11 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg11 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg11' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 11 - layer model ( configuration A ) with batch normalization

def vgg11_bn(num_classes=1000, pretrained='imagenet'):
    """VGG 11-layer model (configuration "A") with batch normalization
    """
    model = models.vgg11_bn(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg11_bn'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg11_bn ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg11_bn ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg11_bn' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 13 - layer model ( configuration B )

def vgg13(num_classes=1000, pretrained='imagenet'):
    """VGG 13-layer model (configuration "B")
    """
    model = models.vgg13(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg13'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg13 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg13 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg13' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 13 - layer model ( configuration B ) with batch normalization

def vgg13_bn(num_classes=1000, pretrained='imagenet'):
    """VGG 13-layer model (configuration "B") with batch normalization
    """
    model = models.vgg13_bn(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg13_bn'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg13_bn ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg13_bn ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg13_bn' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 16 - layer model ( configuration D )

def vgg16(num_classes=1000, pretrained='imagenet'):
    """VGG 16-layer model (configuration "D")
    """
    model = models.vgg16(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg16'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg16 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg16 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg16' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 16 - layer model ( configuration D ) with batch normalization

def vgg16_bn(num_classes=1000, pretrained='imagenet'):
    """VGG 16-layer model (configuration "D") with batch normalization
    """
    model = models.vgg16_bn(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg16_bn'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg16_bn ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg16_bn ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg16_bn' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 19 - layer model ( configuration E )

def vgg19(num_classes=1000, pretrained='imagenet'):
    """VGG 19-layer model (configuration "E")
    """
    model = models.vgg19(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg19'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg19 ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg19 ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg19' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


VGG 19 - layer model ( configuration E ) with batch normalization

def vgg19_bn(num_classes=1000, pretrained='imagenet'):
    """VGG 19-layer model (configuration 'E') with batch normalization
    """
    model = models.vgg19_bn(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg19_bn'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model

def vgg19_bn ( num_classes = 1000 , pretrained = 'imagenet' ) : model = models . vgg19_bn ( pretrained = False ) if pretrained is not None : settings = pretrained_settings [ 'vgg19_bn' ] [ pretrained ] model = load_pretrained ( model , num_classes , settings ) model = modify_vggs ( model ) return model


Parameters ---------- block ( nn . Module ) : Bottleneck class . - For SENet154 : SEBottleneck - For SE - ResNet models : SEResNetBottleneck - For SE - ResNeXt models : SEResNeXtBottleneck layers ( list of ints ) : Number of residual blocks for 4 layers of the network ( layer1 ... layer4 ) . groups ( int ) : Number of groups for the 3x3 convolution in each bottleneck block . - For SENet154 : 64 - For SE - ResNet models : 1 - For SE - ResNeXt models : 32 reduction ( int ) : Reduction ratio for Squeeze - and - Excitation modules . - For all models : 16 dropout_p ( float or None ) : Drop probability for the Dropout layer . If None the Dropout layer is not used . - For SENet154 : 0 . 2 - For SE - ResNet models : None - For SE - ResNeXt models : None inplanes ( int ) : Number of input channels for layer1 . - For SENet154 : 128 - For SE - ResNet models : 64 - For SE - ResNeXt models : 64 input_3x3 ( bool ) : If True use three 3x3 convolutions instead of a single 7x7 convolution in layer0 . - For SENet154 : True - For SE - ResNet models : False - For SE - ResNeXt models : False downsample_kernel_size ( int ) : Kernel size for downsampling convolutions in layer2 layer3 and layer4 . - For SENet154 : 3 - For SE - ResNet models : 1 - For SE - ResNeXt models : 1 downsample_padding ( int ) : Padding for downsampling convolutions in layer2 layer3 and layer4 . - For SENet154 : 1 - For SE - ResNet models : 0 - For SE - ResNeXt models : 0 num_classes ( int ) : Number of outputs in last_linear layer . - For all models : 1000

def __init__(self, block, layers, groups, reduction, dropout_p=0.2,
                 inplanes=128, input_3x3=True, downsample_kernel_size=3,
                 downsample_padding=1, num_classes=1000):
        """
        Parameters
        ----------
        block (nn.Module): Bottleneck class.
            - For SENet154: SEBottleneck
            - For SE-ResNet models: SEResNetBottleneck
            - For SE-ResNeXt models:  SEResNeXtBottleneck
        layers (list of ints): Number of residual blocks for 4 layers of the
            network (layer1...layer4).
        groups (int): Number of groups for the 3x3 convolution in each
            bottleneck block.
            - For SENet154: 64
            - For SE-ResNet models: 1
            - For SE-ResNeXt models:  32
        reduction (int): Reduction ratio for Squeeze-and-Excitation modules.
            - For all models: 16
        dropout_p (float or None): Drop probability for the Dropout layer.
            If `None` the Dropout layer is not used.
            - For SENet154: 0.2
            - For SE-ResNet models: None
            - For SE-ResNeXt models: None
        inplanes (int):  Number of input channels for layer1.
            - For SENet154: 128
            - For SE-ResNet models: 64
            - For SE-ResNeXt models: 64
        input_3x3 (bool): If `True`, use three 3x3 convolutions instead of
            a single 7x7 convolution in layer0.
            - For SENet154: True
            - For SE-ResNet models: False
            - For SE-ResNeXt models: False
        downsample_kernel_size (int): Kernel size for downsampling convolutions
            in layer2, layer3 and layer4.
            - For SENet154: 3
            - For SE-ResNet models: 1
            - For SE-ResNeXt models: 1
        downsample_padding (int): Padding for downsampling convolutions in
            layer2, layer3 and layer4.
            - For SENet154: 1
            - For SE-ResNet models: 0
            - For SE-ResNeXt models: 0
        num_classes (int): Number of outputs in `last_linear` layer.
            - For all models: 1000
        """
        super(SENet, self).__init__()
        self.inplanes = inplanes
        if input_3x3:
            layer0_modules = [
                ('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
                                    bias=False)),
                ('bn1', nn.BatchNorm2d(64)),
                ('relu1', nn.ReLU(inplace=True)),
                ('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
                                    bias=False)),
                ('bn2', nn.BatchNorm2d(64)),
                ('relu2', nn.ReLU(inplace=True)),
                ('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,
                                    bias=False)),
                ('bn3', nn.BatchNorm2d(inplanes)),
                ('relu3', nn.ReLU(inplace=True)),
            ]
        else:
            layer0_modules = [
                ('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,
                                    padding=3, bias=False)),
                ('bn1', nn.BatchNorm2d(inplanes)),
                ('relu1', nn.ReLU(inplace=True)),
            ]
        # To preserve compatibility with Caffe weights `ceil_mode=True`
        # is used instead of `padding=1`.
        layer0_modules.append(('pool', nn.MaxPool2d(3, stride=2,
                                                    ceil_mode=True)))
        self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
        self.layer1 = self._make_layer(
            block,
            planes=64,
            blocks=layers[0],
            groups=groups,
            reduction=reduction,
            downsample_kernel_size=1,
            downsample_padding=0
        )
        self.layer2 = self._make_layer(
            block,
            planes=128,
            blocks=layers[1],
            stride=2,
            groups=groups,
            reduction=reduction,
            downsample_kernel_size=downsample_kernel_size,
            downsample_padding=downsample_padding
        )
        self.layer3 = self._make_layer(
            block,
            planes=256,
            blocks=layers[2],
            stride=2,
            groups=groups,
            reduction=reduction,
            downsample_kernel_size=downsample_kernel_size,
            downsample_padding=downsample_padding
        )
        self.layer4 = self._make_layer(
            block,
            planes=512,
            blocks=layers[3],
            stride=2,
            groups=groups,
            reduction=reduction,
            downsample_kernel_size=downsample_kernel_size,
            downsample_padding=downsample_padding
        )
        self.avg_pool = nn.AvgPool2d(7, stride=1)
        self.dropout = nn.Dropout(dropout_p) if dropout_p is not None else None
        self.last_linear = nn.Linear(512 * block.expansion, num_classes)


def conv3x3(in_planes, out_planes, stride=1):
  "3x3 convolution with padding"
  return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
           padding=1, bias=False)

def conv3x3 ( in_planes , out_planes , stride = 1 ) : return nn . Conv2d ( in_planes , out_planes , kernel_size = 3 , stride = stride , padding = 1 , bias = False )


Constructs a ResNet - 101 model . Args : pretrained ( bool ) : If True returns a model pre - trained on ImageNet

def cafferesnet101(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-101 model.
    Args:
    pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes)
    if pretrained is not None:
        settings = pretrained_settings['cafferesnet101'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        model.mean = settings['mean']
        model.std = settings['std']
    return model

def
cafferesnet101
(
num_classes
=
1000
,
pretrained
=
'imagenet'
)
:
model
=
ResNet
(
Bottleneck
,
[
3
,
4
,
23
,
3
]
,
num_classes
=
num_classes
)
if
pretrained
is
not
:
settings
=
pretrained_settings
[
'cafferesnet101'
]
[
pretrained
]
assert
num_classes
==
settings
[
'num_classes'
]
,
"num_classes should be {}, but is {}"
.
format
(
settings
[
'num_classes'
]
,
num_classes
)
model
.
load_state_dict
(
model_zoo
.
load_url
(
settings
[
'url'
]
)
)
model
.
input_space
=
settings
[
'input_space'
]
model
.
input_size
=
settings
[
'input_size'
]
model
.
input_range
=
settings
[
'input_range'
]
model
.
mean
=
settings
[
'mean'
]
model
.
std
=
settings
[
'std'
]
return
model


r NASNetALarge model architecture from the NASNet <https : // arxiv . org / abs / 1707 . 07012 > _ paper .

def nasnetamobile(num_classes=1000, pretrained='imagenet'):
    r"""NASNetALarge model architecture from the
    `"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
    """
    if pretrained:
        settings = pretrained_settings['nasnetamobile'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)

        # both 'imagenet'&'imagenet+background' are loaded from same parameters
        model = NASNetAMobile(num_classes=num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url'], map_location=None))

       # if pretrained == 'imagenet':
       #     new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
       #     new_last_linear.weight.data = model.last_linear.weight.data[1:]
       #     new_last_linear.bias.data = model.last_linear.bias.data[1:]
       #     model.last_linear = new_last_linear

        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']

        model.mean = settings['mean']
        model.std = settings['std']
    else:
        settings = pretrained_settings['nasnetamobile']['imagenet']
        model = NASNetAMobile(num_classes=num_classes)
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']

        model.mean = settings['mean']
        model.std = settings['std']
    return model

#     model.last_linear = new_last_linear


def conv3x3(in_planes, out_planes, stride=1):
    "3x3 convolution with padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=True)

def conv3x3 ( in_planes , out_planes , stride = 1 ) : return nn . Conv2d ( in_planes , out_planes , kernel_size = 3 , stride = stride , padding = 1 , bias = True )


def fbresnet18(num_classes=1000):
    """Constructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = FBResNet(BasicBlock, [2, 2, 2, 2], num_classes=num_classes)
    return model

def fbresnet18 ( num_classes = 1000 ) : model = FBResNet ( BasicBlock , [ 2 , 2 , 2 , 2 ] , num_classes = num_classes ) return model


def fbresnet34(num_classes=1000):
    """Constructs a ResNet-34 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = FBResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes)
    return model

def fbresnet34 ( num_classes = 1000 ) : model = FBResNet ( BasicBlock , [ 3 , 4 , 6 , 3 ] , num_classes = num_classes ) return model


def fbresnet50(num_classes=1000):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = FBResNet(Bottleneck, [3, 4, 6, 3], num_classes=num_classes)
    return model

def fbresnet50 ( num_classes = 1000 ) : model = FBResNet ( Bottleneck , [ 3 , 4 , 6 , 3 ] , num_classes = num_classes ) return model


def fbresnet101(num_classes=1000):
    """Constructs a ResNet-101 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = FBResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes)
    return model

def fbresnet101 ( num_classes = 1000 ) : model = FBResNet ( Bottleneck , [ 3 , 4 , 23 , 3 ] , num_classes = num_classes ) return model


def fbresnet152(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-152 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = FBResNet(Bottleneck, [3, 8, 36, 3], num_classes=num_classes)
    if pretrained is not None:
        settings = pretrained_settings['fbresnet152'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        model.mean = settings['mean']
        model.std = settings['std']
    return model

def
fbresnet152
(
num_classes
=
1000
,
pretrained
=
'imagenet'
)
:
model
=
FBResNet
(
Bottleneck
,
[
3
,
8
,
36
,
3
]
,
num_classes
=
num_classes
)
if
pretrained
is
not
:
settings
=
pretrained_settings
[
'fbresnet152'
]
[
pretrained
]
assert
num_classes
==
settings
[
'num_classes'
]
,
"num_classes should be {}, but is {}"
.
format
(
settings
[
'num_classes'
]
,
num_classes
)
model
.
load_state_dict
(
model_zoo
.
load_url
(
settings
[
'url'
]
)
)
model
.
input_space
=
settings
[
'input_space'
]
model
.
input_size
=
settings
[
'input_size'
]
model
.
input_range
=
settings
[
'input_range'
]
model
.
mean
=
settings
[
'mean'
]
model
.
std
=
settings
[
'std'
]
return
model


r InceptionResNetV2 model architecture from the InceptionV4 Inception - ResNet ... <https : // arxiv . org / abs / 1602 . 07261 > _ paper .

def inceptionresnetv2(num_classes=1000, pretrained='imagenet'):
    r"""InceptionResNetV2 model architecture from the
    `"InceptionV4, Inception-ResNet..." <https://arxiv.org/abs/1602.07261>`_ paper.
    """
    if pretrained:
        settings = pretrained_settings['inceptionresnetv2'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)

        # both 'imagenet'&'imagenet+background' are loaded from same parameters
        model = InceptionResNetV2(num_classes=1001)
        model.load_state_dict(model_zoo.load_url(settings['url']))

        if pretrained == 'imagenet':
            new_last_linear = nn.Linear(1536, 1000)
            new_last_linear.weight.data = model.last_linear.weight.data[1:]
            new_last_linear.bias.data = model.last_linear.bias.data[1:]
            model.last_linear = new_last_linear

        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']

        model.mean = settings['mean']
        model.std = settings['std']
    else:
        model = InceptionResNetV2(num_classes=num_classes)
    return model


Constructor Args : num_classes : number of classes

def __init__(self, num_classes=1000):
        """ Constructor
        Args:
            num_classes: number of classes
        """
        super(Xception, self).__init__()
        self.num_classes = num_classes

        self.conv1 = nn.Conv2d(3, 32, 3,2, 0, bias=False)
        self.bn1 = nn.BatchNorm2d(32)
        self.relu1 = nn.ReLU(inplace=True)

        self.conv2 = nn.Conv2d(32,64,3,bias=False)
        self.bn2 = nn.BatchNorm2d(64)
        self.relu2 = nn.ReLU(inplace=True)
        #do relu here

        self.block1=Block(64,128,2,2,start_with_relu=False,grow_first=True)
        self.block2=Block(128,256,2,2,start_with_relu=True,grow_first=True)
        self.block3=Block(256,728,2,2,start_with_relu=True,grow_first=True)

        self.block4=Block(728,728,3,1,start_with_relu=True,grow_first=True)
        self.block5=Block(728,728,3,1,start_with_relu=True,grow_first=True)
        self.block6=Block(728,728,3,1,start_with_relu=True,grow_first=True)
        self.block7=Block(728,728,3,1,start_with_relu=True,grow_first=True)

        self.block8=Block(728,728,3,1,start_with_relu=True,grow_first=True)
        self.block9=Block(728,728,3,1,start_with_relu=True,grow_first=True)
        self.block10=Block(728,728,3,1,start_with_relu=True,grow_first=True)
        self.block11=Block(728,728,3,1,start_with_relu=True,grow_first=True)

        self.block12=Block(728,1024,2,2,start_with_relu=True,grow_first=False)

        self.conv3 = SeparableConv2d(1024,1536,3,1,1)
        self.bn3 = nn.BatchNorm2d(1536)
        self.relu3 = nn.ReLU(inplace=True)

        #do relu here
        self.conv4 = SeparableConv2d(1536,2048,3,1,1)
        self.bn4 = nn.BatchNorm2d(2048)

        self.fc = nn.Linear(2048, num_classes)

def
__init__
(
self
,
num_classes
=
1000
)
:
super
(
Xception
,
self
)
.
__init__
(
)
self
.
num_classes
=
num_classes
self
.
conv1
=
nn
.
Conv2d
(
3
,
32
,
3
,
2
,
0
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
32
)
self
.
relu1
=
nn
.
ReLU
(
inplace
=
True
)
self
.
conv2
=
nn
.
Conv2d
(
32
,
64
,
3
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
64
)
self
.
relu2
=
nn
.
ReLU
(
inplace
=
True
)
#do relu here
self
.
block1
=
Block
(
64
,
128
,
2
,
2
,
start_with_relu
=
False
,
grow_first
=
True
)
self
.
block2
=
Block
(
128
,
256
,
2
,
2
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block3
=
Block
(
256
,
728
,
2
,
2
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block4
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block5
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block6
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block7
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block8
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block9
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block10
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block11
=
Block
(
728
,
728
,
3
,
1
,
start_with_relu
=
True
,
grow_first
=
True
)
self
.
block12
=
Block
(
728
,
1024
,
2
,
2
,
start_with_relu
=
True
,
grow_first
=
False
)
self
.
conv3
=
SeparableConv2d
(
1024
,
1536
,
3
,
1
,
1
)
self
.
bn3
=
nn
.
BatchNorm2d
(
1536
)
self
.
relu3
=
nn
.
ReLU
(
inplace
=
True
)
#do relu here
self
.
conv4
=
SeparableConv2d
(
1536
,
2048
,
3
,
1
,
1
)
self
.
bn4
=
nn
.
BatchNorm2d
(
2048
)
self
.
fc
=
nn
.
Linear
(
2048
,
num_classes
)


r PNASNet - 5 model architecture from the Progressive Neural Architecture Search <https : // arxiv . org / abs / 1712 . 00559 > _ paper .

def pnasnet5large(num_classes=1001, pretrained='imagenet'):
    r"""PNASNet-5 model architecture from the
    `"Progressive Neural Architecture Search"
    <https://arxiv.org/abs/1712.00559>`_ paper.
    """
    if pretrained:
        settings = pretrained_settings['pnasnet5large'][pretrained]
        assert num_classes == settings[
            'num_classes'], 'num_classes should be {}, but is {}'.format(
            settings['num_classes'], num_classes)

        # both 'imagenet'&'imagenet+background' are loaded from same parameters
        model = PNASNet5Large(num_classes=1001)
        model.load_state_dict(model_zoo.load_url(settings['url']))

        if pretrained == 'imagenet':
            new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
            new_last_linear.weight.data = model.last_linear.weight.data[1:]
            new_last_linear.bias.data = model.last_linear.bias.data[1:]
            model.last_linear = new_last_linear

        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']

        model.mean = settings['mean']
        model.std = settings['std']
    else:
        model = PNASNet5Large(num_classes=num_classes)
    return model


PolyNet architecture from the paper PolyNet : A Pursuit of Structural Diversity in Very Deep Networks https : // arxiv . org / abs / 1611 . 05725

def polynet(num_classes=1000, pretrained='imagenet'):
    """PolyNet architecture from the paper
    'PolyNet: A Pursuit of Structural Diversity in Very Deep Networks'
    https://arxiv.org/abs/1611.05725
    """
    if pretrained:
        settings = pretrained_settings['polynet'][pretrained]
        assert num_classes == settings['num_classes'], \
            'num_classes should be {}, but is {}'.format(
                settings['num_classes'], num_classes)
        model = PolyNet(num_classes=num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        model.mean = settings['mean']
        model.std = settings['std']
    else:
        model = PolyNet(num_classes=num_classes)
    return model

def
polynet
(
num_classes
=
1000
,
pretrained
=
'imagenet'
)
:
if
pretrained
:
settings
=
pretrained_settings
[
'polynet'
]
[
pretrained
]
assert
num_classes
==
settings
[
'num_classes'
]
,
'num_classes should be {}, but is {}'
.
format
(
settings
[
'num_classes'
]
,
num_classes
)
model
=
PolyNet
(
num_classes
=
num_classes
)
model
.
load_state_dict
(
model_zoo
.
load_url
(
settings
[
'url'
]
)
)
model
.
input_space
=
settings
[
'input_space'
]
model
.
input_size
=
settings
[
'input_size'
]
model
.
input_range
=
settings
[
'input_range'
]
model
.
mean
=
settings
[
'mean'
]
model
.
std
=
settings
[
'std'
]
else
:
model
=
PolyNet
(
num_classes
=
num_classes
)
return
model


def adaptive_avgmax_pool2d(x, pool_type='avg', padding=0, count_include_pad=False):
    """Selectable global pooling function with dynamic input kernel size
    """
    if pool_type == 'avgmaxc':
        x = torch.cat([
            F.avg_pool2d(
                x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad),
            F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
        ], dim=1)
    elif pool_type == 'avgmax':
        x_avg = F.avg_pool2d(
                x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad)
        x_max = F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
        x = 0.5 * (x_avg + x_max)
    elif pool_type == 'max':
        x = F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
    else:
        if pool_type != 'avg':
            print('Invalid pool type %s specified. Defaulting to average pooling.' % pool_type)
        x = F.avg_pool2d(
            x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad)
    return x


def resnet18(pretrained=False, **kwargs):
    """Constructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
    return model

def resnet18 ( pretrained = False , * * kwargs ) : model = ResNet ( BasicBlock , [ 2 , 2 , 2 , 2 ] , * * kwargs ) if pretrained : model . load_state_dict ( model_zoo . load_url ( model_urls [ 'resnet18' ] ) ) return model


def resnet34(pretrained=False, **kwargs):
    """Constructs a ResNet-34 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
    return model

def resnet34 ( pretrained = False , * * kwargs ) : model = ResNet ( BasicBlock , [ 3 , 4 , 6 , 3 ] , * * kwargs ) if pretrained : model . load_state_dict ( model_zoo . load_url ( model_urls [ 'resnet34' ] ) ) return model


def resnet50(pretrained=False, **kwargs):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model

def resnet50 ( pretrained = False , * * kwargs ) : model = ResNet ( Bottleneck , [ 3 , 4 , 6 , 3 ] , * * kwargs ) if pretrained : model . load_state_dict ( model_zoo . load_url ( model_urls [ 'resnet50' ] ) ) return model


def resnet101(pretrained=False, **kwargs):
    """Constructs a ResNet-101 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet101']))
    return model

def resnet101 ( pretrained = False , * * kwargs ) : model = ResNet ( Bottleneck , [ 3 , 4 , 23 , 3 ] , * * kwargs ) if pretrained : model . load_state_dict ( model_zoo . load_url ( model_urls [ 'resnet101' ] ) ) return model


def resnet152(pretrained=False, **kwargs):
    """Constructs a ResNet-152 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
    return model

def resnet152 ( pretrained = False , * * kwargs ) : model = ResNet ( Bottleneck , [ 3 , 8 , 36 , 3 ] , * * kwargs ) if pretrained : model . load_state_dict ( model_zoo . load_url ( model_urls [ 'resnet152' ] ) ) return model


def download_url(url, destination=None, progress_bar=True):
    """Download a URL to a local file.

    Parameters
    ----------
    url : str
        The URL to download.
    destination : str, None
        The destination of the file. If None is given the file is saved to a temporary directory.
    progress_bar : bool
        Whether to show a command-line progress bar while downloading.

    Returns
    -------
    filename : str
        The location of the downloaded file.

    Notes
    -----
    Progress bar use/example adapted from tqdm documentation: https://github.com/tqdm/tqdm
    """

    def my_hook(t):
        last_b = [0]

        def inner(b=1, bsize=1, tsize=None):
            if tsize is not None:
                t.total = tsize
            if b > 0:
                t.update((b - last_b[0]) * bsize)
            last_b[0] = b

        return inner

    if progress_bar:
        with tqdm(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t:
            filename, _ = urlretrieve(url, filename=destination, reporthook=my_hook(t))
    else:
        filename, _ = urlretrieve(url, filename=destination)

def download_url ( url , destination = None , progress_bar = True ) : def my_hook ( t ) : last_b = [ 0 ] def inner ( b = 1 , bsize = 1 , tsize = None ) : if tsize is not None : t . total = tsize if b > 0 : t . update ( ( b - last_b [ 0 ] ) * bsize ) last_b [ 0 ] = b return inner if progress_bar : with tqdm ( unit = 'B' , unit_scale = True , miniters = 1 , desc = url . split ( '/' ) [ - 1 ] ) as t : filename , _ = urlretrieve ( url , filename = destination , reporthook = my_hook ( t ) ) else : filename , _ = urlretrieve ( url , filename = destination )


def reset(self):
        """Resets the meter with empty member variables"""
        self.scores = torch.FloatTensor(torch.FloatStorage())
        self.targets = torch.LongTensor(torch.LongStorage())

def reset ( self ) : self . scores = torch . FloatTensor ( torch . FloatStorage ( ) ) self . targets = torch . LongTensor ( torch . LongStorage ( ) )


Args : output ( Tensor ) : NxK tensor that for each of the N examples indicates the probability of the example belonging to each of the K classes according to the model . The probabilities should sum to one over all classes target ( Tensor ) : binary NxK tensort that encodes which of the K classes are associated with the N - th input ( eg : a row [ 0 1 0 1 ] indicates that the example is associated with classes 2 and 4 ) weight ( optional Tensor ) : Nx1 tensor representing the weight for each example ( each weight > 0 )

def add(self, output, target):
        """
        Args:
            output (Tensor): NxK tensor that for each of the N examples
                indicates the probability of the example belonging to each of
                the K classes, according to the model. The probabilities should
                sum to one over all classes
            target (Tensor): binary NxK tensort that encodes which of the K
                classes are associated with the N-th input
                    (eg: a row [0, 1, 0, 1] indicates that the example is
                         associated with classes 2 and 4)
            weight (optional, Tensor): Nx1 tensor representing the weight for
                each example (each weight > 0)
        """
        if not torch.is_tensor(output):
            output = torch.from_numpy(output)
        if not torch.is_tensor(target):
            target = torch.from_numpy(target)

        if output.dim() == 1:
            output = output.view(-1, 1)
        else:
            assert output.dim() == 2, \
                'wrong output size (should be 1D or 2D with one column \
                per class)'
        if target.dim() == 1:
            target = target.view(-1, 1)
        else:
            assert target.dim() == 2, \
                'wrong target size (should be 1D or 2D with one column \
                per class)'
        if self.scores.numel() > 0:
            assert target.size(1) == self.targets.size(1), \
                'dimensions for output should match previously added examples.'

        # make sure storage is of sufficient size
        if self.scores.storage().size() < self.scores.numel() + output.numel():
            new_size = math.ceil(self.scores.storage().size() * 1.5)
            self.scores.storage().resize_(int(new_size + output.numel()))
            self.targets.storage().resize_(int(new_size + output.numel()))

        # store scores and targets
        offset = self.scores.size(0) if self.scores.dim() > 0 else 0
        self.scores.resize_(offset + output.size(0), output.size(1))
        self.targets.resize_(offset + target.size(0), target.size(1))
        self.scores.narrow(0, offset, output.size(0)).copy_(output)
        self.targets.narrow(0, offset, target.size(0)).copy_(target)


Returns the model s average precision for each class Return : ap ( FloatTensor ) : 1xK tensor with avg precision for each class k

def value(self):
        """Returns the model's average precision for each class
        Return:
            ap (FloatTensor): 1xK tensor, with avg precision for each class k
        """

        if self.scores.numel() == 0:
            return 0
        ap = torch.zeros(self.scores.size(1))
        rg = torch.arange(1, self.scores.size(0)).float()

        # compute average precision for each class
        for k in range(self.scores.size(1)):
            # sort scores
            scores = self.scores[:, k]
            targets = self.targets[:, k]

            # compute average precision
            ap[k] = AveragePrecisionMeter.average_precision(scores, targets, self.difficult_examples)
        return ap


def adjust_learning_rate(optimizer, epoch):
    """Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
    lr = args.lr * (0.1 ** (epoch // 30))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

def adjust_learning_rate ( optimizer , epoch ) : lr = args . lr * ( 0.1 ** ( epoch // 30 ) ) for param_group in optimizer . param_groups : param_group [ 'lr' ] = lr


def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res

def accuracy ( output , target , topk = ( 1 , ) ) : maxk = max ( topk ) batch_size = target . size ( 0 ) _ , pred = output . topk ( maxk , 1 , True , True ) pred = pred . t ( ) correct = pred . eq ( target . view ( 1 , - 1 ) . expand_as ( pred ) ) res = [ ] for k in topk : correct_k = correct [ : k ] . view ( - 1 ) . float ( ) . sum ( 0 ) res . append ( correct_k . mul_ ( 100.0 / batch_size ) ) return res