Creating training loops for data augmentation

amy2: <p>I want to apply data augmentation to my dataset " faceLandmarks dataset " in a way that I run multiple training loops. One based on dataset without transform, another one based on dataset with transform  and after that for every sample I get extra images. this is what I tried</p>
<blockquote>
<p>class FaceLandmarksDataset(Dataset):</p>
</blockquote>
<pre><code>   def __init__(self, csv_file, root_dir, transform=None):
             self.landmarks_frame = pd.read_csv(csv_file)
             self.root_dir = root_dir
            self.transform = transform
   def __len__(self):
      return len(self.landmarks_framee

   def __getitem__(self, idx):
      if torch.is_tensor(idx):
      idx = idx.tolist()
    
      self.transform = transforms.Compose([Rescale,
        RandomCrop,
        ToTensor()])
     img_name = os.path.join(self.root_dir,
                        self.landmarks_frame.iloc[idx, 0])

     image = io.imread(img_name)
    landmarks = self.landmarks_frame.iloc[idx, 1:]
    landmarks = np.array([landmarks], dtype=float).reshape(-1, 2)
    #start adding th loop to have tha transformed version 
        # Assign a label indicating 'none' augmentation by default
    aug= 'none'

        # Apply transformation if it exists and is a list with augmentation labels
   if self.transform:
      if isinstance(self.transform, list):
         aug = self.transform[idx % len(self.transform)]

   sample = {'image': image, 'landmarks': landmarks, 'aug': aug}

   if self.transform and aug != 'none':
      sample = self.transform(sample)
      sample = {'image': image, 'landmarks': landmarks}

return sample

 # dataloader for the dataset ---------------------------------
transformed_dataset =
FaceLandmarksDataset(csv_file='/faces_dataset/faces/faces/face_la 
              ndmarks.csv',root_dir='C/faces_dataset/faces/faces/')
                                  
 dataloader = DataLoader(transformed_dataset, batch_size=4,
                shuffle=True, num_workers=0)
</code></pre>
<p>then I got confused how can I call both the transformed and original sample in the dataloader</p>

ptrblck: <p>You could create separate <code>Dataset</code> instances with and without a transformation being passed to their <code>__init__</code> method. To do so fix the recreation of the transformation in the <code>__getitem__</code>:</p>
<pre><code class="lang-python">      self.transform = transforms.Compose([Rescale,
        RandomCrop,
        ToTensor()])
</code></pre>
<p>and use the <code>self.transform</code> directly.</p>

amy2: <p>so if I undertand what your saying right I should make</p>
<blockquote>
<p>class FaceLandmarksDataset(Dataset):</p>
</blockquote>
<p>and another one</p>
<blockquote>
<p>class AugmentedFaceLandmarksDataset(Dataset):</p>
</blockquote>
<p>with the corresponding <code> _init</code>_ <code>_getitem_ </code>and <code>_len_</code> functions<br>
and concatenate both for the dataloader <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

ptrblck: <p>No, you should fix the <code>self.transform</code> usage in your current <code>FaceLandmarksDataset</code> and just reuse the attribute initialized in the <code>__init__</code> and create two datasets:</p>
<pre><code class="lang-python">raw_dataset = FaceLandmarkDataset(csv_file, root_dir, transform=transforms.ToTensor())
transformed_dataset = FaceLandmarkDataset(csv_file, root_dir, transform=transforms.Compose([transforms.Rescale(...), transforms.RandomCrop(...), transforms.ToTensor()]))
</code></pre>