Too many values to unpack (expected 2)

Hi all,

@MONAI
I am using MONAI Compose and Dataset to transform my image dataset and train and validate a neural network… However, I am getting the following error…

in train
for batch_idx, (data, target) in enumerate(dataloader):
ValueError: too many values to unpack (expected 2)

here is my code:

train_ds = Dataset(data=train_files, transform=train_transforms)
    dataloader_train = torch.utils.data.DataLoader(
        train_ds,
        batch_size=2, shuffle=True, num_workers=4, pin_memory=True
    )

val_ds = Dataset(data=val_files, transform=val_transforms)
    dataloader_val = torch.utils.data.DataLoader(
        val_ds, batch_size=1, shuffle=False, num_workers=4, pin_memory=True
    )


def train(model, dataloader, optimizer, loss_func):
    model.train()
    total_loss = 0
    for batch_idx, (data, target) in enumerate(dataloader):
        data, target = data.cuda(), target.cuda()
        optimizer.zero_grad()
        output = model(data)
        loss = loss_func(output, target)
        loss.backward()
        optimizer.step()

        total_loss += loss.item()

    return total_loss/len(dataloader)

I would greatly appreciate any help with this…Spent so much time…no success !
Thank you,

modify this to:

for batch_idx, sample in enumerate(dataloader):
        data, target = sample['data'].cuda(), sample['target'].cuda() # or something similar

Thank you for the comment… I applied this…but I get the following error…


in train
data, target = sample[‘data’].cuda(), sample[‘target’].cuda()
KeyError: ‘data’

Of course ‘data’ and ‘target’ are just used as example keys for your dataset.
What are the actual keys to access data from train_ds and val_ds ?

replaced “data” and “target” with actual keys (image and label)…it worked!.. Thank you

hello guys !!
someone help me !!
i try to implement the lda2vec algorithm from link with my own data.
when i try model preprocess.py , i’m getting this error.


how to fix it.