Loading an Image Dataset where the Target (or Ground Truth) are also Images

adityashukzy: <p>I’m working on a project with some friends on a computer vision model where we have an image dataset and we transform the images into black and white and keep the target or ground truth as the original color image. In essence, it’s an image colorization model where we learn parameters to colorize black and white images.</p>
<p>I’ve seen several tutorials regarding ImageFolder and DataLoader but the main problem is that although DataLoader loads the images, the Y_train part (that is, the target) are labels. Meaning that the name of the folder in which the dataset is stored (say, for example, ‘/Dogs/’) will be the target label. However, in my model, the final output should also be an image only.</p>
<p>Hence I want to know what I can do to take the image dataset and make a black and white copy of each image to keep as input for the CNN later on and have the original color image as the final target output image.</p>
<p>For reference, we are using the ~500 MB Places2 validation set as our main data.</p>

tom: <p>If you do the transformation to black and white on the fly, you can just do it in your training loop or add it as an additional transform.<br>
That said, it’s very easy and maybe instructive to just implement your own dataset loading pairs of images, too (using the transforms of torchvision). Really, all you need to do is load the images, convert them to tensors and return a pair of them. PyTorch will take care of the rest.</p>
<p>Best regards</p>
<p>Thomas</p>

adityashukzy: <p>Thanks for the response, Tom! I wanted to ask if you could link me to some resources as to:</p>
<p>a) Create your own dataset loading class. I did look up the documentation tutorial but found it a bit unwieldy for me as beginner.<br>
b) A comprehensive (and preferably beginner-friendly) understanding of what exactly the ImageFolder and DataLoader classes do.</p>
<p>I’m sorry if I’m missing something very obvious but I’m really not able to understand their exact purpose, the difference between them, and how and why to use them. Tom, if you could explain this to me as an ML amateur, I would be ever so delighted! And if possible, explaining in terms of my project use-case would probably make it more understandable!</p>
<p>Thanks!</p>

tom: <p>It is really as simple as defining a class (preferable a subclass of Dataset) that has <code>__len__</code> and <code>__getitem__</code>. The len is typically trivial and getitem should return a single record from the dataset for any number 0…len-1. You might look at the <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener">data loading tutorial</a> for inspiration. You can really return anything from the dataset, but so a pair of tensors would probably fit best for your application.<br>
Our <a href="https://pytorch.org/deep-learning-with-pytorch" rel="nofollow noopener">book</a> also has some elementary examples.<br>
The image folder really only does the same and I’d say you’re ready to <a href="https://github.com/pytorch/vision/blob/1b41525443f8955487e5432743d346fa5c351ddf/torchvision/datasets/folder.py#L142" rel="nofollow noopener">just dive into the source</a> after the tutorial.</p>
<p>Best regards</p>
<p>Thomas</p>
<p>PS I might add that a considerable amount of magic happens in the dataloader to get batching, parallel loading from multiple workers etc., but you wouldn’t need to concern yourself with it at this point.</p>