As @smth suggested, the error is raised by an out-of-bounds indexing.
Based on the stack trace it seems that your model output contains logits (or log probabilities) for 2 classes, while the target uses a class index of 2, which would assume at least 3 classes.
nn.CrossEntropyLoss and nn.NLLLoss expect a model output in the shape [batch_size, nb_classes] and a target in the shape [batch_size] containing class indices in the range [0, nb_classes-1].
Here is a small code snippet to reproduce this error:
batch_size = 2
nb_classes = 2
output = torch.randn(batch_size, nb_classes, requires_grad=True)
target = torch.randint(0, nb_classes, (batch_size,))

criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)

target[0] = 2
loss = criterion(output, target)
> IndexError: Target 2 is out of bounds.

How many classes are created in the ImageFolder and what is the shape of your model output?