TypeError: cross_entropy_loss()

HI, Here is my code and I have some errors.
I don’t know why the ‘y’ is dict.
Thank you for your help in advnace :slight_smile:

training_data = datasets.VOCDetection(
    root="/data/",
    image_set="train",
    #download=True,
    transform=transforms.Compose([transforms.Resize((1000,600)), transforms.ToTensor()])   
)

test_data = datasets.VOCDetection(
    root="/data/",
    image_set="val",
    #download=True,
    transform=transforms.Compose([transforms.Resize((1000,600)), transforms.ToTensor()])
)


train_dataloader = DataLoader(training_data, batch_size=2)
test_dataloader = DataLoader(test_data, batch_size=2)


class MyNet(nn.Module):
    def __init__(self):     
        super(MyNet, self).__init__()
        self.conv1 = nn.Conv2d(3,6,5)
        self.conv2 = nn.Conv2d(6,16,5)
        #self.conv1.in_channels = 1

        self.fc1 = nn.Linear(580944, 120) 
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x): 
        x = F.max_pool2d(F.relu(self.conv1(x)), (2,2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = torch.flatten(x, 1)     
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x    # output

model = MyNet()

learning_rate = 1e-3
batch_size = 64
epochs = 5

loss_fn = nn.CrossEntropyLoss()

# optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# Define training and test
def train_loop(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    for batch, (X, y) in enumerate(dataloader):
        # calculate prediction and loss
        pred = model(X)
        #y = torch.as_tensor(y)
        #y = torch.Tensor(y)
        loss = loss_fn(pred, y) # -->> this line occurs error

        # backward propagation
        optimizer.zero_grad()
        loss.backward()
        optimizer.step() 

        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss : {loss:>7f} [{current:>5d}/{size:>5d}]")

def test_loop(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    test_loss, correct = 0, 0

    with torch.no_grad():
        for X, y in dataloader:
            pred = model(X)
            test_loss += loss_fn(pred, y).item()
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()

    test_loss /= num_batches
    correct /= size
    print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")


# reset
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)


# training
epochs = 10
for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train_loop(train_dataloader, model, loss_fn, optimizer)
    test_loop(test_dataloader, model, loss_fn)

Here is my error:

  File "model.py", line 105, in train_loop
    loss = loss_fn(pred, y)
TypeError: cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not dict

If you take a look at the documentation of the VOCDection dataset you use, you can see that it the __getitem__ method returns the following values:

(image, target) where target is a dictionary of the XML tree

So after this line
for batch, (X, y) in enumerate(dataloader):
X is an image and y a dictionary.

I am not sure what this dataset is exactly about, but I would recommend you take a detailed look at your dataset in order to fully understand what the labels are and how you should use them :slight_smile:

1 Like

hi,
VOCDetection dataset is benchmark for object detection task.
each image has multiple object in it.
base on the code you’re trying to do classifcation.