NameError: name 'timm' is not defined

I ran into the following error when I tried to use the new timm integration functionality. The problem was that I had installed timm after importing fastai.vision.all. To fix this, I just needed to restart my notebook so the import was done again.

Here was the code I ran:

learn = vision_learner(dls, 'ghostnet_050', pretrained=False, metrics=accuracy, opt_func=Adam, wd=1e-5)

and here was the stack trace:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/tmp/ipykernel_38235/3334928029.py in <module>
----> 1 learn = vision_learner(dls, 'ghostnet_050', pretrained=False, metrics=accuracy, opt_func=Adam, wd=1e-5)

/data/fastai/vision/learner.py in vision_learner(dls, arch, normalize, n_out, pretrained, loss_func, opt_func, lr, splitter, cbs, metrics, path, model_dir, wd, wd_bn_bias, train_bn, moms, **kwargs)
    202     meta = model_meta.get(arch, _default_meta)
    203     if normalize: _add_norm(dls, meta, pretrained)
--> 204     if isinstance(arch, str): model = create_timm_model(arch, n_out, default_split, pretrained, **kwargs)
    205     else: model = create_vision_model(arch, n_out, pretrained=pretrained, **kwargs)
    206 

/data/fastai/vision/learner.py in create_timm_model(arch, n_out, cut, pretrained, n_in, init, custom_head, concat_pool, **kwargs)
    178                      concat_pool=True, **kwargs):
    179     "Create custom architecture using `arch`, `n_in` and `n_out` from the `timm` library"
--> 180     body = TimmBody(arch, pretrained, None, n_in)
    181     nf = body.model.num_features
    182     return add_head(body, nf, n_out, init=init, head=custom_head, concat_pool=concat_pool, pool=body.needs_pool, **kwargs)

/data/fastai/vision/learner.py in __init__(self, arch, pretrained, cut, n_in)
    167     def __init__(self, arch:str, pretrained:bool=True, cut=None, n_in:int=3):
    168         super().__init__()
--> 169         model = timm.create_model(arch, pretrained=pretrained, num_classes=0, in_chans=n_in)
    170         self.needs_pool = model.default_cfg.get('pool_size', None)
    171         self.model = model if cut is None else cut_model(model, cut)

NameError: name 'timm' is not defined
13 Likes

Yup there’s not much we can do about that - timm has to be installed before you use your notebook.

5 Likes

Mostly just wanted to put it out there so if anybody is searching for that error, they can maybe find this and use it to fix their issue :slight_smile:

19 Likes

Hi Jeremy I encountered the same problem, the question I have is that I can not run learn = vision_learner(dls, 'ghostnet_050', pretrained=False, metrics=accuracy just as Kevin described since I didn’t install time before I use my notebook, but I can run timm.list_models('convnext*') in this case (installed after starting notebook), why is that?

3 Likes

if you import it before import fastai like the following, it works for me

import timm
from fastai.vision.all import *

6 Likes

Yes faced the same error. Importing timm library before fastai libraries worked for me.

2 Likes

I pip installed timm and it worked for me.

Same for me.

Then, I restarted the kernel and imported everything back again.

Worked like a charm.

1 Like

Thank you for this post! I had tried every configuration for installing timm EXCEPT putting it at the top of the install/import list. The frustration was unspeakable. This made everything run beautifully. YOU ROCK!

3 Likes

I just ran into this error and found the solution here. So thanks for sharing, Kevin! :+1:

2 Likes

!pip install timm

I find that vice versa timm also includes fastai

1 Like

Appreciate this!

2 Likes

Most likely, you will only need to restart the kernel and run all the above again (assuming you have already installed timm using !pip install timm).

I used !pip install timm in Colab but I am still facing the same issue. Can anyone please suggest a solution to this ?

Make sure that after you connect to the runtime in colab, you first install timm and then import fastai:

If I import fastai before pip installing timm, I get the timm is not defined error as you have when creating the Learner.

Oh now it’s working !! Thank you for your help

1 Like

Thank you! This worked for me!
For me the problem was that the train.ipynb notebook downloaded from the HuggingFace repository for lesson 3 has the order of imports for timm and fastai.vision.all inverted. Putting timm on top fixes the error.