I am training a model of YOLOv8 on a custom dataset which is DeepFashion2 dataset. The dataset has 13 classes. and I gave the nc=13 in the yaml file, but when I start the training I found the error of IndexError: index 13 is out of bounds for dimension 1 with size 13
I tried to solve the problem by minimizing the nc to be 12, but that does not work and displays another error that the nc should match the classes number
Here is the yaml file:
train: E:/DeepFashion2Dataset/training/train/images
val: E:/DeepFashion2Dataset/val/images
test: E:/DeepFashion2Dataset/test/images
nc: 13
names: ['short sleeve top', 'long sleeve top', 'short sleeve outwear', 'long sleeve outwear', 'vest', 'sling', 'shorts', 'trousers', 'skirt', 'short sleeve dress', 'long sleeve dress', 'vest dress', 'sling dress']
Here is the training code:
pip install ultralytics
from ultralytics import YOLO
# Load a model
#model = YOLO("yolov8n.yaml") # build a new model from scratch
model=YOLO()
#model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
model.train(data="E:/DeepFashion2Dataset/training/data.yaml", epochs=30, task="detect")
Print("Done Successfully...")
the ourput:
Ultralytics YOLOv8.0.85 Python-3.11.1 torch-2.0.0+cpu CPU
yolo\engine\trainer: task=detect, mode=train, model=yolov8n.pt, data=E:/DeepFashion2Dataset/training/data.yaml, epochs=30, patience=50, batch=16, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=8, project=None, name=None, exist_ok=False, pretrained=False, optimizer=SGD, verbose=True, seed=0, deterministic=True, single_cls=False, image_weights=False, rect=False, cos_lr=False, close_mosaic=0, resume=False, amp=True, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, show=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, vid_stride=1, line_thickness=3, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, boxes=True, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, cfg=None, v5loader=False, tracker=botsort.yaml, save_dir=runs\detect\train15
Overriding model.yaml nc=80 with nc=13
from n params module arguments
0 -1 1 464 ultralytics.nn.modules.Conv [3, 16, 3, 2]
1 -1 1 4672 ultralytics.nn.modules.Conv [16, 32, 3, 2]
2 -1 1 7360 ultralytics.nn.modules.C2f [32, 32, 1, True]
3 -1 1 18560 ultralytics.nn.modules.Conv [32, 64, 3, 2]
4 -1 2 49664 ultralytics.nn.modules.C2f [64, 64, 2, True]
5 -1 1 73984 ultralytics.nn.modules.Conv [64, 128, 3, 2]
6 -1 2 197632 ultralytics.nn.modules.C2f [128, 128, 2, True]
7 -1 1 295424 ultralytics.nn.modules.Conv [128, 256, 3, 2]
8 -1 1 460288 ultralytics.nn.modules.C2f [256, 256, 1, True]
9 -1 1 164608 ultralytics.nn.modules.SPPF [256, 256, 5]
10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
11 [-1, 6] 1 0 ultralytics.nn.modules.Concat [1]
12 -1 1 148224 ultralytics.nn.modules.C2f [384, 128, 1]
13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
14 [-1, 4] 1 0 ultralytics.nn.modules.Concat [1]
15 -1 1 37248 ultralytics.nn.modules.C2f [192, 64, 1]
16 -1 1 36992 ultralytics.nn.modules.Conv [64, 64, 3, 2]
17 [-1, 12] 1 0 ultralytics.nn.modules.Concat [1]
18 -1 1 123648 ultralytics.nn.modules.C2f [192, 128, 1]
19 -1 1 147712 ultralytics.nn.modules.Conv [128, 128, 3, 2]
20 [-1, 9] 1 0 ultralytics.nn.modules.Concat [1]
21 -1 1 493056 ultralytics.nn.modules.C2f [384, 256, 1]
22 [15, 18, 21] 1 753847 ultralytics.nn.modules.Detect [13, [64, 128, 256]]
Model summary: 225 layers, 3013383 parameters, 3013367 gradients, 8.2 GFLOPs
Transferred 319/355 items from pretrained weights
optimizer: SGD(lr=0.01) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.0005), 63 bias
train: Scanning E:\DeepFashion2Dataset\training\train\labels.cache... 191961 images, 0 backgrounds, 0 corrupt: 100%|███
val: Scanning E:\DeepFashion2Dataset\val\labels.cache... 32153 images, 0 backgrounds, 0 corrupt: 100%|██████████| 32153
Plotting labels to runs\detect\train15\labels.jpg...
Image sizes 640 train, 640 val
Using 0 dataloader workers
Logging results to runs\detect\train15
Starting training for 30 epochs...
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
0%| | 0/11998 [00:03<?, ?it/s]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[22], line 7
5 model=YOLO()
6 #model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
----> 7 model.train(data="E:/DeepFashion2Dataset/training/data.yaml", epochs=30, task="detect")
10 Print("Done Successfully...")
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\engine\model.py:370, in YOLO.train(self, **kwargs)
368 self.model = self.trainer.model
369 self.trainer.hub_session = self.session # attach optional HUB session
--> 370 self.trainer.train()
371 # Update model and cfg after training
372 if RANK in (-1, 0):
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\engine\trainer.py:191, in BaseTrainer.train(self)
189 ddp_cleanup(self, str(file))
190 else:
--> 191 self._do_train(world_size)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\engine\trainer.py:322, in BaseTrainer._do_train(self, world_size)
320 batch = self.preprocess_batch(batch)
321 preds = self.model(batch['img'])
--> 322 self.loss, self.loss_items = self.criterion(preds, batch)
323 if RANK != -1:
324 self.loss *= world_size
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\v8\detect\train.py:77, in DetectionTrainer.criterion(self, preds, batch)
75 if not hasattr(self, 'compute_loss'):
76 self.compute_loss = Loss(de_parallel(self.model))
---> 77 return self.compute_loss(preds, batch)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\v8\detect\train.py:189, in Loss.__call__(self, preds, batch)
186 # pboxes
187 pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4)
--> 189 _, target_bboxes, target_scores, fg_mask, _ = self.assigner(
190 pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype),
191 anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt)
193 target_bboxes /= stride_tensor
194 target_scores_sum = max(target_scores.sum(), 1)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\nn\modules\module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\utils\_contextlib.py:115, in context_decorator.<locals>.decorate_context(*args, **kwargs)
112 @functools.wraps(func)
113 def decorate_context(*args, **kwargs):
114 with ctx_factory():
--> 115 return func(*args, **kwargs)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\utils\tal.py:111, in TaskAlignedAssigner.forward(self, pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt)
106 device = gt_bboxes.device
107 return (torch.full_like(pd_scores[..., 0], self.bg_idx).to(device), torch.zeros_like(pd_bboxes).to(device),
108 torch.zeros_like(pd_scores).to(device), torch.zeros_like(pd_scores[..., 0]).to(device),
109 torch.zeros_like(pd_scores[..., 0]).to(device))
--> 111 mask_pos, align_metric, overlaps = self.get_pos_mask(pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points,
112 mask_gt)
114 target_gt_idx, fg_mask, mask_pos = select_highest_overlaps(mask_pos, overlaps, self.n_max_boxes)
116 # Assigned target
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\utils\tal.py:132, in TaskAlignedAssigner.get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt)
130 mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes)
131 # Get anchor_align metric, (b, max_num_obj, h*w)
--> 132 align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_in_gts * mask_gt)
133 # Get topk_metric mask, (b, max_num_obj, h*w)
134 mask_topk = self.select_topk_candidates(align_metric, topk_mask=mask_gt.repeat([1, 1, self.topk]).bool())
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics\yolo\utils\tal.py:151, in TaskAlignedAssigner.get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_gt)
149 ind[1] = gt_labels.long().squeeze(-1) # b, max_num_obj
150 # Get the scores of each grid for each gt cls
--> 151 bbox_scores[mask_gt] = pd_scores[ind[0], :, ind[1]][mask_gt] # b, max_num_obj, h*w
153 # (b, max_num_obj, 1, 4), (b, 1, h*w, 4)
154 pd_boxes = pd_bboxes.unsqueeze(1).repeat(1, self.n_max_boxes, 1, 1)[mask_gt]
IndexError: index 13 is out of bounds for dimension 1 with size 13
DeepFashion2 is indexed 1-13, not 0-12, so your labels with id 13 are out of bounds. Setting nc=14 (hence making acceptable values 0-13) should let you run the code and confirm this is the problem.