Advertisement
naren_paste

Yolov5_Custom

Sep 26th, 2023
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | Source Code | 0 0
  1. ## Create a Custom Python Script:
  2.  
  3. # Filename: custom_config.py
  4. import os
  5.  
  6. # Define your class names
  7. class_names = ['class1', 'class2', 'class3']
  8.  
  9. # Specify the path to your dataset
  10. data_dir = '/path/to/dataset'
  11.  
  12. # Create a list of image and annotation paths
  13. images = [os.path.join(data_dir, 'images', img) for img in os.listdir(os.path.join(data_dir, 'images')) if img.endswith('.jpg')]
  14. annotations = [os.path.join(data_dir, 'annotations', ann) for ann in os.listdir(os.path.join(data_dir, 'annotations')) if ann.endswith('.txt')]
  15.  
  16. # Create a dictionary for the data configuration
  17. data = {
  18.     'train': images,
  19.     'val': [],  # You can split your dataset into train and validation
  20.     'nc': len(class_names),
  21.     'names': class_names
  22. }
  23. ###########################
  24. # In your training script (e.g., train.py for YOLOv5), import the custom_config.py and use the data dictionary.
  25.  
  26. # Filename: train.py
  27. from custom_config import data, class_names
  28. from pathlib import Path
  29. import yaml
  30.  
  31. # Create a directory for saving YOLOv5 configuration
  32. save_dir = Path('yolov5_data')
  33. save_dir.mkdir(parents=True, exist_ok=True)
  34.  
  35. # Save the custom data configuration as a YAML file
  36. with open(save_dir / 'custom_data.yaml', 'w') as f:
  37.     yaml.dump(data, f)
  38.  
  39. # Now, you can use this custom_data.yaml in YOLOv5 training
  40. !python train.py --data save_dir/custom_data.yaml --cfg models/yolov5s.yaml --batch-size 16 --epochs 50
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement