Company-Logo Detection

HarshSurya
ArIES IIT Roorkee
Published in
5 min readMar 1, 2021

The Problem Statement

With the IPL frenzy having recently subsided, the motivation for this project was to create a logo-detector to detect the different brands that are advertised during a sports match when there are always hundreds of thousands of eyes glued to the screen. Our aim was then to analyze the data accumulated via these detections.

The Proposed Solution

Our project revolves around using the state-of-the-art object detection algorithm YOLO and a vast dataset of the commonly occurring brands. After training the YOLO model on the dataset we obtain the weights required to make detections on a pre-loaded video. The names of the brands detected and the number of frames they were present in are stored to later display a pie-chart comparing the screen-time different brands got.

Working

We have utilized the LogosInTheWild dataset created by the German Scientist, Andreas Specker, and his team. Since it is not available publicly, we had to mail him for the usage rights. After obtaining the dataset our next step was to clean the data and extract the annotations from the XML files in a text file.

Data cleaning and extraction

Clone the following repo in our system’s terminal — https://github.com/ilmonteux/logohunter

Firstly, we moved the unzipped dataset to logohunter/data/ and remove the brand folders which are not commonly seen in India. Then we ran the following commands to clean the dataset and extract the annotations i.e the coordinates of the bounding boxes of the logos.

The last command reads the XML files and generates data_train.txt and data_test.txt files, with on each line the path to the images and the bounding box specification for each object in the image.

Data Augmentation

In the original dataset, there is a significant class imbalance among the brands. To remove this imbalance, we augmented the images in each brand into a number of versions that were inversely proportional to their original size. For example, if BMW originally had 60 images and Coca-cola 200, we made 6 versions of each image in BMW and two versions of each image in Coca-cola. We mainly augmented the images by rotating, shearing, and randomly horizontally flipping some of them. Before running the data_augmentation script, we need to create an empty folder called ‘annotations’ in data/LogosInTheWild-v2/data_cleaned.

Create two new folders called ‘train’ and ‘valid’ with sub-folders ‘images’ and ‘labels’ inside both of them all within annotations.

Now run the data_augmentation_final.ipynb in your jupyter notebook.

Training

We have used the YOLOv5 object detection model for training our custom dataset. YOLOv5 is written in the Ultralytics PyTorch framework, which is very easy to use and inferences very fast as compared to other YOLO models.

We have trained the dataset on Google Colab since it supports free GPU and deep learning applications can be easily developed using popular libraries such as Keras, TensorFlow, PyTorch, and OpenCV.

Before training, upload the zip file of the dataset i.e. dataset.zip file to your google drive.

To train our logo detector, we have taken the following steps:

  • Setup the environment
  • Install YOLOv5 dependencies
  • Mount the google drive account to Colab
  • Unzip the dataset.zip file
  • Define YOLOv5 Model Configuration and Architecture
  • Train a custom YOLOv5 Detector
  • Run YOLOv5 Inference on test video
  • Export Saved YOLOv5 Weights for Future Inference

Setup the environment and install dependencies

Obtain the logo-detection dataset

Mount your google drive account to the colab notebook using the commands below and then move the compressed dataset.zip file from content/gdrive/My Drive to /content

Unzip the dataset.zip file to extract the images and XML files.

Now, move the train and valid folders to content/ and the data.yaml file to content/yolov5

Define YOLOv5 Model Configuration and Architecture

We have used the YOLOv5l model for our object detector. However, we can use any of the four YOLOv5 model weights:

  1. yolov5s
  2. yolov5m
  3. yolov5l
  4. yolov5x

Next,we downloaded the pretrained weights of the preferred model i.e. YOLOv5l in this case.

After downloading, move the downloaded yolov5l.pt model weight into the weight folder of the root project directory. Now, we need to modify the YAML configuration file corresponding the model weights we just downloaded Open the yolov5l.yaml file from content/yolov5/models/yolov5l and change the number of classes ‘nc’ in from ‘80’ to ‘527’since our logo-detector model contains a total of 527 classes.

Training Custom YOLOv5 Detector

Change the directory to root project directly and run the following command:

The above command will start the training process. We have used ‘32’ as the batch size and have trained our model for ‘100’ epochs for better performance.

After the training process is completed, the trained weights are saved by default in our weights folder.

Run YOLOv5 inference on the video

Export Saved YOLOv5 to your google drive Weights for Future Inference

Run the command given below:

--

--