Github url

pretrained-models.pytorch

by Cadene

Pretrained ConvNets for pytorch: NASNet, ResNeXt, ResNet, InceptionV4, InceptionResnetV2, Xception, ...

6.9K Stars 1.5K Forks Last release: Not found BSD 3-Clause "New" or "Revised" License 154 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Pretrained models for Pytorch (Work in progress)

The goal of this repo is:

  • to help to reproduce research papers results (transfer learning setups for instance),
  • to access pretrained ConvNets with a unique interface/API inspired by torchvision.

News: - 27/10/2018: Fix compatibility issues, Add tests, Add travis - 04/06/2018: PolyNet and PNASNet-5-Large thanks to Alex Parinov- 16/04/2018: SE-ResNet* and SE-ResNeXt* thanks to Alex Parinov- 09/04/2018: SENet154 thanks to Alex Parinov- 22/03/2018: CaffeResNet101 (good for localization with FasterRCNN) - 21/03/2018: NASNet Mobile thanks to Veronika Yurchuk and Anastasiia- 25/01/2018: DualPathNetworks thanks to Ross Wightman, Xception thanks to T Standley, improved TransformImage API - 13/01/2018:

pip install pretrainedmodels

,

pretrainedmodels.model\_names

,

pretrainedmodels.pretrained\_settings

Installation

  1. python3 with anaconda
  2. pytorch with/out CUDA

Install from pip

  1. pip install pretrainedmodels

Install from repo

  1. git clone https://github.com/Cadene/pretrained-models.pytorch.git
  2. cd pretrained-models.pytorch
  3. python setup.py install

Quick examples

  • To import
    pretrainedmodels
    :
import pretrainedmodels
  • To print the available pretrained models:
print(pretrainedmodels.model\_names) \> ['fbresnet152', 'bninception', 'resnext101\_32x4d', 'resnext101\_64x4d', 'inceptionv4', 'inceptionresnetv2', 'alexnet', 'densenet121', 'densenet169', 'densenet201', 'densenet161', 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'inceptionv3', 'squeezenet1\_0', 'squeezenet1\_1', 'vgg11', 'vgg11\_bn', 'vgg13', 'vgg13\_bn', 'vgg16', 'vgg16\_bn', 'vgg19\_bn', 'vgg19', 'nasnetalarge', 'nasnetamobile', 'cafferesnet101', 'senet154', 'se\_resnet50', 'se\_resnet101', 'se\_resnet152', 'se\_resnext50\_32x4d', 'se\_resnext101\_32x4d', 'cafferesnet101', 'polynet', 'pnasnet5large']
  • To print the available pretrained settings for a chosen model:
print(pretrainedmodels.pretrained\_settings['nasnetalarge']) \> {'imagenet': {'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', 'input\_space': 'RGB', 'input\_size': [3, 331, 331], 'input\_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num\_classes': 1000}, 'imagenet+background': {'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', 'input\_space': 'RGB', 'input\_size': [3, 331, 331], 'input\_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num\_classes': 1001}}
  • To load a pretrained models from imagenet:
model\_name = 'nasnetalarge' # could be fbresnet152 or inceptionresnetv2 model = pretrainedmodels.\_\_dict\_\_[model\_name](num\_classes=1000, pretrained='imagenet') model.eval()

Note: By default, models will be downloaded to your

$HOME/.torch

folder. You can modify this behavior using the

$TORCH\_HOME

variable as follow:

export TORCH\_HOME="/local/pretrainedmodels"
  • To load an image and do a complete forward pass:
import torch import pretrainedmodels.utils as utils load\_img = utils.LoadImage() # transformations depending on the model # rescale, center crop, normalize, and others (ex: ToBGR, ToRange255) tf\_img = utils.TransformImage(model) path\_img = 'data/cat.jpg' input\_img = load\_img(path\_img) input\_tensor = tf\_img(input\_img) # 3x400x225 -\> 3x299x299 size may differ input\_tensor = input\_tensor.unsqueeze(0) # 3x299x299 -\> 1x3x299x299 input = torch.autograd.Variable(input\_tensor, requires\_grad=False) output\_logits = model(input) # 1x1000
  • To extract features (beware this API is not available for all networks):
output\_features = model.features(input) # 1x14x14x2048 size may differ output\_logits = model.logits(output\_features) # 1x1000

Few use cases

Compute imagenet logits

$ python examples/imagenet\_logits.py -h \> nasnetalarge, resnet152, inceptionresnetv2, inceptionv4, ...
$ python examples/imagenet\_logits.py -a nasnetalarge --path\_img data/cat.jpg \> 'nasnetalarge': data/cat.jpg' is a 'tiger cat'

Compute imagenet evaluation metrics

$ python examples/imagenet\_eval.py /local/common-data/imagenet\_2012/images -a nasnetalarge -b 20 -e \> \* [email protected] 82.693, [email protected] 96.13

Evaluation on imagenet

Accuracy on validation set (single model)

Results were obtained using (center cropped) images of the same size than during the training process.

Model | Version | [email protected] | [email protected] --- | --- | --- | --- PNASNet-5-Large | Tensorflow | 82.858 | 96.182PNASNet-5-Large | Our porting | 82.736 | 95.992 NASNet-A-Large | Tensorflow | 82.693 | 96.163NASNet-A-Large | Our porting | 82.566 | 96.086 SENet154 | Caffe | 81.32 | 95.53SENet154 | Our porting | 81.304 | 95.498 PolyNet | Caffe | 81.29 | 95.75PolyNet | Our porting | 81.002 | 95.624 InceptionResNetV2 | Tensorflow | 80.4 | 95.3 InceptionV4 | Tensorflow | 80.2 | 95.3SE-ResNeXt101_32x4d | Our porting | 80.236 | 95.028 SE-ResNeXt101_32x4d | Caffe | 80.19 | 95.04InceptionResNetV2 | Our porting | 80.170 | 95.234InceptionV4 | Our porting | 80.062 | 94.926DualPathNet107_5k | Our porting | 79.746 | 94.684 ResNeXt101_64x4d | Torch7 | 79.6 | 94.7DualPathNet131 | Our porting | 79.432 | 94.574DualPathNet92_5k | Our porting | 79.400 | 94.620DualPathNet98 | Our porting | 79.224 | 94.488SE-ResNeXt50_32x4d | Our porting | 79.076 | 94.434 SE-ResNeXt50_32x4d | Caffe | 79.03 | 94.46Xception | Keras | 79.000 | 94.500ResNeXt101_64x4d | Our porting | 78.956 | 94.252Xception | Our porting | 78.888 | 94.292 ResNeXt101_32x4d | Torch7 | 78.8 | 94.4 SE-ResNet152 | Caffe | 78.66 | 94.46SE-ResNet152 | Our porting | 78.658 | 94.374 ResNet152 | Pytorch | 78.428 | 94.110SE-ResNet101 | Our porting | 78.396 | 94.258 SE-ResNet101 | Caffe | 78.25 | 94.28ResNeXt101_32x4d | Our porting | 78.188 | 93.886 FBResNet152 | Torch7 | 77.84 | 93.84 SE-ResNet50 | Caffe | 77.63 | 93.64SE-ResNet50 | Our porting | 77.636 | 93.752DenseNet161 | Pytorch | 77.560 | 93.798ResNet101 | Pytorch | 77.438 | 93.672FBResNet152 | Our porting | 77.386 | 93.594InceptionV3 | Pytorch | 77.294 | 93.454DenseNet201 | Pytorch | 77.152 | 93.548DualPathNet68b_5k | Our porting | 77.034 | 93.590CaffeResnet101 | Caffe | 76.400 | 92.900CaffeResnet101 | Our porting | 76.200 | 92.766DenseNet169 | Pytorch | 76.026 | 92.992ResNet50 | Pytorch | 76.002 | 92.980DualPathNet68 | Our porting | 75.868 | 92.774DenseNet121 | Pytorch | 74.646 | 92.136VGG19_BN | Pytorch | 74.266 | 92.066 NASNet-A-Mobile | Tensorflow | 74.0 | 91.6NASNet-A-Mobile | Our porting | 74.080 | 91.740ResNet34 | Pytorch | 73.554 | 91.456BNInception | Our porting | 73.524 | 91.562VGG16_BN | Pytorch | 73.518 | 91.608VGG19 | Pytorch | 72.080 | 90.822VGG16 | Pytorch | 71.636 | 90.354VGG13_BN | Pytorch | 71.508 | 90.494VGG11_BN | Pytorch | 70.452 | 89.818ResNet18 | Pytorch | 70.142 | 89.274VGG13 | Pytorch | 69.662 | 89.264VGG11 | Pytorch | 68.970 | 88.746SqueezeNet1_1 | Pytorch | 58.250 | 80.800SqueezeNet1_0 | Pytorch | 58.108 | 80.428Alexnet | Pytorch | 56.432 | 79.194

Notes: - the Pytorch version of ResNet152 is not a porting of the Torch7 but has been retrained by facebook. - For the PolyNet evaluation each image was resized to 378x378 without preserving the aspect ratio and then the central 331×331 patch from the resulting image was used.

Beware, the accuracy reported here is not always representative of the transferable capacity of the network on other tasks and datasets. You must try them all! :P

Reproducing results

Please see Compute imagenet validation metrics

Documentation

Available models

NASNet*

Source: TensorFlow Slim repo

  • nasnetalarge(num\_classes=1000, pretrained='imagenet')
  • nasnetalarge(num\_classes=1001, pretrained='imagenet+background')
  • nasnetamobile(num\_classes=1000, pretrained='imagenet')

FaceBook ResNet*

Source: Torch7 repo of FaceBook

There are a bit different from the ResNet* of torchvision. ResNet152 is currently the only one available.

  • fbresnet152(num\_classes=1000, pretrained='imagenet')

Caffe ResNet*

Source: Caffe repo of KaimingHe

  • cafferesnet101(num\_classes=1000, pretrained='imagenet')

Inception*

Source: TensorFlow Slim repo and Pytorch/Vision repo for

inceptionv3
  • inceptionresnetv2(num\_classes=1000, pretrained='imagenet')
  • inceptionresnetv2(num\_classes=1001, pretrained='imagenet+background')
  • inceptionv4(num\_classes=1000, pretrained='imagenet')
  • inceptionv4(num\_classes=1001, pretrained='imagenet+background')
  • inceptionv3(num\_classes=1000, pretrained='imagenet')

BNInception

Source: Trained with Caffe by Xiong Yuanjun

  • bninception(num\_classes=1000, pretrained='imagenet')

ResNeXt*

Source: ResNeXt repo of FaceBook

  • resnext101\_32x4d(num\_classes=1000, pretrained='imagenet')
  • resnext101\_62x4d(num\_classes=1000, pretrained='imagenet')

DualPathNetworks

Source: MXNET repo of Chen Yunpeng

The porting has been made possible by Ross Wightman in his PyTorch repo.

As you can see here DualPathNetworks allows you to try different scales. The default one in this repo is 0.875 meaning that the original input size is 256 before croping to 224.

  • dpn68(num\_classes=1000, pretrained='imagenet')
  • dpn98(num\_classes=1000, pretrained='imagenet')
  • dpn131(num\_classes=1000, pretrained='imagenet')
  • dpn68b(num\_classes=1000, pretrained='imagenet+5k')
  • dpn92(num\_classes=1000, pretrained='imagenet+5k')
  • dpn107(num\_classes=1000, pretrained='imagenet+5k')
'imagenet+5k'

means that the network has been pretrained on imagenet5k before being finetuned on imagenet1k.

Xception

Source: Keras repo

The porting has been made possible by T Standley.

  • xception(num\_classes=1000, pretrained='imagenet')

SENet*

Source: Caffe repo of Jie Hu

  • senet154(num\_classes=1000, pretrained='imagenet')
  • se\_resnet50(num\_classes=1000, pretrained='imagenet')
  • se\_resnet101(num\_classes=1000, pretrained='imagenet')
  • se\_resnet152(num\_classes=1000, pretrained='imagenet')
  • se\_resnext50\_32x4d(num\_classes=1000, pretrained='imagenet')
  • se\_resnext101\_32x4d(num\_classes=1000, pretrained='imagenet')

PNASNet*

Source: TensorFlow Slim repo

  • pnasnet5large(num\_classes=1000, pretrained='imagenet')
  • pnasnet5large(num\_classes=1001, pretrained='imagenet+background')

PolyNet

Source: Caffe repo of the CUHK Multimedia Lab

  • polynet(num\_classes=1000, pretrained='imagenet')

TorchVision

Source: Pytorch/Vision repo

(

inceptionv3

included in Inception*)

resnet18(num\_classes=1000, pretrained='imagenet')
  • resnet34(num\_classes=1000, pretrained='imagenet')
  • resnet50(num\_classes=1000, pretrained='imagenet')
  • resnet101(num\_classes=1000, pretrained='imagenet')
  • resnet152(num\_classes=1000, pretrained='imagenet')
  • densenet121(num\_classes=1000, pretrained='imagenet')
  • densenet161(num\_classes=1000, pretrained='imagenet')
  • densenet169(num\_classes=1000, pretrained='imagenet')
  • densenet201(num\_classes=1000, pretrained='imagenet')
  • squeezenet1\_0(num\_classes=1000, pretrained='imagenet')
  • squeezenet1\_1(num\_classes=1000, pretrained='imagenet')
  • alexnet(num\_classes=1000, pretrained='imagenet')
  • vgg11(num\_classes=1000, pretrained='imagenet')
  • vgg13(num\_classes=1000, pretrained='imagenet')
  • vgg16(num\_classes=1000, pretrained='imagenet')
  • vgg19(num\_classes=1000, pretrained='imagenet')
  • vgg11\_bn(num\_classes=1000, pretrained='imagenet')
  • vgg13\_bn(num\_classes=1000, pretrained='imagenet')
  • vgg16\_bn(num\_classes=1000, pretrained='imagenet')
  • vgg19\_bn(num\_classes=1000, pretrained='imagenet')

Model API

Once a pretrained model has been loaded, you can use it that way.

Important note: All image must be loaded using

PIL

which scales the pixel values between 0 and 1.

model.input\_size

Attribut of type

list

composed of 3 numbers:

  • number of color channels,
  • height of the input image,
  • width of the input image.

Example:

  • [3, 299, 299]
    for inception* networks,
  • [3, 224, 224]
    for resnet* networks.

model.input\_space

Attribut of type

str

representating the color space of the image. Can be

RGB

or

BGR

.

model.input\_range

Attribut of type

list

composed of 2 numbers:

  • min pixel value,
  • max pixel value.

Example:

  • [0, 1]
    for resnet* and inception* networks,
  • [0, 255]
    for bninception network.

model.mean

Attribut of type

list

composed of 3 numbers which are used to normalize the input image (substract "color-channel-wise").

Example:

  • [0.5, 0.5, 0.5]
    for inception* networks,
  • [0.485, 0.456, 0.406]
    for resnet* networks.

model.std

Attribut of type

list

composed of 3 numbers which are used to normalize the input image (divide "color-channel-wise").

Example:

  • [0.5, 0.5, 0.5]
    for inception* networks,
  • [0.229, 0.224, 0.225]
    for resnet* networks.

model.features

/!\ work in progress (may not be available)

Method which is used to extract the features from the image.

Example when the model is loaded using

fbresnet152

:

print(input\_224.size()) # (1,3,224,224) output = model.features(input\_224) print(output.size()) # (1,2048,1,1) # print(input\_448.size()) # (1,3,448,448) output = model.features(input\_448) # print(output.size()) # (1,2048,7,7)

model.logits

/!\ work in progress (may not be available)

Method which is used to classify the features from the image.

Example when the model is loaded using

fbresnet152

:

output = model.features(input\_224) print(output.size()) # (1,2048, 1, 1) output = model.logits(output) print(output.size()) # (1,1000)

model.forward

Method used to call

model.features

and

model.logits

. It can be overwritten as desired.

Note: A good practice is to use

model.\_\_call\_\_

as your function of choice to forward an input to your model. See the example bellow.

# Without model.\_\_call\_\_ output = model.forward(input\_224) print(output.size()) # (1,1000) # With model.\_\_call\_\_ output = model(input\_224) print(output.size()) # (1,1000)

model.last\_linear

Attribut of type

nn.Linear

. This module is the last one to be called during the forward pass.

  • Can be replaced by an adapted
    nn.Linear
    for fine tuning.
  • Can be replaced by
    pretrained.utils.Identity
    for features extraction.

Example when the model is loaded using

fbresnet152

:

print(input\_224.size()) # (1,3,224,224) output = model.features(input\_224) print(output.size()) # (1,2048,1,1) output = model.logits(output) print(output.size()) # (1,1000) # fine tuning dim\_feats = model.last\_linear.in\_features # =2048 nb\_classes = 4 model.last\_linear = nn.Linear(dim\_feats, nb\_classes) output = model(input\_224) print(output.size()) # (1,4) # features extraction model.last\_linear = pretrained.utils.Identity() output = model(input\_224) print(output.size()) # (1,2048)

Reproducing

Hand porting of ResNet152

th pretrainedmodels/fbresnet/resnet152\_dump.lua python pretrainedmodels/fbresnet/resnet152\_load.py

Automatic porting of ResNeXt

https://github.com/clcarwin/convert_torch_to\_pytorch

Hand porting of NASNet, InceptionV4 and InceptionResNetV2

https://github.com/Cadene/tensorflow-model-zoo.torch

Acknowledgement

Thanks to the deep learning community and especially to the contributers of the pytorch ecosystem.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.