BALD - Bayesian Active Learning

verance: <p>Hello,</p>
<p>I am trying to implement an AL pipeline on my project using BALD query strategy. I am using <a href="https://github.com/ej0cl6/deep-active-learning" rel="noopener nofollow ugc">this</a> lovely project but I am facing some issues, mainly because my project is a 2 class segmentation problem.</p>
<p>My input images are  RGB 256x256.</p>
<p>Following the project:</p>
<p>Initially, the author creates a dataloader, runs the prediction and applies softmax on top of those predictions.</p>
<pre><code class="lang-auto">def predict_prob_dropout_split(self, X, Y, n_drop):
        loader_te = DataLoader(self.handler(X, Y, transform=self.args['transform']),
                            shuffle=False, **self.args['loader_te_args'])

        self.clf.train()
        probs = torch.zeros([n_drop, len(Y), len(np.unique(Y))])
        for i in range(n_drop):
            print('n_drop {}/{}'.format(i+1, n_drop))
            with torch.no_grad():
                for x, y, idxs in loader_te:
                    x, y = x.to(self.device), y.to(self.device)
                    out, e1 = self.clf(x)
                    probs[i][idxs] += F.softmax(out, dim=1).cpu()
        
        return probs
</code></pre>
<p>However, I am just using a test dataset that I created using <code>SubsetRandomSampler</code> to generate a random subset of images to create my <code>pool_loader</code>.</p>
<pre><code class="lang-auto">pool_loader = DataLoader(dataset, batch_size=1, num_workers=num_workers,
                                          sampler=SubsetRandomSampler(pool_idx))
</code></pre>
<p>Then, I run my model in this pool_loader and the idea is to save the predictions as the author did, with the probs tensor in the line: <code>probs[i][idxs] += F.softmax(out, dim=1).cpu()</code></p>
<pre><code class="lang-auto">pool_size = 10
probs = torch.zeros([10, pool_size, 2])
for i in range(10):
    with torch.no_grad():
        with tqdm(enumerate(pool_loader)) as iterator:
            for idx,batch in iterator:
                logits = model(batch.to(device, dtype=torch.float)).cpu().detach()
                logits = logits.squeeze()
                probs[i][idx] += logits
   
</code></pre>
<p>I replaced the <code>len(np.unique(Y))</code> with 2, since I only have two classes.</p>
<p><code>logits.shape = torch.Size([1, 1, 256, 256])</code></p>
<p>After squeezing:</p>
<p><code>logits.shape = torch.Size([256, 256])</code></p>
<p>I think everything looks good until here but then I get the following error on line <code>probs[i][idx] = logits</code></p>
<blockquote>
<p>RuntimeError: expand(torch.FloatTensor{[256, 256]}, size=[2]): the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (2)</p>
</blockquote>
<p>The idea is to save the probabilities over those 10 runs for the 10 images of my pool loader. With this, calculate the entropy by doing this:</p>
<pre><code class="lang-auto">pb = probs.mean(0)
entropy1 = (-pb*torch.log(pb)).sum(1)
entropy2 = (-probs*torch.log(probs)).sum(2).mean(0)
U = entropy2 - entropy1
return idxs_unlabeled[U.sort()[1][:n]]
</code></pre>
<p>For me, it looks a very interesting use case and I would like to learn a bit more about it. First, I would be glad to know what I am doing wrong and understand how I can fit my results to calculate the entropy of my 10 trials.</p>
<p>Any suggestion is welcome <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Kind regards</p>

verance: <p>I guess that my question is: how do you apply BALD to a semantic segmentation problem? I guess the project I posted above refers to a classification problem where he applies softmax to get one single number with the classes probability and then calculates entropy based on that.</p>
<p>In the case of semantic segmentation, we have a pixel wise probability. How can you calculate the entropy from that?</p>