Imbalanced dataset

gkrisp9: <p>Hi, I try to use a method to improve my models performance on an imbalanced dataset with 5 classes. I found the <code>ImbalancedDatasetSampler</code> from <code>torchsampler</code> but when I try to use it on the dataloader:</p>
<blockquote>
<p>train_loader = DataLoader(train_set, batch_size=32, sampler=ImbalancedDatasetSampler(train_set))</p>
</blockquote>
<h2><a name="i-get-this-attribute-error-1" class="anchor" href="#i-get-this-attribute-error-1"></a>I get this attribute error:</h2>
<p>AttributeError                            Traceback (most recent call last)<br>
 in ()<br>
1 from torchsampler import ImbalancedDatasetSampler<br>
2 <span class="hashtag-raw">#TRAIN</span> LOADER<br>
----&gt; 3 train_loader = DataLoader(train_set, batch_size=32, sampler=ImbalancedDatasetSampler(train_set))<br>
4 <span class="hashtag-raw">#VALID</span> LOADER<br>
5 valid_loader = DataLoader(valid_set, batch_size = 8, shuffle = True)</p>
<p>2 frames<br>
/usr/local/lib/python3.7/dist-packages/torchsampler/imbalanced.py in <strong>init</strong>(self, dataset, indices, num_samples, callback_get_label)<br>
28         # distribution of classes in the dataset<br>
29         df = pd.DataFrame()<br>
—&gt; 30         df[“label”] = self._get_labels(dataset)<br>
31         df.index = self.indices<br>
32         df = df.sort_index()</p>
<p>/usr/local/lib/python3.7/dist-packages/torchsampler/imbalanced.py in _get_labels(self, dataset)<br>
48             return dataset.samples[:][1]<br>
49         elif isinstance(dataset, torch.utils.data.Subset):<br>
—&gt; 50             return dataset.dataset.imgs[:][1]<br>
51         elif isinstance(dataset, torch.utils.data.Dataset):<br>
52             return dataset.get_labels()</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py in <strong>getattr</strong>(self, attribute_name)<br>
81             return function<br>
82         else:<br>
—&gt; 83             raise AttributeError<br>
84<br>
85     <span class="mention">@classmethod</span></p>
<p>AttributeError:</p>
<p>Does anybody know why I get this error?</p>
<p>My custom dataset code to get the images (DICOM files) and the labels is:</p>
<pre><code class="lang-auto">class MyCustomDataset(Dataset):
  def __init__(self, path = "/content/drive/MyDrive/subjects"):
    dataframe = pd.read_csv("/content/drive/MyDrive/KL_grade.csv", sep = ';')
    self.labels = {}
    id = list(dataframe["id"])
    grades = list(dataframe["grade"])
    for i,g in zip(id, grades):
      self.labels[str(i).zfill(5)] = g
    
    self.ids = [a.split("/")[-1] for a in sorted(glob.glob(f"/content/drive/MyDrive/subjects/" + "/*"))]

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

  def __getitem__(self, idx):
    imgs = load_3d_dicom_images(self.ids[idx])
    label = self.labels[self.ids[idx]]

    return torch.tensor(imgs, dtype = torch.float32), torch.tensor(label, dtype = torch.long)
</code></pre>

ptrblck: <p>Based on the error message it seems <code>torchsampler</code> tries to call <code>dataset.dataset.imgs</code>, which apparently isn’t a valid attribute. I’m not familiar with the implementation of <code>torchsampler</code> and also cannot find a repository of it to check what might be failing.</p>

gkrisp9: <p>Hi Peter, thanks for the response. Do you know any techniques I could use to overcome this issue ? I train some models but I have come up with overfitting. The validation loss and accuracy are saturating while training loss decreases and training accuracy gets to over 90%.<br>
I tried to add weights to cross entropy loss and add a sampler when I define the train loader but neither of these methods work.<br>
I am working with 435 DICOM files (160x384x384) and I have 5 classes.</p>

ptrblck: <p>I don’t know what’s causing the error, so would need to see the <code>torchsampler</code> implementation to figure our what might be causing it and how to fix it.<br>
Generally, you could use the PyTorch <code>WeightedRandomSampler</code> as described e.g. <a href="https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264/2">here</a>.</p>