Iterating through ImageFolder for sample, target

username: John_J_Watson
cooked: <p>I am trying to use the ImageFolder class to read a bunch of images which are arranged this way:</p>
<pre><code class="lang-auto">0(folder)
    image01
    image01

1(folder)
    image01
    image01

2(folder)
    image01
    image01
</code></pre>
<p>Then I do something lilke:</p>
<p><code>    ds = torchvision.datasets.ImageFolder(  root=path_to, transform=p   )</code></p>
<p>I can see the list of classes through:</p>
<p><code>list_of_classes=list(map(int, list(ds.classes))  )</code></p>
<p>Now reading through the docs, I iterate through the sample and targets lke so:</p>
<pre><code class="lang-auto">for idx, (sample, target) in enumerate(ds):
    print(sample, target)
</code></pre>
<p>For some reason, I notice that the <code>target is showing the indices of ds.classes</code> rather than the <code>target value itself</code>.</p>
<p>Is this the intended behaviour? I was expecting it to simply show me the classes (0,1,2) in this case.</p>

username: ptrblck
cooked: <p>Assuming <code>dataset.classes</code> returns the folder names, the corresponding indices should be the class indices (i.e. the target values), shouldn’t it?</p>

username: John_J_Watson
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>: thank you for your reply but I am not sure I understand what you have said <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>
<p>So, <code>dataset.classes</code> does return folder names, but when I loop through <code>ds</code> , the <code>target</code> is showing me indices of <code> dataset.classes</code> rather than the folder name <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>
<p>To the NN I would pass the sample and the target(which here is the folder name), not the indices, right?</p>
<p>I am confused as to why this is… I obviously do not understand something <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>
<p>So, when I do:</p>
<pre><code class="lang-auto">for idx, (sample, target) in enumerate(train_dataset):
    print(sample, list_of_classes[target] )
</code></pre>
<p>it seems to pick up the correct labels(aka target/folder_names). Is this the correct way to do it?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="John_J_Watson" data-post="3" data-topic="82291">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/john_j_watson/48/16421_2.png" class="avatar"> John_J_Watson:</div>
<blockquote>
<p>So, <code>dataset.classes</code> does return folder names, but when I loop through <code>ds</code> , the <code>target</code> is showing me indices of <code> dataset.classes</code> rather than the folder name <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>To the NN I would pass the sample and the target(which here is the folder name), not the indices, right?</p>
</blockquote>
</aside>
<p>That is expected and the target should contain the class indices, not the names.<br>
So e.g. for three folders your target should contain values in <code>[0, 1, 2]</code> <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"><br>
These target values are used to index the output of the model during the loss calculation, so they are used as numerical values instead of the folder names.</p>

username: John_J_Watson
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>: thank you for this clarification. I think I got confused because my folder_names were also integers. It does make sense, since we compute the probability of classes at the end and thus indexing makes sense.</p>
<p>Thank you again. This was VERY helpful.</p>