Course Content

Lesson 5.6: Addressing Class Underrepresentation with a Weighted Loss Function

When working with computer vision datasets, especially in object detection and classification tasks, the balance of class representation can significantly influence model performance. Classes in your dataset can often be underrepresented, leading to a lack of diversity in the instances that your model learns from. This can cause the model to struggle in correctly identifying these underrepresented classes.

Understanding Class Underrepresentation

Let’s take an example: imagine a computer vision model being trained to identify cars and motorcycles in images. In the training dataset, there are plenty of examples of cars (99%) but very few instances of motorcycles (1%). In such a case, predicting a car every time leads to 99% accuracy. This means that the model may learn this heuristic and neglect the 1% of motorcycles. 99% might look good on paper, but in this case, the model achieves 0% accuracy in motorcycle detection, which may be problematic in some use cases. This lack of motorcycles in the training data is a case of class underrepresentation. The model, during training, doesn’t get enough exposure to motorcycles, leading to a highly skewed learning process.

The Weighted Loss Function Approach

One common approach to handle this problem is to use a weighted loss function. Essentially, we assign a higher weight to the underrepresented classes in the loss function. This weight adjustment tells our model that the underrepresented class (in our example, motorcycles) is more important to get right.

Consider a standard cross-entropy loss function used in object detection or segmentation tasks. The weighted version might look like:

Weighted Loss = -1 * ∑ [ w_i * y_true_i * log(y_pred_i) ]

Here, y_true_i and y_pred_i are the actual and predicted values for class i, and w_i is the weight for that class. The summation goes over all classes.

In our loss function, each prediction error for the underrepresented class (motorcycles) would be magnified due to the higher weight assigned to it. This increased error signals the model to correct itself more when it makes mistakes on motorcycles, helping it to better learn the characteristics of this class.

Implementation and Caveats

The weights can be assigned inversely proportional to the class frequency or calculated based on other criteria depending on the specifics of your problem. The primary aim here is to increase the contribution of the underrepresented class to the overall loss, forcing the model to pay more attention to it.

However, this approach must be used carefully. Overweighting the underrepresented class may cause the model to overfit to this class, leading to increased false positives. Therefore, it is important to monitor the model’s performance on a validation set and adjust the weights accordingly to ensure a balance in performance across all classes.


Using a weighted loss function is a practical and effective strategy to handle class underrepresentation in computer vision datasets. It’s about enhancing the model’s ability to generalize well across all classes, improving prediction accuracy, and building a robust model that can better understand the diverse and complex visual world.

Add Your Heading Text Here
					from transformers import AutoFeatureExtractor, AutoModelForImageClassification

extractor = AutoFeatureExtractor.from_pretrained("microsoft/resnet-50")

model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50")