Why are transform in dataset?

Chong_Toby: <p>Hello, I’m currently writing a dataset for some images.<br>
One question arises when I try to create my own  transform, why is it implemented in dataset?<br>
Wouldn’t it be more beneficial to put it inside the network so we can have different transforms for different networks (for some networks we need normalization and some we don’t, or different kind of normalization)?</p>
<p>Just curious about this design choice.<br>
Thank you!</p>

JuanFMontesinos: <p>I don’t really understand the question. Transforms are applied over original data usually to reduce the dimensionality.<br>
It’s not the same to have a workflow of full HD images which are 1080 p than having 120p images. The amount of data, working time etcetera you need is totally different.</p>
<p>Transforms are also used to normalize data or to create data augmentation (by fliping images for example).</p>
<p>But the most important part is that these transformations are non-differentiable operations, therefore they can never be part of a NN since you wouldn’t be able to backpropagate error through them.</p>
<p>In fact, activation functions are “kind of” non linear transformations but they are differentiable</p>

Chong_Toby: <p>Sorry for not giving enough context.<br>
This is the problem for using multiple pretrained models, where your data transformation depends on the datasets that you trained the original networks on, not the current dataset that you use.</p>
<p>Right now, I am trying to put the same data through 2 networks trained with different dataset (hence with different normalization) and this become a problem that would be solved if we don’t put the transformation inside the dataset.</p>
<p>But I can see how non-differentiable can be a huge issue here, and my use case probably isn’t the most popular one.</p>
<p>Thank you.</p>

JuanFMontesinos: <p>Well even if that’s the case, those operations takes usually lot of time. I was using a dataset in which i choose random samples to create grountruth in a unsupervised way. Forward-backward takes 3 s to be done. This preprocess time of picking 2 samples and making gt was taking other 3 sec. In the end preprocess that way was very time consuming for doing it online and i had to fix it beforehand.</p>
<p>What i mean is that all those operations are very time consuming and they slow down the training process. That’s the main reason why it’s not implemented inside the dataset.</p>
<p>In addition, you are thinking in very simple cases, like feeding a net with images which just need few operations like cropping or rearranging, but there are much more complex nets in which you can’t generalize those transformations or which are computationally very expensive</p>

mariosfourn: <p>I agree with <a class="mention" href="/u/chong_toby">@Chong_Toby</a>. I believe that it would be a much better design decision to put transforms as part of the <code>torch.utils.data.DadaLoader()</code>  class. Here’s my experience with this.</p>
<p>It is very common to apply different data augmentation to training and test sets (see Resnet, VGG and etc). Now I have a set of images with some labels and create my own Dataset class. For the sake of modularity and flexibility I would much rather use the <code>torch.utils.data.random_split()</code> class to split my dataset into training and validation than manually split the dataset.</p>
<p>However, this means that I cannot apply different transformations to the split datasets because the transforms are applied to the original combined dataset. However, if they were applied as part of the <code>DataLoader</code> it would fix this issue.</p>

Noam_Salomonski: <p>Bumping this, as I also wonder why transforms are not at the data loader level</p>
<p>As I conceive it, dataset should reflect original data, whereas data-loader reflects, well, loaded data just before training.</p>
<p>How is the same original data intended to be used with different augmentations?</p>

ptrblck: <p>I think the experimental <code>TorchData</code> pipelines might be interesting for you as they might provide more flexibility for your use cases. Tutorials can be found <a href="https://pytorch.org/data/main/tutorial.html">here</a> and any feedback on this API is more than welcome.</p>