fastai / fastbook

The fastai book, published as Jupyter Notebooks
Other
21.51k stars 8.33k forks source link

TypeError: 'NoneType' object is not iterable #585

Open jez120 opened 1 year ago

jez120 commented 1 year ago

lesson 2, getting this error, how to fix this?

dls = bears.dataloaders(path)


TypeError Traceback (most recent call last) in <cell line: 1>() ----> 1 dls = bears.dataloaders(path)

6 frames /usr/local/lib/python3.10/dist-packages/fastai/data/core.py in setup(self, train_setup) 395 x = f(x) 396 self.types.append(type(x)) --> 397 types = L(t if is_listy(t) else [t] for t in self.types).concat().unique() 398 self.pretty_types = '\n'.join([f' - {t}' for t in types]) 399

TypeError: 'NoneType' object is not iterable

youssefokeil commented 1 year ago

I am getting the same error

essteer commented 7 months ago

I had the same problem, but was able to get around it by changing some of the code to match the image search code in the "Is it s bird? Creating a model from your own data" notebook from Lesson 1.

Couple of notes:

  1. These changes work as of 15 February 2024.
  2. I ran the notebook in Colab - results unknown for Kaggle and Jupyter.
  3. As per the updated Lesson 2 video, we can now use DuckDuckGo instead of Bing, without the need for an API key. The code below is based on DuckDuckGo.

My code up to "Turning Your Model into an Online Application" section is as follows:

#hide
! [ -e /content ] && pip install -Uqq fastbook
import fastbook
fastbook.setup_book()

#hide
from fastbook import *
from fastai.vision.widgets import *
# Add below import (based on Is It A Bird? notebook)
from fastdownload import download_url

# Replaced search_images_bing with DuckDuckGo
search_images_ddg

# Use function definition from "Is it a bird?" notebook
def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    return L(search_images_ddg(term, max_images=max_images))

results = search_images_ddg('grizzly bear')
ims = results.attrgot('contentUrl')
len(ims)

#hide
ims = ['http://3.bp.blogspot.com/-S1scRCkI3vY/UHzV2kucsPI/AAAAAAAAA-k/YQ5UzHEm9Ss/s1600/Grizzly%2BBear%2BWildlife.jpg']

dest = 'images/grizzly.jpg'
download_url(ims[0], dest)

bear_types = 'grizzly','black','teddy'
path = Path('bears')

from time import sleep

for o in bear_types:
    dest = (path/o)
    dest.mkdir(exist_ok=True, parents=True)
    # results = search_images(f'{o} bear')
    download_images(dest, urls=search_images(f'{o} bear'))
    sleep(5)  # Pause between bear_types searches to avoid over-loading server

fns = get_image_files(path)
fns

len(fns)

failed = verify_images(fns)
failed

failed.map(Path.unlink);

bears = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=RandomSplitter(valid_pct=0.2, seed=42),
    get_y=parent_label,
    item_tfms=Resize(128))

dls = bears.dataloaders(path)

dls.valid.show_batch(max_n=4, nrows=1)

bears = bears.new(item_tfms=Resize(128, ResizeMethod.Squish))
dls = bears.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)

bears = bears.new(item_tfms=Resize(128, ResizeMethod.Pad, pad_mode='zeros'))
dls = bears.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)

bears = bears.new(item_tfms=RandomResizedCrop(128, min_scale=0.3))
dls = bears.dataloaders(path)
dls.train.show_batch(max_n=4, nrows=1, unique=True)

bears = bears.new(item_tfms=Resize(128), batch_tfms=aug_transforms(mult=2))
dls = bears.dataloaders(path)
dls.train.show_batch(max_n=8, nrows=2, unique=True)

bears = bears.new(
    item_tfms=RandomResizedCrop(224, min_scale=0.5),
    batch_tfms=aug_transforms())
dls = bears.dataloaders(path)

learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(4)

interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

interp.plot_top_losses(5, nrows=1)

#hide_output
cleaner = ImageClassifierCleaner(learn)
cleaner

#hide
for idx in cleaner.delete(): cleaner.fns[idx].unlink()
for idx,cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]), path/cat)

Hope this helps until a fix is introduced!