ValueError: only one element tensors can be converted to Python scalars

Hey, I am having a value error below is my code:
def test(net, testloader, device: str):
“”“Validate the network on the entire test set and report loss and accuracy.”“”
criterion = torch.nn.CrossEntropyLoss()
correct, loss = 0, 0.0
net.eval()
net.to(device)
with torch.no_grad():
for data in testloader:
images, labels = torch.tensor(data[0]).to(device), data[1].to(device)

        # Print statements to debug
        print("Raw labels:", data[1])
        print("Converted labels:", labels)

        # Handling the case where labels has only one element
        if labels.numel() == 1:
            labels = labels.view(1)  # Convert it to a 1-dimensional tensor
        else:
            # Assuming labels are segmentation masks
            labels = labels.squeeze(1)

        outputs = net(images)
        loss += criterion(outputs, labels).item()
        _, predicted = torch.max(outputs.data, 1)

        # Print statements to debug
        print("Predicted:", predicted)

        # Handling the case where labels has only one element
        if labels.numel() == 1:
            correct += int(predicted == labels.item())
        else:
            correct += (predicted == labels).sum().item()

# Assuming labels are segmentation masks
accuracy = correct / (len(testloader.dataset) * labels.shape[1] * labels.shape[2])
return loss, accuracy

error: Error executing job with overrides:
Traceback (most recent call last):
File “C:\Users\gunav\Downloads\Part-I\main.py”, line 67, in main
history = fl.simulation.start_simulation(
File “C:\Users\gunav\miniconda3\envs\FL\lib\site-packages\flwr\simulation\app.py”, line 197, in start_simulation
hist = _fl(
File “C:\Users\gunav\miniconda3\envs\FL\lib\site-packages\flwr\server\app.py”, line 217, in _fl
hist = server.fit(num_rounds=config.num_rounds, timeout=config.round_timeout)
File “C:\Users\gunav\miniconda3\envs\FL\lib\site-packages\flwr\server\server.py”, line 89, in fit
res = self.strategy.evaluate(0, parameters=self.parameters)
File “C:\Users\gunav\miniconda3\envs\FL\lib\site-packages\flwr\server\strategy\fedavg.py”, line 164, in evaluate
eval_res = self.evaluate_fn(server_round, parameters_ndarrays, {})
File “C:\Users\gunav\Downloads\Part-I\server.py”, line 61, in evaluate_fn
loss, accuracy = test(model, testloader, device)
File “C:\Users\gunav\Downloads\Part-I\model.py”, line 46, in test
images, labels = torch.tensor(data[0]).to(device), data[1].to(device)
ValueError: only one element tensors can be converted to Python scalars