Building a Brand Detection Model with YOLOv11 in Google Colab

πŸ“¦ Building a Custom Object Detection Model (Brand Detection) with YOLOv11 in Google Colab 




Object detection has become significantly more accessible thanks to modern architectures like YOLO (You Only Look Once). This article walks through the entire process of setting up and training a YOLO model from scratch using custom image dataset — right inside Google Colab.


πŸ“ 1. Uploading and Preparing the Dataset

▶ Mount Google Drive (if your dataset is there)

from google.colab import drive
drive.mount('/content/gdrive')

This gives Colab access to your Google Drive so it can copy your dataset stored there.

▶ Copy the dataset ZIP file from Google Drive

!cp /content/gdrive/MyDrive/yolo/data.zip /content

Copies the ZIP archive containing your dataset into the Colab runtime.


▶ Alternative: Download from a URL


!wget -O /content/data.zip https://drive.google.com/file/d/1Hdrk5_dnJvJPoupPsJ0PaRyLPPDP7b4f/view?usp=sharing/data.zip


Downloads the dataset from a public URL directly into the Colab environment.


πŸ“¦ 2. Preparing the Data for Training

▶ Unzip the Dataset

!unzip -q /content/data.zip -d /content/custom_data

Extracts the dataset contents into a custom folder named (custom_data).


▶ Create Train and Validation Split Automatically

!wget -O /content/train_val_split.py
https://drive.google.com/file/d/1Hdrk5_dnJvJPoupPsJ0PaRyLPPDP7b4f/view?usp=sharing/
train_val_split.py

Downloads a Python script that splits your dataset into 90% training and 10% validation
images, in the correct folder structure expected by YOLO.


⚙️ 3. Install YOLO Training Requirements

!pip install ultralytics


Installs the official ultralytics package, which includes support for YOLOv5,
YOLOv8, and YOLOv11.


πŸ›  4. Create the Configuration File (data.yaml)

▶ Why data.yaml?

YOLO models need a data.yaml file that tells the trainer:

  • Where to find training and validation images

  • How many classes there are

  • What the class names are

▶ Code to Auto-Generate the YAML File

import yaml import os def create_data_yaml(path_to_classes_txt, path_to_data_yaml): # Read class.txt to get class names if not os.path.exists(path_to_classes_txt): print(f'classes.txt file not found! Please create a classes.txt labelmap
    and move it to {path_to_classes_txt}') return with open(path_to_classes_txt, 'r') as f: classes = [] for line in f.readlines(): if len(line.strip()) == 0: continue classes.append(line.strip()) number_of_classes = len(classes) # Create data dictionary data = { 'path': '/content/data', 'train': 'train/images', 'val': 'validation/images', 'nc': number_of_classes, 'names': classes } # Write data to YAML file with open(path_to_data_yaml, 'w') as f: yaml.dump(data, f, sort_keys=False) print(f'Created config file at {path_to_data_yaml}') return # Define path to classes.txt and run function path_to_classes_txt = '/content/custom_data/classes.txt' path_to_data_yaml = '/content/data.yaml' create_data_yaml(path_to_classes_txt, path_to_data_yaml)

This function:
  • Reads class names from classes.txt

  • Builds a dictionary with paths and labels

  • Saves it as data.yaml

▶ Run the Function

create_data_yaml('/content/custom_data/classes.txt', '/content/data.yaml')



🧠 5. Configure Training Settings

You can control:

  • Model size: (yolo11s.pt, yolo11m.pt, etc.)

  • Epochs: How many times to loop through training data

  • Resolution: Image size (e.g. 640x640)


!yolo detect train data=/content/data.yaml model=yolo11s.pt epochs=60 imgsz=640


πŸ’‘ Note: Larger models like yolo11xl.pt offer better accuracy but train slower.

For starters, yolo11s.pt is a good balance.



πŸ“Š 6. Test the Trained Model

Once training is done, the best weights are automatically saved.

!yolo detect predict model=runs/detect/train/weights/best.pt

source=data/validation/images save=True


This runs inference on your validation set using the trained model and saves predictions.

▶ Display Results in Notebook

import glob from IPython.display import Image, display for image_path in glob.glob('/content/runs/detect/predict/*.jpg')[:10]: display(Image(filename=image_path, height=400))


πŸ“· 7. Run Prediction on New Images

▶ Download a test image

!wget -O /content/data/test/images/test.jpg
"https://images-cdn.ubuy.co.in/63526eb9c797ce1c1e1f26b4-snickers-m-amp-m-39-s.jpg"


▶ Predict and Show Result

!yolo detect predict model=runs/detect/train/weights/best.pt source=data/test/images save=True


from IPython.display import Image, display
display(Image(filename='/content/runs/detect/predict2/test.jpg', height=400))

This will show bounding boxes and labels on the new image using the trained model.



Comments

Popular posts from this blog

Building a Handwritten Multi-Digit Recognition