Python 如何知道fit_生成器中提供的类的字典是否与正确的类关联?
我有一个不平衡的数据集,所以我使用了类权重字典,我使用这个函数计算类的权重:Python 如何知道fit_生成器中提供的类的字典是否与正确的类关联?,python,keras,deep-learning,computer-vision,conv-neural-network,Python,Keras,Deep Learning,Computer Vision,Conv Neural Network,我有一个不平衡的数据集,所以我使用了类权重字典,我使用这个函数计算类的权重: from sklearn.utils import class_weight def create_dict(dirs=['class1/','class2/','class3/','class4'], path = '/home/trainset/'): all = [] for i in range(len(dirs)): c_path = path + dirs[i]
from sklearn.utils import class_weight
def create_dict(dirs=['class1/','class2/','class3/','class4'], path = '/home/trainset/'):
all = []
for i in range(len(dirs)):
c_path = path + dirs[i]
l = [i for f in listdir(c_path) if isfile(join(c_path, f))]
print(len(l))
print('************************************************************')
all+= l
y = np.asarray(all)
print(len(all))
class_weights = class_weight.compute_class_weight('balanced', np.unique(y), y)
print(class_weights)
class_weights = dict(enumerate(class_weights))
print(class_weights)
f = open('weights_dict.txt','w')
f.write(str(class_weights))
return class_weights
class_weights = create_dict(dirs)
data_generator = ImageDataGenerator(rescale=1.0/255.0,
horizontal_flip=True,
vertical_flip=True,
brightness_range=[0.2,1.0])
train_generator = data_generator.flow_from_directory(
'/home/trainset/',
target_size=(image_size, image_size),
batch_size=BATCH_SIZE_TRAINING,
seed = 7)
然后,我给它一个fit_生成函数,如下所示:
fit_history = model.fit_generator(train_generator,
epochs = NUM_EPOCHS,
class_weight = class_weights,
validation_data=validation_generator,
verbose=2,
callbacks = [cb_checkpointer, cb_early_stopper, reduce_lr])
这些模型在准确性方面做得很好,但我不知道是否正确的权重被附加到了正确的类上,我怎么能确定呢!如何使每一类与它的重量相匹配
另外,我正在使用Keras实现,我使用Sklearn库中的compute_class_weight函数