Changes to Code
Browse files- README.md +14 -14
- app.py +180 -164
- train_YOLOv11.ipynb +369 -0
README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
-
---
|
2 |
-
title: YOLO
|
3 |
-
emoji: π
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: purple
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.44.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: mit
|
11 |
-
short_description: Inference for various YOLO11 trained models in contexts.
|
12 |
-
---
|
13 |
-
|
14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
---
|
2 |
+
title: YOLO-Application Toolkit
|
3 |
+
emoji: π
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.44.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
short_description: Inference for various YOLO11 trained models in contexts.
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -1,165 +1,181 @@
|
|
1 |
-
from typing import Tuple
|
2 |
-
import gradio as gr
|
3 |
-
import numpy as np
|
4 |
-
import supervision as sv
|
5 |
-
from ultralytics import YOLO
|
6 |
-
import os
|
7 |
-
|
8 |
-
MARKDOWN = """
|
9 |
-
<h1 style='text-align: left'>YOLO
|
10 |
-
<p>Welcome to the YOLO
|
11 |
-
|
12 |
-
Easily detect objects in images
|
13 |
-
|
14 |
-
|
15 |
-
**YOLO11**
|
16 |
-
<div style="display: flex; align-items: center;">
|
17 |
-
<a href="https://docs.ultralytics.com/models/yolo11/" style="margin-right: 10px;">
|
18 |
-
<img src="https://badges.aleen42.com/src/github.svg">
|
19 |
-
</a>
|
20 |
-
<a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolo11-object-detection-on-custom-dataset.ipynb?ref=blog.roboflow.com" style="margin-right: 10px;">
|
21 |
-
<img src="https://colab.research.google.com/assets/colab-badge.svg">
|
22 |
-
</a>
|
23 |
-
</div>
|
24 |
-
|
25 |
-
Powered by
|
26 |
-
[
|
27 |
-
|
28 |
-
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"
|
39 |
-
"
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
"
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
)
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
"
|
116 |
-
"
|
117 |
-
"
|
118 |
-
|
119 |
-
)
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
)
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
demo.launch(debug=False, show_error=True, max_threads=1)
|
|
|
1 |
+
from typing import Tuple
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import supervision as sv
|
5 |
+
from ultralytics import YOLO
|
6 |
+
import os
|
7 |
+
|
8 |
+
MARKDOWN = """
|
9 |
+
<h1 style='text-align: left'>YOLO-Application Toolkit π</h1>
|
10 |
+
<p>Welcome to the YOLO-Application Toolkit! This demo highlights the powerful detection capabilities of various YOLO models pre-trained on different datasets. π
|
11 |
+
|
12 |
+
Easily detect different objects for various contexts in images on the go. Perfect for quick experimentation and practical use. ππ</p>
|
13 |
+
|
14 |
+
|
15 |
+
**YOLO11**
|
16 |
+
<div style="display: flex; align-items: center;">
|
17 |
+
<a href="https://docs.ultralytics.com/models/yolo11/" style="margin-right: 10px;">
|
18 |
+
<img src="https://badges.aleen42.com/src/github.svg">
|
19 |
+
</a>
|
20 |
+
<a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolo11-object-detection-on-custom-dataset.ipynb?ref=blog.roboflow.com" style="margin-right: 10px;">
|
21 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg">
|
22 |
+
</a>
|
23 |
+
</div>
|
24 |
+
|
25 |
+
Powered by
|
26 |
+
[Ultralytics](https://github.com/ultralytics/ultralytics).π₯
|
27 |
+
|
28 |
+
"""
|
29 |
+
# Roboflow [Inference](https://github.com/roboflow/inference), [Supervision](https://github.com/roboflow/supervision) and [Ultralytics](https://github.com/ultralytics/ultralytics).π₯
|
30 |
+
|
31 |
+
# Load models dynamically
|
32 |
+
|
33 |
+
MODELS = {
|
34 |
+
"YOLO11m (COCO128)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m.pt"),
|
35 |
+
"American Sign Language (ASL) (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_sign_language.pt"),
|
36 |
+
# "Microscopic Cell Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_microscope_cells.pt"),
|
37 |
+
"Website Screenshots (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_website_screenshots.pt"),
|
38 |
+
"Zoo Animals (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_zoo_animals.pt"),
|
39 |
+
"Pinned Circuit Boards (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_circuit_boards.pt"),
|
40 |
+
"Smoke Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_smoke_detection.pt"),
|
41 |
+
"Blood Cell Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_blood_cells.pt"),
|
42 |
+
"Coins Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_coins.pt"),
|
43 |
+
"Pizza Toppings Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_pizza.pt"),
|
44 |
+
"Aquarium Fish Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_aquarium_fish.pt"),
|
45 |
+
"Pelvis X-ray Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_pelvis_xray.pt"),
|
46 |
+
"Road Signs Detection (YOLO11m)": YOLO("https://huggingface.co/mbar0075/YOLO-Application-Toolkit/resolve/main/yolo11m_road_signs.pt"),
|
47 |
+
}
|
48 |
+
|
49 |
+
|
50 |
+
example_dir = "https://huggingface.co/spaces/mbar0075/YOLO-Application-Toolkit/resolve/main/examples/"
|
51 |
+
# Your existing example dictionary
|
52 |
+
EXAMPLE_DICT = {
|
53 |
+
"YOLO11m (COCO128)": example_dir + "1.jpg",
|
54 |
+
"American Sign Language (ASL) (YOLO11m)": example_dir + "2.jpg",
|
55 |
+
# "Microscopic Cell Detection (YOLO11m)": example_dir + "3.jpg",
|
56 |
+
"Website Screenshots (YOLO11m)": example_dir + "4.jpg",
|
57 |
+
"Zoo Animals (YOLO11m)": example_dir + "5.jpg",
|
58 |
+
"Pinned Circuit Boards (YOLO11m)": example_dir + "6.jpg",
|
59 |
+
"Smoke Detection (YOLO11m)": example_dir + "7.jpg",
|
60 |
+
"Blood Cell Detection (YOLO11m)": example_dir + "8.jpg",
|
61 |
+
"Coins Detection (YOLO11m)": example_dir + "9.jpeg",
|
62 |
+
"Pizza Toppings Detection (YOLO11m)": example_dir + "10.jpg",
|
63 |
+
"Aquarium Fish Detection (YOLO11m)": example_dir + "11.jpg",
|
64 |
+
"Pelvis X-ray Detection (YOLO11m)": example_dir + "12.jpg",
|
65 |
+
"Road Signs Detection (YOLO11m)": example_dir + "13.jpg",
|
66 |
+
}
|
67 |
+
|
68 |
+
LABEL_ANNOTATORS = sv.LabelAnnotator()
|
69 |
+
BOUNDING_BOX_ANNOTATORS = sv.BoxAnnotator()
|
70 |
+
|
71 |
+
def detect_and_annotate(
|
72 |
+
model,
|
73 |
+
input_image: np.ndarray,
|
74 |
+
confidence_threshold: float,
|
75 |
+
iou_threshold: float,
|
76 |
+
class_id_mapping: dict = None
|
77 |
+
) -> np.ndarray:
|
78 |
+
result = model(input_image, conf=confidence_threshold, iou=iou_threshold)[0]
|
79 |
+
# Extracting Annotated Image
|
80 |
+
return result.plot()
|
81 |
+
|
82 |
+
# For supervision annotations:
|
83 |
+
detections = sv.Detections.from_ultralytics(result)
|
84 |
+
|
85 |
+
if class_id_mapping:
|
86 |
+
detections.class_id = np.array([class_id_mapping[class_id] for class_id in detections.class_id])
|
87 |
+
|
88 |
+
labels = [f"{class_name} ({confidence:.2f})" for class_name, confidence in zip(detections['class_name'], detections.confidence)]
|
89 |
+
|
90 |
+
annotated_image = input_image.copy()
|
91 |
+
annotated_image = BOUNDING_BOX_ANNOTATORS.annotate(scene=annotated_image, detections=detections)
|
92 |
+
annotated_image = LABEL_ANNOTATORS.annotate(scene=annotated_image, detections=detections, labels=labels)
|
93 |
+
return annotated_image
|
94 |
+
|
95 |
+
def process_image(
|
96 |
+
input_image,
|
97 |
+
yolov11_confidence_threshold: float,
|
98 |
+
iou_threshold: float,
|
99 |
+
model_name: str
|
100 |
+
) -> np.ndarray:
|
101 |
+
# Load the selected model from the preloaded models
|
102 |
+
model = MODELS[model_name]
|
103 |
+
|
104 |
+
# Process the image
|
105 |
+
return detect_and_annotate(model, np.array(input_image), yolov11_confidence_threshold, iou_threshold)
|
106 |
+
|
107 |
+
# Gradio UI components
|
108 |
+
yolo_11s_confidence_threshold_component = gr.Slider(
|
109 |
+
minimum=0,
|
110 |
+
maximum=1.0,
|
111 |
+
value=0.3,
|
112 |
+
step=0.01,
|
113 |
+
label="YOLO11m Confidence Threshold",
|
114 |
+
info=(
|
115 |
+
"The confidence threshold for the YOLO model. Lower the threshold to "
|
116 |
+
"reduce false negatives, enhancing the model's sensitivity to detect "
|
117 |
+
"sought-after objects. Conversely, increase the threshold to minimize false "
|
118 |
+
"positives, preventing the model from identifying objects it shouldn't."
|
119 |
+
)
|
120 |
+
)
|
121 |
+
|
122 |
+
iou_threshold_component = gr.Slider(
|
123 |
+
minimum=0,
|
124 |
+
maximum=1.0,
|
125 |
+
value=0.5,
|
126 |
+
step=0.01,
|
127 |
+
label="IoU Threshold",
|
128 |
+
info=(
|
129 |
+
"The Intersection over Union (IoU) threshold for non-maximum suppression. "
|
130 |
+
"Decrease the value to lessen the occurrence of overlapping bounding boxes, "
|
131 |
+
"making the detection process stricter. On the other hand, increase the value "
|
132 |
+
"to allow more overlapping bounding boxes, accommodating a broader range of "
|
133 |
+
"detections."
|
134 |
+
)
|
135 |
+
)
|
136 |
+
|
137 |
+
model_dropdown = gr.Dropdown(
|
138 |
+
choices=list(MODELS.keys()),
|
139 |
+
label="Select Model",
|
140 |
+
value="YOLO11m (COCO128)",
|
141 |
+
info=(
|
142 |
+
"Choose the YOLO model you want to use for object detection. Each model is "
|
143 |
+
"trained on a specific dataset, making them suitable for various detection tasks."
|
144 |
+
)
|
145 |
+
)
|
146 |
+
|
147 |
+
def update_example(model_name):
|
148 |
+
return EXAMPLE_DICT[model_name]
|
149 |
+
|
150 |
+
with gr.Blocks() as demo:
|
151 |
+
gr.Markdown(MARKDOWN)
|
152 |
+
|
153 |
+
with gr.Accordion("Configuration", open=False):
|
154 |
+
yolo_11s_confidence_threshold_component.render()
|
155 |
+
iou_threshold_component.render()
|
156 |
+
|
157 |
+
with gr.Row():
|
158 |
+
model_dropdown.render()
|
159 |
+
|
160 |
+
with gr.Row():
|
161 |
+
image_input_component = gr.Image(type='pil', label='Input Image')
|
162 |
+
yolo_11s_output_component = gr.Image(type='pil', label='YOLO11s Output')
|
163 |
+
|
164 |
+
submit_button = gr.Button(value='Submit', scale=1, variant='primary')
|
165 |
+
|
166 |
+
gr.Examples(
|
167 |
+
fn=process_image,
|
168 |
+
examples=[[EXAMPLE_DICT[i], 0.3, 0.5, i] for i in EXAMPLE_DICT.keys()],
|
169 |
+
inputs=[image_input_component, yolo_11s_confidence_threshold_component, iou_threshold_component, model_dropdown],
|
170 |
+
outputs=[yolo_11s_output_component]
|
171 |
+
)
|
172 |
+
|
173 |
+
model_dropdown.change(fn=update_example, inputs=model_dropdown, outputs=image_input_component)
|
174 |
+
|
175 |
+
submit_button.click(
|
176 |
+
fn=process_image,
|
177 |
+
inputs=[image_input_component, yolo_11s_confidence_threshold_component, iou_threshold_component, model_dropdown],
|
178 |
+
outputs=[yolo_11s_output_component]
|
179 |
+
)
|
180 |
+
|
181 |
demo.launch(debug=False, show_error=True, max_threads=1)
|
train_YOLOv11.ipynb
ADDED
@@ -0,0 +1,369 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"<center><h1>YOLO11 Training</h1>\n",
|
8 |
+
"<h2>Matthias Bartolo</h2>\n",
|
9 |
+
"\n",
|
10 |
+
"</center>"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "markdown",
|
15 |
+
"metadata": {},
|
16 |
+
"source": [
|
17 |
+
"<h3>Package Imports</h3>"
|
18 |
+
]
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"cell_type": "code",
|
22 |
+
"execution_count": 1,
|
23 |
+
"metadata": {
|
24 |
+
"colab": {
|
25 |
+
"base_uri": "https://localhost:8080/",
|
26 |
+
"height": 1000
|
27 |
+
},
|
28 |
+
"id": "aFbKjvakDnZE",
|
29 |
+
"outputId": "dbe28130-1112-4141-e265-65d3f6b06acc"
|
30 |
+
},
|
31 |
+
"outputs": [],
|
32 |
+
"source": [
|
33 |
+
"# !pip install --upgrade roboflow ultralytics"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "markdown",
|
38 |
+
"metadata": {
|
39 |
+
"id": "dWZ2DvvEDnZF"
|
40 |
+
},
|
41 |
+
"source": [
|
42 |
+
"**<h3>Required libraries.</h3>**"
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "code",
|
47 |
+
"execution_count": 2,
|
48 |
+
"metadata": {
|
49 |
+
"id": "9Zbf3zFkDnZG"
|
50 |
+
},
|
51 |
+
"outputs": [],
|
52 |
+
"source": [
|
53 |
+
"import torch\n",
|
54 |
+
"import os\n",
|
55 |
+
"import ultralytics\n",
|
56 |
+
"import locale\n",
|
57 |
+
"import glob\n",
|
58 |
+
"import pandas as pd\n",
|
59 |
+
"import matplotlib.pyplot as plt\n",
|
60 |
+
"\n",
|
61 |
+
"from IPython import display\n",
|
62 |
+
"from ultralytics import YOLO\n",
|
63 |
+
"from IPython.display import display, Image\n",
|
64 |
+
"from roboflow import Roboflow\n",
|
65 |
+
"\n",
|
66 |
+
"%matplotlib inline"
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"cell_type": "markdown",
|
71 |
+
"metadata": {
|
72 |
+
"id": "FyRdDYkqAKN4"
|
73 |
+
},
|
74 |
+
"source": [
|
75 |
+
"**<h3>Using GPU if one is available.</h3>**"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": null,
|
81 |
+
"metadata": {},
|
82 |
+
"outputs": [],
|
83 |
+
"source": [
|
84 |
+
"!nvidia-smi"
|
85 |
+
]
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"cell_type": "code",
|
89 |
+
"execution_count": null,
|
90 |
+
"metadata": {
|
91 |
+
"colab": {
|
92 |
+
"base_uri": "https://localhost:8080/"
|
93 |
+
},
|
94 |
+
"id": "dgNdkO48DnZG",
|
95 |
+
"outputId": "11ca77d8-7b09-4d02-a899-0dfdc5aaac2d"
|
96 |
+
},
|
97 |
+
"outputs": [],
|
98 |
+
"source": [
|
99 |
+
"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
|
100 |
+
"print(device)"
|
101 |
+
]
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"cell_type": "code",
|
105 |
+
"execution_count": null,
|
106 |
+
"metadata": {
|
107 |
+
"colab": {
|
108 |
+
"base_uri": "https://localhost:8080/"
|
109 |
+
},
|
110 |
+
"id": "CjpPg4mGKc1v",
|
111 |
+
"outputId": "7035eeee-b2d1-438c-bb57-188bd34ea8eb"
|
112 |
+
},
|
113 |
+
"outputs": [],
|
114 |
+
"source": [
|
115 |
+
"# Retrieving the current working directory\n",
|
116 |
+
"HOME = os.getcwd()\n",
|
117 |
+
"print(HOME)"
|
118 |
+
]
|
119 |
+
},
|
120 |
+
{
|
121 |
+
"cell_type": "markdown",
|
122 |
+
"metadata": {
|
123 |
+
"id": "3C3EO_2zNChu"
|
124 |
+
},
|
125 |
+
"source": [
|
126 |
+
"**<h3>Downloading the Roboflow dataset.</h3>**"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": null,
|
132 |
+
"metadata": {
|
133 |
+
"colab": {
|
134 |
+
"base_uri": "https://localhost:8080/"
|
135 |
+
},
|
136 |
+
"id": "BSd93ZJzZZKt",
|
137 |
+
"outputId": "cd51037c-4df9-415e-ecd8-540168fb544a"
|
138 |
+
},
|
139 |
+
"outputs": [],
|
140 |
+
"source": [
|
141 |
+
"if not os.path.isdir(os.path.join(HOME, 'datasets')):\n",
|
142 |
+
" os.mkdir(os.path.join(HOME, 'datasets'))\n",
|
143 |
+
"os.chdir(os.path.join(HOME, 'datasets'))\n",
|
144 |
+
"\n",
|
145 |
+
"\n",
|
146 |
+
"rf = Roboflow(api_key=\"<ROBOFLOW API KEY>\")\n",
|
147 |
+
"project = rf.workspace(\"<WORKSPACE>\").project(\"<PROJECT>\")\n",
|
148 |
+
"version = project.version(2)\n",
|
149 |
+
"dataset = version.download(\"yolov11\")"
|
150 |
+
]
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"cell_type": "markdown",
|
154 |
+
"metadata": {},
|
155 |
+
"source": [
|
156 |
+
"**<h3>Training the YOLO11 model.</h3>**"
|
157 |
+
]
|
158 |
+
},
|
159 |
+
{
|
160 |
+
"cell_type": "code",
|
161 |
+
"execution_count": null,
|
162 |
+
"metadata": {
|
163 |
+
"colab": {
|
164 |
+
"base_uri": "https://localhost:8080/"
|
165 |
+
},
|
166 |
+
"id": "_WRhnBXjDnZH",
|
167 |
+
"outputId": "09bcb11a-5656-4c53-8564-51b29cc08d6c"
|
168 |
+
},
|
169 |
+
"outputs": [],
|
170 |
+
"source": [
|
171 |
+
"# Specifying the paths\n",
|
172 |
+
"model_path = 'yolo11m.pt'\n",
|
173 |
+
"\n",
|
174 |
+
"yaml_path = dataset.location+\"/data.yaml\"\n",
|
175 |
+
"\n",
|
176 |
+
"# Creating YOLO object\n",
|
177 |
+
"model = YOLO(model_path)\n",
|
178 |
+
"\n",
|
179 |
+
"# Specifying training parameters\n",
|
180 |
+
"num_epochs = 100 # Number of epochs\n",
|
181 |
+
"batch_size = 16 #8 # Adjust based on GPU memory\n",
|
182 |
+
"image_size = 640 # Decrease for faster training\n",
|
183 |
+
"\n",
|
184 |
+
"# Training configuration\n",
|
185 |
+
"train_config = {\n",
|
186 |
+
" 'data': yaml_path,\n",
|
187 |
+
" 'imgsz': image_size,\n",
|
188 |
+
" 'batch': batch_size,\n",
|
189 |
+
" 'epochs': num_epochs,\n",
|
190 |
+
" 'device': 0, # Use GPU 0\n",
|
191 |
+
" # 'workers': 1, # Number of data loading workers\n",
|
192 |
+
" 'optimizer': 'Adam', # Use Adam optimizer\n",
|
193 |
+
" 'cache': True,#'disk', # Cache images for faster training\n",
|
194 |
+
" 'patience': 10, # epochs to wait before decreasing LR\n",
|
195 |
+
" 'val': True, # Run validation during training\n",
|
196 |
+
" 'plots': True, # Run plots during training\n",
|
197 |
+
"}\n",
|
198 |
+
"\n",
|
199 |
+
"# Train the model\n",
|
200 |
+
"model.train(**train_config)\n"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "markdown",
|
205 |
+
"metadata": {
|
206 |
+
"id": "6ODk1VTlevxn"
|
207 |
+
},
|
208 |
+
"source": [
|
209 |
+
"**<h3>Validating the YOLO11 model on the Validation subset.</h3>**"
|
210 |
+
]
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"cell_type": "code",
|
214 |
+
"execution_count": 8,
|
215 |
+
"metadata": {
|
216 |
+
"colab": {
|
217 |
+
"base_uri": "https://localhost:8080/"
|
218 |
+
},
|
219 |
+
"id": "IkrxsRHoV67H",
|
220 |
+
"outputId": "ecb3eac0-3a4f-4df6-fdb0-9db829ad7a76"
|
221 |
+
},
|
222 |
+
"outputs": [],
|
223 |
+
"source": [
|
224 |
+
"locale.getpreferredencoding = lambda: \"UTF-8\"\n",
|
225 |
+
"# !pip install aspose-words"
|
226 |
+
]
|
227 |
+
},
|
228 |
+
{
|
229 |
+
"cell_type": "code",
|
230 |
+
"execution_count": null,
|
231 |
+
"metadata": {
|
232 |
+
"colab": {
|
233 |
+
"base_uri": "https://localhost:8080/"
|
234 |
+
},
|
235 |
+
"id": "YpyuwrNlXc1P",
|
236 |
+
"outputId": "f4718557-cd29-4208-d5fa-ad0776a893b7"
|
237 |
+
},
|
238 |
+
"outputs": [],
|
239 |
+
"source": [
|
240 |
+
"model.val() #This will output a train file however it will be on the validation data"
|
241 |
+
]
|
242 |
+
},
|
243 |
+
{
|
244 |
+
"cell_type": "markdown",
|
245 |
+
"metadata": {},
|
246 |
+
"source": [
|
247 |
+
"**<h3>Validating the YOLO11 model on the Testing subset.</h3>**"
|
248 |
+
]
|
249 |
+
},
|
250 |
+
{
|
251 |
+
"cell_type": "code",
|
252 |
+
"execution_count": null,
|
253 |
+
"metadata": {},
|
254 |
+
"outputs": [],
|
255 |
+
"source": [
|
256 |
+
"model.val(split='test') #This will output a train file however it will be on the test data"
|
257 |
+
]
|
258 |
+
},
|
259 |
+
{
|
260 |
+
"cell_type": "markdown",
|
261 |
+
"metadata": {
|
262 |
+
"id": "i4eASbcWkQBq"
|
263 |
+
},
|
264 |
+
"source": [
|
265 |
+
"**<h3>Testing the YOLO11 model on the Testing subset.</h3>**"
|
266 |
+
]
|
267 |
+
},
|
268 |
+
{
|
269 |
+
"cell_type": "code",
|
270 |
+
"execution_count": null,
|
271 |
+
"metadata": {
|
272 |
+
"colab": {
|
273 |
+
"base_uri": "https://localhost:8080/"
|
274 |
+
},
|
275 |
+
"id": "Wjc1ctZykYuf",
|
276 |
+
"outputId": "38b730a6-7d15-4f9f-b812-e814fc861557"
|
277 |
+
},
|
278 |
+
"outputs": [],
|
279 |
+
"source": [
|
280 |
+
"!yolo task=detect \\\n",
|
281 |
+
"mode=predict \\\n",
|
282 |
+
"model={HOME}/datasets/runs/detect/train1/weights/best.pt \\\n",
|
283 |
+
"source={dataset.location}/test/images \\\n",
|
284 |
+
"save=True"
|
285 |
+
]
|
286 |
+
},
|
287 |
+
{
|
288 |
+
"cell_type": "markdown",
|
289 |
+
"metadata": {
|
290 |
+
"id": "uBkrV5y5X9CH"
|
291 |
+
},
|
292 |
+
"source": [
|
293 |
+
"**<h3>Training Results.</h3>**"
|
294 |
+
]
|
295 |
+
},
|
296 |
+
{
|
297 |
+
"cell_type": "code",
|
298 |
+
"execution_count": null,
|
299 |
+
"metadata": {
|
300 |
+
"colab": {
|
301 |
+
"base_uri": "https://localhost:8080/",
|
302 |
+
"height": 447
|
303 |
+
},
|
304 |
+
"id": "mWCxLBpMbKoQ",
|
305 |
+
"outputId": "722f7b87-4d71-4e44-85aa-0c6a33f08362"
|
306 |
+
},
|
307 |
+
"outputs": [],
|
308 |
+
"source": [
|
309 |
+
"from IPython.display import Image as IPyImage\n",
|
310 |
+
"\n",
|
311 |
+
"IPyImage(filename=f'{HOME}/datasets/runs/detect/train/results.png', width=600)"
|
312 |
+
]
|
313 |
+
},
|
314 |
+
{
|
315 |
+
"cell_type": "markdown",
|
316 |
+
"metadata": {
|
317 |
+
"id": "t6EZwLBNfjKP"
|
318 |
+
},
|
319 |
+
"source": [
|
320 |
+
"**<h3>Testing Resultant Images.</h3>**"
|
321 |
+
]
|
322 |
+
},
|
323 |
+
{
|
324 |
+
"cell_type": "code",
|
325 |
+
"execution_count": null,
|
326 |
+
"metadata": {
|
327 |
+
"colab": {
|
328 |
+
"background_save": true
|
329 |
+
},
|
330 |
+
"id": "mzkcnDekgUWf"
|
331 |
+
},
|
332 |
+
"outputs": [],
|
333 |
+
"source": [
|
334 |
+
"counter =0\n",
|
335 |
+
"limit = 10\n",
|
336 |
+
"for image_path in glob.glob(f'{HOME}/datasets/runs/detect/predict/*.jpg'):\n",
|
337 |
+
" display(Image(filename=image_path))\n",
|
338 |
+
" print(\"\\n\")\n",
|
339 |
+
" counter += 1\n",
|
340 |
+
" if counter == limit:\n",
|
341 |
+
" break"
|
342 |
+
]
|
343 |
+
}
|
344 |
+
],
|
345 |
+
"metadata": {
|
346 |
+
"accelerator": "GPU",
|
347 |
+
"colab": {
|
348 |
+
"provenance": []
|
349 |
+
},
|
350 |
+
"kernelspec": {
|
351 |
+
"display_name": "Python 3",
|
352 |
+
"name": "python3"
|
353 |
+
},
|
354 |
+
"language_info": {
|
355 |
+
"codemirror_mode": {
|
356 |
+
"name": "ipython",
|
357 |
+
"version": 3
|
358 |
+
},
|
359 |
+
"file_extension": ".py",
|
360 |
+
"mimetype": "text/x-python",
|
361 |
+
"name": "python",
|
362 |
+
"nbconvert_exporter": "python",
|
363 |
+
"pygments_lexer": "ipython3",
|
364 |
+
"version": "3.9.19"
|
365 |
+
}
|
366 |
+
},
|
367 |
+
"nbformat": 4,
|
368 |
+
"nbformat_minor": 0
|
369 |
+
}
|