Error when calculating loss when fine tuning fcn_resnet101 segmentation model

I an trying to fine tune the fcn_resnet101 segmentation model with my own data and I am getting AttributeError: 'collections.OrderedDict' object has no attribute 'log_softmax' error when I run my code.

Below is a full print out of the error:

    model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs, is_inception=(model_name=="inception"))
  File "", line 266, in train_model
    loss = criterion(outputs, labels.long())
  File "/home/info/.local/lib/python3.5/site-packages/torch/nn/modules/", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/info/.local/lib/python3.5/site-packages/torch/nn/modules/", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/info/.local/lib/python3.5/site-packages/torch/nn/", line 1995, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/info/.local/lib/python3.5/site-packages/torch/nn/", line 1316, in log_softmax
    ret = input.log_softmax(dim)
AttributeError: 'collections.OrderedDict' object has no attribute 'log_softmax'

It seems like the error is happening when calculating the loss with the criterion function, but I am not sure what to make of the ‘log_softmax’ error to try to find where I went wrong with my code.

Below is the code for the train_model function that I adjusted from this tutorial:

def train_model(model, dataloaders, criterion, optimizer, num_epochs=25, is_inception=False):

    since = time.time()
    val_acc_history = []
    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0
    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))

        print('-' * 10)

        # Each epoch has a training and validation phase

        for phase in ['train', 'val']:

            if phase == 'train':
                model.train()  # Set model to training mode
                model.eval()   # Set model to evaluate mode

            running_loss = 0

            total_train = 0

            correct_train = 0

            # Iterate over data.

            for inputs, labels in dataloaders[phase]:

                inputs = #OriginalImage

                labels = #Masks

                # zero the parameter gradients


                # forward

                # track history if only in train

                with torch.set_grad_enabled(phase == 'train'):

                    # Get model outputs and calculate loss

                    # Special case for inception because in training it has an auxiliary output. In train

                    #   mode we calculate the loss by summing the final output and the auxiliary output

                    #   but in testing we only consider the final output.

                    if is_inception and phase == 'train':

                        # From

                        outputs, aux_outputs = model(inputs)

                        loss1 = criterion(outputs, labels)

                        loss2 = criterion(aux_outputs, labels)

                        loss = loss1 + 0.4*loss2


                        outputs = model(inputs)

                        loss = criterion(outputs, labels.long())

                    _, preds = torch.max(outputs, 1)

                    # backward + optimize only if in training phase

                    if phase == 'train':



                # statistics

                running_loss += loss.item() #* inputs.size(0)

                total_train += labels.nelement()

                correct_train += preds.eq(

                train_accuracy = 100 * correct_train / total_train

            epoch_loss = running_loss / len(dataloaders[phase].dataset)

            epoch_acc = train_accuracy/len(dataloaders[phase].dataset)

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

            # deep copy the model

            if phase == 'val' and epoch_acc > best_acc:

                best_acc = epoch_acc

                best_model_wts = copy.deepcopy(model.state_dict())

            if phase == 'val':



    time_elapsed = time.time() - since

    print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))

    print('Best val Acc: {:4f}'.format(best_acc))

    # load best model weights


    return model, val_acc_history

and below is the part where I set the criterion and call the train_model function:

# Setup the loss fxn

criterion = nn.CrossEntropyLoss()

# Train and evaluate

model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs, is_inception=(model_name=="inception"))

If this isn’t enough information to help me figure out why I am getting this error, let me know and I’ll post other parts of my code. I just didn’t want to fill this post with unnecessary code. Appreciate your help!

I think I found the solution.

I didn’t realize the line below returned a dictionary so I had to index it.

outputs = model(inputs)

When I changed it to the line below I no longer get the same error

outputs = model(inputs)['out']