1. Home
  2. Explore
  3. Computer Science
  4. Training YOLO in Colab

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Training YOLO in Colab

Speaker 1Speaker 2

Training YOLO in Colab

5:05
0:00-5:05
  1. Michael

    Hey Corey, have you ever thought about training your own object detection model? I've been looking into training a YOLO model to detect different types of candy.

  2. Corey

    That sounds awesome! I've always wanted to try training a custom model, but I thought it was super complicated. Is it something we can actually do without a ton of expensive hardware?

  3. Michael

    Absolutely! You can do most of the work in Google Colab, which gives you free access to a GPU. The whole process is pretty straightforward once you break it down: gather images, label them, train the model in Colab, and then run it locally.

  4. Corey

    Okay, so first step is gathering the dataset. How many images do you need? I imagine it has to be a lot.

  5. Michael

    For a proof of concept, 100 to 200 images is a great starting point. The key is to have variety — different backgrounds, angles, and lighting that match what the model will see in real use. I took photos of candy around my house with my phone and webcam.

  6. Corey

    That makes sense. But what if you don't have the actual objects? Can you use online sources?

  7. Michael

    For sure! You can find pre-labeled images on Roboflow Universe, Kaggle, or Google's Open Images V7 dataset. But for my candy detector, I took my own pictures so I could control the conditions.

  8. Corey

    Alright, so once you have the images, you need to label them. That sounds tedious. Is there a good tool for that?

  9. Michael

    Yes, we use Label Studio. It's free, open-source, and has a nice interface. It even has automated labeling features. Actually, the company behind it, HumanSignal, sponsored this guide. You install it via Anaconda — first install Anaconda, then create a Python environment, and pip install label-studio.

  10. Corey

    So you run it locally, but you interact through a web browser? That's neat. And I assume you have to set up a project and import images?

  11. Michael

    Exactly. You create a project, give it a name like 'candy images', then import your images. But there's a limit — you can only import 100 at a time. If you have more than a thousand, you'll want to link a local storage folder. Then in the labeling setup, you choose object detection, define your class names — like AirHeads, Skittles, etc. — and you're ready to draw bounding boxes.

  12. Corey

    Drawing bounding boxes manually takes forever, but you can use hotkeys, right? I saw you can press number keys to select classes quickly.

  13. Michael

    Yes, that speeds it up a lot. You can zoom in, adjust boxes, and submit each image. Labeling does take a while, so put on some music. Once you're done, export in YOLO format — it gives you a zip file with images and labels in the right folder structure.

  14. Corey

    Perfect. Then we upload that zip to Google Colab. So next we train the model? What do we need to set up in Colab?

  15. Michael

    First, make sure you're using a T4 GPU runtime. Then upload your data.zip. If it's from a different source, ensure the folder structure matches: images and labels folders plus a classes.txt file. You can also use my pre-made candy or coin dataset to test the process.

  16. Corey

    Got it. After uploading, we unzip it and split into train and validation sets. I think you mentioned a script that does 90-10 split?

  17. Michael

    Right, the notebook has a code block that downloads and runs a script to create the train/val split automatically. Then we install the Ultralytics library and generate a data.yaml config file that points to the training and validation folders and lists the classes.

  18. Corey

    Now we can finally train! But there are parameters like model size, epochs, and image resolution. How do we choose?

  19. Michael

    YOLO11 is the latest, but YOLOv8 and v5 are still solid. For model size, nano is smallest, extra large is biggest. I recommend YOLO11s as a good balance. For epochs: less than 200 images, try 60 epochs; more than 200, 40 epochs is plenty. Resolution default is 640x640, but you can go lower for speed.

  20. Corey

    And training just runs through epochs showing mAP, precision, recall. It can take minutes to hours depending on dataset size. But Colab might time out after 5 hours, right?

  21. Michael

    Yes, so for bigger datasets consider Colab Pro. When it's done, the best model weights are saved as best.pt. There's also a results.png showing how metrics improved over epochs — mAP should flatten out toward the end.

  22. Corey

    Then we test inference on the validation images. The notebook has code to run detection and display results. If the model isn't accurate, we can check labels, increase epochs, or use a larger model.

  23. Michael

    Exactly. Once you're happy, you download the model. The notebook zips it as my_model.pt and you can load it in your local Anaconda environment. You'll need Ultralytics installed, and if you have an NVIDIA GPU, install the GPU-enabled PyTorch.

  24. Corey

    Then we can run inference on images, video, or even a webcam using a custom Python script. I saw you have a yolo_detect.py script that loads the model and draws boxes in real-time.

  25. Michael

    Yes, you can run it with a USB camera, a video file, or a folder of images. For example: python yolo_detect.py --model my_model.pt --source usb0. It runs fast on a GPU. And you can press Q to quit.

  26. Corey

    That's so cool! So from there you can build applications like a candy calorie counter that counts objects and estimates calories. I've seen your example on GitHub.

  27. Michael

    Exactly. The possibilities are endless once you have a trained model. I'm also working on a guide for running YOLO on a Raspberry Pi. But for now, congratulations — you've successfully trained a YOLO model!

  28. Corey

    Thanks for walking me through this, Michael. I'm definitely going to try it with my own dataset. Maybe I'll train a model to detect different types of birds in my backyard.

  29. Michael

    Great idea! Start with a small dataset, label carefully, and iterate. Let me know how it goes. Good luck!

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent