MCPcopy Index your code
hub / github.com/Cadene/pretrained-models.pytorch

github.com/Cadene/pretrained-models.pytorch @main sqlite

repository ↗ · DeepWiki ↗
493 symbols 1,139 edges 35 files 69 documented · 14%
README

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

  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
> * Acc@1 82.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.

Model Version Acc@1 Acc@5
PNASNet-5-Large Tensorflow 82.858 96.182
PNASNet-5-Large Our porting 82.736 95.992
NASNet-A-Large Tensorflow 82.693 96.163
NASNet-A-Large Our porting 82.566 96.086
SENet154 Caffe 81.32 95.53
SENet154 Our porting 81.304 95.498
PolyNet Caffe 81.29 95.75
PolyNet Our porting 81.002 95.624
InceptionResNetV2 Tensorflow 80.4 95.3
InceptionV4 Tensorflow 80.2 95.3
SE-ResNeXt101_32x4d Our porting 80.236 95.028
SE-ResNeXt101_32x4d Caffe 80.19 95.04
InceptionResNetV2 Our porting 80.170 95.234
InceptionV4 Our porting 80.062 94.926
DualPathNet107_5k Our porting 79.746 94.684
ResNeXt101_64x4d Torch7 79.6 94.7
DualPathNet131 Our porting 79.432 94.574

Core symbols most depended-on inside this repo

load_pretrained
called by 21
pretrainedmodels/models/torchvision_models.py
features
called by 13
pretrainedmodels/models/senet.py
update
called by 10
examples/imagenet_eval.py
logits
called by 9
pretrainedmodels/models/dpn.py
modify_vggs
called by 8
pretrainedmodels/models/torchvision_models.py
initialize_pretrained_model
called by 6
pretrainedmodels/models/senet.py
modify_resnets
called by 5
pretrainedmodels/models/torchvision_models.py
inceptionv4
called by 4
pretrainedmodels/models/inceptionv4.py

Shape

Method 268
Class 120
Function 105

Languages

Python100%

Modules by API surface

pretrainedmodels/models/polynet.py51 symbols
pretrainedmodels/models/nasnet_mobile.py42 symbols
pretrainedmodels/models/nasnet.py42 symbols
pretrainedmodels/models/inceptionv4.py32 symbols
pretrainedmodels/models/torchvision_models.py31 symbols
pretrainedmodels/models/pnasnet.py29 symbols
pretrainedmodels/models/dpn.py29 symbols
pretrainedmodels/models/inceptionresnetv2.py27 symbols
pretrainedmodels/models/senet.py24 symbols
pretrainedmodels/utils.py18 symbols
pretrainedmodels/models/fbresnet.py18 symbols
pretrainedmodels/models/fbresnet/resnet152_load.py16 symbols

For agents

$ claude mcp add pretrained-models.pytorch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact