AttributeError: 'dict' object has no attribute 'log_softmax'

AAMonza01: <p>So we are getting this AttributeError, which seems similar to the other errors posted on this site but, we using a Mask R-CNN Class and a pretrained Resnet50+MaskR-CNN backbone segmentation model. Therefore the model.aux_logits = False Flag doesn’t help here. We are using a Custom Dataset so this might also be the cause of this problem but the target dict is the same as in the TorchVision Object Detection Finetuning Tutorial having Boxes, Labels, Masks image_id, area, iscrowd defind at the same positions.</p>
<pre><code class="lang-auto"> # create training loss function and optimizer
self._loss_fn = nn.CrossEntropyLoss()
self._optimizer = torch.optim.SGD(self._model.parameters(), lr=1e-3)
for epoch in range(start_epoch, end_epoch, 1):
            # train one epoch and test the model afterwards
            print("epoch {} ---------------------".format(epoch))
            self.load()
            train_one_epoch(self._model, self._optimizer, self._train_dataloader, self._device, epoch, print_freq=10)
            self.save()
            # evaluate on the test dataset
            print('size of train dataloader is: ', len(self._train_dataloader))
            for images, targets in next(iter(self._train_dataloader)):
                # loss and correct predicts
                images, targets = images.to(self._device), targets.to(self._device)
                # update optimizer
                self._optimizer.step()
                images = images.unsqueeze(0)
                self._model.eval()
                self._model.forward(images)
                predictions = self._model(images)
                self._model.aux_logits = False
                loss = self._loss_fn(predictions[0], targets)
                total_loss += loss.item()
                total_correct += self.get_num_correct(self._model, targets)

</code></pre>
<p>images and targets give this output:</p>
<pre><code class="lang-auto">                print(type(targets))
                print(targets.size())
                print(targets.dim())
                print(type(images))
                print(images.size())
                print(images.dim())
&lt;class 'torch.Tensor'&gt;
torch.Size([3, 749, 832])
3
&lt;class 'torch.Tensor'&gt;
torch.Size([3, 757, 898])
3
</code></pre>
<p>the custom MaskRCNN Class implementation:</p>
<pre><code class="lang-auto">class MaskRCNN(FasterRCNN, nn.Module):
    def __init__(self, backbone, num_classes=None,
                 mask_roi_pool=None, mask_head=None, mask_predictor=None):
        super(MaskRCNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5),
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5),

        self.fc1 = nn.Linear(in_features=128 * 4 * 4, out_features=500),
        self.fc2 = nn.Linear(in_features=500, out_features=60),
        self.relu = nn.ReLU(),
        self.out = nn.Linear(in_features=60, out_features=13)

    def forward(self, images, **kwargs):
        images = nn.ReLU(self.conv1(images))
        images = F.max_pool2d(images, kernel_size=2, stride=2)
        images = nn.ReLU(self.conv2(images))
        images = self.max_pool2d(images, kernel_size=2, stride=2)
        images = torch.flatten(images, start_dim=1)
        images = nn.ReLU(self.fc1(images))
        images = nn.ReLU(self.fc2(images))
        images = self.out(images)
        images = nn.LogSoftmax(images)
        return images

</code></pre>
<p>The Error Message:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/tmp/pycharm_project_453/nn_main.py", line 157, in &lt;module&gt;
    main()
  File "/tmp/pycharm_project_453/nn_main.py", line 149, in main
    nn_env.train_epochs(total_epochs, model, lr_scheduler)
  File "/tmp/pycharm_project_453/nn_environment.py", line 267, in train_epochs
    loss = self._loss_fn(predictions[0], targets)
  File "/home/user/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 1048, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/user/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2693, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/user/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1672, in log_softmax
    ret = input.log_softmax(dim)
AttributeError: 'dict' object has no attribute 'log_softmax'

Process finished with exit code 1

</code></pre>
<p>Could you please suggest what might causing that issue? Thanks in advance for the help!</p>

tom: <aside class="quote no-group" data-username="AAMonza01" data-post="1" data-topic="123521">
<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/a/ec9cab/48.png" class="avatar"> AAMonza01:</div>
<blockquote>
<pre><code class="lang-auto">  File "/tmp/pycharm_project_453/nn_environment.py", line 267, in train_epochs
    loss = self._loss_fn(predictions[0], targets)
</code></pre>
</blockquote>
</aside>
<p>Likely, <code>predictions[0]</code> is a dictionary (probably with a tensor in there) but the loss you feed it to (cross entropy?) expects a tensor.<br>
Sounds to me like your tooling in nn_environment is geared towards classification (only) and does not know how to cope with what the MarkRCNN outputs.</p>
<p>Best regards</p>
<p>Thomas</p>

AAMonza01: <p>Thank you for your quick reply, Thomas. But i think i didn’t quite get what you meant, i tryed to split the dict into several concatenated tensors with this code:</p>
<pre><code class="lang-auto">pred_labels = [test_class_list[i] for i in list(predict[0]['labels'].numpy())]
loss = self._loss_fn(pred_labels[0], targets)
</code></pre>
<p>Sadly this would give me just a str back:</p>
<pre><code class="lang-auto">  File "/tmp/pycharm_project_453/nn_environment.py", line 326, in train_epochs
    loss = self._loss_fn(pred_labels[0], targets)
  File "/home/hsuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/hsuser/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 1048, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/hsuser/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2693, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/hsuser/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1672, in log_softmax
    ret = input.log_softmax(dim)
AttributeError: 'str' object has no attribute 'log_softmax'
</code></pre>
<p>then i tryed to convert it via scikit LabelEncoder into a numeric type array and then into a tensor, but that would just give me this message:</p>
<pre><code class="lang-auto">IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
</code></pre>
<p>So how can i make a Input tensor of (C=1,N=1) for CrossEntropyLoss and then the target tensor of size (1,) while output should be a tensor of size (1, 1)? Or do i have some kind of logical error in my assumption?</p>

tom: <p>The input tensor should probably just be <code>predict[0]['labels']</code> or so, so it is a tensor of softmax scores of size batch, num_classes.</p>