FocalTversky loss function expected input/output size?

username: Ripley
cooked: <p>I am currently working on a semantic segmentation model and I am trying out a different loss function is this case. The loss function i am using is FocalTversky Loss function.</p>
<p>‘’’<br>
class FocalTverskyLoss(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()</p>
<pre><code>def forward(self, preds, target, alpha=0.7, beta = 0.3, epsilon=1e-6, gamma=3):
    
    preds = torch.sigmoid(preds) 
    
    #flatten label and preds tensors
    preds = preds.reshape(-1)
    target = target.reshape(-1)
    
    #True Positives, False Positives &amp; False Negatives 
    TP = (preds * target).sum()
    FP = ((1-target) * preds).sum()
    FN = (target * (1-preds)).sum()
    Tversky = (TP + epsilon)/(TP + alpha*FP + beta*FN + epsilon)
    FocalTversky = (1 - Tversky)**gamma
    
    return FocalTversky 
</code></pre>
<p>‘’’</p>
<p>However when I run it in my code like this</p>
<p>outputs = self.model(images)<br>
loss = self.loss_function(preds=outputs,target=labels).to(device)<br>
train_loss += loss.item()<br>
loss.backward()<br>
self.optimizer.step()<br>
self.optimizer.zero_grad()</p>
<p>it gives this error,</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
1 imgdir = ‘Z:\HuaSheng\datasets\sen12msgrss\DFC_Public_Dataset’<br>
----&gt; 2 trainer(imgdir= imgdir, classes = list(range(0,10)) ,fsave=‘Rip_chkpt_FTL.pth’, reloadmode=‘same’, checkpoint=None, num_epochs = 110)</p>
<p> in <strong>init</strong>(self, imgdir, classes, num_epochs, fsave, reloadmode, checkpoint, bs, report)<br>
59         for self.epoch in range(self.num_epochs):<br>
60             print(’\n’+’*'<em>6+‘TRAIN FOR ONE EPOCH’+’</em>'<em>6)<br>
—&gt; 61             train_loss = self.train()<br>
62<br>
63             print(’\n’+’</em>‘<em>6+‘EVAL FOR ONE EPOCH’+’</em>’*6)</p>
<p> in train(self)<br>
163             self.writer.flush()<br>
164<br>
–&gt; 165             loss = self.loss_function(preds=outputs,target=labels.view(1, -1)).to(device)<br>
166             #weight=torch.FloatTensor([0.,2.5,0.,1.5,2.1,0.3,4.5,0.,4.5,0.]).to(device)<br>
167             train_loss += loss.item()</p>
<p>~\anaconda3\envs\pytLocal38\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
–&gt; 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, preds, target, alpha, beta, epsilon, gamma)<br>
57<br>
58         <span class="hashtag">#True</span> Positives, False Positives &amp; False Negatives<br>
—&gt; 59         TP = (preds * target).sum()<br>
60         FP = ((1-target) * preds).sum()<br>
61         FN = (target * (1-preds)).sum()</p>
<p>RuntimeError: The size of tensor a (5017600) must match the size of tensor b (501760) at non-singleton dimension 0</p>
<p>My input size for my preds and target is torch.Size([10, 10, 224, 224]), torch.Size([1, 501760])</p>
<p>Is there a way I can make my target dim 0 into 10? Or is that even right for me to do when I am training it? Thanks alot for your help</p>

username: ptrblck
cooked: <p>Could you explain what the shapes of <code>preds</code> and <code>target</code> represent?<br>
Currently it seems <code>preds</code> uses a batch size of 10, while you have a single target, which will then raise this error after flattening these tensors.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Ripley
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, sorry for the poor posting format! haha The target represent the labels of the image and the prediction is the output after fitting in the model. The image I am working on right now consist of 13 channel images with 10 classes inside. The chip size of the image is 224. Where every pixel in the image contains a classes used for semantic segmantation modelling.</p>
<p>However, I have come across that using this loss function is not suitable as it is a multiclassifcation problem where as this focaltverskyloss is based off binarycrossentropy loss. So I think it may not be suitable to use this loss</p>

username: ptrblck
cooked: <p>Thanks for the update. While your use case makes sense, it’s still unclear why the batch size of the output images is 10 while the target has only a single sample.<br>
If your target is supposed to contain 10 values, then I guess a reshaping operation might be wrong on this tensor.</p>