简体   繁体   中英

Class weights on imbalanced CNN

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?

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM