Loading VOC 2012 dataset with Dataloaders

Gaurav_Pandey: <p>VOC 2012 dataset consists of images and their corresponding segmentation maps. I want to apply similar transforms to both the image and its segmentation map while loading. Any suggestions about how to proceed for this task?</p>

apaszke: <p>We don’t have a ready solution implemented, but there has been some discussion <a href="https://github.com/pytorch/vision/issues/9">in a torchvision issue</a>.</p>

ncullen93: <p><a class="mention" href="/u/gaurav_pandey">@Gaurav_Pandey</a>  you can easily adapt a dataset to handle co_transforms in the <code>__call__</code> function (e.g. see this gist which has general structure that handles co_transforms):</p>
<aside class="onebox githubgist">
  <header class="source">
      <a href="https://gist.github.com/ncullen93/14b458cb4bd237bab2a41a185f710808" target="_blank" rel="nofollow noopener">gist.github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://gist.github.com/ncullen93/14b458cb4bd237bab2a41a185f710808" target="_blank" rel="nofollow noopener">https://gist.github.com/ncullen93/14b458cb4bd237bab2a41a185f710808</a></h4>
<h5>co_classes.py</h5>
<pre><code class="Python">"""
Custom datasets from both in-memory and out-of-memory data
"""

import torch.utils.data as data

from PIL import Image
import os
import os.path
</code></pre>
This file has been truncated. <a href="https://gist.github.com/ncullen93/14b458cb4bd237bab2a41a185f710808" target="_blank" rel="nofollow noopener">show original</a>

<p>
</p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>and here are some relevant affine transforms to actually use – you’ll see the transforms must take in two arguments for the input and target images:</p>
<aside class="onebox githubgist">
  <header class="source">
      <a href="https://gist.github.com/ncullen93/425ca642955f73452ebc097b3b46c493" target="_blank" rel="nofollow noopener">gist.github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://gist.github.com/ncullen93/425ca642955f73452ebc097b3b46c493" target="_blank" rel="nofollow noopener">https://gist.github.com/ncullen93/425ca642955f73452ebc097b3b46c493</a></h4>
<h5>affine_transforms.py</h5>
<pre><code class="Python">"""
Affine transforms implemented on torch tensors, and
only requiring one interpolation

Included:
- Affine()
- AffineCompose()
- Rotation()
- Translation()
- Shear()</code></pre>
This file has been truncated. <a href="https://gist.github.com/ncullen93/425ca642955f73452ebc097b3b46c493" target="_blank" rel="nofollow noopener">show original</a>

<p>
</p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


Gaurav_Pandey: <p>Thanks guys. That was very helpful.</p>

bodokaiser: <p>I also implemented a <a href="http://github.com/bodokaiser/piwise" rel="nofollow noopener">dataset</a> for VOC2012.</p>

isalirezag: <p><a class="mention" href="/u/apaszke">@apaszke</a> does pytorch have any plan to add VOC dataset like what it has for coco?</p>

HT_Wang: <p>Now Torchvision has VOC dataset <a href="https://github.com/pytorch/vision/blob/master/torchvision/datasets/voc.py" rel="nofollow noopener">implemented</a>, but it’s not that user friendly. Specifically, it doesn’t allow co-transform (data augmentation such as rotation and scaling) and separate transforms (normalization on images) to exist at the same time (raise an error <a href="https://github.com/pytorch/vision/blob/c4f77c249778b310e0e3f11af5625e3afc24be11/torchvision/datasets/vision.py#L16" rel="nofollow noopener">here</a>). Many good third-party implementations like <a href="https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/dataloaders/datasets/pascal.py" rel="nofollow noopener">this one</a>.</p>