PyTorch output image name

The code below is used as part of my training model to print prediction. I would like to print each image name as well as the prediction. Can anyone advise how to do this please?

for epoch in range(50):
    print("Epoch %d" % epoch)
    for images, paths in tqdm(loader_train):
        images =
        targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
        targets = targets.float().to(device)

        output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
        preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)


Does not paths contain image names already? if yes so, then you can stack column wise then convert it to a matrix.

batch_size = 10
paths = np.array(['names']*batch_size).reshape(-1, 1)
preds = torch.randint(0, 5, (batch_size, )).view(-1, 1)
names_preds = np.hstack((paths, preds.numpy()),)


Yes. paths contain the image names. How can i extract the image name against the prediction please? If I try to call path in this way fd.write( ā€˜,ā€™.join(map(str, paths + ā€˜\nā€™))) i get an error message I think i need to transform it somehow. Many Thanks for your help.

Have you tried my snippet? I tried to simulate your case in exact same structure (dtype, etc).
I think it is easier to first construct a matrix of path, labels of batches then saving/printing them.