Unable to create ImageDataBunch from folders

(Krishna Kishor Kammaje) #1


I created 5 classes of fruits and uploaded to my GCP based fastai setup as below.
I just have train folder and do not have valid and test folders (which I believe are optional).


But whenever I try to create the ImageDataBunch as below,

sPath = '/home/jupyter/tutorials/fastai/course-v3/data/Fruits' data = ImageDataBunch.from_folder(path=sPath, valid_pct='20', size=224)

I get the error
IndexError: index 0 is out of bounds for axis 1 with size 0

Can anyone help, please?

1 Like

(Krishna Kishor Kammaje) #2

This worked.
data = (ImageItemList.from_folder(sPath).random_split_by_pct().label_from_folder().transform(tfms, size=224).databunch())



Hi krisho007, I have exacty the same problem as yours [IndexError: index 0 is out of bounds for axis 1 with size 0 ] . My data folder structure is similar to yours as well, but I can’t seem to solve it with the method you mentioned using the ImageItemList… can you elaborate more details how you replace from data=ImageDataBunch.from_folder to data=ImageItemList.from_folder? Thanks.


(Constantin) #4

Can you post the output of print(sPath) and print(ImageItemList.from_folder(sPath))?


(Krishna Kishor Kammaje) #5

Your issue is most probably with having the right sPath. If it cannot find any data in the sPath, then you will end up having same error.


(George Zhang) #6

Check out the docs.


I am sure that you can spot the difference between your setup and the correct ImageNet style setup. :wink:



Just realize my error is slightly different than yours, mine was like " IndexError: index 0 is out of bounds for axis 0 with size 0" My error was at axis 0 not axis 1 as yours.

After multiple attempts checking posts and docs, I still couldn’t rectify the error. So I did a silly trial by open up a new notebook, copy all the scripts over line by line, and it run FINE with the ImageDataBunch <@@> ! So basically I still don’t know what causes the ImageDataBunch not running with the original notebook… but since I can continue with the rest of the steps afterward with the new notebook, I proceed as is. Just in case anyone having problem like this, probably the best way is to ‘reset’ it with new notebook :smile: and your day is still :sunny: afterward than crunching the codes again and again :cloud_with_lightning: but still doesn’t work…


(David Albrecht) #8

I found that setting valid_pct to a value fixed the error when I got it. The method seems to be looking for train, validation, and testing folders, but if you have the data split by class in folders then you’ll probably need to tell the method how to split those images.