Beginner: Setup ✅

Is there a way to get notebooks with pinned versions of dependencies? The notebooks from pages like Practical Deep Learning for Coders - 4: Natural Language (NLP) generally don’t work for me, and the problem generally looks like version conflicts between the various libraries.

For example, when I clone the latest notebook from How does a neural net really work? | Kaggle
I get an error about timm not being installed. So I add a new code block with

!pip install timm

Then, I run it again and I get a little further, but it still errors out with


NameError Traceback (most recent call last)
/tmp/ipykernel_17/2451914947.py in
----> 1 learn = vision_learner(dls, resnet34, metrics=error_rate)
2 learn.fine_tune(3)

NameError: name ‘vision_learner’ is not defined

which is a bit harder to diagnose.

I seem to hit these kinds of problems on every notebook.

See how you go with… how to pin versions with pip install - Google Search

Did you link to the right notebook? (How does a neural net really work? | Kaggle) does not appear to use vision_learner or fine_tune.

Generally if you see “NameError: name ‘vision_learner’ is not defined”. It indicates that the library has not been imported. e.g
from fastai.vision.all import *

“Is it a bird? Creating a model from your own data notebook” has an issue with the vision learner predict method with Pillow images.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_18/3073739047.py in <module>
----> 1 is_bird,_,probs = learn.predict(PILImage.create('bird.jpg'))
      2 print(f"This is a: {is_bird}.")
      3 print(f"Probability it's a bird: {probs[0]:.4f}")
      4 im.to_thumb(256,256)

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in predict(self, item, rm_type_tfms, with_input)
    319     def predict(self, item, rm_type_tfms=None, with_input=False):
    320         dl = self.dls.test_dl([item], rm_type_tfms=rm_type_tfms, num_workers=0)
--> 321         inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
    322         i = getattr(self.dls, 'n_inp', -1)
    323         inp = (inp,) if i==1 else tuplify(inp)

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in get_preds(self, ds_idx, dl, with_input, with_decoded, with_loss, act, inner, reorder, cbs, **kwargs)
    306         if with_loss: ctx_mgrs.append(self.loss_not_reduced())
    307         with ContextManagers(ctx_mgrs):
--> 308             self._do_epoch_validate(dl=dl)
    309             if act is None: act = getcallable(self.loss_func, 'activation')
    310             res = cb.all_tensors()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
    242         if dl is None: dl = self.dls[ds_idx]
    243         self.dl = dl
--> 244         with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
    245 
    246     def _do_epoch(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    197 
    198     def _with_events(self, f, event_type, ex, final=noop):
--> 199         try: self(f'before_{event_type}');  f()
    200         except ex: self(f'after_cancel_{event_type}')
    201         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    203     def all_batches(self):
    204         self.n_iter = len(self.dl)
--> 205         for o in enumerate(self.dl): self.one_batch(*o)
    206 
    207     def _backward(self): self.loss_grad.backward()

/opt/conda/lib/python3.7/site-packages/fastai/data/load.py in __iter__(self)
    125         self.before_iter()
    126         self.__idxs=self.get_idxs() # called in context of main process (not workers/subprocesses)
--> 127         for b in _loaders[self.fake_l.num_workers==0](self.fake_l):
    128             # pin_memory causes tuples to be converted to lists, so convert them back to tuples
    129             if self.pin_memory and type(b) == list: b = tuple(b)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    519             if self._sampler_iter is None:
    520                 self._reset()
--> 521             data = self._next_data()
    522             self._num_yielded += 1
    523             if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    559     def _next_data(self):
    560         index = self._next_index()  # may raise StopIteration
--> 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    562         if self._pin_memory:
    563             data = _utils.pin_memory.pin_memory(data)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     32                 raise StopIteration
     33         else:
---> 34             data = next(self.dataset_iter)
     35         return self.collate_fn(data)
     36 

/opt/conda/lib/python3.7/site-packages/fastai/data/load.py in create_batches(self, samps)
    136         if self.dataset is not None: self.it = iter(self.dataset)
    137         res = filter(lambda o:o is not None, map(self.do_item, samps))
--> 138         yield from map(self.do_batch, self.chunkify(res))
    139 
    140     def new(self, dataset=None, cls=None, **kwargs):

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in chunked(it, chunk_sz, drop_last, n_chunks)
    228     if not isinstance(it, Iterator): it = iter(it)
    229     while True:
--> 230         res = list(itertools.islice(it, chunk_sz))
    231         if res and (len(res)==chunk_sz or not drop_last): yield res
    232         if len(res)<chunk_sz: return

/opt/conda/lib/python3.7/site-packages/fastai/data/load.py in do_item(self, s)
    151     def prebatched(self): return self.bs is None
    152     def do_item(self, s):
--> 153         try: return self.after_item(self.create_item(s))
    154         except SkipItemException: return None
    155     def chunkify(self, b): return b if self.prebatched else chunked(b, self.bs, self.drop_last)

/opt/conda/lib/python3.7/site-packages/fastai/data/load.py in create_item(self, s)
    158     def retain(self, res, b):  return retain_types(res, b[0] if is_listy(b) else b)
    159     def create_item(self, s):
--> 160         if self.indexed: return self.dataset[s or 0]
    161         elif s is None:  return next(self.it)
    162         else: raise IndexError("Cannot index an iterable dataset numerically - must use `None`.")

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in __getitem__(self, it)
    456 
    457     def __getitem__(self, it):
--> 458         res = tuple([tl[it] for tl in self.tls])
    459         return res if is_indexer(it) else list(zip(*res))
    460 

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in <listcomp>(.0)
    456 
    457     def __getitem__(self, it):
--> 458         res = tuple([tl[it] for tl in self.tls])
    459         return res if is_indexer(it) else list(zip(*res))
    460 

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in __getitem__(self, idx)
    415         res = super().__getitem__(idx)
    416         if self._after_item is None: return res
--> 417         return self._after_item(res) if is_indexer(idx) else res.map(self._after_item)
    418 
    419 # %% ../../nbs/03_data.core.ipynb 53

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in _after_item(self, o)
    375             raise
    376     def subset(self, i): return self._new(self._get(self.splits[i]), split_idx=i)
--> 377     def _after_item(self, o): return self.tfms(o)
    378     def __repr__(self): return f"{self.__class__.__name__}: {self.items}\ntfms - {self.tfms.fs}"
    379     def __iter__(self): return (self[i] for i in range(len(self)))

/opt/conda/lib/python3.7/site-packages/fastcore/transform.py in __call__(self, o)
    206         self.fs = self.fs.sorted(key='order')
    207 
--> 208     def __call__(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
    209     def __repr__(self): return f"Pipeline: {' -> '.join([f.name for f in self.fs if f.name != 'noop'])}"
    210     def __getitem__(self,i): return self.fs[i]

/opt/conda/lib/python3.7/site-packages/fastcore/transform.py in compose_tfms(x, tfms, is_enc, reverse, **kwargs)
    156     for f in tfms:
    157         if not is_enc: f = f.decode
--> 158         x = f(x, **kwargs)
    159     return x
    160 

/opt/conda/lib/python3.7/site-packages/fastcore/transform.py in __call__(self, x, **kwargs)
     79     @property
     80     def name(self): return getattr(self, '_name', _get_name(self))
---> 81     def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
     82     def decode  (self, x, **kwargs): return self._call('decodes', x, **kwargs)
     83     def __repr__(self): return f'{self.name}:\nencodes: {self.encodes}decodes: {self.decodes}'

/opt/conda/lib/python3.7/site-packages/fastcore/transform.py in _call(self, fn, x, split_idx, **kwargs)
     89     def _call(self, fn, x, split_idx=None, **kwargs):
     90         if split_idx!=self.split_idx and self.split_idx is not None: return x
---> 91         return self._do_call(getattr(self, fn), x, **kwargs)
     92 
     93     def _do_call(self, f, x, **kwargs):

/opt/conda/lib/python3.7/site-packages/fastcore/transform.py in _do_call(self, f, x, **kwargs)
     95             if f is None: return x
     96             ret = f.returns(x) if hasattr(f,'returns') else None
---> 97             return retain_type(f(x, **kwargs), x, ret)
     98         res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
     99         return retain_type(res, x)

/opt/conda/lib/python3.7/site-packages/fastcore/dispatch.py in __call__(self, *args, **kwargs)
    118         elif self.inst is not None: f = MethodType(f, self.inst)
    119         elif self.owner is not None: f = MethodType(f, self.owner)
--> 120         return f(*args, **kwargs)
    121 
    122     def __get__(self, inst, owner):

/opt/conda/lib/python3.7/site-packages/fastai/vision/core.py in create(cls, fn, **kwargs)
    123         if isinstance(fn,bytes): fn = io.BytesIO(fn)
    124         if isinstance(fn,Image.Image) and not isinstance(fn,cls): return cls(fn)
--> 125         return cls(load_image(fn, **merge(cls._open_args, kwargs)))
    126 
    127     def show(self, ctx=None, **kwargs):

/opt/conda/lib/python3.7/site-packages/fastai/vision/core.py in load_image(fn, mode)
     96 def load_image(fn, mode=None):
     97     "Open and load a `PIL.Image` and convert to `mode`"
---> 98     im = Image.open(fn)
     99     im.load()
    100     im = im._new(im.im)

/opt/conda/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode, formats)
   2960         exclusive_fp = True
   2961 
-> 2962     prefix = fp.read(16)
   2963 
   2964     preinit()

/opt/conda/lib/python3.7/site-packages/PIL/Image.py in __getattr__(self, name)
    517             )
    518             return self._category
--> 519         raise AttributeError(name)
    520 
    521     @property

AttributeError: read

Since version 2.7.11, you can no longer pass PIL images. You can just add the filename as a string and it will work again.

I commented on the exact same issue here:

1 Like

Hmm. You make a good point. I must have accidentally renamed it. And now I’m not sure where I got the original from. But here’s the workbook I’m having trouble with. How does a neural net really work? | Kaggle

On kaggle, you need to run the install for the libraries you need, not all libraries are included in it by default, and in some cases those libraries are old versions. the -U in !pip install fastai -U will upgrade this kaggle session to use the latest fastai library version.

So make sure you run those initial installs each session…

!pip install timm
!pip install fastai -U

The reason why it seem ok for some fastai imports but not for vision_leaner() in this case was because that function did not exist in the older version that Kaggle loaded (it used to be called cnn_leaner() but the name was changed to better reflect the variety of vision models now that aren’t just CNN’s)

You can check the versions with

from fastai.test_utils import show_install
show_install()
1 Like

Hi team,

I’m trying to setup the system, and I have created an account on Kaggle and done a clone of the github repo, and then created a notebook ok Kaggle and made File → Link to Github. Then tried the first instruction “from fastbook import *”, and it returns me an error:

`---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_27/1480085386.py in
----> 1 from fastbook import *

ModuleNotFoundError: No module named ‘fastbook’`

How can I solve it? I attatch you a screenshot.

Thank you very much.

Josep

You need to install it first. Just use the command below:

!pip install -Uqq fastbook

Hope this helps :slight_smile:

1 Like

Hello! I am trying to get the Kaggle notebook to run for lesson 1: “Is it a bird?” However, I’m getting stuck on the very first cell. It seems like it’s picking up that my internet is not enabled. However, I don’t see an option to enable it!

My account is phone-verified:
Cattura2

Has anyone experienced this before?

Notebook settings are kinda hidden away. You can access it by clicking on the left arrow in right bottom of the screen:

image

Relevant setting is in Notebook options section:

1 Like

Wonderful, bugo, that solved my problem. Thank you very much.

1 Like

After scratching my head for a while over the various cloud notebook options, I wrote this blog post to hopefully help others:

3 Likes

Getting the following error when running the following commands from docs.fast.ai:

“conda install -c fastchan fastai”
or
“conda install -c fastchan fastai anaconda”

It happily chugs along downloading packages until it hits this:
------------- 219.9MB / 552.4MB @ 6.4MB/s cython-blis 29.5ssympy 17.0 B @ ??.?MB/s 0.3s
Multi-download failed. Reason: Transfer finalized, status: 404 [https://conda.anaconda.org/fastchan/win-64/sympy-1.11.1-py310h5588dad_2.tar.bz2] 17 bytes

So I assume that the link for sympy 1.11.1 in fastai ( I’m not very familiar with conda package dependency management structures ) is not an active link. Copy and pasting that link into a browser shows “release not found”.

I next wiped the environment, installed sympy seperately with "mamba install sympy " and then ran “mamba install -c fastchan fastai”

Now conda is throwing an “unexpected error” but doesn’t list the actual error. It only shows the environment variables.

I’ll try the “pytorch > pip install fastai” method next.

I’m posting this just in case anyone else sees or has seen these issues.

EDIT:

To be clear I’m trying to install this on a local environment, not one of the notebook hosting providers.

EDIT 2: edited for clarity.

SOLUTION:

PyTorch does not support python 3.11 so I re-created the environment with 3.9. I still got the sympy error, but this time after installing it separately I was able to install the rest of fastai using:

mamba install -c fastchan fastai -y

1 Like

Hi everyone,
I made my copy of the first lesson (Is it a bird?) on Kaggle, but am stuck on step 1:

from duckduckgo_search import ddg_images
from fastcore.all import *

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    return L(ddg_images(term, max_results=max_images)).itemgot('image')

returns

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_18/1717929076.py in <module>
----> 1 from duckduckgo_search import ddg_images
      2 from fastcore.all import *
      3 
      4 def search_images(term, max_images=30):
      5     print(f"Searching for '{term}'")

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/__init__.py in <module>
      8 
      9 # isort: off
---> 10 from .compat import (
     11     ddg,
     12     ddg_answers,

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py in <module>
      2 import warnings
      3 
----> 4 from .duckduckgo_search import DDGS
      5 
      6 logger = logging.getLogger(__name__)

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in <module>
     11 
     12 import requests
---> 13 from requests.exceptions import HTTPError, JSONDecodeError, Timeout
     14 from requests.models import Response
     15 

ImportError: cannot import name 'JSONDecodeError' from 'requests.exceptions' (/opt/conda/lib/python3.7/site-packages/requests/exceptions.py)

In the next cell it says that I should try again, if I get a JSON error, but even after many tries it seems not to work. I am new to Python, so any hint is appreciated. Thanks!

1 Like

Yeah I am getting same error too
Please help

Me too - the exact same error.

@hewag1975, @Devesh2000, @oliverbogler,
As a diagnostic, select menu Run > Factory Reset, then add the following cell to the top of the notebook and execute it…

!pip install -U requests
import requests 
print("IMPORTED VERSION IS ", requests.__version__)
from requests.exceptions import JSONDecodeError
print("NOW OKAY")

and you might see…

Although you see it reports that version 2.31.0 was successfuly installed in the operating system, it shows an older version 2.26.0 was imported from a cache.

Now go Run > Restart and clear outputs, and run that first cell again. This time you should see…
image

and your original problem should also have been resolved.

The root cause was that history shows JSONDecodeError was not added until version 2.27.0

5 Likes

Thank you - that has indeed removed this error - much appreciated.