这是我正在使用的代码(主要从Kaggle提取):
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
...
outputs = Conv2D(4, (1, 1), activation='sigmoid') (c9)
model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='adam', loss='dice', metrics=[mean_iou])
results = model.fit(X_train, Y_train, validation_split=0.1, batch_size=8, epochs=30, class_weight=class_weights)
Run Code Online (Sandbox Code Playgroud)
我有4个班级非常不平衡。A级等于70%,B级= 15%,C级= 10%,D级= 5%。但是,我最关心D类。因此,我进行了以下类型的计算:D_weight = A/D = 70/5 = 14B类和A类的权重依此类推。(如果有更好的方法来选择这些权重,那就放心了)
在最后一行,我想正确设置class_weights和我做它像这样:class_weights = {0: 1.0, 1: 6, 2: 7, 3: 14}。
但是,当我这样做时,出现以下错误。
class_weight3维尺寸目标不支持。
是否可以在最后一层之后添加一个密集层并将其用作虚拟层,以便我可以传递class_weights然后仅使用最后一个conv2d层的输出进行预测?
如果这不可能,那么我将如何修改损失函数(不过我知道这篇文章,但是,将权重传递给损失函数并不会减少损失,因为损失函数是针对每个类分别调用的)?目前,我正在使用以下损失函数:
def dice_coef(y_true, y_pred):
smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * …Run Code Online (Sandbox Code Playgroud)