[Python] ValueError: too many values to unpack (expected 2) when loading multiple clips for...

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    i run this script to load multiple clips from dataset for validation

    def collate_fn(batch):
    batch_clips, batch_targets = zip(*batch)
    batch_clips = [clip for multi_clips in batch_clips for clip in multi_clips]
    batch_targets = [target for multi_targets in batch_targets for target in multi_targets]

    batch_clips = torch.stack(batch_clips, 0)
    batch_targets = torch.stack(batch_targets, 0)

    return batch_clips, batch_targets
    class ActionDatasetMultiClips(ActionDataset):
    def __loading(self, path, frame_indices):
    clips = []
    for clip_frame_indices in frame_indices:
    valid_indices = [i for i in clip_frame_indices if i < len(path)]
    if not valid_indices:
    raise ValueError(f"No valid frame indices for clip: {clip_frame_indices}")
    clip = [Image.open(path).convert('RGB') for i in valid_indices]
    if self.spatial_transform is not None:
    self.spatial_transform.randomize_parameters()
    clip = [self.spatial_transform(img) for img in clip]
    clips.append(torch.stack(clip, 0))
    return clips

    def __getitem__(self, idx):
    sequence_key = list(self.sequences.keys())[idx]
    sequence_data = self.sequences[sequence_key]

    frame_paths = [data['image_path'] for data in sequence_data]

    if self.temporal_transform:
    frame_indices = list(range(len(frame_paths)))
    frame_indices = self.temporal_transform(frame_indices)

    if isinstance(frame_indices[0], list):
    frame_indices = [i for sublist in frame_indices for i in sublist]

    frame_indices = [i for i in frame_indices if i < len(frame_paths)]
    if not frame_indices:
    raise ValueError(f"No valid frame indices for sequence {sequence_key}")

    clips = self.__loading(frame_paths, [frame_indices])

    action_name = sequence_data[0]['action_name']
    condition = sequence_data[0]['condition']
    action_name_id = self.action_name_to_id[action_name]
    condition_id = self.condition_name_to_id[condition]

    targets = [torch.tensor([action_name_id, condition_id], dtype=torch.long) for _ in range(len(clips))]
    return clips, targets


    and this is the function to train one epoch

    def validate_one_epoch(model, dataloader, criterion, device):
    model.eval()
    total_loss = 0.0
    action_correct = 0
    condition_correct = 0
    total_samples = 0

    with torch.no_grad():
    for frames, labels in tqdm(dataloader, desc="Validation", leave=False):
    frames, labels = frames.to(device), labels.to(device)

    action_logits, condition_logits = model(frames)

    # Calcul des pertes
    action_loss = criterion(action_logits, labels[:, 0])
    condition_loss = criterion(condition_logits, labels[:, 1])
    loss = action_loss + condition_loss

    # Calcul de l'accuracy
    accuracy_sequence, accuracy_condition, accuracy_global = calculate_accuracy(
    action_logits, condition_logits, labels[:, 0], labels[:, 1]
    )

    # Accumulation des métriques
    total_loss += loss.item()
    action_correct += accuracy_sequence * labels.size(0)
    condition_correct += accuracy_condition * labels.size(0)
    total_samples += labels.size(0)

    # Calcul des moyennes
    avg_loss = total_loss / len(dataloader)
    action_acc = action_correct / total_samples
    condition_acc = condition_correct / total_samples

    return avg_loss, action_acc, condition_acc


    and the dataloader

    val_dataset = ActionDatasetMultiClips(
    annotation_path="D:/organized_files/val.csv", spatial_transform=spatial_augmentation,
    temporal_transform=temporal_augmentation,max_sequence_length=16)

    val_loader = DataLoader(val_dataset, batch_size=(BATCH_SIZE//VAL_SAMPLE), shuffle=False,collate_fn=collate_fn,num_workers=8)
    return val_loader, val_dataset


    but when trying to acces the val dataloader

    Traceback (most recent call last):

    File ~\anaconda3\envs\cnnsvm\Lib\site-packages\spyder_kernels\customize\utils.py:209 in
    exec_encapsulate_locals
    exec_fun(compile(code_ast, filename, "exec"), globals)

    File d:\organized_files\le\train.py:118
    val_loss, val_action_acc, val_condition_acc = validate_one_epoch(

    File d:\organized_files\transformer_exemple\utils.py:86 in validate_one_epoch
    for frames, labels in tqdm(dataloader, desc="Validation", leave=False):

    ValueError: too many values to unpack (expected 2)


    I suppose that the loader returns other elements then the clips and labels this the output of the val_dataloader


    Batch contents:​

    Item 0: <class 'torch.Tensor'> - torch.Size([2, 32, 3, 224, 224]) Item 1: <class 'torch.Tensor'> - torch.Size([2, 2]) Batch 1: Frames shape: torch.Size([2, 32, 3, 224, 224]) Labels shape: torch.Size([2, 2]) First frame values (min, max, mean): -2.0665297508239746, 2.517995834350586, 0.5251008868217468 Labels: tensor([[0, 1], [1, 1]])`

    how to fix this ?

    Continue reading...

Compartilhe esta Página