Original filenames from test directory predicting with is_test true?

Hi, how do I get the original filenames for a test folder? Created a ImageDataBunch like so passing in a folder ‘test’:

data = ImageDataBunch.from_df(ROOT_PATH, trn_csv,'train_png', ds_tfms=get_transforms(), size=224, suffix='.png', sep=" ", test='test')

# created a learn from ConvLearner
fbeta = Fbeta()
learn = ConvLearner(data, models.resnet34, metrics=fbeta)

# ran learn fit
learn.fit ...

# ran learn.get_preds with is_test true
pred_test, y_test = learn.get_preds(is_test=True)

# how do I get the original filenames for the predictions?

Now I’m preparing a CSV to submit results to Kaggle. How do I get the original filenames from the test directory?

Update

Seems that it’s in the data object

# to get original filenames
data.test_dl.dataset.ds.x

Update to last version and then you can access through data.test_ds.x

3 Likes

Thanks!

1 Like

I had to go a bit deeper because I didn’t see test_ds, so I had to use the following:

data.test_dl.dl.dataset.x

Maybe I’m not on the latest version though.

1 Like

Thanks @joshfp
After updating fastai I can do:

data.test_ds.ds.x[0].parts[-1]

to get the filename of the 1st test file.

For more details how to deal with PosixPath : https://docs.python.org/3/library/pathlib.html

By the way, after updating fastai, you should note that ConvLearner has been replaced with create_cnn
source:

2 Likes

Is this still working for anyone with 1.0.39?

When I enter

data.test_ds.x
OR
data.test_ds.x

I get the images, rather than the filenames.

Thanks!

Edit: OK nevermind this is answered here:

Need to use data.test_ds.items instead of data.test_ds.x

5 Likes

Thank you very much! data.valid_ds.items[0] - still works (my version of fastai is 1.0.60).