Tried to train a model for Image segmentation.  Used a pretrained model by torchvision FCN-Resnet101 and changed the final two layers to have a model for output number of classes = 30.
I chose the Network as:

def Net():
print(“Loading in pretrained network …”)
model = models.segmentation.fcn_resnet101(pretrained=True).eval().to(device)
for param in model.parameters():
param.requires_grad = False
model.classifier = nn.Sequential(
    nn.Conv2d(2048, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False),
    nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
    nn.ReLU(),
    nn.Dropout(p=0.1, inplace=False),
    nn.Conv2d(512, 30, kernel_size=(1, 1), stride=(1, 1))
    )
model.aux_classifier = nn.Sequential(
    nn.Conv2d(1024, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False),
    nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
    nn.ReLU(),
    nn.Dropout(p=0.1, inplace=False),
    nn.Conv2d(256, 30, kernel_size=(1, 1), stride=(1, 1))
    )
print(model)
return model


and the training was done in such a manner:

def train_model(model, train_loader, val_loader, epochs=1):
learning_rate = 1e-4
optimizer = optim.Adam(model.aux_classifier.parameters(), lr=learning_rate)
model = model.to(device)  # move the model parameters to CPU/GPU
for e in range(epochs):
    print("Epoch: ", e)
    for t, (x, y) in enumerate(train_loader):
        model.train()
        x = x.to(device)
        y = y.type(torch.LongTensor)
        y = y.to(device)
        y = y.squeeze(1)

        scores = model(x)['out']
        loss = F.cross_entropy(scores, y)
        torch.cuda.empty_cache()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if t % 100 == 0:
            print('Iteration %d, loss = %.4f' % (t, loss.item()))
            check_val_accuracy(val_loader, model)
            print()


I got this error:

Traceback (most recent call last):
File “main.py”, line 341, in module
main()
File “main.py”, line 334, in main
train_model(model, train_loader, val_loader, epochs=3)
File “main.py”, line 125, in train_model
loss = F.cross_entropy(scores, y)
File “/media/vicknesh/Studies/CSCI508/Project/venv/lib/python3.6/site-packages/torch/nn/functional.py”, line 2021, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File “/media/vicknesh/Studies/CSCI508/Project/venv/lib/python3.6/site-packages/torch/nn/functional.py”, line 1840, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target 24000 is out of bounds.

Even though my output number of classes are 30 my targets are going out of bounds and that too 24000. I even tried CrossEntropyLoss(). What mistake can you see?