Can't Load Test Folder Image File

I use the following code snippet to load train and test data:

  data = (ImageList.from_df(df, path/'train')
       .split_by_rand_pct()
       .label_from_df()
       .add_test_folder(path/'test')
       .transform(data_tfms, size=224)
       .databunch(bs=64))

Please note that the test folder contains image file with extensions(.png and .jpeg). Although the above code snippet works perfectly to load train dataset and train the cnn learner perfectly. But the problem is this code snippet does not load the test dataset properly. When I try to show the length of test data using, len(data.test_ds), it shows only 1. Even though the test folder contains almost thousands of image files.

Is there any problem with the image file extension, as the test folder contains .png and .jpeg files?
Am I missing something to load the test data?

hello @mmiakashs , try removing the path from your variable. It looks like the function already adds the base path.

def add_test_folder(self, test_folder:str='test', label:Any=None):
    "Add test set containing items from `test_folder` and an arbitrary `label`."
    # note: labels will be ignored if available in the test dataset
    items = self.x.__class__.from_folder(self.path/test_folder)
    return self.add_test(items.items, label=label)

Thanks for the reply. I tried this approach, but it didn’t solve my problem :frowning:

Hello @mmiakashs , did you put your test images inside the train directory? You have the path set up as path/‘train’ so when the data loader is looking for test images it will look for ‘path’/train/test .

1 Like

Thanks a lot. I moved my test folder in the train folder, now it works :slight_smile: . However, isn’t there any way to use separate train and test folder?

hello @mmiakashs, you can set your path to the root directory and put your train and test folders there. Then you can specify the folder when you build your data loader.

eg: (ImageList.from_df(df, path, folder=‘train’)

1 Like

Great !!
Now I am facing another problem. I can find any way to retrieve the test dataset image file names. is there any way to retrieve the test dataset file names?

Hello @mmiakashs, each item in test_ds is a tuple with 0 being the image and 1 being the path (including the filename). You can pull the filename from that.

eg: data.train_ds[0][1].name

1 Like