简体   繁体   English

不平衡 CNN 上的类权重

[英]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.我正在尝试对属于 4 个类别的一组 X 射线图像实施简单的 CNN 分类。 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 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.例如,如果你有一个包含 1000 张狗图像和 100 张猫图像的数据集,你的分类器就会偏向于狗类。 If it predicted dog each time it would be correct 90 percent of the time.如果它每次都预测狗,那么它在 90% 的时间里都是正确的。 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.为了补偿不平衡,class_weights 字典使您可以在计算损失时对猫的样本进行加权,其权重是狗的 10 倍。 One way is to use the class_weight method from sklearn as shown below一种方法是使用 sklearn 中的 class_weight 方法,如下所示

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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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