634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published
637 | by the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # train-yolov10-custom-data-full-guide
2 |
3 |
4 |
5 |
6 | Train Yolov10 object detection custom data FULL GUIDE | Computer vision tutorial
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Yolov10ObjectDetectionCustomTraining.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "gpuType": "T4"
8 | },
9 | "kernelspec": {
10 | "name": "python3",
11 | "display_name": "Python 3"
12 | },
13 | "language_info": {
14 | "name": "python"
15 | },
16 | "accelerator": "GPU"
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "code",
21 | "execution_count": null,
22 | "metadata": {
23 | "id": "ZJol0bHVnCND"
24 | },
25 | "outputs": [],
26 | "source": [
27 | "### 1. Mount Google Drive ###\n",
28 | "\n",
29 | "from google.colab import drive\n",
30 | "\n",
31 | "drive.mount('/content/gdrive')"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "source": [
37 | "### 2. Prepare data ###\n",
38 | "\n",
39 | "!scp '/content/gdrive/My Drive/Yolov10ObjectDetectionFullGuide/data.zip' '/content/data.zip'\n",
40 | "\n",
41 | "!unzip '/content/data.zip' -d '/content/'"
42 | ],
43 | "metadata": {
44 | "id": "5lvcOogrnENq",
45 | "collapsed": true
46 | },
47 | "execution_count": null,
48 | "outputs": []
49 | },
50 | {
51 | "cell_type": "code",
52 | "source": [
53 | "### 3. Install packages ###\n",
54 | "\n",
55 | "!git clone https://github.com/THU-MIG/yolov10.git\n",
56 | "!cd yolov10 && pip install ."
57 | ],
58 | "metadata": {
59 | "id": "eM4dTakBnGEc",
60 | "collapsed": true
61 | },
62 | "execution_count": null,
63 | "outputs": []
64 | },
65 | {
66 | "cell_type": "code",
67 | "source": [
68 | "### 4. Train model ###\n",
69 | "\n",
70 | "import os\n",
71 | "\n",
72 | "from ultralytics import YOLOv10\n",
73 | "\n",
74 | "\n",
75 | "config_path = '/content/gdrive/My Drive/Yolov10ObjectDetectionFullGuide/config.yaml'\n",
76 | "\n",
77 | "# Load a model\n",
78 | "model = YOLOv10.from_pretrained(\"jameslahm/yolov10n\") # load pre trained model\n",
79 | "\n",
80 | "# Use the model\n",
81 | "model.train(data=config_path, epochs=200, batch=32) # train the model\n"
82 | ],
83 | "metadata": {
84 | "id": "XsoRd4zKnHKm",
85 | "collapsed": true
86 | },
87 | "execution_count": null,
88 | "outputs": []
89 | },
90 | {
91 | "cell_type": "code",
92 | "source": [
93 | "### 5. Download results ###\n",
94 | "\n",
95 | "from google.colab import files\n",
96 | "\n",
97 | "\n",
98 | "!zip -r /content/runs.zip /content/runs\n",
99 | "\n",
100 | "files.download('/content/runs.zip')\n"
101 | ],
102 | "metadata": {
103 | "id": "7ndOIyhz-EYD",
104 | "collapsed": true
105 | },
106 | "execution_count": null,
107 | "outputs": []
108 | }
109 | ]
110 | }
--------------------------------------------------------------------------------
/config.yaml:
--------------------------------------------------------------------------------
1 | path: /content/data # dataset root dir
2 | train: images/train # train images (relative to 'path')
3 | val: images/val # val images (relative to 'path')
4 |
5 | # Classes
6 | names:
7 | 0: duck
8 |
--------------------------------------------------------------------------------
/predict.py:
--------------------------------------------------------------------------------
1 | import cv2
2 | import torch
3 | from ultralytics import YOLOv10 as YOLO
4 |
5 | # Load the YOLOv8 model (choose 'yolov8n.pt', 'yolov8s.pt', etc. for different sizes)
6 | model = YOLO('./runs/detect/train/weights/last.pt') # or another version of YOLOv8 (e.g., yolov8s.pt for small)
7 |
8 | # Load the video file
9 | input_video_path = 'video.mp4'
10 | output_video_path = 'out.mp4'
11 |
12 | # Open the video using OpenCV
13 | video_capture = cv2.VideoCapture(input_video_path)
14 |
15 | # Get video properties
16 | frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
17 | frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
18 | fps = int(video_capture.get(cv2.CAP_PROP_FPS))
19 | total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
20 |
21 | # Define the codec and create VideoWriter object to save output video
22 | fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec
23 | out_video = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
24 |
25 | # Iterate over each frame
26 | frame_count = 0
27 | while video_capture.isOpened():
28 | ret, frame = video_capture.read() # Read a frame
29 | if not ret:
30 | break
31 |
32 | # Apply YOLOv8 object detection
33 | results = model(frame)[0]
34 |
35 | # Iterate through the detections and draw bounding boxes
36 | for result in results.boxes.data.tolist(): # Each detection in the format [x1, y1, x2, y2, conf, class]
37 | x1, y1, x2, y2, conf, cls = result[:6]
38 | label = f'{model.names[cls]} {conf:.2f}'
39 |
40 | # Draw bounding box and label on the frame
41 | if conf > 0.5:
42 | cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 4) # Bounding box
43 | # cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
44 |
45 | # Write the processed frame to the output video
46 | out_video.write(frame)
47 |
48 | # Print progress
49 | frame_count += 1
50 | print(f'Processed frame {frame_count}/{total_frames}')
51 |
52 | # Release resources
53 | video_capture.release()
54 | out_video.release()
55 | cv2.destroyAllWindows()
56 |
57 | print(f'Output video saved to {output_video_path}')
58 |
59 |
--------------------------------------------------------------------------------
/thumbnail.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/computervisioneng/train-yolov10-custom-data-full-guide/1cffb76773d2be70cc2c619295d1c9a552494986/thumbnail.jpg
--------------------------------------------------------------------------------
/train.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 | from ultralytics import YOLOv10
4 |
5 |
6 | config_path = './config.yaml'
7 |
8 | # Load a model
9 | model = YOLOv10.from_pretrained("jameslahm/yolov10n") # load pre trained model
10 |
11 | # Use the model
12 | model.train(data=config_path, epochs=200, batch=32) # train the model
13 |
--------------------------------------------------------------------------------