ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.88k stars 16.14k forks source link

How will I overcome this problem? #12243

Closed Ejo262 closed 10 months ago

Ejo262 commented 11 months ago

Search before asking

Question

How will I overcome this problem?

ValueError: shape mismatch: value array of shape (6,) could not be broadcast to indexing result of shape (6,2)

I'm getting this error in k-fold cross validation and I don't know what's causing it.

Additional

No response

glenn-jocher commented 11 months ago

@Ejo262 hi there,

To overcome the "ValueError: shape mismatch" issue in k-fold cross-validation, you can check the following:

  1. Ensure that the dimensions of the value array match the indexing result. In this case, the value array should have a shape of (6,), while the indexing result should have a shape of (6,2).

  2. Verify that the data being used in the cross-validation process is structured correctly and has the expected dimensions. Make sure the input data has the same shape and format when performing cross-validation.

  3. Double-check the code implementation in the cross-validation process and look for any potential errors or inconsistencies that could cause the shape mismatch.

If you continue to encounter issues, please provide additional details about your code implementation and the specific steps you've taken so far. This will help the community and the Ultralytics team better understand the problem you're facing and provide more targeted assistance.

Best regards,

Glenn Jocher

Ejo262 commented 11 months ago

This is my code and I keep getting the same error

import datetime import shutil from pathlib import Path from collections import Counter

import yaml import numpy as np import pandas as pd from ultralytics import YOLO from sklearn.model_selection import KFold

dataset_path = Path(r'C:\Users\kadir\tel-kalıp3\datasets') labels = sorted(dataset_path.rglob("labels/*.txt"))

yaml_file = r'C:\Users\kadir\tel-kalıp3\custom.yaml' with open(yaml_file, 'r', encoding="utf8") as y: classes = yaml.safe_load(y)['names'] cls_idx = list(range(len(classes)))

indx = [l.stem for l in labels] labels_df = pd.DataFrame([], columns=cls_idx, index=indx) for label in labels: lbl_counter = Counter()

with open(label,'r') as lf:
    lines = lf.readlines()

for l in lines:
    lbl_counter[int(l.split(' ')[0])] += 1

labels_df.loc[label.stem] = lbl_counter

labels_df = labels_df.fillna(0.0)

ksplit = 5 kf = KFold(n_splits=ksplit, shuffle=True, random_state=20)

kfolds = list(kf.split(labels_df))

folds = [f'split_{n}' for n in range(1, ksplit + 1)] folds_df = pd.DataFrame(index=indx, columns=folds)

for idx, (train, val) in enumerate(kfolds, start=1): foldsdf[f'split{idx}'].loc[labels_df.iloc[train].index] = 'train' foldsdf[f'split{idx}'].loc[labels_df.iloc[val].index] = 'val'

fold_lbl_distrb = pd.DataFrame(index=folds, columns=cls_idx)

for n, (train_indices, val_indices) in enumerate(kfolds, start=1): train_totals = labels_df.iloc[train_indices].sum() val_totals = labels_df.iloc[val_indices].sum()

ratio = val_totals / (train_totals + 1E-7)
fold_lbl_distrb.loc[f'split_{n}'] = ratio

supported_extensions = ['.jpg', '.jpeg', '.png']

images = []

for ext in supported_extensions: images.extend(sorted((dataset_path / 'images').rglob(f"*{ext}")))

save_path = Path(datasetpath / f'{datetime.date.today().isoformat()}{ksplit}-Fold_Cross-val') save_path.mkdir(parents=True, exist_ok=True) ds_yamls = []

for split in folds_df.columns: split_dir = save_path / split split_dir.mkdir(parents=True, exist_ok=True) (split_dir / 'train' / 'images').mkdir(parents=True, exist_ok=True) (split_dir / 'train' / 'labels').mkdir(parents=True, exist_ok=True) (split_dir / 'val' / 'images').mkdir(parents=True, exist_ok=True) (split_dir / 'val' / 'labels').mkdir(parents=True, exist_ok=True)

dataset_yaml = split_dir / f'{split}_dataset.yaml'
ds_yamls.append(dataset_yaml)

with open(dataset_yaml, 'w') as ds_y:
    yaml.safe_dump({
        'path': split_dir.as_posix(),
        'train': 'train',
        'val': 'val',
        'names': classes
    }, ds_y)

for image, label in zip(images, labels): for split, k_split in folds_df.loc[image.stem].items(): img_to_path = save_path / split / k_split / 'images' lbl_to_path = save_path / split / k_split / 'labels'

    shutil.copy(image, img_to_path / image.name)
    shutil.copy(label, lbl_to_path / label.name)

weights_path = 'yolov8n.pt' model = YOLO(weights_path, task='detect')

results = {}

batch = 8 project = 'kfold_demo' epochs = 100

for k in range(ksplit): dataset_yaml = ds_yamls[k] model.train(data=dataset_yaml,epochs=epochs, batch=batch, project=project, degrees = 0, fliplr = 0.0, imgsz=640) # include any train arguments results[k] = model.metrics

glenn-jocher commented 11 months ago

@Ejo262 it seems that you are encountering a "ValueError: shape mismatch" error in your code. This error typically occurs when there is a mismatch in the dimensions of arrays or tensors being operated on.

In your specific case, the error is likely caused by a shape mismatch between a value array and an indexing result. The value array should have a shape of (6,), while the indexing result should have a shape of (6,2).

To resolve this issue, you can check the following:

  1. Make sure that the dimensions of the value array and the indexing result match. Check if there are any inconsistencies or errors when creating or modifying these arrays.

  2. Verify that the input data used in the cross-validation process has the expected dimensions and format. Ensure that the data is structured correctly and matches the required input shape.

  3. Double-check your code implementation in the cross-validation process. Look for any potential mistakes or inconsistencies that could lead to a shape mismatch.

If you still face issues, please provide more details about the specific line or section of code where the error occurs. This will help the community and the Ultralytics team provide more targeted assistance.

Keep up the good work!

-Glenn Jocher, Ultralytics YOLOv5 Team

github-actions[bot] commented 10 months ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐