多类 CNN 的宏观指标(召回/F1...)

akh*_*tos 5 python machine-learning keras tensorflow

我使用 CNN 对不平衡数据集进行图像分类。我对 tensorflow 后端完全陌生。这是多类问题(不是多标签),我有 16 个类。类是一种热编码。

我想计算每个时期的 MACRO 指标:F1、精度和召回率。

我找到了一个代码来打印这些宏指标,但它只适用于验证集来自:https : //medium.com/@thongonary/how-to-compute-f1-score-for-each-epoch-in-keras-a1acd17715a2

class Metrics(Callback):

 def on_train_begin(self, logs={}):
  self.val_f1s = []
  self.val_recalls = []
  self.val_precisions = []

 def on_epoch_end(self, epoch, logs={}):
  val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()
  val_targ = self.validation_data[1]
  _val_f1 = f1_score(val_targ, val_predict,average='macro')
  _val_recall = recall_score(val_targ, val_predict,average='macro')
  _val_precision = precision_score(val_targ, val_predict,average='macro')
  self.val_f1s.append(_val_f1)
  self.val_recalls.append(_val_recall)
  self.val_precisions.append(_val_precision)
  print (" — val_f1: %f — val_precision: %f — val_recall %f" % (_val_f1, _val_precision, _val_recall))
  return

metrics = Metrics()
Run Code Online (Sandbox Code Playgroud)

我什至不确定这段代码是否真的有效,因为我们使用

 val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()
Run Code Online (Sandbox Code Playgroud)

在多类分类的情况下,ROUND 会导致错误吗?

我使用此代码在训练集上打印指标(仅回忆起对我来说重要的指标)(也计算验证集,因为它在 model.compute 中使用)代码已改编自:用于在 keras 中召回的自定义宏

 val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()
Run Code Online (Sandbox Code Playgroud)

我运行我的模型



def recall(y_true,y_pred):
     true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
     possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
     return  true_positives / (possible_positives + K.epsilon())

def unweightedRecall(y_true, y_pred):
    return (recall(y_true[:,0],y_pred[:,0]) + recall(y_true[:,1],y_pred[:,1])+recall(y_true[:,2],y_pred[:,2]) + recall(y_true[:,3],y_pred[:,3])
            +recall(y_true[:,4],y_pred[:,4]) + recall(y_true[:,5],y_pred[:,5])
            +recall(y_true[:,6],y_pred[:,6]) + recall(y_true[:,7],y_pred[:,7])
            +recall(y_true[:,8],y_pred[:,8]) + recall(y_true[:,9],y_pred[:,9])
            +recall(y_true[:,10],y_pred[:,10]) + recall(y_true[:,11],y_pred[:,11])
            +recall(y_true[:,12],y_pred[:,12]) + recall(y_true[:,13],y_pred[:,13])
            +recall(y_true[:,14],y_pred[:,14]) + recall(y_true[:,15],y_pred[:,15]))/16.    



Run Code Online (Sandbox Code Playgroud)

VALIDATION 宏调用不同于 2 个不同的代码。

即(看val_unweightedRecallval_recall

Epoch 10/100
19/18 [===============================] - 13s 703ms/step - loss: 1.5167 - unweightedRecall: 0.1269 - acc: 0.5295 - val_loss: 1.5339 - val_unweightedRecall: 0.1272 - val_acc: 0.5519
 — val_f1: 0.168833 — val_precision: 0.197502 — val_recall 0.15636

Run Code Online (Sandbox Code Playgroud)

为什么我使用两个不同的代码在我的宏验证召回上有不同的值?

额外问题:对于已经尝试过这个的人,是否真的值得使用基于我们感兴趣的指标(例如召回)的自定义损失或具有权重的分类交叉熵产生相同的结果?

Ale*_*xis 2

让我以相反的顺序回答这两个问题:

您不能使用召回率作为自定义损失的基础:它不是凸的!如果你不完全理解为什么 Recall 或 precision 或 f1 不能用作损失,请花点时间看看损失的作用(它毕竟是模型中的一个巨大参数)。

事实上,这一轮是针对二元问题的。正如他们所说,如果不是你,那就是别人。但就你而言,这是错误的。我们来抛出代码:

val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()
Run Code Online (Sandbox Code Playgroud)

从内到外,他获取数据 (self.validation_data[0;]) 并预测一个数字(1 个神经元作为输出)。因此,他计算成为 1 的概率。如果该概率超过 0.5,则该轮将其转换为 1,如果低于,则将其转换为 0。如您所见,这对您来说是错误的。在某些情况下,您不会预测任何课程。继这个错误之后,其余的也都是错误的。

现在,解决方案。您想要计算每一步的平均召回率。顺便说一句,“但它只适用于验证集”。是的,这是有意的,您使用验证来验证模型,而不是火车,否则就是作弊。

所以召回率等于所有正例中的真例。让我们这样做吧!

def recall(y_true, y_pred):
     recall = 0
     pred = K.argmax(y_pred)
     true = K.argmax(y_true)
     for i in range(16):
         p = K.cast(K.equal(pred,i),'int32')
         t = K.cast(K.equal(true,i),'int32')
         # Compute the true positive
         common = K.sum(K.dot(K.reshape(t,(1,-1)),K.reshape(p,(-1,1))))
         # divide by all positives in t
         recall += common/ (K.sum(t) + K.epsilon)
     return recall/16
Run Code Online (Sandbox Code Playgroud)

这为您提供了所有类别的平均召回率。您可以打印每个类别的值。

如果您有任何疑问请告诉我!

有关二进制 Recall 的实现,请参阅代码改编自的此问题。