Class weights on imbalanced CNN

765 views Asked by At

I am trying to implement a simple CNN classification on a set of x-ray images belonging to 4 classes. The dataset looks like this:

                                           img              A   B   C   D
   1    [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ...   0   0   0   1
   2    [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ...   0   0   0   1
   3    [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ...   0   0   1   0
   4    [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ...   0   0   0   1 

A-80 B-300 C-70 D-150

How do I go on applying class weights in these settings?

1

There are 1 answers

0
Gerry P On

class weights is a dictionary that compensates for the imbalance in the data set. For example if you had a data set of 1000 dog images and 100 cat images your classifier be biased toward the dog class. If it predicted dog each time it would be correct 90 percent of the time. To compensate for the imbalance the class_weights dictionary enables you to weight samples of cats 10 times higher than that of dogs when calculating loss. One way is to use the class_weight method from sklearn as shown below

from sklearn.utils import class_weight
import numpy as np

class_weights = class_weight.compute_class_weight(
               'balanced',
                np.unique(train_generator.classes), 
                train_generator.classes)