[英]how to correctly output precision, recall and f1score in keras?
I have a data set of images that I divided into Training and Testing folders, each divided into the two classes I am classifying.我有一个图像数据集,分为训练和测试文件夹,每个文件夹分为我要分类的两个类。 I use Keras generators to fit and evaluate the data.
我使用 Keras 生成器来拟合和评估数据。 I found some resources online that I followed to implement precision, recall and f1-score metrics.
我在网上找到了一些资源,我遵循这些资源来实施精度、召回率和 f1-score 指标。 Here is my Code:
这是我的代码:
class_mode = 'binary'
out_activation = 'sigmoid'
epochs = 1
mode = 'grayscale'
cat_or_bin = 'binary_crossentropy'
out_activation = 'sigmoid'
image_size = 224
batch = 128
channels = 1
def model_logistic():
m = Sequential()
m.add(Flatten(input_shape = (image_size, image_size, channels)))
m.add(Dropout(0.2))
m.add(Dense(out,activation=out_activation))
return m
def recall_m(y_true, y_pred):
y_true = K.ones_like(y_true)
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
all_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (all_positives + K.epsilon())
return recall
def precision_m(y_true, y_pred):
y_true = K.ones_like(y_true)
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def f1_score(y_true, y_pred):
precision = precision_m(y_true, y_pred)
recall = recall_m(y_true, y_pred)
return 2 * ((precision * recall) / (precision + recall + K.epsilon()))
train_generator = datagen.flow_from_directory(
directory=dir,
target_size=(image_size, image_size),
color_mode=mode,
batch_size=batch,
classes = {'no_acc':0, 'acc':1},
class_mode=class_mode,
shuffle=True)
Test_generator = datagen.flow_from_directory(
directory=dir_test,
target_size=(image_size, image_size),
color_mode=mode,
batch_size=batch,
classes = {'no_acc':0, 'acc':1},
class_mode=class_mode,
shuffle=True)
STEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size
STEP_SIZE_TEST = test_generator.n // test_generator.batch_size
sgd = optimizers.sgd(learning_rate=0.0001, momentum=0.9, nesterov=True)
model.compile(loss=cat_or_bin, optimizer=sgd, metrics=['accuracy', f1_score, precision_m, recall_m])
H = model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
epochs=epochs,
verbose=1)
(loss,
accuracy,
f1_score, precision, recall) = model.evaluate_generator(test_generator, STEP_SIZE_TEST)
the output is the following for training and evaluation: output 用于训练和评估如下:
422/422 [==============================] - 384s 910ms/step - loss: 0.2392 - accuracy: 0.9303 - f1_score: 0.4661 - precision_m: 0.9502 - recall_m: 0.3174
2.7411930561065674
0.605730414390564
0.0
0.0
0.0
Why does it output zeroes for these metrics?为什么这些指标 output 归零?
EDIT编辑
Keras v2.3 actually now includes these metrics so I added them to my code as such: Keras v2.3 现在实际上包含了这些指标,所以我将它们添加到我的代码中:
from keras.metrics import Precision, Recall
model.compile(loss=cat_or_bin, optimizer=sgd, metrics=['accuracy', Precision(), Recall()])
However, the outputs are still zeroes for these metrics.但是,这些指标的输出仍然为零。
I would advise you to do use callbacks, this would make it easier for you to keep track of these scores at the end of each epoch too-我建议您使用回调,这将使您更容易在每个时期结束时跟踪这些分数 -
Make a callback class-做一个回调类-
class ModelMetrics(tf.keras.callbacks.Callback):
def on_train_begin(self,logs={}):
self.precisions=[]
self.recalls=[]
self.f1_scores=[]
def on_epoch_end(self, batch, logs={}):
y_val_pred=self.model.predict_classes(x_val)
_precision,_recall,_f1,_sample=score(y_val,y_val_pred)
self.precisions.append(_precision)
self.recalls.append(_recall)
self.f1_scores.append(_f1)
Note for the above code to work you would also have to import score
请注意,要使上述代码正常工作,您还必须导入
score
And while fitting the network you could do something like this-在适应网络的同时,您可以执行以下操作-
metrics = ModelMetrics()
history = model.fit(x_train, y_train,
batch_size = batch_size,
epochs = num_epochs,
validation_data = (x_val, y_val),
callbacks = [metrics])
print(metrics.precisions)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.