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)
writer.writeheader()
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})