I have been working with a CNN in PyTorch and I need to save the image path and its associated predicted probabilities for each class (in this case the classes are pass or fail). This is my code to save the preds to a data frame:
preds_df = pd.DataFrame()
class_labels = []
model_ft.eval()
for i, (inputs, labels) in enumerate(dataloaders['train']):
inputs = inputs.to(device)
labels = labels.to(device)
class_labels.append(labels.tolist())
output = model_ft(inputs)
sm = torch.nn.Softmax()
probabilities = sm(output)
arr = probabilities.data.cpu().numpy()
df = pd.DataFrame(arr)
preds_df = preds_df.append(df)
preds_df['prediction'] = preds_df.idxmax(axis=1)
class_list = [item for sublist in class_labels for item in sublist]
preds_df['label'] = class_list
preds_df.columns = ['pass (0)', 'fail (1)', 'prediction', 'label']
preds_df.to_csv('./zoom17CNN_preds.csv')
How can I save the image path as well for each file in the data loader? Thank you!