Adding EfficientNet to fastai vision

(Pranav) #1

In this ( paper published by Google, the authors proposed a new neural network architecture they call “EfficientNet”. 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.

A pytorch implementation of EfficientNet can be found here: Through this, pytorch implementation, we can easily add EfficientNet to fastai.

From the pytorch implementation of EfficientNet:
“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.”

I will make a pull request to Is this the right repo to make a pull request? In this request, I’ll include in the util and model script of the pytorch implementation. Is there anything else I have to do?


(Zachary Mueller) #2

There’s a large discussion on efficienct net already here: EfficientNet

And how to use it

1 Like

(Pranav) #3

I believe the pytorch implementation on github came after all these discussions. And I just want to add this code to fast ai.



Glad to see that!
I’ve installed the fastai dev version by pip install git+
and the Efficientnet pip install efficientnet-pytorch.

How can I use Efficientnet like
learn = cnn_learner(data, models.resnet34, metrics=error_rate)?

learn = cnn_learner(data, models.efficientnet.EfficientNetB1, metrics=error_rate)
doesn’t work?


(Zachary Mueller) #5

You need to use Learner(), not cnn_learner.



Thank you so much for your reply!

I tried learn = Learner(data, models.efficientnet.EfficientNetB1(), metrics=error_rate), it raised “NameError: name ‘data’ is not defined”.
While learn = Learner(data, models.efficientnet.EfficientNetB1, metrics=error_rate), it raised "AttributeError: ‘function’ object has no attribute ‘to’ ".

Seems silly questions… Could you tell me how to use it? Thanks again.


(Zachary Mueller) #7

The first is right, you need to pass the models in as a function. Try specifying data=data, arch= models.efficientnet.EfficientNetB1()

(And make sure data was defined beforehand?) :slight_smile:



Thank you. The code is as following:

from fastai import *
from import *
path = untar_data(URLs.MNIST_TINY)
data = ImageDataBunch.from_folder(path)
# learn = cnn_learner(data, models.resnet18, metrics=accuracy)  # works 

# from efficientnet_pytorch import EfficientNet
# model = EfficientNet.from_pretrained('efficientnet-b0', num_classes=2)
# model._fc = nn.Linear(in_features=1280, out_features=2, bias=True)
# learn = Learner(data, model, metrics=accuracy)                # works

learn = Learner(data, models.efficientnet.EfficientNetB1(), metrics=accuracy) # NameError: name 'data' is not defined

Don’t know what’s wrong…

1 Like


I have the same problem it raised “NameError: name ‘data’ is not defined”. data is defined and does not make a difference if I use:
learn = Learner( data=data,arch=models.efficientnet.EfficientNetB5(),
or if I use
learn = Learner( data,arch=models.efficientnet.EfficientNetB5(),


(Zachary Mueller) #10

Here are the steps I did to get efficient net working @gy0373 @agentili

!pip install efficientnet-pytorch

from fastai import *
from import *
from efficientnet_pytorch import EfficientNet

path = untar_data(URLs.PETS)
path_anno = path/'annotations'
path_img = path/'images'
fnames = get_image_files(path_img)
pat = r'/([^/]+)_\d+.jpg$'

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=32

model = EfficientNet.from_name('efficientnet-b0')
model._fc = nn.Linear(1280, data.c)
learn = Learner(data, model)

I didn’t do this with MNIST as they’re set up for 3 channel inputs whereas MNIST is 2 channel (B/W). Sorry it took so long, did not have a chance to run this briefly until now



Thanks, it it works great!