pretrainedmodels: Pretrained models for Pytorch · Pythie
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
12/01/2018: python setup.py install
08/12/2017: update data url (/!\ git pull is needed)
30/11/2017: improve API (model.features(input), model.logits(features), model.forward(input), model.last_linear)
16/11/2017: nasnet-a-large pretrained model ported by T. Durand and R. Cadene
22/07/2017: torchvision pretrained models
22/07/2017: momentum in inceptionv4 and inceptionresnetv2 to 0.1
17/07/2017: model.input_range attribut
17/07/2017: BNInception pretrained on Imagenet
Summary
Installation
Install from pip
pip install pretrainedmodels
Install from repo
git clone https://github.com/Cadene/pretrained-models.pytorch.git
cd pretrained-models.pytorch
python setup.py install
Quick examples
To 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_MODEL_ZOO variable as follow: export TORCH_MODEL_ZOO="/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.png
> 'nasnetalarge': data/cat.png' is a 'tiger cat'
Compute imagenet evaluation metrics $ python examples/imagenet_eval.py /local/common-data/imagenet_2012/images -a nasnetalarge -b 20 -e
> * Acc@1 92.693, Acc@5 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.
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
Documentation
Available models
NASNet*
nasnetalarge(num_classes=1000, pretrained='imagenet')
nasnetalarge(num_classes=1001, pretrained='imagenet+background')
nasnetamobile(num_classes=1000, pretrained='imagenet')
FaceBook ResNet* 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*
cafferesnet101(num_classes=1000, pretrained='imagenet')
Inception*
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
bninception(num_classes=1000, pretrained='imagenet')
ResNeXt*
resnext101_32x4d(num_classes=1000, pretrained='imagenet')
resnext101_62x4d(num_classes=1000, pretrained='imagenet')
DualPathNetworks 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
xception(num_classes=1000, pretrained='imagenet')
SENet*
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*
pnasnet5large(num_classes=1000, pretrained='imagenet')
pnasnet5large(num_classes=1001, pretrained='imagenet+background')
PolyNet
polynet(num_classes=1000, pretrained='imagenet')
TorchVision
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_sizeAttribut of type list composed of 3 numbers:
number of color channels,
height of the input image,
width of the input image.
[3, 299, 299] for inception* networks,
[3, 224, 224] for resnet* networks.
model.input_spaceAttribut of type str representating the color space of the image. Can be RGB or BGR.
model.input_rangeAttribut of type list composed of 2 numbers:
min pixel value,
max pixel value.
[0, 1] for resnet* and inception* networks,
[0, 255] for bninception network.
model.meanAttribut of type list composed of 3 numbers which are used to normalize the input image (substract "color-channel-wise").
[0.5, 0.5, 0.5] for inception* networks,
[0.485, 0.456, 0.406] for resnet* networks.
model.stdAttribut of type list composed of 3 numbers which are used to normalize the input image (divide "color-channel-wise").
[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.forwardMethod 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_linearAttribut 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
Hand porting of NASNet, InceptionV4 and InceptionResNetV2
Acknowledgement Thanks to the deep learning community and especially to the contributers of the pytorch ecosystem.
Popularity
Downloads, last 7 days 24.4K+4%
Stars, last 7 days –
Direct dependents 31
All dependents 79 17
Low activity
Maintenance, out of 100
Recent release
last release 2952 days ago
Commit activity
0/26 weeks with commits
Release cadence
0 releases in 12 months
Issues & PRs handled
57% closed in the past year
Contributor spread
18 authors in last 100 commits
Releases
Latest 0.7.4
Released Oct 29, 2018
Releases, 12 months 0
First release Jan 13, 2018 Compatibility
Requires Python not specified
Tested on 3.6
License MIT (Permissive) Works well withUse together with pretrainedmodels: extensions, drivers, servers and companion tools.
torch Pythie’s Index: 89Tensors and Dynamic neural networks in Python with strong GPU acceleration 59.1M downloads/mo