My Experiments with YOLOv5:Almost everything you want to know about Yolov5- Series- Part 3

Manjusha Sithik
3 min readDec 27, 2023

--

Image generated by DALL.E

Weighted Cross Entropy for Class imbalance

As we have learned during EDA, the dataset is highly imbalanced, with the ‘Without_Helmet’ class being the minority. Class imbalance problem is usually addressed using oversampling the minority class or under sampling the majority class . Some data will be removed from major class data to under sample the majority Class. And Oversampling for minority classes is done by adding duplicates from the minority class. This balances the dataset in term of number of samples but when looked from the information point of view , we are losing information while under sampling and adding duplicated information while oversampling. It does not really solve the problem in this sense.

Here we are approaching this issue from a different direction, that is ,adjusting the class weight while calculation the cross entropy loss. Cross entropy loss is a metric for measuring the performance of the prediction classification . This is the most commonly used loss function .The basic idea is that we are weighing the loss differently for minor and major class . We assign higher weights to the minority class loss and lesser weight to major class loss so that the minority class loss is penalized more.

𝐿=−[𝑤1 𝑦 𝑙𝑜𝑔 𝑝+𝑤2(1−𝑦)𝑙𝑜𝑔(1−𝑝)]

where 𝑦=1,0 for class1 and class2, 𝑝 is the probability for class1 and 𝑤1 ,𝑤2 are the class weights for class1 and class2.The dataset used in this study is extremely imbalanced in terms of classes. In the calculation of loss in this part, we appropriately adjust the weight of different categories to reduce the imbalance of the classes.

Implementation

Classification loss is calculated using Binary cross-entropy loss function .As for this use case ,Without_Helmet class has got more priority over With_Helmet class ,class Binary Cross Entropy Loss Hyperparameter value is adjusted to give more priority to this class .

The hyperparameters can be found at data/hyps/hyp.scratch-low.yaml . Class BCELoss positive_weight value is set to [.1,.9] to give highest priority to Without_Helmet class .

Class BCELoss parameter

Results

Weighted cross entropy loss was one of the most important methods which yielded confident positive results as shown Below. Weighted Cross Entropy Loss results. All the metrics of both classes witnessed a drastic improvement making the overall performance far better than the base line model. Without_Helmet class precision touched 78% marking 3% increment , recall jumped from 54% to 63% and the mAP50 was upgraded by 13%.Precision ,Recall , mAP50 of With_Helmet class raised by 1%, 4%, 13% respectively . The overall precision, recall, mAP50 was elevated by 2 %, 11 %,12 % respectively. Ratio of correctly identified With_Helmet class and Without_Helmet were improved by .13 and .12 respectively.

BCELoss Results
Baseline Results

A detailed documentation is available at https://docs.ultralytics.com/yolov5/tutorials/hyperparameter_evolution/#before-you-start about all hyperparameters of Yolo . Please find the best suitable parameter to finetune for your problem.

I will be back with the remaining techniques very soon!

Link to Part 1:

Part 2 :

https://medium.com/@manjusha.bs/my-experiments-with-yolov5-almost-everything-you-want-to-know-about-yolov5-series-part-2-6549abdb5b63

--

--

Manjusha Sithik

A Data Scientist Passionate about Computer Vision and Time Series Forecasting