Training network with multiple labels per image

ahmed: <p>I am trying to train a network that has more than 1 label per image. For example, an image may have 2 pedestrians or 1 pedestrian and 1 cyclist etc. The dataset comes with jpgs and xml files for the annotations. I have created the following dataloader for this task:</p>
<pre><code class="lang-auto">class dataLoader(Dataset):
    def __init__(self, path, root, img_trfm=None, resize=None):
        self.filename = get_file_name(path)
        self.data = self.filename
        self.root = root
        self.resize = resize
        self.img_resize = transforms.Resize((resize, resize))
        self.img_trfm = img_trfm
        self.data_len = len(self.data)

    def __getitem__(self, index):
        filename = (self.filename[index])
        objects, num_objs = get_targets(filename+'.xml')
        img_name = objects[-1]
        img = Image.open(self.root+img_name+'.jpg')
        img_trfm = self.img_trfm(img)
        #adds a dictionary for name of classes to 
        #corresponding integers 
        for i in range(num_objs-1):
            objects[i]['bndbox'] = torch.Tensor(objects[i]['bndbox'])
            objects[i]['id'] = get_class_id(objects[i]['label']) 
        return img_trfm, objects[:-1]
    
    def __len__(self):
        return self.data_len
</code></pre>
<p>And this is the output from the dataloader with a batch size of 1:</p>
<pre><code class="lang-auto">dataiter= iter(trainloader)
img, objects = dataiter.next()
print(objects)
</code></pre>
<pre><code class="lang-auto">[{'label': ['person'], 'bndbox': tensor([[444., 220.,  27.,  65.]]), 'id': tensor([1])}, 
{'label': ['person'], 'bndbox': tensor([[468., 220.,  26.,  66.]]), 'id': tensor([1])},
{'label': ['person?'], 'bndbox': tensor([[415., 224.,  20.,  33.]]), 'id': tensor([4])}]
</code></pre>
<p>Please let me know if there is any issues with this approach.</p>
<p>I have been using a basic training function to test if this approach works. However, after a number of tries, I keep getting this error message:</p>
<pre><code class="lang-auto">RuntimeError: bool value of Tensor with more than one value is ambiguous
</code></pre>
<p>This is my training loop:</p>
<pre><code class="lang-auto">for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for data in trainloader:
        images, objects = data
        
        for i in objects:
            label = i['id']
            print(label)
            optimizer.zero_grad()
            outputs = net(images)
            loss = criterion(outputs, label)
            loss.backward()
            optimizer.step()
</code></pre>
<p>Any advice would be greatly appreciated.</p>
<p>Thank you in advance.</p>

ahmed: <p>I just realised that I for to add brackets at the end of nn.CrossEntropyLoss for my criterion. Although the error:</p>
<pre><code class="lang-auto">RuntimeError: bool value of Tensor with more than one value is ambiguous
</code></pre>
<p>seem to be gone, I am now getting this error:</p>
<pre><code class="lang-auto">ValueError: Expected input batch_size (785) to match target batch_size (1).
</code></pre>

ahmed: <p>I managed to fix there error by adjusting my network and I started to get loss values:</p>
<pre><code class="lang-auto">tensor(1.45, grad_fn=&lt;NllLossBackward&gt;)
tensor(1.42, grad_fn=&lt;NllLossBackward&gt;)
</code></pre>
<p>But after getting some loss values, I got the following error:</p>
<pre><code class="lang-auto">ParseError: no element found: line 1, column 0
</code></pre>