GithubHelp home page GithubHelp logo

lukemelas / efficientnet-pytorch Goto Github PK

View Code? Open in Web Editor NEW
7.8K 130.0 1.5K 1.14 MB

A PyTorch implementation of EfficientNet

License: Apache License 2.0

Python 97.97% Shell 2.03%
efficientnet-pytorch imagenet feature-extraction pretrained-models

efficientnet-pytorch's Introduction

EfficientNet PyTorch

Quickstart

Install with pip install efficientnet_pytorch and load a pretrained EfficientNet with:

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')

Updates

Update (April 2, 2021)

The EfficientNetV2 paper has been released! I am working on implementing it as you read this :)

About EfficientNetV2:

EfficientNetV2 is a new family of convolutional networks that have faster training speed and better parameter efficiency than previous models. To develop this family of models, we use a combination of training-aware neural architecture search and scaling, to jointly optimize training speed and parameter efficiency. The models were searched from the search space enriched with new ops such as Fused-MBConv.

Here is a comparison:

Update (Aug 25, 2020)

This update adds:

  • A new include_top (default: True) option (#208)
  • Continuous testing with sotabench
  • Code quality improvements and fixes (#215 #223)

Update (May 14, 2020)

This update adds comprehensive comments and documentation (thanks to @workingcoder).

Update (January 23, 2020)

This update adds a new category of pre-trained model based on adversarial training, called advprop. It is important to note that the preprocessing required for the advprop pretrained models is slightly different from normal ImageNet preprocessing. As a result, by default, advprop models are not used. To load a model with advprop, use:

model = EfficientNet.from_pretrained("efficientnet-b0", advprop=True)

There is also a new, large efficientnet-b8 pretrained model that is only available in advprop form. When using these models, replace ImageNet preprocessing code as follows:

if advprop:  # for models using advprop pretrained weights
    normalize = transforms.Lambda(lambda img: img * 2.0 - 1.0)
else:
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

This update also addresses multiple other issues (#115, #128).

Update (October 15, 2019)

This update allows you to choose whether to use a memory-efficient Swish activation. The memory-efficient version is chosen by default, but it cannot be used when exporting using PyTorch JIT. For this purpose, we have also included a standard (export-friendly) swish activation function. To switch to the export-friendly version, simply call model.set_swish(memory_efficient=False) after loading your desired model. This update addresses issues #88 and #89.

Update (October 12, 2019)

This update makes the Swish activation function more memory-efficient. It also addresses pull requests #72, #73, #85, and #86. Thanks to the authors of all the pull requests!

Update (July 31, 2019)

Upgrade the pip package with pip install --upgrade efficientnet-pytorch

The B6 and B7 models are now available. Additionally, all pretrained models have been updated to use AutoAugment preprocessing, which translates to better performance across the board. Usage is the same as before:

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b7')

Update (June 29, 2019)

This update adds easy model exporting (#20) and feature extraction (#38).

It is also now incredibly simple to load a pretrained model with a new number of classes for transfer learning:

model = EfficientNet.from_pretrained('efficientnet-b1', num_classes=23)

Update (June 23, 2019)

The B4 and B5 models are now available. Their usage is identical to the other models:

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b4')

Overview

This repository contains an op-for-op PyTorch reimplementation of EfficientNet, along with pre-trained models and examples.

The goal of this implementation is to be simple, highly extensible, and easy to integrate into your own projects. This implementation is a work in progress -- new features are currently being implemented.

At the moment, you can easily:

  • Load pretrained EfficientNet models
  • Use EfficientNet models for classification or feature extraction
  • Evaluate EfficientNet models on ImageNet or your own images

Upcoming features: In the next few days, you will be able to:

  • Train new models from scratch on ImageNet with a simple command
  • Quickly finetune an EfficientNet on your own dataset
  • Export EfficientNet models for production

Table of contents

  1. About EfficientNet
  2. About EfficientNet-PyTorch
  3. Installation
  4. Usage
  5. Contributing

About EfficientNet

If you're new to EfficientNets, here is an explanation straight from the official TensorFlow implementation:

EfficientNets are a family of image classification models, which achieve state-of-the-art accuracy, yet being an order-of-magnitude smaller and faster than previous models. We develop EfficientNets based on AutoML and Compound Scaling. In particular, we first use AutoML Mobile framework to develop a mobile-size baseline network, named as EfficientNet-B0; Then, we use the compound scaling method to scale up this baseline to obtain EfficientNet-B1 to B7.

EfficientNets achieve state-of-the-art accuracy on ImageNet with an order of magnitude better efficiency:

  • In high-accuracy regime, our EfficientNet-B7 achieves state-of-the-art 84.4% top-1 / 97.1% top-5 accuracy on ImageNet with 66M parameters and 37B FLOPS, being 8.4x smaller and 6.1x faster on CPU inference than previous best Gpipe.

  • In middle-accuracy regime, our EfficientNet-B1 is 7.6x smaller and 5.7x faster on CPU inference than ResNet-152, with similar ImageNet accuracy.

  • Compared with the widely used ResNet-50, our EfficientNet-B4 improves the top-1 accuracy from 76.3% of ResNet-50 to 82.6% (+6.3%), under similar FLOPS constraint.

About EfficientNet PyTorch

EfficientNet PyTorch is a PyTorch re-implementation of EfficientNet. It is consistent with the original TensorFlow implementation, such that it is easy to load weights from a TensorFlow checkpoint. At the same time, we aim to make our PyTorch implementation as simple, flexible, and extensible as possible.

If you have any feature requests or questions, feel free to leave them as GitHub issues!

Installation

Install via pip:

pip install efficientnet_pytorch

Or install from source:

git clone https://github.com/lukemelas/EfficientNet-PyTorch
cd EfficientNet-Pytorch
pip install -e .

Usage

Loading pretrained models

Load an EfficientNet:

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_name('efficientnet-b0')

Load a pretrained EfficientNet:

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')

Details about the models are below:

Name # Params Top-1 Acc. Pretrained?
efficientnet-b0 5.3M 76.3
efficientnet-b1 7.8M 78.8
efficientnet-b2 9.2M 79.8
efficientnet-b3 12M 81.1
efficientnet-b4 19M 82.6
efficientnet-b5 30M 83.3
efficientnet-b6 43M 84.0
efficientnet-b7 66M 84.4

Example: Classification

Below is a simple, complete example. It may also be found as a jupyter notebook in examples/simple or as a Colab Notebook.

We assume that in your current directory, there is a img.jpg file and a labels_map.txt file (ImageNet class names). These are both included in examples/simple.

import json
from PIL import Image
import torch
from torchvision import transforms

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')

# Preprocess image
tfms = transforms.Compose([transforms.Resize(224), transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
img = tfms(Image.open('img.jpg')).unsqueeze(0)
print(img.shape) # torch.Size([1, 3, 224, 224])

# Load ImageNet class names
labels_map = json.load(open('labels_map.txt'))
labels_map = [labels_map[str(i)] for i in range(1000)]

# Classify
model.eval()
with torch.no_grad():
    outputs = model(img)

# Print predictions
print('-----')
for idx in torch.topk(outputs, k=5).indices.squeeze(0).tolist():
    prob = torch.softmax(outputs, dim=1)[0, idx].item()
    print('{label:<75} ({p:.2f}%)'.format(label=labels_map[idx], p=prob*100))

Example: Feature Extraction

You can easily extract features with model.extract_features:

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')

# ... image preprocessing as in the classification example ...
print(img.shape) # torch.Size([1, 3, 224, 224])

features = model.extract_features(img)
print(features.shape) # torch.Size([1, 1280, 7, 7])

Example: Export to ONNX

Exporting to ONNX for deploying to production is now simple:

import torch
from efficientnet_pytorch import EfficientNet

model = EfficientNet.from_pretrained('efficientnet-b1')
dummy_input = torch.randn(10, 3, 240, 240)

model.set_swish(memory_efficient=False)
torch.onnx.export(model, dummy_input, "test-b1.onnx", verbose=True)

Here is a Colab example.

ImageNet

See examples/imagenet for details about evaluating on ImageNet.

Contributing

If you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions, simply post them as GitHub issues.

I look forward to seeing what the community does with these models!

efficientnet-pytorch's People

Contributors

alexdut avatar authman avatar chrisyeh96 avatar chsasank avatar consilium538 avatar cove9988 avatar creeky123 avatar developer0hye avatar khornlund avatar lukemelas avatar matkalinowski avatar michelml avatar ojdo avatar polariszhao avatar pshwetank avatar qubvel avatar robotrapta avatar rvandeghen avatar ryo-f avatar vfdev-5 avatar workingcoder 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

efficientnet-pytorch's Issues

Is stride correct

I find that in the original paper strides for 7 stages (stage 2-8 in the following table)are:
image
1, 2, 2, 1, 2, 2, 1
but in your code I find it is:
1, 2, 2, 2, 1, 2, 1
Did you do it intentionally or maybe I am wrong?

Error when get intermediate output

Hello,
I'm trying to get intermediate output from Pytorch like this:

model = EfficientNet.from_pretrained('efficientnet-b0')
features = nn.Sequential(*list(model.children())[:-1])
x = torch.rand((4,3,224,224))
y = features(x)

And Colab throws me the error


NotImplementedError Traceback (most recent call last)
in ()
2 features = nn.Sequential(*list(model.children())[:-1])
3 x = torch.rand((4,3,224,224))
----> 4 y = features(x)

3 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
---> 92 input = module(input)
93 return input
94

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in forward(self, *input)
86 registered hooks while the latter silently ignores them.
87 """
---> 88 raise NotImplementedError
89
90 def register_buffer(self, name, tensor):

NotImplementedError:

an error in example.ipynb

preds = torch.topk(logits, k=5).indices.squeeze(0).tolist()

I think you want to find the indexs for top5 using indices , but tuple has no attributes indices when I using the code above.

preds = torch.topk(logits, k=5)[1].squeeze(0).tolist()
It works well.

Finetune on EfficientNet looks like a disaster?

Hi, luke, Thank you for your solid work!
We tried to replace the backbone of FPN from Resnet50 into EfficientNetB0.
but the Focal loss is always large and looks like never converges.
maybe the reason is drop_connect? or tf like padding conv? or something else?
Can you show me some tips? many thanks!

PS:Does anyone else tried to train on object detection task with efficientnet, maybe we can also discuss here.

onnx export warning

I got a warning when I run the export onnx example

UserWarning: ONNX export squeeze with negative axis -1 might cause the onnx model to be incorrect. Negative axis is not supported in ONNX. Axis is converted to 3 based on input shape at export time. Passing an tensor of different rank in execution will be incorrect.
"Passing an tensor of different rank in execution will be incorrect.")

UserWarning: ONNX export squeeze with negative axis -1 might cause the onnx model to be incorrect. Negative axis is not supported in ONNX. Axis is converted to 3 based on input shape at export time. Passing an tensor of different rank in execution will be incorrect.
"Passing an tensor of different rank in execution will be incorrect.")

The problem comes from this line of code

x = F.adaptive_avg_pool2d(x, 1).squeeze(-1).squeeze(-1)

We can use a certain size to eliminate this warning

x = F.adaptive_avg_pool2d(x, 1).squeeze(3).squeeze(2)
# or
x = F.adaptive_avg_pool2d(x, 1)
x = reshape(x.size(0), -1)
``

How to migrate the model to cpu run

If I want to modify the model to cpu, how should I modify the code in main.py? I found a lot of statements about GPU in main.py, not very well modified, I hope to get everyone's opinion.

Error when loading pretrained model with num_classes!=1000

I am trying to load a pretrained model with a different number of classes to apply transfer learning on cifar10. When I run

model = EfficientNet.from_pretrained('efficientnet-b1', num_classes=23)

I got this error:

AttributeError Traceback (most recent call last)
in
----> 1 model = EfficientNet.from_pretrained('efficientnet-b1', num_classes=23)

~/anaconda3/envs/compression/lib/python3.6/site-packages/efficientnet_pytorch/model.py in from_pretrained(cls, model_name, num_classes)
194 def from_pretrained(cls, model_name, num_classes=1000):
195 model = EfficientNet.from_name(model_name, override_params={'num_classes': num_classes})
--> 196 load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000))
197 return model
198

~/anaconda3/envs/compression/lib/python3.6/site-packages/efficientnet_pytorch/utils.py in load_pretrained_weights(model, model_name, load_fc)
294 state_dict.pop('_fc.bias')
295 res = model.load_state_dict(state_dict, strict=False)
--> 296 assert str(res.missing_keys) == str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
297 print('Loaded pretrained weights for {}'.format(model_name))

AttributeError: 'NoneType' object has no attribute 'missing_keys'

Pytorch version 1.0.1.
Can anyone help me?

NotImplementedError when use forward function.

I try define a model based on pretrained EfficientNet as below. But I get a NotImplementedError: when use 'forward' function. However, when I use other pretrained CNN e.g., resnet18 from torchvision, there is no such problem. Can anyone help me? Thanks a lot

'Model definition'
class EfficientNet_scene(nn.Module):

def __init__(self,model_name='efficientnet-b0',class_num=45,initfc_type='normal',gain=0.2):
    super(EfficientNet_scene, self).__init__()
    model = EfficientNet.from_pretrained(model_name)
    aul = [*model.children()]
    self.features = nn.Sequential(*aul[:-1])
    self.fc = nn.Linear(aul[-1].in_features,class_num)

    if hasattr(self.fc, 'bias') and self.fc.bias is not None:
        nn.init.constant_(self.fc.bias.data, 0.0)
    if initfc_type == 'normal':
        nn.init.normal_(self.fc.weight.data, 0.0, gain)
    elif initfc_type == 'xavier':
        nn.init.xavier_normal_(self.fc.weight.data, gain=gain)
    elif initfc_type == 'kaiming':
        nn.init.kaiming_normal_(self.fc.weight.data, a=0, mode='fan_in')
    elif initfc_type == 'orthogonal':
        nn.init.orthogonal_(self.fc.weight.data, gain=gain)

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

net = EfficientNet_scene()
image = torch.randn(1,3,224,224)
b = net(image)

Error information:
NotImplementedError Traceback (most recent call last)
in ()
37 print(net)
38 image = torch.randn(1,3,224,224)
---> 39 b = net(image)
40 print(b)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

in forward(self, x)
30
31 def forward(self,x):
---> 32 x = self.features(x)
33 x = x.reshape(x.size(0), -1)
34 x = self.fc(x)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
---> 92 input = module(input)
93 return input
94

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in forward(self, *input)
86 registered hooks while the latter silently ignores them.
87 """
---> 88 raise NotImplementedError
89
90 def register_buffer(self, name, tensor):

NotImplementedError:

About relu_fn

Hi,
Thanks for sharing your implementation.
I have a question about relu_fn in utils.py, you use x*sigmoid as activation function.
Is it better than ReLU or Swish ?
Just want to know that did you try different activation functions ?
Thanks!!!

Update:
Sorry, Swish activation function in MobilenetV3 is different, and the original Swish is as your implementation.

Problem with pretrained b5

Hey,

when trying to use GPU with the pretrained b5, the binary_tensor in this method remains a normal float tensor and this lets the program crash (line 73).

image

Would it be ok to create a pull request in order to move it to a device / support CUDA gpus?

convert Tensorflow weights to Pytorch

Hi, I can't download the Tensorflow weights form the URL in your code, so I download the weights myself. I also want to use your code to convert the tensorflow weights to Pytorch, what should I do?

load model error

File "test.py", line 11, in
model = EfficientNet.from_pretrained('efficientnet-b0')
File "/EfficientNet_pyt/efficientnet_pytorch/model.py", line 188, in from_pretrained
load_pretrained_weights(model, model_name)
File "/EfficientNet_pyt/efficientnet_pytorch/utils.py", line 244, in load_pretrained_weights
model.load_state_dict(state_dict)
File "/env_pytorch0.4_py3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 721, in load_state_dict
self.class.name, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for EfficientNet:
Unexpected key(s) in state_dict: "_bn0.num_batches_tracked", "_blocks.0._bn1.num_batches_tracked", "_blocks.0._bn2.num_batches_tracked", "_blocks.1._bn0.num_batches_tracked", "_blocks.1._bn1.num_batches_tracked", "_blocks.1._bn2.num_batches_tracked", "_blocks.2._bn0.num_batches_tracked", "_blocks.2._bn1.num_batches_tracked", "_blocks.2._bn2.num_batches_tracked", "_blocks.3._bn0.num_batches_tracked", "_blocks.3._bn1.num_batches_tracked", "_blocks.3._bn2.num_batches_tracked", "_blocks.4._bn0.num_batches_tracked", "_blocks.4._bn1.num_batches_tracked", "_blocks.4._bn2.num_batches_tracked", "_blocks.5._bn0.num_batches_tracked", "_blocks.5._bn1.num_batches_tracked", "_blocks.5._bn2.num_batches_tracked", "_blocks.6._bn0.num_batches_tracked", "_blocks.6._bn1.num_batches_tracked", "_blocks.6._bn2.num_batches_tracked", "_blocks.7._bn0.num_batches_tracked", "_blocks.7._bn1.num_batches_tracked", "_blocks.7._bn2.num_batches_tracked", "_blocks.8._bn0.num_batches_tracked", "_blocks.8._bn1.num_batches_tracked", "_blocks.8._bn2.num_batches_tracked", "_blocks.9._bn0.num_batches_tracked", "_blocks.9._bn1.num_batches_tracked", "_blocks.9._bn2.num_batches_tracked", "_blocks.10._bn0.num_batches_tracked", "_blocks.10._bn1.num_batches_tracked", "_blocks.10._bn2.num_batches_tracked", "_blocks.11._bn0.num_batches_tracked", "_blocks.11._bn1.num_batches_tracked", "_blocks.11._bn2.num_batches_tracked", "_blocks.12._bn0.num_batches_tracked", "_blocks.12._bn1.num_batches_tracked", "_blocks.12._bn2.num_batches_tracked", "_blocks.13._bn0.num_batches_tracked", "_blocks.13._bn1.num_batches_tracked", "_blocks.13._bn2.num_batches_tracked", "_blocks.14._bn0.num_batches_tracked", "_blocks.14._bn1.num_batches_tracked", "_blocks.14._bn2.num_batches_tracked", "_blocks.15._bn0.num_batches_tracked", "_blocks.15._bn1.num_batches_tracked", "_blocks.15._bn2.num_batches_tracked", "_bn1.num_batches_tracked".

Using EfficientNet training Person-reid

Hi,I used EfficientNet to train Person reid on dataset Market1501, but the mAP is only 70+,I don't know why. Does EfficientNet not suitable for feature extraction?

CUDA -> CPU issue

def drop_connect(inputs, p, training):
    """ Drop connect. """
    if not training: return inputs
    batch_size = inputs.shape[0]
    keep_prob = 1 - p
    random_tensor = keep_prob
    random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype)  # uniform [0,1)
    binary_tensor = torch.floor(random_tensor)
    output = inputs / keep_prob * binary_tensor # error happens here
    return output

Faced error: RuntimeError: expected backend CUDA and dtype Float but got backend CPU and dtype Float

when I try to run on GPU, this error happens, the error direct me to this line, I think we should convert binary_tensor to inputs.device:

binary_tensor = torch.floor(random_tensor).to(inputs.device)

Inference speed between the self trained model and the model provided by model_zoo

@lukemelas Hi, Luke,

Thanks for your great work! I have written a training script to train my own model EfficientNet-B0 (based on the pre-trained model on ImageNet). The only modification to the model is that I changed the last fc layer (i.e. the number of classes). And then I tested the inference time using the self trained model, the results are:

CPU time cost: 0.772s
GPU time cost: 0.021s

And you can see that the model is so slow on CPU. Then I tested the inference time with /examples/simple/example.ipynb you provided, the results are:

CPU time cost: 0.162s

Do you know why there is a big gap between the time cost on CPU of these two models?

Thanks a lot !

B0 speed and accuracy is lower than GoogleNet (Inception V-1)?

I have tried different combination of pre-trained EfficientNet B0~B5 and compared it to pre-trained GoogleNet model. I expected the efficientNet b0 will outperform googlenet due to lesser parameters and higher accuracy. Here's the result of panda classification using the provided sample code :

python googlenet.py

Time = 12ms
-----
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca           (94.11%)
Arctic fox, white fox, Alopex lagopus                                       (0.20%)
soccer ball                                                                 (0.19%)
hog, pig, grunter, squealer, Sus scrofa                                     (0.16%)
French bulldog                                                              (0.16%)

python efficientnet.py
Loaded pretrained weights for efficientnet-b0
torch.Size([1, 3, 224, 254])

Time = 50ms
-----
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca           (88.04%)
ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus                 (0.95%)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens         (0.16%)
badger                                                                      (0.14%)
soccer ball

Tested on Gtx 1080ti, the googlenet's inference time is faster and the accuracy also higher than B0. I want to confirm whether it is true or Did I do something wrong when doing the classification?

Training with own dataset

Hello @lukemelas ,
I'm very excited about your code. Because I want to test in a problem I'm working with. I'm just starting with Image classifiers.

Could you please help me or provide an example where we can use our model to train a data-set from scratch?

Best regards!

Testing result

Will you check your reimplementation except for accuracy? The same speed & memory?

How to train efficientnet from b0 to b1

Hi!

I'm trying to understand efficientnet code, but I don't know how to train the network from b0 to b1 under the same dataset.

Does it fine-tune b1 from the pretrained b0 or just re-train?

Thanks!

Benchmark results on imagenet validation set

Here is my benchmark results on imagenet validation set:

model inference time image load time bs #batches Memory(MB) top-1 top-5 top-1(paper) top-5 (paper) notes
EfficientNet-B0 86.2 954.3 64 782 2325 75.81 92.66 76.3 93.2 resize to 224+32, keep aspect ratio, then center crop to 224x224
EfficientNet-B3 304.2 1070.8 64 782 4913 80.28 94.98 81.1 95.5 resize to 300+44, keep aspect ratio, then center crop to 300x300

time unit: ms / batch

A little bit worse than original benchmarks (B3 can reach around 80.9% top-1).

Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

Hi there! Thanks for your great repo, but i faced with some difficulties while trying to inference on device ('cuda:0'). My code:
device = torch.device('cuda:0')
tfms = transforms.Compose([transforms.Resize(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
img = tfms(Image.open('img.png')).unsqueeze(0)
img.to(device)
model.to(device)
model.eval()
with torch.no_grad(): outputs = model(img)
And thrown error:
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

How to tranform efficient-pytorch to efficient-onnx

I have tried to convert efficient-pytorch to efficient-onnx with api (torch.onnx.export), but I meet a problem showing below info
Failed to export an ONNX attribute, since it's not constant, please try to make things (e.g., kernel size) static if possible
How could I fix it ?

Testing results on ImageNet-1k val, 224-crop@256-image, 320-crop@350-image

I evaluate the pre-trained EfficientNet on ImageNet-1k val:

model depth params flops 224-crop @ 256-img 320-crop @ 350-img
eff-b0 82 5.28M 0.778G 75.880 / 92.758 78.286 / 94.108
eff-b1 116 7.79M 1.148G 76.622 / 93.232 79.736 / 94.930
eff-b2 116 9.11M 1.327G 76.484 / 92.932 80.110 / 95.038
eff-b3 131 12.23M 1.938G 75.964 / 92.888 80.700 / 95.204

For more details: http://studyai.com/article/1f9d25de

The results are very different with paper. Does anybody get better results ?

Train on Cifar100 problem

Hello,Luke, I want to use your model to train cifar100, but what strategy should I use? For example, can I use the pre-training model? or resolution?

Error when converting model to TensorRT

When converting the ONNX model to a TensorRT model I get the following error:

Parameter check failed at: ../builder/Network.cpp::addPooling::108, condition: allDimsGtEq(windowSize, 1) && windowSize.h() * windowSize.w() < MAX_KERNEL_DIMS_PRODUCT
Building accelerated plugins... 
Parameter check failed at: ../builder/Network.cpp::addConcatenation::167, condition: nbInputs > 0 && nbInputs < MAX_CONCAT_INPUTS 
Segmentation fault (core dumped)

I believe it relates to the adaptive nature of the pooling layers. I draw this conclusion based on this link: https://devtalk.nvidia.com/default/topic/985607/gpu-accelerated-libraries/tensorrt-1-0-fails-on-squeezenet/

why the params size of b0 model is 20M rather than 5.3M

Hi, @lukemelas ~
I'm so excited about your code.
I have a question. You said that the Params size of the b0 model is 5.3M,but the size of pretrained model what you offered is 20.3M,
and then I test it through torchsummary, which shows that Params size (MB): 20.17, why?


#torchsummary code:
from efficientnet_pytorch import EfficientNet
modelBody= EfficientNet.from_pretrained('efficientnet-b2').to(device)
from torchsummary import summary
summary(modelBody, input_size=(3, 224, 224))


#result
Total params: 5,288,548
Trainable params: 5,288,548
Non-trainable params: 0
Input size (MB): 0.57
Forward/backward pass size (MB): 107.31
Params size (MB): 20.17
Estimated Total Size (MB): 128.06


FLOPs count seems to be off

Hi Luke, Thanks for your great work !

I am interested in the FLOPs of the models implemented.

I have always been using this to count FLOPs.

And for most cases, models from vision:

  1. Resnet50
  2. VGG19
  3. Densenet121
  4. Densenet169
  5. Shufflenetv2_2_0

they all seems to match the FLOPs count from paper.

However in this case it is not. I can not think of a reason why, when i traverse the code down to each module, even added the FLOPs count for both padding inside the conv2dblock and Swish activation .

Do you have any idea?

Thanks in advance.

The speed of training is slow?

Sorry, I found a problem, that was, when I used the pretrained model 'efficientNet-B0' to fine-tune on my own dataset by gpu device, I found the speed of training was slow, thus, is there something wrong? Please give me an answer if having some solutions? Thanks a lot!

Problem in the padding step

Hey!! First of all, many thanks for your great job!! I've a problem using the example jupyter notebook. When I've tried to perform the inference, this error appears:

File "efficientnet_pytorch/model.py", line 157, in forward
    x = self.extract_features(inputs)
  File "efficientnet_pytorch/model.py", line 142, in extract_features
    x = relu_fn(self._bn0(self._conv_stem(inputs)))
  File "/home/poto/anaconda2/envs/pruebas/lib/python2.7/site-packages/torch/nn/modules/module.py", line 475, in __call__
    result = self._slow_forward(*input, **kwargs)
  File "/home/poto/anaconda2/envs/pruebas/lib/python2.7/site-packages/torch/nn/modules/module.py", line 465, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "efficientnet_pytorch/utils.py", line 95, in forward
    x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2])
  File "/home/poto/anaconda2/envs/pruebas/lib/python2.7/site-packages/torch/nn/functional.py", line 2159, in pad
    return ConstantPadNd.apply(input, pad, value)
  File "/home/poto/anaconda2/envs/pruebas/lib/python2.7/site-packages/torch/nn/_functions/padding.py", line 26, in forward
    output = input.new(input.size()[:(ctx.l_diff)] + new_dim).fill_(ctx.value)
TypeError: an integer is required

As you can see I'm using python 2.7, but I think there is no a problem with the python version...what do you think?

Thanks in advance!

How to train efficientnet on CIFAR-10 or CIFAR-100? Image size is 32x32.

The default model input size is 224~600. What adjustments should I make to fit CIFAR-10's 32x32?

def efficientnet_params(model_name):
    """ Map EfficientNet model name to parameter coefficients. """
    params_dict = {
        # Coefficients:   width,depth,res,dropout
        'efficientnet-b0': (1.0, 1.0, 224, 0.2),
        'efficientnet-b1': (1.0, 1.1, 240, 0.2),
        'efficientnet-b2': (1.1, 1.2, 260, 0.3),
        'efficientnet-b3': (1.2, 1.4, 300, 0.3),
        'efficientnet-b4': (1.4, 1.8, 380, 0.4),
        'efficientnet-b5': (1.6, 2.2, 456, 0.4),
        'efficientnet-b6': (1.8, 2.6, 528, 0.5),
        'efficientnet-b7': (2.0, 3.1, 600, 0.5),
    }
    return params_dict[model_name]

https://github.com/lukemelas/EfficientNet-PyTorch/blob/master/efficientnet_pytorch/utils.py#L101-L114

How to extract an intermediate feature from efficientnet?

Hi, I'm making U-Net encoded by efficient-b4.
I tried extracting feature as I did in resnet, but it doesn't work with 'NotImplementedError'

backbone = EfficientNet.from_pretrained('efficientnet-b4')
nn.Sequential(*list(backbone.children())[:3])(torch.rand(2,3,512,512))

Error>


NotImplementedError Traceback (most recent call last)
in
----> 1 nn.Sequential(*list(backbone.children())[:3])(inp)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
---> 92 input = module(input)
93 return input
94

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in forward(self, *input)
86 registered hooks while the latter silently ignores them.
87 """
---> 88 raise NotImplementedError
89
90 def register_buffer(self, name, tensor):

NotImplementedError:

There is a way to extract the final feature by 'extract_features' function, however I want to get an intermediate feature for skip connection.
Is there any method to extract it?

RuntimeError: expected type torch.cuda.FloatTensor but got torch.FloatTensor

Hi, @lukemelas ~
I'm so excited about your code, but today, after i upgrade the pip package today, a new problem appeared:


C:\ProgramData\Anaconda3\lib\site-packages\efficientnet_pytorch\utils.py in drop_connect(inputs, p, training)
72 random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype) # uniform [0,1)
73 binary_tensor = torch.floor(random_tensor)
---> 74 output = inputs / keep_prob * binary_tensor
75 return output
76
RuntimeError: expected type torch.cuda.FloatTensor but got torch.FloatTensor


I sure that i don't change any of my code, just upgrade the pip package "efficientNet-Pytroch"
in my code, i use your model like:


from efficientnet_pytorch import EfficientNet
modelPre = EfficientNet.from_pretrained('efficientnet-b0')


Cannnot training due to Expected CUDA but got CPU instead

Yesterday before the update of B4 band B5, I can still train the model from scratch, however, after the update, I see that it seems that the drop_connect function in the utils is not set to CUDA as I think, so it would conflict with the input data which is loaded to GPU already

EfficientNet -> Onnx -> OpenCV

Hello,

I'm trying to run Efficientnet on OpenCV framework, but getting error on import from ONNX

import cv2


net=cv2.dnn.readNetFromONNX('efficientnet-b0.onnx')

error: OpenCV(4.1.0) /io/opencv/modules/dnn/src/layers/eltwise_layer.cpp:116: error: (-215:Assertion failed) inputs[0] == inputs[i] in function 'getMemoryShapes'

Export to onnx with warnings only - ok
Onnx check - ok

CV2 version - 4.1.0

Memory Issues

Hi Luke,

Thank you for the awesome work. I tried running EfficientNet-B0 on my GTX 1070 (8GB RAM) with an input batch of dimension [44x1x256x256] (single channel image) and I am running into 'CUDA out of memory' (with the model in 'training' mode).

I tried running another implementation and wasn't getting this issue, and after digging in the code, it seems as if the implementation for MBConv (or the re-iteration of MBConv) was too memory hungry.

I really like your implementation of EfficientNet and if I did have more time, I would definitely have a deeper dive into your code. At the mean time, if possible, could you help me check this issue out (maybe it'll speed up training in the future?) ? Thank you!

pretrained efficientnet-b7 ?

Hi, thank you for the awesome work. May I ask whether you have plan to provide the pre-trained efficientnet-b7 model?

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.