PyTorch print image name - AttributeError: 'Tensor' object has no attribute 'files'

Sorry, if this question is very basic. I’m trying to print the image names at each iterations to a csv file whilst training the model. but I am getting the following error message:

filename=output.files AttributeError: ‘Tensor’ object has no attribute ‘files’

# main training loop
global_step = 0
best_test_error = 10000
#filename=CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True)
for epoch in range(50):
    print("Epoch %d" % epoch)
    model.train()
    for images, paths in tqdm(loader_train):
        images = images.to(device)
        targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
        targets = targets.float().to(device)
        filename=CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True)
        #print(filename.files)

        output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
        #print(output.filename)
        preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)
        filename=output.files
        print(preds)
        with open('preds_base_model.csv','a') as fd:
            fd.write( ','.join(map(str, preds.detach().tolist())) + '\n')
            fd.write( ','.join(map(str, targets.detach().tolist())) + '\n')
            fd.write(( ','.join(map(str, filename.detach().tolist())) + '\n')

You started with filename=CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True), which I assume returns the names of the files you’re after, but then you try to assigned filename to be output.files, which fails because the output doesn’t have .files attribute.
Maybe try commenting this out and keep the first filename variable?

If i try to to do:

print(filename)

I get: <dataset2.CellsDataset object at 0x7f8a8315a190>