Could you check the shape of the img and label in the dataset and make sure they have the same size? Based on your posted code and the error message, I guess that label might have different lengths, so you might need to pad this target tensor to the largest length in the batch.