Help with exporting the prediction with respective filenames to CSV

I have fine-tuned and fitted a model and would like to run this on the test dataset and write out the predictions to a CSV file so I can submit. However, I cannot figure out a way to get a prediction with it’s respective filename. I have the piece of code below. The issue is that the batches.filenames contains all the files’ names not just the one that are this “batch”. Any suggestions would be helpful.

def writeToCSVWithModel(vgg, path, batch_size):
    with open(path+'results.csv', 'w') as outFile:
        fieldnames = ['id', 'label']
        writer = csv.DictWriter(outFile, fieldnames=fieldnames)
        batches = vgg.get_batches(path, batch_size=batch_size)
        i = 0
        while next(batches):
            if i>111: break
            i = i+1
            imgs, labels = next(batches)
            predictions = vgg.predict(imgs)
            filenames = batches.filenames

            for j in range(len(predictions)):
                imageId = os.path.splitext(os.path.basename(filenames[j]))[0]
                prediction = predictions[1][j] 
                print(imageId, prediction, sep=', ', end='\n')
                writer.writerow({'id': imageId, 'label': prediction})

For anyone else who comes here… watch session 2.

Any hints on when in the lesson we should watch? It’s 2 hours long and I remember the prof saying something about the end of it containing how to work with kaggle contests, but I haven’t figured it out so far.

Near the start. He shows how he does things himself. Or you can look at the notebook.