Classification with trained model: Dataset __gettitem__ override?

username: chilango
cooked: <p>I have a trained model that I want to use it to classify images. These images, since unclassified, obviously do not follow the format required by Imageloader/Dataset. One <a href="https://github.com/pytorch/examples/issues/236" rel="nofollow noopener">hack</a> is to simulate the nested structure required and move all images to a single subfolder, but this doesn’t work for large datasets. Others suggest a <a href="https://discuss.pytorch.org/t/image-folder-with-no-subfolders/27930/17">custom dataset</a> but I’m unclear how exact to override the <a href="https://pytorch.org/docs/stable/_modules/torch/utils/data/dataset.html#Dataset" rel="nofollow noopener">Dataset</a> class with my own <code>__getitem__</code> method to achieve this.</p>
<p>Since this is such a standard thing (in production, for testing, etc.) I’m surprised there is no straightforward way, or I’m likely just not finding it.</p>
<p>Any pointers appreciated. Thanks.</p>

username: ptrblck
cooked: <p><a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">This tutorial</a> should give you a simple example how to write a custom <code>Dataset</code>.<br>
The <code>__getitem__</code> method will accept an <code>index</code> in the standard use case, which is used to load, process, and return the sample (and target if possible).<br>
You can use other libraries such as <code>PIL</code> to load the image and apply any preprocessing on this sample you like.<br>
The <code>DataLoader</code> will then create a batch of all returned samples for the current indices.</p>

username: chilango
cooked: <p>Thanks, just out of curiosity, I’m curious to hear if you have any opinion about why this is not an out-of-the-box feature? Granted, it’s not difficult to implement but isn’t this a basic feature for a framework like this? Could it be the more academic “bias” (as opposed to industry like TF) of pytorch? Cheers.</p>

username: ptrblck
cooked: <p>I’m not sure which feature you would like to see.<br>
Would you like to use a <code>ImageFolder</code>-like dataset, where you could specify the targets manually (or reassign them somehow)?<br>
Or do you think the custom <code>Dataset</code> implementation is too complicated?</p>

username: chilango
cooked: <p>If I have a trained model and want to use it in production to classify new, unlabeled images in some directory, right now I cannot use an out of the box utility to load them for transformation.</p>
<p>As the API works right now, a user needs to either a) move all the images to a new subdirectory to conform to the nested dir structure expected by <code>ImageFolder</code>  or b) write some custom function as in the tutorial you provide.</p>
<p>If you see <a href="https://github.com/pytorch/examples/issues/236" rel="nofollow noopener">my github example above</a> you will see what I mean. The issue was opened in October 2017, and the last response of a user with similar needs was April 15, 2020. Clearly a subset of users need this basic functionality.  Moving millions of images every day from the root dir to a subdir to have the nested structure is a waste of time and server moneys.</p>
<p>Having every user do either a) or b) above seems silly imo for something that is arguably an elementary purpose of having a trained model, which is actually using it to classify unlabeled images as part of a bigger pipeline.</p>
<p>Something like this would be useful:</p>
<pre><code class="lang-auto">imgs = datasets.FlatImageFolder(
                non_nested_image_dir,
                transform=data_transforms)
</code></pre>
<p>where <code>non_nested_image_dir</code> is simply a dir with the images to be classified inside. Bear in mind that these images have no labels --that’s the purpose of the trained network.</p>
<p>Again, its not that writing a custom function is hard, but the fact that it seems such an ovious thing to have in the API and some users clearly need it (I suspect practitioners building pipelines). Hope this helps clarify what I meant.</p>
<p>Thanks.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="chilango" data-post="5" data-topic="83125">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e47c2d/48.png" class="avatar"> chilango:</div>
<blockquote>
<p>Moving millions of images every day from the root dir to a subdir to have the nested structure is a waste of time and server moneys.</p>
</blockquote>
</aside>
<p>That’s right and I would recommend to use symbolic links instead of moving data around, if necessary.</p>
<aside class="quote no-group" data-username="chilango" data-post="5" data-topic="83125">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e47c2d/48.png" class="avatar"> chilango:</div>
<blockquote>
<p>Again, its not that writing a custom function is hard, but the fact that it seems such an ovious thing to have in the API and some users clearly need it (I suspect practitioners building pipelines). Hope this helps clarify what I meant.</p>
</blockquote>
</aside>
<p>That makes sense and we would be happy to accept a PR for it. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
I would suggest to either post in the linked issue or create a new feature request in the <code>torchvision</code> repository to discuss the implementation.</p>

username: chilango
cooked: <p>Will do, thanks for the pointer.</p>