Classifying soccer shirts: A journey back to AI

Teogenes MouraTeogenes Moura
7 min read

TLDR

  • After a long time, I'm getting back to AI;

  • Putting things in prod and setting up a dev environment is a bit more intrincate than I expected;

  • I've built a classifier for soccer team's uniforms, which you can check out here;

  • It required data cleaning to avoid overfitting, but in the end it did work as expected.

Hello all!

Today, I want to share with you a bit of my journey getting back into the world of AI. I could start by diving directly into code and model training, but I want to tell a short story first. If you want to skip it (which is fine, I swear), head over to the last section or straight to the Jupyter notebook in Github.

A long time ago, in a distant galaxy…

I started studying AI long ago but had to abandon it because of circumstances outside my control. Long story short, when I graduated from college in 2019, I had to take on the first job that would accept me as quickly as possible, which led me to a front-end role. The good news is that I liked it! However, one front-end engineering job led to a full-stack one, which led to another full-stack one… and now, after five years or so I'm finally having the chance to go back and explore the world of AI once again.

The first thing that caught my attention while doing deep learning work was how much the study resources have evolved. Back in 2015, all I can remember was a very theoretical deep learning book, which by the last time I looked at it was half-finished and still the best resource. Now I see that makes sense — It was Ian Goodfellow's Deep Learning book.

As a third or fourth-semester Computer Engineering student, that book (also Pattern Recognition and Machine Learning, by Christopher Bishop) scared me A LOT. I was nowhere near the mathematical abilities needed to fully comprehend either of those texts, so I ended up retreating from my theoretical ML/DL advances.

Nowadays, I see so many good resources for beginners. I'm currently going through the Deep Learning for Coders course by the Fast.ai folks, and it's been a great experience. As they put it in the first video, things that were jokingly hard in 2015 are now so accessible I can achieve them on my own laptop, which is amazing. Scrimba’s AI engineering path is also an excellent resource to learn more about the practical side of things, especially on how to deploy LLM and AI agents as part of a full-stack application (learning React.js and Ruby on Rails came in quite handy after all!)

Today I want to bring my version of the classification notebook used as an example for the first class of the Deep Learning for Coders mentioned above. My goal for this first project wasn't to achieve a perfect model but to get my hands dirty and put a DL model into production.

The somewhat weird path to production

Starting new endeavors is always exciting. It's awesome to get in touch with all these new, shining technologies and make a run with them. One thing that stood out to me as a little bit more difficult than I thought it'd be is deployment. At first, I thought it was cool that both Fast.ai and Scrimba's courses had specific sections on deployment. It seemed as though they were nice extra modules. After all, how hard can it be to put a model into production? Getting a standard web application is just a matter of starting a Rails/React application and sending it to the cloud, maybe getting a Digital Ocean droplet or a Heroku instance.

Boy, was I wrong? I mean — this may be my lack of experience talking, but there are so many services regarding AI models out there, and there’s so much overlapping over them that it felt as if I was lost among a thousand options, none of which were quite what I had in mind. I was expecting a simple service where I’d train a model and it'd be already automatically deployed to an inference endpoint I could consume from a front-end application.

HuggingFace was the one closest to it. During the Fast.ai course, they show you how to deploy a Gradio application. It's quite nice, but it doesn't seem to allow a lot of customization, which is not so good for someone wanting to deploy a fully-fledged ReactJS app. The thought in my head was “well, no problem, I can probably train a model elsewhere and host it through HuggingFace's Inference API”. Someone recommended me using Google Colab to run a notebook where I could train said model, and so I did. I copied all the code I had already worked on in Kaggle (which they use during the Fast.ai course) and pasted it into Google collab. Since it involved fetching some images from DuckDuckGo's service, I left the code running while I got some sleep overnight.

If you have any experience developing these things, you’re probably chuckling at what happened next. I came in the next morning and… nothing was there. After some research I learned that you can't leave a Colab notebook without interaction for more than 12 hours or it'll disconnect (and lose all your data during the process). The alternative Google gives to not losing that data is by having you connect your Google Drive account and saving it there. It's a decent approach: you're using their GPU services for free, so you should at least take care of your own data.

I'm fine with cleaning up some space to do it, but that introduced something I'm quite not a fan of as a programmer: hack-ish code that's not directly related to what I'm intending to do meant solely to correct the unintuitive behavior of the underlying system. I'd have to take care of saving the model after each epoch, zipping and downloading training data here and there… it's nothing super complicated but that I'd like to avoid if at all possible.

Therefore, my next step was to try and see if I could grab a computer with a decent GPU where I wouldn't have to worry about that. There’s quite a few online services that provide this, so I chose the one I felt most comfortable with: RunPod. If you’re not familiar, they allow you to rent Linux instances with powerful GPUs included. I took one of the cheapest On-Demand one, costing $0.32 an hour. It's not much, but it’s also the kind of thing that can accumulate over time and turn into a huge bill, so I'm trying to use it as efficiently as possible. One of the things that surprised me the most about this is the speed of that cheap GPU. I know - it shouldn’t be too surprising that a paid service would deliver a better experience than a free one, but still. While Colab took a somewhat long time to train my model (I can't remember exactly how long it was, but it was above ten minutes for sure), the RunPod instance took 6 seconds to train each epoque. Talk about a return of investment!

RunPod also allowed me to do a couple more things that I wanted to: Easily download the training data, which is something I couldn't find on a first look in Colab and also connecting to the cloud machine (which they call a “pod”) through SSH so I could use my local VSCode to work on the code remotely. It does require a bit of a setup, but I have to say they have a couple blog posts (here and here) which are helpful.

Do you like soccer team shirts?

Once I got to actual coding, I was finally able to train my model and see it in action. I decided to stay close to the example notebook they show in the Fast.ai course and build a classifier for soccer team shirts. If you're not a fan of soccer, you've probably still seen a few of the uniforms the teams use. They're either completely different (think Real Madrid and Barcelona) or almost exact copies (think Newcastle United and Juventus)

juventus vs newcasttle shirts

In the image above, Juventus is on top and Newcastle is below. Would you be able to tell which is which without following soccer? I probably wouldn't. My idea then was to build a classifier to try and distinguish soccer shirts from one team to another. I decided to start out with Brazilian teams, because well… I am Brazilian 😅 But also, because the team I support, Botafogo, also has a t-shirt that looks a lot like the ones above:

Camisa Botafogo Oficial 1 Reebok 2024

Could my AI model distinguish between those very alike t-shirts? That sparked up my curiosity, so I wrote a Jupyter notebook to make that exploration which is available below or through this link and is the continuation to this blog post. Have a nice read!

0
Subscribe to my newsletter

Read articles from Teogenes Moura directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Teogenes Moura
Teogenes Moura

Software engineer transitioning into ML/AI Engineering