How to add test data into ImageDataBunch.from_df and make predictions?

You’ll want to make a seperate databunch for just it, and pass in the ImageList to the validation when you want to run analysis via learn.validate(). The reason is the test sets in fastai are unlabeled. So here we can make a labeled test set to work with. An example using tabular is shown below:

data = (TabularList.from_df(train_set, path=Path(''), cat_names=cat_var, 
                            cont_names=cont_var, procs=procs)
       .split_by_rand_pct(0.2)
       .label_from_df(dep_var, classes=classes)
       .databunch(bs=5000))

data_test = (TabularList.from_df(test, path=Path(''), cat_names=cat_var, 
                            cont_names=cont_var, procs=procs, processor=data.processor)
       .split_none()
       .label_from_df(dep_var, classes=classes)
       .databunch(bs=5000))

Notice here that I make a separate databunch, classes is to ensure they have the same classes, and the processor is to make sure they align correctly transformation wise. If you need it I can quickly write one for an ImageList in a moment but see if you can’t work it out yourself first. Also notice the split_none() on the test set databunch.

Then when you are ready to use it and validate, you can do the following:

learn.data.valid_dl = data_test.train_dl
learn.validate()

Good luck!

3 Likes