Training the Rt-DETR model with custom dataset

763 views Asked by At

I am asking this question because I am a little novice in these matters. I want to start the model training with RT-DETR. I am using a custom dataset. But I think I have a problem with my mad.yaml file. I want to start training the model, but it doesn't work. Can you help me?

I first read the data set and then split it. `

import opendatasets as od
import pandas
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
import shutil
import cv2
import os
from tqdm import tqdm

od.download(
    "https://www.kaggle.com/datasets/a2015003713/militaryaircraftdetectiondataset")
%pip install ultralytics
import ultralytics
ultralytics.checks()
classes = np.array(['A10','A400M','AG600','AV8B',
        'B1','B2','B52','Be200',
        'C130','C2','C17','C5','E2','E7','EF2000',
        'F117','F14','F15','F16','F18','F22','F35','F4',
        'JAS39','MQ9','Mig31','Mirage2000','P3','RQ4','Rafale',
        'SR71','Su34','Su57',
        'Tu160','Tu95','Tornado',
        'U2','US2', 'V22','XB70','YF23','Vulcan','J20']
)
print(len(classes))

csv_paths = glob.glob('/content/militaryaircraftdetectiondataset/dataset/*.csv')
jpg_paths = glob.glob('/content/militaryaircraftdetectiondataset/dataset/*.jpg')
csv_paths.sort()
jpg_paths.sort()
print('number of images:',len(csv_paths))


os.makedirs('/content/militaryaircraftdetectiondataset/data/train/images', exist_ok=True)
os.makedirs('/content/militaryaircraftdetectiondataset/data/train/labels', exist_ok=True)

os.makedirs('/content/militaryaircraftdetectiondataset/data/valid/images', exist_ok=True)
os.makedirs('/content/militaryaircraftdetectiondataset/data/valid/labels', exist_ok=True)

os.makedirs('/content/militaryaircraftdetectiondataset/data/test/images', exist_ok=True)
os.makedirs('/content/militaryaircraftdetectiondataset/data/test/labels', exist_ok=True)

# split
for i, (csv_path, jpg_path) in enumerate(tqdm(zip(csv_paths, jpg_paths))):
    annotations = np.array(pd.read_csv(csv_path))
    
    if os.path.basename(csv_path)[0] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'f']:
        jpg_file_path = './yolov7/data/train/images/' + os.path.basename(jpg_path)
        txt_file_path = './yolov7/data/train/labels/' + os.path.basename(csv_path)[:-4]+'.txt'
        shutil.copy(jpg_path, jpg_file_path)
        continue
    elif os.path.basename(csv_path)[0] in ['c', 'd', 'e']:
        jpg_file_path = '/content/militaryaircraftdetectiondataset/data/valid/images/' + os.path.basename(jpg_path)
        txt_file_path = '/content/militaryaircraftdetectiondataset/data/valid/labels/' + os.path.basename(csv_path)[:-4]+'.txt'
        shutil.copy(jpg_path, jpg_file_path)
    else:
        jpg_file_path = './yolov7/data/test/images/' + os.path.basename(jpg_path)
        txt_file_path = './yolov7/data/test/labels/' + os.path.basename(csv_path)[:-4]+'.txt'
        shutil.copy(jpg_path, jpg_file_path)
        continue
    
    with open(txt_file_path, mode='w') as f:
        try:
            for annotation in annotations:
                width = annotation[1]
                height = annotation[2]
                class_name = annotation[3]
                xmin = annotation[4]
                ymin = annotation[5]
                xmax = annotation[6]
                ymax = annotation[7]
                x_center = 0.5*(xmin+xmax)
                y_center = 0.5*(ymin+ymax)
                b_width = xmax - xmin
                b_height= ymax - ymin
                class_num = np.where(classes==class_name)[0][0]
                output_string = '{} {} {} {} {}\n'.format(class_num,
                                                        x_center/width,
                                                        y_center/height,
                                                        b_width/width,
                                                        b_height/height)
                f.write(output_string)
        except:
            print(txt_file_path)
            0/0

print('test:',len(glob.glob('/content/militaryaircraftdetectiondataset/data/test/labels/*.txt')))
print('valid:',len(glob.glob('/content/militaryaircraftdetectiondataset/data/valid/labels/*.txt')))
print('train',len(glob.glob('/content/militaryaircraftdetectiondataset/data/train/labels/*.txt')))

classes_string = ', '.join([f'"{c}"' for c in classes])
with open('/content/drive/MyDrive/Colab Notebooks/ultralytics/mad.yaml', mode='w') as f:
    yaml_string='train: /content/militaryaircraftdetectiondataset/data/train\n'\
              + 'val: /content/militaryaircraftdetectiondataset/data/valid\n'\
              + 'test: /content/militaryaircraftdetectiondataset/data/test\n'\
              + f'nc: {len(classes)}\n'\
              + f'names: [{classes_string}]'
    f.write(yaml_string)
print(yaml_string)

`

And then my mad.yaml file looks like this.

train: /content/militaryaircraftdetectiondataset/data/train
val: /content/militaryaircraftdetectiondataset/data/valid
test: /content/militaryaircraftdetectiondataset/data/test
nc: 43
names: ["A10", "A400M", "AG600", "AV8B", "B1", "B2", "B52", "Be200", "C130", "C2", "C17", "C5", "E2", "E7", "EF2000", "F117", "F14", "F15", "F16", "F18", "F22", "F35", "F4", "JAS39", "MQ9", "Mig31", "Mirage2000", "P3", "RQ4", "Rafale", "SR71", "Su34", "Su57", "Tu160", "Tu95", "Tornado", "U2", "US2", "V22", "XB70", "YF23", "Vulcan", "J20"]

and then I included from ultralytics import RTDETR.

from ultralytics import RTDETR
model = RTDETR('rtdetr-l.pt')
model.info()

Everything has gone great so far, but I would appreciate it if you could tell me how to send my mad.yaml file here before I start the train process.I tried it this way:

results = model.train(data = '/content/drive/MyDrive/Colab Notebooks/ultralytics/mad.yaml', epochs=100, imgsz=640)

However, it gave the following error.

Ultralytics YOLOv8.0.207  Python-3.10.12 torch-2.1.0+cu118 CPU (Intel Xeon 2.20GHz)
    engine/trainer: task=detect, mode=train, model=rtdetr-l.pt, data=/content/drive/MyDrive/Colab Notebooks/ultralytics/mad.yaml, epochs=100, patience=50, batch=16, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=8, project=None, name=train5, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, freeze=None, 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, stream_buffer=False, line_width=None, 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, tracker=botsort.yaml, save_dir=runs/detect/train5
    WARNING ⚠️ no model scale passed. Assuming scale='l'.

                   from  n    params  module                                       arguments                     
  0                  -1  1     25248  ultralytics.nn.modules.block.HGStem          [3, 32, 48]                   
  1                  -1  6    155072  ultralytics.nn.modules.block.HGBlock         [48, 48, 128, 3, 6]           
  2                  -1  1      1408  ultralytics.nn.modules.conv.DWConv           [128, 128, 3, 2, 1, False]    
  3                  -1  6    839296  ultralytics.nn.modules.block.HGBlock         [128, 96, 512, 3, 6]          
  4                  -1  1      5632  ultralytics.nn.modules.conv.DWConv           [512, 512, 3, 2, 1, False]    
  5                  -1  6   1695360  ultralytics.nn.modules.block.HGBlock         [512, 192, 1024, 5, 6, True, False]
  6                  -1  6   2055808  ultralytics.nn.modules.block.HGBlock         [1024, 192, 1024, 5, 6, True, True]
  7                  -1  6   2055808  ultralytics.nn.modules.block.HGBlock         [1024, 192, 1024, 5, 6, True, True]
  8                  -1  1     11264  ultralytics.nn.modules.conv.DWConv           [1024, 1024, 3, 2, 1, False]  
  9                  -1  6   6708480  ultralytics.nn.modules.block.HGBlock         [1024, 384, 2048, 5, 6, True, False]
 10                  -1  1    524800  ultralytics.nn.modules.conv.Conv             [2048, 256, 1, 1, None, 1, 1, False]
 11                  -1  1    789760  ultralytics.nn.modules.transformer.AIFI      [256, 1024, 8]                
 12                  -1  1     66048  ultralytics.nn.modules.conv.Conv             [256, 256, 1, 1]              
 13                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 14                   7  1    262656  ultralytics.nn.modules.conv.Conv             [1024, 256, 1, 1, None, 1, 1, False]
 15            [-2, -1]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 16                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 17                  -1  1     66048  ultralytics.nn.modules.conv.Conv             [256, 256, 1, 1]              
 18                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 19                   3  1    131584  ultralytics.nn.modules.conv.Conv             [512, 256, 1, 1, None, 1, 1, False]
 20            [-2, -1]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 21                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 22                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 23            [-1, 17]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 24                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 25                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 26            [-1, 12]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 27                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 28        [21, 24, 27]  1   7390217  ultralytics.nn.modules.head.RTDETRDecoder    [43, [256, 256, 256]]         
rt-detr-l summary: 673 layers, 32894441 parameters, 32894441 gradients

Transferred 941/941 items from pretrained weights
TensorBoard: Start with 'tensorboard --logdir runs/detect/train5', view at http://localhost:6006/
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/ultralytics/data/base.py in get_img_files(self, img_path)
    117             # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in IMG_FORMATS])  # pathlib
--> 118             assert im_files, f'{self.prefix}No images found in {img_path}'
    119         except Exception as e:

AssertionError: train: No images found in /content/militaryaircraftdetectiondataset/data/train

The above exception was the direct cause of the following exception:

FileNotFoundError                         Traceback (most recent call last)
10 frames
/usr/local/lib/python3.10/dist-packages/ultralytics/data/base.py in get_img_files(self, img_path)
    118             assert im_files, f'{self.prefix}No images found in {img_path}'
    119         except Exception as e:
--> 120             raise FileNotFoundError(f'{self.prefix}Error loading data from {img_path}\n{HELP_URL}') from e
    121         if self.fraction < 1:
    122             im_files = im_files[:round(len(im_files) * self.fraction)]

FileNotFoundError: train: Error loading data from /content/militaryaircraftdetectiondataset/data/train
See https://docs.ultralytics.com/datasets/detect for dataset formatting guidance.

I waited for the model to start training

0

There are 0 answers