Coder Social home page Coder Social logo

cadene / pretrained-models.pytorch Goto Github PK

View Code? Open in Web Editor NEW
9.0K 217.0 1.8K 532 KB

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

License: BSD 3-Clause "New" or "Revised" License

Lua 0.32% Python 99.68%
imagenet resnet resnext pretrained pytorch inception

pretrained-models.pytorch's Introduction

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
DualPathNet92_5k Our porting 79.400 94.620
DualPathNet98 Our porting 79.224 94.488
SE-ResNeXt50_32x4d Our porting 79.076 94.434
SE-ResNeXt50_32x4d Caffe 79.03 94.46
Xception Keras 79.000 94.500
ResNeXt101_64x4d Our porting 78.956 94.252
Xception Our porting 78.888 94.292
ResNeXt101_32x4d Torch7 78.8 94.4
SE-ResNet152 Caffe 78.66 94.46
SE-ResNet152 Our porting 78.658 94.374
ResNet152 Pytorch 78.428 94.110
SE-ResNet101 Our porting 78.396 94.258
SE-ResNet101 Caffe 78.25 94.28
ResNeXt101_32x4d Our porting 78.188 93.886
FBResNet152 Torch7 77.84 93.84
SE-ResNet50 Caffe 77.63 93.64
SE-ResNet50 Our porting 77.636 93.752
DenseNet161 Pytorch 77.560 93.798
ResNet101 Pytorch 77.438 93.672
FBResNet152 Our porting 77.386 93.594
InceptionV3 Pytorch 77.294 93.454
DenseNet201 Pytorch 77.152 93.548
DualPathNet68b_5k Our porting 77.034 93.590
CaffeResnet101 Caffe 76.400 92.900
CaffeResnet101 Our porting 76.200 92.766
DenseNet169 Pytorch 76.026 92.992
ResNet50 Pytorch 76.002 92.980
DualPathNet68 Our porting 75.868 92.774
DenseNet121 Pytorch 74.646 92.136
VGG19_BN Pytorch 74.266 92.066
NASNet-A-Mobile Tensorflow 74.0 91.6
NASNet-A-Mobile Our porting 74.080 91.740
ResNet34 Pytorch 73.554 91.456
BNInception Our porting 73.524 91.562
VGG16_BN Pytorch 73.518 91.608
VGG19 Pytorch 72.080 90.822
VGG16 Pytorch 71.636 90.354
VGG13_BN Pytorch 71.508 90.494
VGG11_BN Pytorch 70.452 89.818
ResNet18 Pytorch 70.142 89.274
VGG13 Pytorch 69.662 89.264
VGG11 Pytorch 68.970 88.746
SqueezeNet1_1 Pytorch 58.250 80.800
SqueezeNet1_0 Pytorch 58.108 80.428
Alexnet 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.

pretrained-models.pytorch's People

Contributors

aussetg avatar cadene avatar creafz avatar dnaq avatar ekagra-ranjan avatar gaoyiminggithub avatar jack-lin-ds-ai avatar kamalesh0406 avatar micaelcarvalho avatar pppw avatar stevelaskaridis avatar tfriedel avatar theeluwin avatar tstandley avatar veronikayu avatar vfdev-5 avatar vriesdemichael avatar wandering007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pretrained-models.pytorch's Issues

vggm not usable

Hi,

currently vggm is not an usable architecture even though it is already implemented. Could you please fix it?

Thank you!

Torchvision_models problem

Hi,
When I finetuned resnet model in pretrained-models.pytorch, it gave errors:

File "/data/lixinpeng/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "/data/lixinpeng/pretrained-models.pytorch/pretrainedmodels/models/torchvision_models.py", line 322, in forward
    x = self.logits(x)
  File "/data/lixinpeng/pretrained-models.pytorch/pretrainedmodels/models/torchvision_models.py", line 317, in logits
    x = self.last_linear(x)
  File "/data/lixinpeng/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "/data/lixinpeng/anaconda2/lib/python2.7/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/data/lixinpeng/anaconda2/lib/python2.7/site-packages/torch/nn/functional.py", line 835, in linear
    return torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch at /opt/conda/conda-bld/pytorch_1513363039688/work/torch/lib/THC/generic/THCTensorMathBlas.cu:243

I preprocessd data with the code:

'train': transforms.Compose([
    transforms.RandomGrayscale(p=0.1),
    transforms.RandomHorizontalFlip(),
    transforms.RandomVerticalFlip(),
    transforms.ColorJitter(),
    pretrainedmodels.utils.TransformImage(model)
])

But when I finetuned resnet model in official torchvision.models, it worked well. I am confused with this problem, and have you used resnet in this repo successfully?

NasNet

Sorry to bother you. I am a new one in NasNet. So, I have some questions about the architecture of the NasNet. I want to ask that what is the meaning of the CellStem0 and CellStem1 module? Is there some references to explain it? Looking forward to your reply.

[NASNet-A] Pytorch Separable

Hi,

I've actually looked at Pytorch commit for the new separable convolutions and I was wrong, they do not fuse SepConv and Pointwise they instead apply multiple filters per channel and do not pool across channels.

This means that:

  • Everything I've done is actually incorrect, embarrassing ;)
  • The old code actually works with the master branch and is automatically sped up ! Great !

Fix:

Revert to commit 99deb0e

Modify NASNet

Dear @Cadene,
Thank you for your fantastic repository. I want to modify NASNet (i.e., apply some minor modifications on NASNet by remove Dropout, add sigmoid layer at the end of net and etc.). I have implemented below code:

import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
from nasnet import nasnetalarge

num_classes = 10

# *** Define Modified NASNet Class:
class ModifiedNASNet(nn.Module):
    def __init__(self, num_classes):
        super(ModifiedNASNet, self).__init__()

        original_model = nasnetalarge(num_classes=1000, pretrained='imagenet')

        # Everything except the last classifier:
        self.features = nn.Sequential(*list(original_model.children())[:-4])

        # Set number of features:
        num_feats = 4032

        # Plug our linear layer:
        self.classifier = nn.Sequential(nn.Linear(num_feats, num_classes))

    def forward(self, x):
        f = self.features(x)
        out = F.relu(f, inplace=True)
        out = F.avg_pool2d(out, kernel_size=11, stride=1, padding=0, ceil_mode=False, count_include_pad=True)
        out = out.view(out.size(0), -1) # Flatten
        out = self.classifier(out)
        out = F.sigmoid(out)
        return out

# *** test above class:
model = ModifiedNASNet(num_classes).cuda()
# print(model)
x = Variable(torch.randn(1, 3, 331, 331), volatile=True).cuda()
out = model(x)
print(out)

However, the following error has occurred:
TypeError: forward() missing 1 required positional argument: 'x_stem_0'
Would you please kindly help me to address this problem?

Speed of fine-tuning NASNet

First thanks for your pretrained model, it helps me a lot!

I try to fine-tune NASNet in my own dataset, but it seems to be very slow. It takes 10 second to go over a batch of size 8 in TitanX, almost 15 times slower than InceptionResnetV2. Is it normal?

validation set label

I already got pretrained AlexNet model, and want to reproduce results, but i couldn't get the label of validation set.

Pretrained data for nasnet not available on the servers

'url': 'http://webia.lip6.fr/~cadene/Downloads/pretrained-models.pytorch/nasnetalarge-dc8c1432.pth',

https://github.com/Cadene/pretrained-models.pytorch/blob/master/pretrainedmodels/nasnet.py#L19

@Cadene Thanks a lot for these pretrained models. I was trying to use nasnet in one of my experiments, when I realised that these files (linked above) don't exist on the server. Do I need to fetch these from somewhere else ?

I'm trying to figure this stuff out, so any tiny bit of help would be super. 🍻

default parameters of inception_v4 model conflicts

when I use pretrainedmodels.inceptionv4(), it will throw error:

AssertionError: num_classes should be 1000, but is 1000

I have to specify num_classes to 1000 or pretrained to imagenet+background. can't use the default parameters to construct

Inconsistent behavior of torchvision_models and other models on '.features'

when I use models such as inceptionv4, I can use
model = nn.Sequential(model.features, nn.AvgPool2d(8, count_include_pad=False))
to extract features of images, but when I use resnet152:
model = nn.Sequential(model.features, nn.AvgPool2d(7, stride=1))
it throws errors:

Traceback (most recent call last):
  File "prepro_feats.py", line 109, in <module>
    model.features, nn.AvgPool2d(7, stride=1))
  File "/home/xiading/anaconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 50, in __init__
    self.add_module(str(idx), module)
  File "/home/xiading/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 139, in add_module
    torch.typename(module)))
TypeError: pretrainedmodels.models.torchvision_models.modify_resnets.<locals>.features is not a Module subclass

I hope these models can have a consitent behavior on their methods and attributes.

extracting image features as fastest as possible

Hi,

I have around 1.5 M small size images, and I need to extract their feature using nasnet. I use the following code.

import torch
import pretrainedmodels
import pretrainedmodels.utils as utils
import codecs
import sys

model_name = 'nasnetalarge'
model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')
model.eval()


load_img = utils.LoadImage()
tf_img = utils.TransformImage(model)

tf_img = utils.TransformImage(model)

image_names = codecs.open("data/image_ids.train", encoding="utf-8").readlines()
im_dir = "/home/hasan/Documents/Amazon/data/Amazon/data"

for a in range(len(image_names)):
        path_img = (im_dir + "/" + image_names[a].replace("\n", "") + ".jpg")
        input_img = load_img(path_img)
        input_tensor = tf_img(input_img)
        input_tensor = input_tensor.unsqueeze(0)
        input = torch.autograd.Variable(input_tensor, requires_grad=False)
        output_logits = model(input)
        output_features = model.features(input)
        output_logits = model.logits(output_features)
        print output_logits

Is there any way to make it fast? How can I make it to work with GPU? I checked that my pytorch uses GPU successfully with the following commands:

In [1]: import torch

In [2]: torch.cuda.current_device()
Out[2]: 0

In [3]: torch.cuda.device(0)
Out[3]: <torch.cuda.device at 0x7f2132913c50>

In [4]: torch.cuda.device_count()
Out[4]: 1

In [5]: torch.cuda.get_device_name(0)
Out[5]: 'GeForce GTX 1080

But when I run my code, I check nvidia-smi, and I see that my GPU is not allocated by my script.

How I can solve this problem?

Thanks,

sth wrong with senet

the line 193 shoule be change to width = int(math.floor(planes * base_width / 64) * groups)

toy-example python2.7 compatibility

Thank you for your great work! 🎉

My environment was set based on Python 2.7, I met two problems while directly running toy-example.py

The first is:

Traceback (most recent call last):
  File "test/toy-example.py", line 9, in <module>
    import pretrainedmodels
  File "../pretrained-models.pytorch/pretrainedmodels/__init__.py", line 6, in <module>
    from .torchvision import *
  File "../pretrained-models.pytorch/pretrainedmodels/torchvision.py", line 1, in <module>
    import torchvision.models as models
ImportError: No module named models

I think this problem is caused by the name conflicts between the official torchvison and your pretrainedmodels/torchvision.py
so I simply rename pretrainedmodels/torchvision.py to pretrainedmodels/torchvision2.py, and modify the line from .torchvision2 import * in pretrainedmodels/__init__.py at the same time.
It worked.

Then, another problem:

Traceback (most recent call last):
  File "test/toy-example.py", line 35, in <module>
    transforms.Scale(round(max(model.input_size)*1.143)),
  File "/home/user/virtualenv/ENV/local/lib/python2.7/site-packages/torchvision/transforms.py", line 174, in __init__
    assert isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)
AssertionError

The argument passed to transformer round(max(model.input_size)*1.143) is not always an integer according to the python 2 documentation
By changing that line to

transforms.Scale(int(round(max(model.input_size)*1.143))),

finally got the example running.

So, if you'd like, the code could improve its compatibility with Python 2.7.

Thank you so much! 😃

resnext speed

Hey,
I tried resnext-101(32x4d) for 160x160 crops and also the torchvision resnet-101. For me resnext needs nearly double the time to train compared to the resnet. (Only thing I changed is the final GlobalAveragePooling and classifier layer)
Is this expected behavior?
Here it says the required FLOPs are the same, but obviously frameworks like Pytorch may be slower because of a more complicated network structure)

License is missing

Hi Remi!

Awesome work, would be great if you could clarify the license.

Is the maxpool and avgpool interchanged here?

self.comb_iter_1_left = nn.AvgPool2d(3, stride=2, padding=1)
self.comb_iter_1_right = TwoSeparablesReduction(96, 42, 7, 2, 3, bias=False)
self.comb_iter_2_left = nn.MaxPool2d(3, stride=2, padding=1)
self.comb_iter_2_right = TwoSeparablesReduction(96, 42, 5, 2, 2, bias=False)

I compared the tensorflow implementation, I think your implementation in ReductionCell0 is correct and the CellStem0 should stay the same as ReductionCell0.

how did you train nasnet_large?

 Notes for training large NASNet model on ImageNet<br>
 batch size (per replica): 16
 learning rate: 0.015 * 100
 learning rate decay factor: 0.97
 num epochs per decay: 2.4
 sync sgd with 100 replicas
 auxiliary head loss weighting: 0.4
 label smoothing: 0.1
 clip global norm of all gradients by 10

I found this in nasnet.py in Tensorflow, but it need 100GPUs, I am going to use batch parallel, and by the way, it said it has a # auxiliary head loss weighting: 0.4, and in TF'code, it write like this

  #############################
      # Specify the loss function #
  #############################
  if 'AuxLogits' in end_points:
      slim.losses.softmax_cross_entropy(
            end_points['AuxLogits'], labels,
            label_smoothing=FLAGS.label_smoothing, weights=0.4,
            scope='aux_loss')
      slim.losses.softmax_cross_entropy(
          logits, labels, label_smoothing=FLAGS.label_smoothing, weights=1.0)
      return end_points

how did you deal with this AuxLogits? use 100 GPUs like google? I know batch parelle which can simulate large batch size, would it affect accuracy? I change your code to MXNET, but can only arrive 8 in K80,
@Cadene

Upgrade `resnext101_*` to unified api

As for now model-returning functions have no num_classes param and model classes being returned don't have fields that are expected to be in the circumstances of unified behavior (input_size, mean, std, etc.)

Location of pretrained weights

Hi Cadene,

Where can I download the pretrained weights?
The *.pth files you mention in the code?
I am looking for the inception-resnet-v2 model.

Thanks in advance,

'HTTP Error 403: Forbidden' when downloading pretrained models

Hi Cadene,

When I use your code as the example,

model_name = 'bninception'
model = pretrainedmodels.__dict__[model_name](1000)

got an error: urllib2.HTTPError: HTTP Error 403: Forbidden.
If I open the url in browser, the message was
You don't have permission to access /~cadene/Downloads/pretrained-models.pytorch/bn_inception-9f5701afb96c8044.pth on this server..

So, is there something wrong with the server or the urls?

Transform own model tutorial

Hi Cadene

Thanks for your great work.

Is there a way to transform my own tf-trained model to pytorch model ? Is there a tutorial for this ? Or which part of your code I should follow ?

Best
Joe

Unable to NASNetALarge with toy example

I am able to run python test/toy-example.py -a fbresnet152 but when I try python test/toy-example.py -a NASNetALarge I got this error:

pretrained-models.pytorch$ python test/toy-example.py -a NASNetALarge
Traceback (most recent call last):
  File "test/toy-example.py", line 24, in <module>
    model = pretrainedmodels.__dict__[args.arch](num_classes=1000, pretrained='imagenet')
TypeError: __init__() got an unexpected keyword argument 'pretrained'

Changing batch sizes

Is it possible to change the batch size to allow for a larger amount of input at once instead of doing it sort of on-line? I used the code to perform a forward pass which works perfectly but would like to extend the amount of images it can classify in one go.

Also: how exactly can i extract feature information per layer given an input? not extremely important but it would be interesting.

Thanks for making it easy to get pretrained models btw!

Number of parameters?

Has anyone compared number of parameters?
I tried some good accuracy models, but they are very slow.

In terms of both accuracy and speed, which model is the best one to choose?

BatchNorm momentum set to 0 in Inception models

Both Inception models initialize BatchNorm with self.bn = nn.BatchNorm2d(out_planes, eps=0.001, momentum=0, affine=True)

Setting momentum to 0 prevents the running mean and var from being updated during training. When training one of these models on a different dataset, this causes a significant discrepancy between train and eval modes.

NASNet

Dear @Cadene,
Thank your for your nice repository. Recently the Google Brain Team released a fantastic CNN model, NASNet, in TF-slim, which achieved the state-of-the-art Top-1 Accuracy on ImageNet by 82.7 %. Would you please kindly port this model into your nice repository (i.e., PyTorch Pre-Trained Models)?

Xception has differing dimensions than the pretrained model

Hey, when I try to load the pretrained xception model with imagenet,
pretrained_model = xception(pretrained='imagenet')
I encounter this bug:

RuntimeError: While copying the parameter named block1.rep.0.conv1.weight, whose dimensions in the model are torch.Size([64, 1, 3, 3]) and whose dimensions in the checkpoint are torch.Size([64, 3, 3]).

It's a bit weird, any idea how to fix it?

BNInception Training Problem

Hi,

I tried training bninception on my own dataset but it is not working. May I know the problem.

Epoch 0/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:22<00:00, 2.94it/s]
train Loss: 0.0383 Acc: 0.1293
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.47it/s]
val Loss: 2.5915 Acc: 0.1079

Epoch 1/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:17<00:00, 3.84it/s]
train Loss: 0.0382 Acc: 0.1364
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.73it/s]
val Loss: 0.0425 Acc: 0.0809

Epoch 2/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:17<00:00, 3.80it/s]
train Loss: 0.0382 Acc: 0.1286
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.35it/s]
val Loss: 70.5504 Acc: 0.0560

Epoch 3/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:17<00:00, 3.74it/s]
train Loss: 0.0382 Acc: 0.1214
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.53it/s]
val Loss: 228.3415 Acc: 0.0560

Epoch 4/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:17<00:00, 3.80it/s]
train Loss: 0.0382 Acc: 0.1408
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.46it/s]
val Loss: 12274.5517 Acc: 0.1079

Epoch 5/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:18<00:00, 3.68it/s]
train Loss: 0.0383 Acc: 0.1202
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.00it/s]
val Loss: 74337.8683 Acc: 0.0560

Epoch 6/99

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 67/67 [00:17<00:00, 3.83it/s]
train Loss: 0.0382 Acc: 0.1228
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:02<00:00, 3.00it/s]
val Loss: 152427.3008 Acc: 0.0560

I have basically used my own dataloaders and training function. I trained nasnet and other networks available here and they are working fine, only bninception is not working.

used batch_size=64, converted to BGR mode and Range255, also used mean and std as provided in the bninception.py file.

My implementation is quite lengthy, so not producing here.

Toy example incorrect answers

With toy example fixing with (num_classes=1000, pretrained='imagenet') the classes being returned are very strange:

  • inceptionresnetv2
data/cat.jpg is a n03903868 pedestal, plinth, footstall
data/lena.jpg is a n03903868 pedestal, plinth, footstall
  • inceptionv4
data/cat.jpg is a b'n01677366 common iguana, iguana, Iguana iguana'
data/lena.jpg is a b'n01677366 common iguana, iguana, Iguana iguana'
  • fbresnet152
data/cat.jpg is a n01677366 common iguana, iguana, Iguana iguana
data/lena.jpg is a n01768244 trilobite

Synset neighbours of the returned classes don't fit either so it's not some form of +- 1 indexing problem.

NasNet execution time

I was trying to adapt your model definition to include the CIFAR-10 and mobile version of the Nasnet network, ended up with the network here: https://github.com/gngdb/pytorch-cifar/blob/master/models/nasnet.py

I've checked the output is the same as the original version of your code, so I'm pretty sure the large version is correct. The only problem is I just tried to train the CIFAR-10 network and it's extremely slow. Have you noticed the execution time per image with the large version in this repository is slow? When I run it I'm getting about 4.5 seconds per image.

InceptionV3 normalisation

Hi-great work with putting all the models together.

Model mean and std given at https://github.com/Cadene/pretrained-models.pytorch#modelmean for inceptionv3 are different from what is provided with pytorchVison https://github.com/pytorch/vision#models. Where they say "The images have to be loaded in to a range of [0, 1] and then normalized using mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]" for all the models including inception v3.

Am I missing something?

BatchNorm momentum in BNInception

Hi,

I accidentally spotted that momentum value for BatchNorm2d in BNInception is set to 0.9 which seems to be quite extreme for batch normalization given that the default value is 0.1
Isn't it a typo?

Thx

Fail to load bninception

Hello, when I try to load the bninception model with the following codes, something weird happens. I need some help, thanks.

model_name = 'nasnetalarge' # could be fbresnet152 or inceptionresnetv2
model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')

running the above code produces this:

anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py:514: UserWarning: src is not broadcastable to dst, but they have the same number of elements.  Falling back to deprecated pointwise behavior.
  own_state[name].copy_(param)

Toy example fails

Subj with AssertionError: num_classes should be 1000, but is 1001, because imagenet and 1001 classes are actually incosistent in the example.

BTW default params for inceptionresnetv2 reprodeced in the toy example are fail-generating either.

NASNet Bug

hello

I've read the tensorflow version of nasnet and compared with your implementation.
I guess

x_cell_6 = self.cell_6(x_reduction_cell_0, x_cell_4)

should be
x_cell_6 = self.cell_6(x_reduction_cell_0, x_cell_5)

and

x_cell_12 = self.cell_12(x_reduction_cell_1, x_cell_10)

should be
x_cell_12 = self.cell_12(x_reduction_cell_1, x_cell_11)

isn't it?

Error when loading pre-trained ResNeXt weights

After invoking ResNeXt from here like this

resnext = resnext101_32x4d(num_classes=1000, pretrained='imagenet')

I get this error


KeyError Traceback (most recent call last)
in ()
----> 1 resnext = resnext101_32x4d(num_classes=1000, pretrained='imagenet')

in resnext101_32x4d(num_classes, pretrained)
81 settings = pretrained_settings['resnext101_32x4d'][pretrained]
82 assert num_classes == settings['num_classes'], "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
---> 83 model.load_state_dict(model_zoo.load_url(settings['url']))
84 model.input_space = settings['input_space']
85 model.input_size = settings['input_size']

/opt/conda/lib/python3.5/site-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict)
353 if name not in own_state:
354 raise KeyError('unexpected key "{}" in state_dict'
--> 355 .format(name))
356 if isinstance(param, Parameter):
357 # backwards compatibility for serialized parameters

KeyError: 'unexpected key "features.0.weight" in state_dict'

imagenet + background?

Hi,
Could you elaborate on how this type of model is trained? Where do you get the background images?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.