Stacing Dataloaders and Datasets

Zador: <p>Hi,</p>
<p>In the image below I have described a process that I want to construct.</p>
<p>The environment: I have n scenes and in each scene I have m snapshots.</p>
<p>My goal: For each scene, a dataloader should sample B batch size number of snapshots. These samples are then to be appropriately processed and entered into the red pool in the form of a pytorch dataset. After this, the base dataloader can take A batch size number of scenes from the n number of scenes (processed snapshots from corresponding scenes).</p>
<p>So what I need is nested dataloaders inside of dataloaders.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/417d52e0454c815f47a11496c32d26e7573c1f34.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/417d52e0454c815f47a11496c32d26e7573c1f34" title="253137264_1284573162013970_91931277473775382_n"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/417d52e0454c815f47a11496c32d26e7573c1f34_2_690x434.jpeg" alt="253137264_1284573162013970_91931277473775382_n" data-base62-sha1="9llxvsw4rc2jSzhMSNcdb4yBKYY" width="690" height="434" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/417d52e0454c815f47a11496c32d26e7573c1f34_2_690x434.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/417d52e0454c815f47a11496c32d26e7573c1f34_2_1035x651.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/417d52e0454c815f47a11496c32d26e7573c1f34_2_1380x868.jpeg 2x" data-dominant-color="F0F3ED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">253137264_1284573162013970_91931277473775382_n</span><span class="informations">1729×1089 163 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>For the case where I simply take the entire m number of snapshots from a given scene (i.e. I do not need the nested DataLoader) I have written the following code:</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    def __init__(self, data_dir):
        self.data_dir = data_dir
        self.data_files = os.listdir(self.data_dir)
        self.data_files.sort()

    def __getitem__(self, idx):
        return self.load_file(self.data_files[idx])

    def __len__(self):

        return len(self.data_files)

    def load_file(self, hdf5_file):

        f = File(self.data_dir+'/'+hdf5_file, "r")
        inputs, target = some_irrelevant_function_for_processing_data(f.get('...'))
        

        return inputs, target

if __name__ == '__main__':
    processed_path = 'some/path'
    dataset = MyDataset(data_dir=processed_path)
    loader = DataLoader(dataset, num_workers=8, batch_size=1)
    for inputs, target in loader:
        print(inputs.shape, target.shape)
</code></pre>
<p>Finally what I would like is to efficiently leverage workers across the DataLoading framework.</p>
<p>I have not been able to find relevant posts about this. If you can link me relevant posts or give me advice on how to set this up that would be great!</p>
<p>Thanks in advance.</p>

my3bikaht: <p>I think you’ve pretty much described it already. Use outer dataloader for snapshots, create new instance of inner dataloader inside the loop and use it to work with scenes.</p>