Last Updated on 09/11/2025 by Eran Feit
Effortless YOLOv8 Segmentation Tutorial for Multi-Class Football
This post walks you through an end-to-end YOLOv8 segmentation tutorial focused on multi-class football images.
You’ll start by setting up a clean environment, preparing labeled data with proper YOLOv8 segmentation annotations, training a multi-class instance segmentation model, and finally visualizing colorful masks for each class (players, lines, zones, etc.).
Every piece of code is copy-paste ready and explained line by line so you can adapt it to your own football analytics, broadcast tools, or sports science projects—without getting lost in configuration chaos.
By the end, this tutorial fully delivers on its promise: a friendly, powerful, production-minded workflow for YOLOv8 multi-class segmentation.
Understanding Multi-Class Segmentation with YOLOv8 (Concept Intro)
Training a multi-class model for image segmentation means teaching YOLOv8 to paint different pixel masks for different objects in the same frame—players vs. pitch vs. lines vs. ads vs. ball.
Instead of simple boxes, you get dense pixel-level understanding: which team is where, which areas belong to the field, and how objects interact spatially.
With YOLOv8-seg, you reuse a strong pretrained backbone and only fine-tune on your custom football dataset, which shortens training time and improves stability compared to building a model from scratch.
This approach scales naturally: add more classes as your project grows, from analysis overlays to tactical heatmaps and broadcast augmentation.
If you want more hands-on guides on pixel-level masks and real-world projects, explore my dedicated Image Segmentation tutorials collection for more step-by-step workflows.
You can download the code here : https://ko-fi.com/s/1ed1ea7299
You can find more tutorials in my blog : https://eranfeit.net/blog/
Download the dataset from here : https://www.kaggle.com/datasets/sadhliroomyprime/football-semantic-segmentation
Link for the post in Medium : https://medium.com/@feitgemel/yolov8-segmentation-tutorial-for-multi-class-football-2b7996cda945
Want to get started with Computer Vision or take your skills to the next level ?
If you’re just beginning, I recommend this step-by-step course designed to introduce you to the foundations of Computer Vision – Complete Computer Vision Bootcamp With PyTorch & TensorFlow
If you’re already experienced and looking for more advanced techniques, check out this deep-dive course – Modern Computer Vision GPT, PyTorch, Keras, OpenCV4
What This Tutorial Will Build (Tutorial Intro)
In this YOLOv8 segmentation tutorial, you’ll:
- Create a dedicated environment with compatible PyTorch, CUDA, and Ultralytics YOLOv8 versions.
- Prepare a football segmentation dataset and YOLO-format labels for multiple classes.
- Train
yolov8l-segon your custom config for multi-class segmentation. - Run inference on a test image, generate per-class colored masks, and visualize results side-by-side with the original image.
Everything is structured so you can switch datasets, adjust classes, or point this exact pipeline at different sports or domains with minimal changes.
Part 1 – Setting Up a Stable YOLOv8 Segmentation Environment
Before training any serious YOLOv8 segmentation tutorial pipeline, you want an isolated environment.
A clean Conda env protects your other projects from version conflicts and ensures CUDA, PyTorch, and ultralytics all play nicely together.
This setup targets CUDA 11.8 with a matching PyTorch stack, but if you’re on CPU only, you can keep the same structure and adjust the PyTorch install line.
Once this part is done, you have a reproducible base you can reuse across different multiclass segmentation experiments.
### Create a new Conda environment dedicated to YOLOv8 segmentation. conda create --name YoloV8 python=3.8 ### Activate the YOLOv8 environment so all packages install in isolation. conda activate YoloV8 ### Verify that CUDA toolkit is visible so PyTorch can use your GPU. nvcc --version ### Install PyTorch, TorchVision and TorchAudio compiled for CUDA 11.8. conda install pytorch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 pytorch-cuda=11.8 -c pytorch -c nvidia ### Install the Ultralytics YOLO package for detection and segmentation. pip install ultralytics==8.1.0 ### Remove conflicting headless OpenCV builds that may break cv2. pip uninstall opencv-python-headless -y ### Remove any existing OpenCV install to avoid version clashes. pip uninstall opencv-python -y ### Install a compatible OpenCV build with GUI support. pip install "opencv-python>=4.6.0" Summary (Part 1)
You now have a clean YOLOv8-ready environment tuned for segmentation experiments.
This avoids hidden dependency bugs and ensures your yolov8 segmentation tutorial runs consistently on both your machine and future projects.
New to the YOLOv8 ecosystem? Start with my Complete YOLOv8 Classification Tutorial for Beginners to understand the core workflow before diving deeper into segmentation.
Part 2 – Preparing a Multi-Class Football Segmentation Dataset
High-quality segmentation starts with clean labels.
Here you use a football semantic segmentation dataset and convert its annotations into YOLOv8 segmentation TXT format using a tool like Roboflow, making sure each mask and class is mapped correctly.
The key idea: one folder for images, one structure of YOLO TXT labels, and a YAML file that points YOLOv8 to train/val/test splits and class names.
Getting this step right is what unlocks accurate multi-class masks later—especially when separating players, pitch, lines, and other game elements.
### Create a root folder for your football segmentation project. mkdir -p "C:/Data-sets/Football-Seg.v1i.yolov8" ### Create a folder where all cleaned training images will live. mkdir -p "C:/Data-sets/Football-Seg.v1i.yolov8/images" ### Create a folder for YOLOv8 segmentation label files in TXT format. mkdir -p "C:/Data-sets/Football-Seg.v1i.yolov8/labels" ### (After exporting from your annotation tool) move all images into the images directory. move "path_to_exported_images\*.jpg" "C:/Data-sets/Football-Seg.v1i.yolov8/images" ### Move all YOLOv8 TXT label files into the labels directory. move "path_to_exported_labels\*.txt" "C:/Data-sets/Football-Seg.v1i.yolov8/labels" ### Open your dataset YAML file in a text editor to update image and label paths. notepad "Best-Semantic-Segmentation-models/Yolo-V8/Segment-Multi-Class-Football-Segmentation/config.yaml" Summary (Part 2)
Your dataset is now structured the way YOLOv8 expects for multiclass image segmentation yolo projects.
Each TXT file carries polygon-based masks linked to football-specific classes, setting you up for strong, interpretable segmentation results.
Part 3 – Training the YOLOv8 Multi-Class Segmentation Model
Understanding Your config.yaml – The Brain of Your YOLOv8 Segmentation Setup
Before YOLOv8 can train a powerful multi-class segmentation model, it needs clear instructions on where your data lives and what each class means.
That’s exactly what the config.yaml file does: it acts as the central blueprint that connects your images, label files, and class names into one consistent configuration that YOLOv8 can trust.
Inside this file, you define the paths for your train, val, and optional test image folders, making sure they match the structure created when you exported your dataset and YOLO TXT labels.
You also list your football-related classes in the correct order—players, goalkeeper, referee, ball, pitch, lines, goalposts, ads, crowd, and more—so every segmentation mask is mapped to the right label during training and evaluation.
A clean, well-structured config.yaml is often the difference between a smooth yolov8 segmentation tutorial experience and hours of silent bugs or wrong predictions.
Once this file is correct, YOLOv8 can focus on what it does best: learning accurate, multi-class masks from your football images with zero confusion about where the data is or how it’s labeled.
Inside the YAML, set train/val/test paths to your YOLOv8-ready folders :
“Config.yaml”
train: C:/Data-sets/Football-Seg.v1i.yolov8/train/images val: C:/Data-sets/Football-Seg.v1i.yolov8/valid/images test: C:/Data-sets/Football-Seg.v1i.yolov8/test/images nc: 10 names: ['Advertisements', 'Audience', 'Ball', 'Goal Bar', 'Goalkeeper A', 'Goalkeeper B', 'Ground', 'Referee', 'Team A', 'Team B'] If you are working with polygon masks and custom datasets, you may also like my Detectron2 Custom Dataset Training Made Easy tutorial, which covers a similar annotation-to-training pipeline.
Now you fine-tune yolov8l-seg.pt on your football dataset.
This configuration uses a clear project folder, named experiment, robust batch size, and early stopping with patience, making the training process both powerful and safe for longer runs.
You can tweak epochs, image size, and batch size depending on your GPU memory, but this script is a strong starting point for most users.
The goal is a model that understands multiple football classes at pixel level, not just bounding boxes.
### Import the YOLO class from the Ultralytics package to handle training and inference. from ultralytics import YOLO ### Define the main function to keep the training pipeline organized and reusable. def main(): ### Load the pretrained YOLOv8 large segmentation model as a strong starting point. model = YOLO('yolov8l-seg.pt') ### Set the base directory where training runs, logs, and weights will be stored. project = "C:/Data-sets/Football-Seg.v1i.yolov8" ### Name this specific experiment so multiple runs stay neatly separated. experiment = "My-model" ### Choose a batch size that balances speed and GPU memory usage. batchSize = 16 ### Launch model training with your custom YAML configuration. results = model.train( ### Path to the dataset configuration file with classes and paths. data="Best-Semantic-Segmentation-models/Yolo-V8/Segment-Multi-Class-Football-Segmentation/config.yaml", ### Number of epochs to allow the model to learn complex patterns. epochs=200, ### Folder where results will be saved. project=project, ### Subfolder name inside the project directory. name=experiment, ### Batch size for each training step. batch=batchSize, ### Device index: 0 means first GPU; use 'cpu' if no GPU is available. device=0, ### Training and inference image size (square). imgsz=640, ### Early stopping patience to halt when validation stops improving. patience=30, ### Print rich training logs to monitor progress. verbose=True, ### Enable validation during training to track real performance. val=True ) ### Standard Python entry point to run training when this script is executed. if __name__ == '__main__': main() Summary (Part 3)
This training script converts your dataset into a robust yolov8 instance segmentation model tuned for football scenes.
You now own weights that can separate multiple classes directly on real match images.
For another practical example of YOLOv8 segmentation in action, check out my YOLOv8 Segmentation Tutorial for Real Flood Detection, where the same concepts are applied to satellite and map imagery.
Part 4 – Testing the Model and Visualizing Multi-Class Masks
Training is only useful if you can see what the model actually predicts.
In this step, you load the trained weights, run them on a sample football image, and create a colorful mask overlay per class.
Random but consistent colors help you visually inspect each detected object while confirming that IDs map correctly to semantic classes.
Finally, you display original vs. mask side by side using Matplotlib, making it easy to debug and present results.
Here is our test image :

### Import YOLO for loading the trained segmentation model. from ultralytics import YOLO ### Import NumPy for numerical operations on masks and arrays. import numpy as np ### Import OpenCV for image loading, resizing, and saving. import cv2 ### Import random to generate distinct colors for each class. import random ### Define the path to the trained YOLOv8 segmentation weights. model_path = "C:/Data-sets/Football-Seg.v1i.yolov8/My-model/weights/best.pt" ### Define the path to a sample football image for visualization. image_path = "Best-Semantic-Segmentation-models/Yolo-V8/Segment-Multi-Class-Football-Segmentation/football_test.png" ### Read the input image from disk in BGR format. img = cv2.imread(image_path) ### Extract the image height, width, and number of channels. H, W, _ = img.shape ### Print the image shape to verify it loaded correctly. print(img.shape) ### Load the trained YOLOv8 segmentation model from disk. model = YOLO(model_path) ### Run inference on the image to get segmentation predictions. results = model(img) ### Select the first (and only) result from the prediction list. result = results[0] ### Access the class name mapping stored inside the model. names = model.names ### Print all class names to confirm the index-to-label mapping. print(names) ### Define the expected number of classes used during training. num_classes = 10 ### Create an empty list that will store a unique color for each class. random_colors = [] ### Generate random BGR colors for each class index. for _ in range(num_classes): ### Sample a random blue channel value. blue = random.randint(0, 255) ### Sample a random green channel value. green = random.randint(0, 255) ### Sample a random red channel value. red = random.randint(0, 255) ### Pack the three values into a BGR color tuple. color = (blue, green, red) ### Append the generated color to the list. random_colors.append(color) ### Print the list of random colors to verify the mapping. print(random_colors) ### Create an empty color image to accumulate all predicted masks. final_mask = np.zeros((H, W, 3), dtype=np.uint8) ### Extract the predicted class IDs from the detection boxes. predicted_classes = result.boxes.cls.cpu().numpy() ### Print the list of predicted class IDs for debugging. print(predicted_classes) ### Iterate over each predicted mask and its corresponding index. for j, mask in enumerate(result.masks.data): ### Move the mask tensor to CPU, convert to NumPy, and scale to 0-255. mask = mask.cpu().numpy() * 255.0 ### Read the class ID for the current instance. classId = int(predicted_classes[j]) ### Print which object index maps to which class. print("Object " + str(j) + " detected as " + str(classId) + " - " + names[classId]) ### Resize the mask to match the original image dimensions. mask = cv2.resize(mask, (W, H)) ### Select the color associated with this class ID. color = random_colors[classId] ### Create an empty image for the colored mask of this instance. colored_mask = np.zeros((H, W, 3), dtype=np.uint8) ### Color all pixels where the mask is active. colored_mask[mask > 0] = color ### Merge this instance mask into the final accumulated mask. final_mask = np.maximum(final_mask, colored_mask) ### Build a unique filename for saving the individual instance mask. file_name = "output" + str(j) + ".png" ### Save the colored mask for this instance to disk. cv2.imwrite("C:/Data-sets/Football-Seg.v1i.yolov8/My-model/" + file_name, colored_mask) ### Save the combined final mask image containing all instances. cv2.imwrite("C:/Data-sets/Football-Seg.v1i.yolov8/My-model/final_mask.png", final_mask) ### Display the original image in an OpenCV window. cv2.imshow("img", img) ### Display the final combined mask in another window. cv2.imshow("final mask", final_mask) ### Wait for a key press before closing the OpenCV windows. cv2.waitKey(0) ### Import Matplotlib for side-by-side visualization inside notebooks. import matplotlib.pyplot as plt ### Create a figure with one row and two columns for comparison. fig, axes = plt.subplots(1, 2, figsize=(12, 6)) ### Show the original image on the left, converted from BGR to RGB. axes[0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) ### Set a descriptive title for the original image. axes[0].set_title("Original image") ### Hide axis ticks for a cleaner look. axes[0].axis('off') ### Show the final segmentation mask on the right, also converted to RGB. axes[1].imshow(cv2.cvtColor(final_mask, cv2.COLOR_BGR2RGB)) ### Set a descriptive title for the mask visualization. axes[1].set_title("Final mask") ### Hide axis ticks for consistency. axes[1].axis('off') ### Render the Matplotlib figure to visualize both images. plt.show() Summary (Part 4)
You now have a complete evaluation script that loads your best weights, generates football player segmentation YOLOv8 masks, and visualizes them clearly.
This makes debugging easier and gives you immediate, visually satisfying proof that your model works.
If you want to bootstrap high-quality masks even faster, follow my Segment Anything tutorial: Generate YOLOv8 Masks Fast to combine SAM with YOLOv8 for rapid mask generation.
FAQ :
Can YOLOv8 handle multi-class football segmentation in one model?
Yes, YOLOv8 can learn multiple football classes in a single segmentation model as long as your labels and YAML configuration are consistent.
Do I need polygon masks for YOLOv8 segmentation?
Yes, YOLOv8 segmentation expects polygon-style mask annotations converted into its TXT format for accurate pixel-level predictions.
What backbone is used in this YOLOv8 segmentation tutorial?
This tutorial uses yolov8l-seg.pt, a strong large backbone suitable for high-quality multi-class segmentation tasks.
How many epochs should I train my YOLOv8 segmentation model?
Around 100–200 epochs with early stopping is a solid range; monitor validation loss and mAP to decide when to stop.
Why is my YOLOv8 football segmentation underperforming?
Common causes are noisy labels, incorrect class IDs, unbalanced data, or too aggressive augmentations; fix these first.
What image size is recommended for YOLOv8 football masks?
An image size of 640 is a practical default; increase it for finer lines if your GPU memory allows.
Can I visualize each class with a different color mask?
Yes, you can map each class ID to a unique color and overlay masks to quickly inspect predictions.
Is this tutorial suitable for beginners in segmentation?
Yes, it uses copy-paste code, explains each command, and is approachable for beginners while still useful for advanced users.
Can I adapt this pipeline to video segmentation?
Yes, run inference frame by frame, apply the same segmentation model, and optionally track objects over time.
How do I deploy YOLOv8 football segmentation models?
Export to ONNX or TensorRT and integrate into your inference pipeline for real-time match analysis or broadcast overlays.
Conclusion
Training a multi-class YOLOv8 segmentation model for football gives you far more than pretty masks—it gives you structured, pixel-level understanding of every frame.
With the environment locked in, data organized, and a clear training script, you’ve removed most of the friction that usually blocks people from getting useful results.
Your visualization step closes the loop: you can immediately see where the model is strong, where labels need fixing, and how well it separates players, field, and context.
From here, it’s natural to extend this pipeline into tracking, tactical pattern mining, or integrations with tools like SAM or Detectron2 for even more advanced workflows.
This tutorial is deliberately practical, so you can clone the structure for any sport or domain: swap the dataset, adjust the classes, keep the same proven yolov8 segmentation tutorial backbone, and ship real models instead of just reading about them.
Connect :
☕ Buy me a coffee — https://ko-fi.com/eranfeit
🖥️ Email : feitgemel@gmail.com
🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb
Enjoy,
Eran
