Please Help: Transfer Learning using pretrained segmentation.fcn_resnet101

username: Younggee
cooked: <p>Please i have been trying to train this model (segmentaion_fcn_resnet101) but i kept on getting this error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/f/1fc8768a9f2b6243367c2f7275ef09247f313e1b.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1fc8768a9f2b6243367c2f7275ef09247f313e1b" title="Capturep"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/f/1fc8768a9f2b6243367c2f7275ef09247f313e1b_2_690x304.jpeg" alt="Capturep" data-base62-sha1="4xai5gJO7n8QHY223QRETaw3BWj" width="690" height="304" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/f/1fc8768a9f2b6243367c2f7275ef09247f313e1b_2_690x304.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/1/f/1fc8768a9f2b6243367c2f7275ef09247f313e1b.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/f/1fc8768a9f2b6243367c2f7275ef09247f313e1b.jpeg 2x" data-dominant-color="F3F3F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capturep</span><span class="informations">740×327 40.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>And my train_loader and val_loader<br>
if <strong>name</strong> == ‘<strong>main</strong>’:</p>
<pre><code>train_dataset = ListDataset('/content/gdrive/My Drive/dataset/Train', 'Train')

train_loader = DataLoader(train_dataset, batch_size=5, shuffle=True, sampler=None,

           batch_sampler=None, num_workers=0, collate_fn=None,

           pin_memory=False, drop_last=False, timeout=0,

           worker_init_fn=None)

val_dataset = ListDataset('/content/gdrive/My Drive/dataset/Validate', 'Validate')

val_loader = DataLoader(dataset=val_dataset, batch_size=5, shuffle=True, sampler=None,

           batch_sampler=None, num_workers=0, collate_fn=None,

           pin_memory=False, drop_last=False, timeout=0,

           worker_init_fn=None)
</code></pre>
<p>model = models.segmentation.fcn_resnet101(pretrained= True, aux_loss=None)</p>
<p>for param in model.parameters():</p>
<p>param.requires_grad=False<br>
model.classifier[4] = nn.Conv2d(512, 2, kernel_size=(1, 1), stride=(1, 1))</p>
<p>model.aux_classifier = None</p>
<p>def train_model(model, criterion, optimizer, scheduler, num_epochs=5):</p>
<pre><code>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)

    model.train()

    # Each epoch has a training and validation phase

    

    running_loss = 0.0

    running_corrects = 0

        # Iterate over data.

    for inputs, labels in train_loader:

        

        inputs = inputs.to(device)

        labels = labels.to(device)

            # zero the parameter gradients

        optimizer.zero_grad()

        

            # forward

            # track history if only in train

        with torch.set_grad_enabled(True):

          outputs = model(inputs)

          loss = criterion(outputs, labels)

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

                    

        # backward + optimize 

          

        loss.backward()

        optimizer.step()

            # statistics

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

        running_corrects += torch.sum(preds == labels.data)

        

        scheduler.step()

    epoch_loss = running_loss / len(train_loader.dataset)

    epoch_acc = running_corrects.double() / len(train_loader.dataset)

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

    ## A Loop for Validation data

    model.eval()

    for val_inputs, val_labels in val_loader:

                  

        val_inputs = val_inputs.to(device)

        val_labels = val_labels.to(device)

            # zero the parameter gradients

        optimizer.zero_grad()

        with torch.no_grad():

          val_outputs = model(val_inputs)

          loss = criterion(val_outputs, val_labels)  

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

        

        # deep copy the model

        if epoch_acc &gt; best_acc:

          best_acc = epoch_acc

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

    print()

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

model.load_state_dict(best_model_wts)

return model
</code></pre>
<p>#<span class="hashtag-raw">#Training</span><br>
for param in model.parameters():</p>
<p>param.requires_grad=False</p>
<p>device = torch.device('cuda’if torch.cuda.is_available() else ‘cpu’)</p>
<p>model = model.to(device)</p>
<p>criterion = nn.CrossEntropyLoss()</p>
<p>optimizer = optim.SGD(model.parameters(), lr = 0.001, momentum=0.9)</p>
<p>exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)</p>
<p>num_epochs = 5</p>
<p>model = train_model(model, criterion, optimizer, exp_lr_scheduler, num_epochs=num_epochs)</p>

username: ptrblck
cooked: <p><code>fcn_resnet101</code> will return an <code>OrderedDict</code> instead of a single tensor, which explains your error.<br>
You should be able to get the output tensor via:</p>
<pre><code class="lang-python">output = model(input)
out = output['out']
</code></pre>
<p>and could pass it to the criterion.</p>

username: Younggee
cooked: <p>Thanks for your reply, i imported the model as<br>
model = models.segmentation.fcn_resnet101(pretrained= True, aux_loss=False)<br>
for param in model.parameters():<br>
param.requires_grad=False</p>
<p>model.classifier[4] = nn.Conv2d(512, 2, kernel_size=(1, 1), stride=(1, 1))<br>
model.aux_classifier = None</p>
<p>… i have changed the line u suggested to<br>
with torch.set_grad_enabled(True):<br>
outputs = model(inputs)<br>
print(type(outputs))<br>
labels = labels.long()<br>
loss = criterion(outputs[‘out’], labels)<br>
_, preds = torch.max(outputs, 1)</p>
<p>but getting this error :<br>
TypeError: max() received an invalid combination of arguments - got (collections.OrderedDict, int), but expected one of:</p>
<ul>
<li>(Tensor input)</li>
<li>(Tensor input, name dim, bool keepdim, *, tuple of Tensors out)</li>
<li>(Tensor input, Tensor other, *, Tensor out)</li>
<li>(Tensor input, int dim, bool keepdim, *, tuple of Tensors out)</li>
</ul>

username: ptrblck
cooked: <p>The <code>torch.max</code> operation raises now the error, since you are again passing the <code>OrderedDict</code> to it instead of the tensor. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Younggee
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="5" data-topic="88637">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>torch.max</code> operation</p>
</blockquote>
</aside>
<p>I solved that with this but could not call:<br>
loss.backward()</p>
<p>during training and ended up with this<br>
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED (createCuDNNHandle at /pytorch/aten/src/ATen/cudnn/Handle.cpp:9)</p>

username: ptrblck
cooked: <p>This might be raised, if you are running out of memory.<br>
Check the memory usage via <code>nvidia-smi</code> and try to lower the batch size.<br>
If that’s not the case, disable <code>cudnn</code> via <code>torch.backends.cudnn.enabled = False</code> and rerun the code.<br>
If the code is working fine without cudnn and is not running OOM with cudnn, could you post a code snippet to reproduce this issue as well as your current setup (PyTorch, CUDA, cudnn version and the used hardware)?</p>

username: Younggee
cooked: <p>Alright i will do just that but i am running the training on Google Colab</p>

username: Younggee
cooked: <p>I think i have seen where the error is coming from, i changed the runtime type to cpu …<br>
This is the size of my dataset from the <strong>getitem</strong>  (5, 3, 256, 256) and i added this line of code (<span class="hashtag">#Edit</span> label tensor to remove channel since nn.CrossEntropy Loss() receives (B, H, W)<br>
mask_array = torch.cat((mask_array[:1], mask_array[256:]))<br>
mask_array = torch.squeeze(mask_array, 0))<br>
to squeeze only the mask(label) to (5, 256,256)</p>
<p>for my training i have this:<br>
outputs = model(inputs)<br>
print(type(outputs))<br>
labels = labels.long()<br>
loss = criterion(outputs[‘out’], labels) ## the error is coming from here<br>
_, preds = torch.max(outputs[‘out’], 1)<br>
# backward + optimize<br>
loss.backward()<br>
optimizer.step()</p>
<p>still ended up with this error : IndexError: Target -2 is out of bounds</p>
<p>Thanks for your help</p>