Dataloader for two labels:coarse_label anf fine_label

pesci85: <p>I have to have two labels: coarse_label and fine_label, under there is the dataloader for a single label. How to modify the dataloader?<br>
here I have the piece of train code for cifar 100  with only label:<br>
if args.onlygan:<br>
loaders = {<br>
‘train’: trainloader<br>
}<br>
else:<br>
loaders = {<br>
‘train’: trainloader,<br>
‘test’: testloader<br>
} for epoch in range(args.epochs):</p>
<pre><code class="lang-auto">    #print("EPOCH: ", epoch, "/", args.epochs)
    for split in loaders:
        data_loader = loaders[split]

        for  i ,(inputs,labels) in enumerate(tqdm(data_loader, desc=f'EPOCH: {epoch+1}/{args.epochs}')):
            inputs=inputs.to(device)
            labels=labels.to(device)
            

in a separate file:
trainset = torchvision.datasets.CIFAR100(root='./data' ,train=True,
                             download=True, transform=transform)
``` trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
                                              shuffle=True, num_workers=4)

My doubt is this: if there are inputs and labels for the cifar 100 in the train, the inputs, coarse labels and labels should be in the train. The dataloader for how it is implemented with 2 labels load.</code></pre>

ptrblck: <p>You could create a custom <code>Dataset</code> which would return both labels with the input data as described in <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">this tutorial</a>.</p>

pesci85: <p>import torch<br>
import torchvision.transforms as transforms<br>
import torchvision<br>
from tqdm import tqdm<br>
<span class="hashtag">#from</span> utils.saver import Saver<br>
from torchvision.utils import save_image<br>
import pickle<br>
def get_loaders(batch_size):</p>
<pre><code>classes = ('beaver', 'dolphin', 'otter', 'seal','whale',
          'aquarium fish','flatfish','ray', 'shark', 'trout',
      'orchids', 'poppies','roses','sunflowers','tulips',
      'bottles',' bowls', 'cans', 'cups', 'plates',
      'apples',' mushrooms', 'oranges',' pears','sweet peppers',
      'clock','computer keyboard','lamp','telephone', 'television',
      'bed', 'chair','couch', 'table', 'wardrobe',
      'bee',' beetle','butterfly','caterpillar', 'cockroach',
      'bear', 'leopard', 'lion', 'tiger',' wolf',
          'bridge', 'castle', 'house','road', 'skyscraper',
      'cloud', 'forest', 'mountain', 'plain', 'sea',
      'camel','cattle','chimpanzee','elephant','kangaroo',
      'fox',' porcupine',' possum', 'raccoon',' skunk',
          'crab', 'lobster', 'snail', 'spider', 'worm',
      'baby', 'boy', 'girl', 'man',' woman',
      'crocodile', 'dinosaur', 'lizard',' snake', 'turtle',
      'hamster', 'mouse', 'rabbit', 'shrew','squirrel',
      'maple','oak', 'palm',' pine', 'willow',
      'bicycle', 'bus','motorcycle', 'pickup truck', 'train',
          'lawn-mower', 'rocket',' streetcar', 'tank', 'tractor')
transform = transforms.Compose(
    [   #transforms.Resize(224), #for alexnet
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
)
trainset = torchvision.datasets.CIFAR100(root='./data', train=True,
                         download=True, transform=transform)


testset = torchvision.datasets.CIFAR100(root='./data', train=False,
                        download=True, transform=transform)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
                                          shuffle=True, num_workers=4)


testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
                                         shuffle=False, num_workers=4) 

                                         
dict=pickle.load(open('./data/cifar-100-python/meta','rb'))
label_names=dict
print(label_names)


#print(dict['coarse_label_names'])



return trainloader, testloader, classes,label_names,trainset
</code></pre>
<p>this is the file i had for the cifar100, i wonder if i can specify the labels inside Dataloader () type label_mode = ‘coarse_label’ or coarse label = True since the above dataloader nn allows me to have two labels.<br>
assuming that the cifar10 train file contains a dictionary with data, labels, for the cifar 100 the train would contain data, labels and coarse_labels?</p>

ptrblck: <aside class="quote no-group" data-username="pesci85" data-post="3" data-topic="87458">
<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/p/4da419/48.png" class="avatar"> pesci85:</div>
<blockquote>
<p>i wonder if i can specify the labels inside Dataloader () type label_mode = ‘coarse_label’ or coarse label = True since the above dataloader nn allows me to have two labels.</p>
</blockquote>
</aside>
<p>The <code>DataLoader</code> will just use the passed <code>Dataset</code> to create batches by loading each sample via the <code>Dataset.__getitem__</code> method.<br>
I don’t see, where your current script allows the <code>DataLoader</code> to return two labels.<br>
Do you mean that your <code>get_loaders</code> method returns multiple values?</p>
<aside class="quote no-group" data-username="pesci85" data-post="3" data-topic="87458">
<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/p/4da419/48.png" class="avatar"> pesci85:</div>
<blockquote>
<p>assuming that the cifar10 train file contains a dictionary with data, labels, for the cifar 100 the train would contain data, labels and coarse_labels?</p>
</blockquote>
</aside>
<p><code>CIFAR10</code> uses lists to store the data and target as seen <a href="https://github.com/pytorch/vision/blob/fa6af6d1e6f050d0a930e6f59894b7dd40aa869c/torchvision/datasets/cifar.py#L69-L70">here</a>.<br>
If you want to add another label or manipulate the already loaded labels, I would recommend to write a custom <code>Dataset</code> and apply the modifications in the new <code>__init__</code> method.</p>