Lesson 2: 'NoneType' object is not iterable

when entering the below code,
dls = bears.dataloaders(path)

I receive the error message: ‘NoneType’ object is not iterable. What should I do?

Hi Sarrah,

I hesitate to offer advice as I’m very new to DL/coding! I had a similar issue which was caused by my path object not set-up correctly (backslash v forward slash).

Hopefully that helps?

1 Like

Hi Troy, thanks so much for replying! Totally new to DL & coding too so I really appreciate the help. What path object are you referring to? Is it in the code snippet attached above or somewhere on top?

I tried adding a backslash here but ended up with an error.

I didn’t use that code as had issues with Azure keys so used duckduckgo image downloader which dropped images into a file structure directly. Probably means I’m likely to mislead you!

It should be forward slash so sorry for distracting you.
Is your path object unchanged:
bear_types = ‘grizzly’,‘black’,‘teddy’
path = Path(‘bears’)

The error message here is not exactly obvious. After some debugging, I realised it comes from having 0 (zero) items in the source, maybe because the path is not correct.

You can inspect if this is the case by passing the verbose=True parameter:

dls = bears.dataloaders(path, verbose=True)
3 Likes

I`m having the same issue. On further inspection it seemed that that the following block didn’t download any pictures (it just created the folders). So I assume that caused the bug in the code. (iterating over an empty list?)
Question now is, how to make it download the bear pictures in the first place?

‘’’
if not path.exists():
path.mkdir()
for o in bear_types:
dest = (path/o)
dest.mkdir(exist_ok=True)
results = search_images_bing(key, f’{o} bear’)
download_images(dest, urls=results.attrgot(‘contentUrl’))
‘’’

Yes, Oliver. I have the same issue and found that the images were not downloaded in the first place. That is, ‘fns’ is empty.
So, the lecture was filmed last year and MS might have changed how they provided free services (I had a hard time finding the API key but managed to do it reading other replies down in the thread). In short, images are not downloaded when you iterate three bears instantly using the ‘for’ loop. It seems to cause an overload issue for a free account.
Instead, we should put codes one by one in each cell.

dest=(path/‘grizzly’)

dest.mkdir(exist_ok=True)

results = search_images_bing(key, ‘grizzly bear’)

download_images(dest, urls=results.attrgot(‘contentUrl’))

then in the next cell

dest=(path/‘black’)

dest.mkdir(exist_ok=True)

results = search_images_bing(key, ‘black bear’)

download_images(dest, urls=results.attrgot(‘contentUrl’))

In the last cell,

dest=(path/‘teddy’)

dest.mkdir(exist_ok=True)

results = search_images_bing(key, ‘teddy bear’)

download_images(dest, urls=results.attrgot(‘contentUrl’))

You see it will take some time running each cell, which means the code is actually working.
Hope this helps.

Hi,

It seems the problem was solved by upgrading to the latest version of FastAI, 2.5.2 , as there was a bug in the download_images() function in 2.5.0.

And indeed the code as in Chapter 2 is now running well and downloading the actual pictures

Wow. Has Jeremy looked into this and updated fast.ai? The original code did not work last night, but now does. I did not check the version last night, but the current one is 2.5.2 as you mention, and it works well now. Thanks!

Thats great!
No, fastai version 2.5.2 worked already. I was running a server on Jarvislabs.ai and they updated now to the lastest version of Fastai.
Good luck!

Having it inside the block is intentional. That way, once you’ve downloaded your images, re-running the cell won’t re-download them.

1 Like

Phillippe, I don’t know why you were having problems, but the Kaggle notebook linked from the current course works correctly and uses DDG.

Blaming me for spending hours debugging your problems in using the work I’ve provided for you for free isn’t cool, at all.

Hey Philippe, I don’t know why you’ve deleted your comment but I ran into the same issue with the ‘NoneType’ error.

I struggled a bit to stitch this notebook and make the code work.

Check the my notebook. Bear Detector Notebook

This is a bit of an old post but i did run into this on the duck duck download portion of the code, in my case there was a .fastai hidden folder created on my mac that the path was accessing to store the tar file, i deleted the contents, restarted my notebook and reran the command and it was able to get past this.

Hi all! Just started following the lectures and just in case anyone is running into this issue I was able to solve it by changing the code block to the following

if not path.exists():
    path.mkdir()
for o in bear_types:
    dest = (path/o)
    dest.mkdir(exist_ok=True)
    results = search_images_ddg(f'{o} bear')
    download_images(dest, urls=results)

ddg results return the image urls directly instead of bing so there’s no reason to look up for the contentUrl attribute in each result
This assumes you’re using ddg results instead of bing ones

Hello Everyone,

I’ve been trying to use Fastai package through Azure Databricks, the directory where my set of images is located is in the Azure Datalake, so I am using the piece of code below:

basePath = "datalakeaccount/Federado/ArquitecturaDigital/JF/VisionComputador/MuestraImagenesSiO2/"

In such folder I do have this structure:

item_tfms = [Resize((800), method='squish')]

dls = ImageDataLoaders.from_folder(basePath, train = "Train", valid = "Val", size = 800, bs = 12, item_tfms=item_tfms, verbose=True)

but I am getting this bug:

Any help will be appreciate

In the screenshot, the path is set to adlPath, make sure that it’s the correct path and that it exists because as you have mentioned the path is stored in the basePath variable.

I encountered the same problem and solve it in the beginning with tofu’s solution.
But having the for loop outside the if block downloads again the pictures at each run.
And Jeremy didn’t want that, as he mentions above.
So I moved the for loop inside the if block and this time it worked without error and as intended.