Problem with extract label from my dataset

nima_pw: <p>Hi.<br>
I have an image dataset with 35 classes, all the images are in one folder, and one part of the names of the images is their label. An example of image names is like this:<br>
D34_Samsung_GalaxyS3Mini-images-flat-D01_I_flat_0001.jpg<br>
And the label becomes D01 here.</p>
<p>In the Dataset class definition, the target variable should return the image label, right? If we consider the index, 34 should be returned for this example.</p>
<p>I have a code to define the dataset:</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    
    def __init__(self, imgs , transform = None):
        self.imgs = imgs
        self.transform = transform or transforms.ToTensor()
        self.class_to_idx = {}

    def __getitem__(self, index):
        
        image_path = self.imgs[index]
        target = image_path.split('_')[0]
        target = re.findall(r'D\d+.+' , target)
        
        image = Image.open(image_path)
        
        if self.transform is not None:
            image = self.transform(image)

        if target[0] in self.class_to_idx : 
            target = [self.class_to_idx[target[0]]]
        else : 
            self.class_to_idx[target[0]] = len(self.class_to_idx)
            target = [self.class_to_idx[target[0]]]

        return image , target
    
    def __len__(self):
        return len(self.imgs)
</code></pre>
<p>But when I tested it, I realized it does not extract the labels correctly. That is, every time, the labels are always a number between 0 and 15 (batch-size=16).<br>
There are 35 classes, but the target is always between 0 and 15. that is, the batch size; also, an image may get a different label each time the code is executed!</p>
<p>An output of the above code is shown in pic No.1</p>
<p>So I changed the Dataset code. I removed a few lines of code and directly obtained the label from the name of the images instead of using class_to_idx:</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    
    def __init__(self, imgs , transform = None):
        self.imgs = imgs
        self.transform = transform or transforms.ToTensor()
        self.class_to_idx = {}

    def __getitem__(self, index):
        
        image_path = self.imgs[index]
        target = image_path.split('_')[0]
        target = target.split('D')[1]
        target = int(target)
        
        image = Image.open(image_path)
        
        if self.transform is not None:
            image = self.transform(image)

        return image , target
    
    def __len__(self):
        return len(self.imgs)
</code></pre>
<p>When I did the test, the numbers were no longer between 0 and 15, and there were real labels of images. (Pic No.2)</p>
<p>My problem is that when I train the model with the first code, my CNN model trains correctly and does not give an error.<br>
But by the second code (my edition), even though the output was correct in the test, the model cannot train and errors (Pic No.3)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/b/eb6f00296b81284853baacb4b3755b72a6b0a121.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/eb6f00296b81284853baacb4b3755b72a6b0a121" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/b/eb6f00296b81284853baacb4b3755b72a6b0a121_2_690x431.jpeg" alt="image" data-base62-sha1="xAK1rC667cHaoPZiXD16HbEMIFz" width="690" height="431" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/b/eb6f00296b81284853baacb4b3755b72a6b0a121_2_690x431.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/b/eb6f00296b81284853baacb4b3755b72a6b0a121_2_1035x646.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/b/eb6f00296b81284853baacb4b3755b72a6b0a121.jpeg 2x" data-dominant-color="E0DCD8"><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">1280×800 87.1 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>
<p>Whatever I search, the answers I see are related to the model. But there is no problem with the model, and it does not give an error with the first code.<br>
Thank you for your advice.</p>

ptrblck: <p>Your target tensor seems to be empty as seen in this code:</p>
<pre><code class="lang-python">criterion = nn.CrossEntropyLoss()
output = torch.randn(16, 10, requires_grad=True)
target = torch.randint(0, 10, (0,))
print(target)
# tensor([], dtype=torch.int64)

loss = criterion(output, target)
# ValueError: Expected input batch_size (16) to match target batch_size (0).
</code></pre>
<p>Check why that’s the case and make sure it contains the expected number of elemens.</p>

nima_pw: <p>I changed the code as below, and my problem was solved:<br>
There was a need to return “target” as a dictionary.</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    
    def __init__(self, imgs , transform = None):
        self.imgs = imgs
        self.transform = transform or transforms.ToTensor()
        self.class_to_idx = {}

    def __getitem__(self, index):
        
        image_path = self.imgs[index]
        target = image_path.split('-')[0]
        label = target.split('_')[0]
        label = label.split('D')[1]
        name = target
        
        image = Image.open(image_path)
        
        if self.transform is not None:
            image = self.transform(image)

        if target in self.class_to_idx : 
            target = [self.class_to_idx[target]]
        else : 
            self.class_to_idx[target] = (int(label)-1)
            target = [self.class_to_idx[target]]

        return image , target
    
    def __len__(self):
        return len(self.imgs)
</code></pre>