Skip to content

Commit 207e491

Browse files
committed
finished first draft of colab chapter
1 parent 67f5b00 commit 207e491

4 files changed

Lines changed: 52 additions & 216 deletions

File tree

docs/assets/yolo/colab4.png

36.1 KB
Loading

docs/yolo/train/colab.md

Lines changed: 48 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ After we have already learned how to train a YOLO model locally, we will now use
3333
<iframe width="560" height="315" src="https://www.youtube.com/embed/r0RspiLG260?start=465" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
3434
</div>
3535

36-
## Setting up Google Colab
36+
## Google Colab
37+
### Setup
3738

3839
Navigate to [Google Colab](https://colab.research.google.com/) and sign in (right upper corner) with your Google account. Create a new notebook by clicking **New notebook**.
3940

@@ -104,109 +105,56 @@ The last setup step is to install the YOLO library. There are two ways to do thi
104105
105106
---
106107
107-
## Preparing the files
108+
### Preparing the files
108109
109-
Before training, you need to upload your dataset and the configuration file to Colab. There are several approaches:
110+
Before training, you need to upload your dataset and the configuration file to Colab. There are multiple options available to do this, like mounting Google Drive (especially for larger datasets), or downloading from an URL link. We take a closer look on the easiest option: to directly upload the files to the Colab session.
110111
111-
xxxxxxxxxxxxxxx
112-
xxxxxxxxxxxxx
112+
On the left side of colab you see the files icon :material-folder-outline:. By clicking on it, something like a file explorer will open. We can now upload the files by simply dragging and dropping the files into this area. In our case, we want to upload the `annotations` folder and the `config.yaml` file. For both we need to make small changes compared to the local training.
113113
114+
Since the upload only allows files, the `annotations` folder can not be uploaded directly. Therefore we need to create a zip file (here called `annotated.zip`) of the folder and upload this file instead.
115+
The upload can take a few minutes. The progress is shown in the bottom of the file explorer.
114116
115-
xxxxxxxxxxxxxxxx
117+
<figure markdown="span"> ![Colab](../../assets/yolo/colab4.png){width=60% }</figure>
116118
117-
118-
### Option A: Google Drive (Recommended)
119-
120-
The most convenient method is to upload your dataset to Google Drive and mount it in Colab.
121-
122-
**Step 1: Upload to Google Drive**
123-
124-
Upload your entire `annotations` folder (containing `images` and `labels` subfolders) to your Google Drive.
125-
126-
```plaintext
127-
📁 My Drive/
128-
└── 📁 yolo_training/
129-
└── 📁 annotations/
130-
├── 📁 images/
131-
| ├── 📁 train/
132-
| └── 📁 val/
133-
└── 📁 labels/
134-
├── 📁 train/
135-
└── 📁 val/
136-
```
137-
138-
**Step 2: Mount Google Drive in Colab**
119+
In order to unzip the file, we need to run the following code in a new code cell in colab:
139120
140121
```python
141-
from google.colab import drive
142-
drive.mount('/content/drive')
122+
# Unzip images to a custom data folder
123+
!unzip -q /content/annotated.zip -d /content/dataset
143124
```
144125

145-
A popup will ask you to authorize access. After mounting, your files are accessible at `/content/drive/MyDrive/`.
146-
147-
### Option B: Direct Upload (Small Datasets)
148-
149-
For small datasets, you can upload directly to the Colab session:
150-
151-
```python
152-
from google.colab import files
153-
uploaded = files.upload() # Opens file picker
154-
```
126+
After the unzipping, the `annotations` folder should be available in the file explorer.
155127

156128
???+ warning "Session Storage"
157129
Files uploaded directly to Colab are **temporary** and will be deleted when the session ends. Use Google Drive for persistent storage.
158130

159-
### Option C: Download from URL
160-
161-
If your dataset is hosted online (e.g., GitHub, cloud storage):
162-
163-
```python
164-
!wget https://example.com/your-dataset.zip
165-
!unzip your-dataset.zip -d /content/dataset
166-
```
167-
168-
---
169-
170-
## Training in Colab
171131

172-
### Install Ultralytics
132+
The second file we need to upload is the `config.yaml` file. We already created this file in the [training chapter](training.md#configuration-file). We need to make small changes to the path to the dataset.
173133

174-
First, install the YOLO library:
175-
176-
```python
177-
!pip install ultralytics -q
178-
```
179-
180-
The `-q` flag suppresses verbose output.
181-
182-
### Create Configuration File
183-
184-
Create the `config.yaml` file directly in Colab. The paths need to point to your mounted Google Drive location:
185-
186-
```python
187-
config_content = """
134+
```yaml hl_lines="2"
188135
# Data
189-
path: /content/drive/MyDrive/yolo_training/annotations
190-
train: images/train
191-
val: images/val
136+
path: '/content/dataset' # path to your project folder
137+
train: images/train # train images (relative to 'path')
138+
val: images/val # val images (relative to 'path')
139+
#test: # test images (optional) (relative to 'path')
192140

193-
nc: 2
141+
nc: 2 # number of classes
194142

195143
# Classes
196144
names:
197-
0: 10euro
145+
0: 10euro # Name of the Object
198146
1: 5euro
199-
"""
200-
201-
with open('/content/config.yaml', 'w') as f:
202-
f.write(config_content)
203147
```
204148
205-
### Start Training
149+
After uploading both things - the annotations and the config file - we are ready to start training.
150+
151+
---
152+
153+
### Training in Colab
206154
207155
Now you can train your model exactly as you would locally:
208156
209-
```python
157+
```python hl_lines="10"
210158
from ultralytics import YOLO
211159

212160
# Load a pre-trained model
@@ -215,158 +163,46 @@ model = YOLO('yolo11n.pt')
215163
# Train the model
216164
results = model.train(
217165
data='/content/config.yaml',
218-
epochs=50,
219-
imgsz=640,
220-
batch=16,
221-
device=0 # Use GPU
166+
epochs=10,
167+
device=0 # Explicitly tells YOLO to use the GPU
222168
)
223169
```
224170

225-
???+ tip "Colab Training Settings"
226-
- **batch size**: Colab's T4 GPU has ~15GB memory. You can often use `batch=32` or higher
227-
- **device=0**: Explicitly tells YOLO to use the GPU
228-
- **epochs**: With GPU acceleration, you can afford to train for more epochs
229-
230-
### Monitor Training Progress
231-
232-
Training progress is displayed directly in the notebook output. You can also view the generated plots:
233-
234-
```python
235-
from IPython.display import Image, display
236-
237-
# Display training results
238-
display(Image(filename='/content/runs/detect/train/results.png'))
239-
```
240-
241-
---
242-
243-
## Saving Your Trained Model
171+
???+ info "Time Consumption"
172+
Some self performed tests to train the model for 10 epochs on ~300 images showed the following time consumption:
244173

245-
After training completes, you need to save your model before the Colab session expires.
174+
- locally on a CPU (in this case an Intel Core i9-12900): ~8.5 minutes.
175+
- locally on a GPU (in this case a NVIDIA GeForce RTX 3060): ~1.5 minutes.
176+
- in Colab on a Tesla T4 GPU: ~1.5 minutes.
246177

247-
### Copy to Google Drive
178+
What we can see here is that the performance of the training depends massively on the hardware. If you are not in possession of a GPU, it is a good idea to use Colab to train your model.
248179

249-
```python
250-
import shutil
251-
252-
# Copy best model to Google Drive
253-
shutil.copy(
254-
'/content/runs/detect/train/weights/best.pt',
255-
'/content/drive/MyDrive/yolo_training/best.pt'
256-
)
257180

258-
# Copy last model as backup
259-
shutil.copy(
260-
'/content/runs/detect/train/weights/last.pt',
261-
'/content/drive/MyDrive/yolo_training/last.pt'
262-
)
263-
264-
print("Models saved to Google Drive!")
265-
```
181+
### Working with the results
266182

267-
### Download to Local Machine
183+
After the training is finished, we can work with the results just as we did locally. In the colab file explorer you can see the `runs` folder with the same results as explained in the [training chapter](training.md#training-results).
184+
Everything - including the model weights - can be downloaded by right clicking on the file and selecting "Download".
268185

269-
Alternatively, download the model directly to your computer:
186+
How to download all files? You can use the following code in a new code cell in colab:
270187

271188
```python
272-
from google.colab import files
273-
files.download('/content/runs/detect/train/weights/best.pt')
274-
```
275-
276-
---
277-
278-
## Complete Colab Notebook
279-
280-
Here is a complete notebook template you can use:
189+
import os
281190

282-
```python
283-
# Cell 1: Setup
284-
from google.colab import drive
285-
drive.mount('/content/drive')
286-
287-
!pip install ultralytics -q
288-
289-
# Cell 2: Configuration
290-
config_content = """
291-
path: /content/drive/MyDrive/yolo_training/annotations
292-
train: images/train
293-
val: images/val
294-
nc: 2
295-
names:
296-
0: 10euro
297-
1: 5euro
298-
"""
299-
300-
with open('/content/config.yaml', 'w') as f:
301-
f.write(config_content)
302-
303-
# Cell 3: Training
304-
from ultralytics import YOLO
305-
306-
model = YOLO('yolo11n.pt')
307-
results = model.train(
308-
data='/content/config.yaml',
309-
epochs=50,
310-
device=0
311-
)
312-
313-
# Cell 4: Save Model
314-
import shutil
315-
shutil.copy(
316-
'/content/runs/detect/train/weights/best.pt',
317-
'/content/drive/MyDrive/yolo_training/best.pt'
318-
)
319-
print("Training complete! Model saved to Google Drive.")
191+
os.system('zip -r runs.zip runs/detect/trainX')
320192
```
321193

322-
---
323-
324-
## Tips for Colab Training
325-
326-
???+ tip "Prevent Session Timeout"
327-
Free Colab sessions disconnect after ~90 minutes of inactivity. Keep the browser tab active during training. For longer runs, consider [Colab Pro](https://colab.research.google.com/signup).
328-
329-
???+ tip "Check GPU Allocation"
330-
Sometimes Colab assigns a slower GPU or no GPU at all due to high demand. Always verify GPU availability before starting long training runs.
331-
332-
???+ tip "Use Checkpoints"
333-
If training might exceed session limits, save intermediate checkpoints to Google Drive:
334-
```python
335-
model.train(data='config.yaml', epochs=50, save_period=10) # Save every 10 epochs
336-
```
337-
338-
???+ tip "Resume Training"
339-
If your session disconnects, you can resume training from the last checkpoint:
340-
```python
341-
model = YOLO('/content/drive/MyDrive/yolo_training/last.pt')
342-
model.train(data='/content/config.yaml', epochs=50, resume=True)
343-
```
344-
345-
---
346-
347-
## Alternative Cloud Platforms
348-
349-
Google Colab is not the only option. Here are some alternatives:
194+
This will create a zip file of the `runs/detect/trainX` folder which you can then download by right clicking on the file and selecting "Download".
350195

351-
| Platform | Free Tier | GPU Access | Session Limit |
352-
|:---------|:----------|:-----------|:--------------|
353-
| [Google Colab](https://colab.research.google.com/) | Yes | T4/V100 | ~12 hours |
354-
| [Kaggle Notebooks](https://www.kaggle.com/code) | Yes | P100/T4 | 30 hours/week |
355-
| [Lightning AI](https://lightning.ai/) | Yes | T4 | Limited |
356-
| [Paperspace Gradient](https://www.paperspace.com/) | Limited | Various | Varies |
357196

358-
Kaggle is a particularly good alternative with generous GPU quotas and a large community for machine learning.
197+
Now you are all set! You can download you model and try to start the [inference process](inference.md).
359198

360199
---
361200

362-
## Summary
363-
364-
Cloud-based training with Google Colab offers a practical solution when local hardware is limited. The key advantages are:
365201

366-
- **Free GPU access** for faster training
367-
- **No setup required** - pre-configured environment
368-
- **Accessible from anywhere** with just a browser
202+
???+ success "🎉 Congratulations"
203+
You have now trained your own YOLO model on Google Colab. If you to not have a GPU, this is a really good way to speed up the training process.
369204

370-
For our Euro note detection project, Colab enables training that would otherwise be impractical on a CPU-only laptop. The trained model can then be downloaded and used locally for inference.
371-
372-
After completing the training (either locally or in Colab), proceed to the [Inference chapter](./inference.md) to test your model on real images and video streams.
205+
<div style="text-align: center; display: flex; flex-direction: column; align-items: center; margin-bottom: 2rem;">
206+
<div class="tenor-gif-embed" data-postid="12090663477161380777" data-share-method="host" data-aspect-ratio="1" data-width="50%"><a href="https://tenor.com/view/going-fast-fast-riding-fast-riding-fast-bike-gif-12090663477161380777">Going Fast Riding Fast GIF</a>from <a href="https://tenor.com/search/going+fast-gifs">Going Fast GIFs</a></div> <script type="text/javascript" async src="https://tenor.com/embed.js"></script>
207+
<figcaption style="margin-top: 0.5rem;"><i>"Hold on tight!"</i></figcaption>
208+
</div>

docs/yolo/train/index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ Let's get started! 🚀
2525

2626
In addition to the theoretical foundation, we will look at the following chapters using a practical example. This will enable us to better understand and apply the theoretical concepts.
2727

28-
???+ warning "Training Hardware"
29-
Training a YOLO model requires a lot of computational resources. The best way to train a computer vision model is to use a GPU. Since a lot of you might work on a laptop without a GPU you can try to train the model on a CPU, but it will take much longer to train the model.
30-
If your hardware is limited, it is a good idea to use a free online service like [Google Colab](https://colab.research.google.com/) or [Kaggle](https://www.kaggle.com/) to train your model. In the [bonus chapter](colab.md) you will find a guide on how to use Colab to train your model.
31-
3228

3329
## Prerequisites
3430

docs/yolo/train/training.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ Training will take some time, depending on the dataset size, the settings and th
121121
<figcaption>(Source: <a href="https://www.yourtango.com/2019321990/funny-coffee-memes-quotes-march-caffeine-awareness-month">YourTango</a>) </figcaption>
122122
</figure>
123123

124+
???+ warning "Training Hardware"
125+
Training a YOLO model requires a lot of computational resources. The best way to train a computer vision model is to use a GPU. Since a lot of you might work on a laptop without a GPU you can try to train the model on a CPU, but it will take much longer to train the model.
126+
If your hardware is limited, it is a good idea to use a free online service like [Google Colab](https://colab.research.google.com/) or [Kaggle](https://www.kaggle.com/) to train your model. In the [bonus chapter](colab.md) you will find a guide on how to use Colab to train your model.
127+
124128

125129
### Interpreting the Output
126130

0 commit comments

Comments
 (0)