State-of-the-art for torchvision datasets/transforms/models design

adamjstewart: <p>Hi, I’m working on developing a library similar to torchvision for geospatial data. Think multi-spectral satellite imagery datasets/transforms/pre-trained models.</p>
<p>When perusing the torchvision source code, I noticed that there are a lot of inconsistencies due to the long history of development and changing needs of users. For example:</p>
<ul>
<li>Datasets return images and targets, or images and masks and classes, or images and dicts with bounding boxes</li>
<li>Datasets accept transform, target_transform, and/or transforms</li>
<li>Transforms support PIL Images and/or torch Tensors</li>
<li>Transforms subclass object, or nn.Module (for TorchScript support)</li>
</ul>
<p>My question is, if you were going to write a library like torchvision from scratch, which of these are state-of-the-art best practices, and which of these are just leftover for backwards compatibility? Is best practice to write all new transforms as a subclass of nn.Module, or is there some advantage to more generic object transforms? Since I’m working with multi-spectral imagery, PIL Images won’t work at all, so I’ll likely write all transforms for torch Tensors. If I want consistency between datasets and transforms for various tasks (object detection, instance segmentation, etc.), should all datasets return (and all transforms accept) dicts with keys for possible components (image, mask, bounding boxes, class labels, etc.)?</p>
<p>I’m also curious what other issues the torchvision developers have faced over the years and how they solved them. Would love to meet over a video call.</p>

edgarriba: <p>As for the transforms, we faced similar issues for n-dimensional arrays and that’s why we started <a href="http://kornia.org" rel="noopener nofollow ugc">kornia.org</a> and subsequently <code>kornia.augmentation</code> submodule. We are now including more advanced augmentations and specialised containers to handle other types of data like masks, key points, etc. See it here: <a href="https://app.reviewnb.com/kornia/tutorials/pull/12/" rel="noopener nofollow ugc">https://app.reviewnb.com/kornia/tutorials/pull/12/</a></p>

fmassa: <p>Hi Adam,</p>
<p>This is a great question. Indeed, torchvision has cumulated a lot of tech debt over time, which is hard to change due to BC considerations, but which we would gladly change otherwise.</p>
<p>We have initiated some discussions around things we would love to change in torchvision if we didn’t have any BC considerations, we will be posting the summary of the discussion on GitHub once it’s fleshed out, but here are some specific points to your questions:</p>
<h2><a name="datasets-1" class="anchor" href="#datasets-1"></a>Datasets</h2>
<p>We are in the process of re-designing all of our datasets so that they are <a href="https://github.com/pytorch/pytorch/tree/master/torch/utils/data/datapipes">built using DataPipes</a>. This will allow them to be used to stream data from both disk but also the internet, like what WebDataset does.</p>
<p>In this process, we will also be making more consistent the return types of datasets – it will probably be a dict everywhere, which contains fields for the <code>image</code> / <code>label</code> / etc (but this is still being fleshed out and <a class="mention" href="/u/pmeier">@pmeier</a> is actively working on the design)</p>
<p>In this context, a transform will not be part of the dataset anymore, but will just be a <code>.map</code> transform on top of the DataPipe.</p>
<h2><a name="transforms-2" class="anchor" href="#transforms-2"></a>Transforms</h2>
<h3><a name="pil-and-tensors-3" class="anchor" href="#pil-and-tensors-3"></a>PIL and Tensors</h3>
<p>We started with PIL because it was widely available in Python and was kind of the standard for image processing. With the push for sharing the same transform implementation between Python and C++, we enabled Tensor Transforms, which also give other benefits such as GPU and autograd.</p>
<p>In my view, the PIL-based transforms are mostly legacy now, but we won’t be deprecating those until we have improved all the gaps that there are still compared to the Tensor Transforms (speed and antialising for <code>resize</code> are two things we are improving in the upcoming release)…</p>
<p>About making the transforms be <code>nn.Module</code> vs plain <code>object</code>, this was to facilitate torchscript support.<br>
That being said, I’m not super happy with the current class API for transforms, as it doesn’t allow for a nice way of enabling joint transforms for boxes / images / etc, so a class-based API should be rethink. For now only the functional API for the transforms is powerful enough.</p>
<p>I think it would be interesting to consider that the concept of a “model” can involve everything from data decoding up to the final model output, which means that transforms could be part of the model (and thus are analogous to <code>nn.Module</code> such as <code>nn.Conv2d</code>). The current difficulty about this is how to make it efficient, without also making the dataloader part of the model.</p>
<h3><a name="for-a-few-more-specific-questions-4" class="anchor" href="#for-a-few-more-specific-questions-4"></a>For a few more specific questions:</h3>
<blockquote>
<p>should all datasets return (and all transforms accept) dicts with keys for possible components</p>
</blockquote>
<p>Yes, I think that’s what we would be doing for the new dataset implementations. For the transforms, I don’t yet know, but a redesign would be good.</p>
<p>Happy to chat over a video call, ping me on slack and we can continue the discussion.</p>

shijianjian: <p>Hi Adam,</p>
<p>Whilst working on deep learning tasks, I would much prefer to decouple those three components as much as possible. So, for each of those tasks:</p>
<ul>
<li>Datasets, to read images and labels and convert to the proper dtype only.</li>
<li>Transforms, to perform transformation, but with optimized batch transformation features. Also, it should be painless to transform common labels (e.g. mask, bounding box, keypoints).</li>
<li>Models, to take in images and output the prediction, and things like normalization shall happen inside models than in augmentations in order to be more “end-to-end”.</li>
</ul>
<p><a href="https://kornia.readthedocs.io/en/latest/augmentation.html" rel="noopener nofollow ugc">Kornia augmentation</a> offered solutions for the better decoupling.</p>
<h2><a name="transforms-1" class="anchor" href="#transforms-1"></a>Transforms</h2>
<p>Firstly, batch processing is the basis of kornia transformation, which takes the advantage of PyTorch itself (tensor-based computation, nn.Module implementation). Recently, we introduced <a href="https://kornia.readthedocs.io/en/latest/augmentation.container.html#augmentation-sequential" rel="noopener nofollow ugc">AugmentationSequential</a> to offer syntax sugars for image augmentations. For example:</p>
<pre data-code-wrap="python"><code class="lang-python">aug_list = AugmentationSequential(
    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0),
    K.RandomAffine(360, [0.1, 0.1], [0.7, 1.2], [30., 50.], p=1.0),
    K.RandomPerspective(0.5, p=1.0),
    data_keys=["input", "bbox", "keypoints", "mask"],  # Just to define the future input here.
    return_transform=False,
    same_on_batch=False,
)
# forward the operation
out_tensors = aug_list(img_tensor, bbox, keypoints, mask)
# Inverse the operation
out_tensor_inv = aug_list.inverse(*out_tensor)
</code></pre>
<p>Then the results are like following (left-&gt;right: original, transformed, inversed back):<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/4/24bb0f4520f547d3a321440293c1d44921ecadf8.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/24bb0f4520f547d3a321440293c1d44921ecadf8" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24bb0f4520f547d3a321440293c1d44921ecadf8_2_690x119.jpeg" alt="image" data-base62-sha1="5eVXFpgnc2c4i5kpyI6UIqHCHJC" width="690" height="119" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24bb0f4520f547d3a321440293c1d44921ecadf8_2_690x119.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24bb0f4520f547d3a321440293c1d44921ecadf8_2_1035x178.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24bb0f4520f547d3a321440293c1d44921ecadf8_2_1380x238.jpeg 2x" data-dominant-color="878A85"><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">image</span><span class="informations">2370×410 209 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>
<h2><a name="datasets-and-models-2" class="anchor" href="#datasets-and-models-2"></a>Datasets and Models</h2>
<p>As you might aware that I have skipped the datasets part in the first <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20">, since we have just introduced batch augmentation. Unlike the traditional augmentation strategy (to include transformations in Datasets implementation), we would recommend to put the image augmentation computations into the next stage. For example:</p>
<pre data-code-wrap="python"><code class="lang-python"># Define the augmentation
augment = AugmentationSequential(
    K.ColorJitter(0.1, 0.1, 0.1, 0.1, p=1.0),
    K.RandomAffine(360, [0.1, 0.1], [0.7, 1.2], [30., 50.], p=1.0),
    data_keys=["input", "bbox"]
)
# Define the model
model = torchvision.models.resnet50()
# Define a more "end-to-end" model
model = nn.Sequential(
    K.Normalize([0.5, 0.5, 0.5], [0.6, 0.6, 0.6]),
    model,
)
</code></pre>
<p>As a result, this adaptation can be very convenience for lots of daily routines. Say:</p>
<pre data-code-wrap="python"><code class="lang-python"># For training ...
out_tensors = augment(img_tensor, bbox)
model(*out_tensors)
# For testing ...
model(img_tensor)
</code></pre>
<p>Save and load without thinking about preprocessing:</p>
<pre data-code-wrap="python"><code class="lang-python">model.save("model.pt")
model = torch.load("model.pt")
# No need to think about normalize, since it has been done already in the saved model.
model(img_tensor)
</code></pre>
<p>By far, I think Kornia augmentation has resolved most issues you are facing. Let us know if anything!</p>

adamjstewart: <p>Thanks, this is great! I just submitted an application for the PyTorch Slack. Will contact you to set up a meeting with myself and some other folks at Microsoft Research who are working with me on this.</p>
<p>I can’t say I’m completely familiar with DataPipes, but I’ll take a look at that. It sounds like we’re in agreement that Datasets should return dicts and that transforms should accept Tensors (especially for my multi-spectral imagery application). <a class="mention" href="/u/pmeier">@pmeier</a> I would be very interested to know your thoughts on standard dict key names for this.</p>

adamjstewart: <p>I’ve never heard of Kornia, this is really interesting. I especially like the ability to invert augmentation sequences. The ability to save augmentation as part of the model is also interesting, and further emphasizes the need to subclass nn.Module so that these models/transforms can be exported to ONNX. I’ll keep this in mind!</p>

edgarriba: <p>The augmentation module in kornia it’s just an extension of the package. Our main focus is low level computer vision more for industrial and robotics applications in terms of differentiable tensors. We inherit a lot from OpenCV (and soon more) and in the long term we also target to express vision algorithms in a “symbolic” way as you mention with technologies like onnx or similar.</p>

pmeier: <p><a class="mention" href="/u/adamjstewart">@adamjstewart</a></p>
<blockquote>
<p>I would be very interested to know your thoughts on standard dict key names for this.</p>
</blockquote>
<p>There is currently no consensus on that. We have some ideas though. Maybe I can join the call and we can discuss this there a little.</p>

adamjstewart: <p>For anyone following this discussion, TorchGeo has now been made public: <a href="https://github.com/microsoft/torchgeo" class="inline-onebox" rel="noopener nofollow ugc">GitHub - microsoft/torchgeo: TorchGeo: datasets, transforms, and models for geospatial data</a></p>
<p>We’re hoping to put out a release sometime in the 1 month timeframe. So far things are mostly in the old style (Dataset instead of DataPipes), but we’re less concerned about backwards compatibility so we can always change this at a later date.</p>