Get target list from Dataset

seunghyuck.kim: <p>Hi, Can I get target list from Dataset object?<br>
like [0, 1, 0, 3, 5, …]<br>
I am getting it by loop code like the last 3 rows of the code below, but it’s not only time consuming, but also looks not good.<br>
Thank you for any advice.</p>
<pre><code class="lang-auto">    train_dataset = ImageFolder(os.path.join(data_dir), train_transform)

    # Temporary dataset and dataloader to create train/val split
    trainval_dataset = ImageFolder(os.path.join(data_dir), transform=transforms.ToTensor()) 
    trainval_loader = DataLoader(trainval_dataset, batch_size=train_batch_size,
                                 shuffle=False, drop_last=False,
                                 num_workers=num_workers, pin_memory=False)

    train_indices, val_indices = get_train_val_indices(trainval_loader, fold_index=fold_index, n_splits=5)

    train_dataset = Subset(train_dataset, train_indices)
    targetlst = []
    for i in range(len(train_dataset)):
        image, target = train_dataset[i]
        targetlst.append(target)
</code></pre>

eqy: <p>Since <code>train_dataset</code> is an instance of <code>ImageFolder</code> which is really just an instance of <code>DatasetFolder</code>, you can access it directly e.g.,</p>
<pre><code class="lang-auto">targetlst = list(train_dataset.targets)
</code></pre>
<p>If you don’t mutate <code>targetlst</code> you can likely get away with using <code>train_dataset.targets</code> directly.</p>
<p>Relevant code in <code>torchvision</code>:</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/vision/blob/f5843099d895e72c27ffa9d29cc91dd8df7f3832/torchvision/datasets/folder.py#L154" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/f5843099d895e72c27ffa9d29cc91dd8df7f3832/torchvision/datasets/folder.py#L154" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/f5843099d895e72c27ffa9d29cc91dd8df7f3832/torchvision/datasets/folder.py#L154</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="144" style="counter-reset: li-counter 143 ;">
<li>                                        target_transform=target_transform)</li>
<li>    classes, class_to_idx = self.find_classes(self.root)</li>
<li>    samples = self.make_dataset(self.root, class_to_idx, extensions, is_valid_file)</li>
<li>
</li>
<li>    self.loader = loader</li>
<li>    self.extensions = extensions</li>
<li>
</li>
<li>    self.classes = classes</li>
<li>    self.class_to_idx = class_to_idx</li>
<li>    self.samples = samples</li>
<li class="selected">    self.targets = [s[1] for s in samples]</li>
<li>
</li>
<li>@staticmethod</li>
<li>def make_dataset(</li>
<li>    directory: str,</li>
<li>    class_to_idx: Dict[str, int],</li>
<li>    extensions: Optional[Tuple[str, ...]] = None,</li>
<li>    is_valid_file: Optional[Callable[[str], bool]] = None,</li>
<li>) -&gt; List[Tuple[str, int]]:</li>
<li>    """Generates a list of samples of a form (path_to_sample, class).</li>
<li>
</li>
</ol></code></pre>


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


seunghyuck.kim: <p>Thank you!<br>
It seems Subset object has no “targets” attribute, but I could make it with ImageFolder, as you said.</p>