How to get the filenames of misclassified images in the test data set?

CSstudent: <p>I am trying to use AlexNet to classify spectrogram images generated for 3s audio segments. I have successfully trained my dataset and am trying to identify which images the model misclassified.</p>
<p>I am able to obtain the filename of a image by calling iterator.dataset.data_df.adressfname, however I am unsure how to use this statement in the for loop in the get_predictions function. If I try to retrieve the filenames using <strong>iterator.dataset.data_df.adressfname[i]</strong>, I get the following error: <em><strong>expected Tensor as element 0 in argument 0, but got str</strong></em></p>
<p>Ultimately, I want to create a dataframe that contains the filename, actual label and predicted label. Does anyone have any suggestions?</p>
<pre><code class="lang-auto">class CustomDataset(Dataset):
  def __init__(self, img_path, csv_file, transforms):
    self.imgs_path = img_path
    self.csv_train_file = csv_file
    file_list = glob.glob(self.imgs_path + "*")
    self.data = []
    self.data_df = pd.read_csv(self.csv_train_file)
    self.transforms = transforms

    for ind in self.data_df.index:
      img_path = self.data_df['spectrogramSegFilename'][ind]
      class_name = self.data_df['dx'][ind]
      self.data.append([img_path, class_name])
    self.class_map = {"ProbableAD" : 0, "Control": 1}
    self.img_dim = (256, 256)

  def __len__(self):
    return len(self.data)

  def __getitem__(self, idx):
    img_path, class_name = self.data[idx]
    img = cv2.imread(img_path)
    img = cv2.resize(img, self.img_dim)
    class_id = self.class_map[class_name]
    img_tensor = torch.from_numpy(img)
    img_tensor = img_tensor.permute(2, 0, 1)
    data = self.transforms(img_tensor)
    class_id = torch.tensor([class_id])
    return data, class_id

if __name__ == "__main__":
    transformations = transforms.Compose([transforms.ToPILImage(), transforms.Resize(256), transforms.CenterCrop(256), transforms.ToTensor(), transforms.Normalize((0.49966475, 0.1840554, 0.34930056), (0.35317238, 0.17343724, 0.1894943))])
    train_dataset = CustomDataset("/spectrogram_images/spectrogram_train/", "train_features_segmented.csv", transformations)
    test_dataset = CustomDataset("/spectrogram_images/spectrogram_test/", "test_features_segmented.csv", transformations)
    train_dataset = CustomDataset("spectrogram_images/spectrogram_train/", "/train_features_segmented.csv")
    test_dataset = CustomDataset("spectrogram_images/spectrogram_test/", "/test_features_segmented.csv")
    train_data_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
    test_data_loader = DataLoader(test_dataset, batch_size=64, shuffle=True)


def get_predictions(model, iterator, device):

    model.eval()

    images = []
    labels = []
    probs = []
    participant_ids = []

    with torch.no_grad():

      for i, (x, y) in enumerate(iterator): 

            x = x.to(device)
            y_pred = model(x)
            y_prob = F.softmax(y_pred, dim=-1)
            participant_ids.append(iterator.dataset.data_df.adressfname[i])
            images.append(x.cpu())
            labels.append(y.cpu())
            probs.append(y_prob.cpu())
            
    images = torch.cat(images, dim=0)
    labels = torch.cat(labels, dim=0)
    probs = torch.cat(probs, dim=0)
    participant_ids = torch.cat(participant_ids, dim=0)

    return images, labels, probs, participant_ids
</code></pre>

ptrblck: <p>I don’t know what kind of object <code>iterator.dataset.data_df.adressfname</code> is and why the error is raised, but would it work to return the file name in the <code>__getitem__</code> additionally to the <code>data</code> and <code>class_id</code>?<br>
This would allow you to store or process this file name directly after the corresponding sample was misclassifed.</p>