[英]How to get Recall and Precision from Tensorflow binary image classification
How to get Recall and Precision from Tensorflow binary image classification?如何从 Tensorflow 二值图像分类中获得召回率和精度?
I use this code to evaluate my validation dataset, but I just got loss and accuracy我使用此代码来评估我的验证数据集,但我得到了损失和准确性
model.evaluate(validationDataset)
The output like this output 像这样
3/3 [==============================] - 1s 262ms/step - loss: 0.1850 - accuracy: 0.9459
[0.18497566878795624, 0.9459459185600281]
how to get Recall and Precision easily?如何轻松获得 Recall 和 Precision?
at least I've got the confusion table with this code至少我有这个代码的混淆表
tf.math.confusion_matrix(labels=validation_label, predictions=prediction_result).numpy()
Output: Output:
array([[3, 1],
[ 0, 2]], dtype=int32)
prediction result code:预测结果代码:
prediction_result = (model.predict(val_ds) > 0.5).astype("int32")
Output: Output:
array([[0],
[1],
[1],
[0],
[1],
[0]], dtype=int32)
validation label code:验证 label 代码:
validation_label = np.concatenate([y for x, y in val_ds], axis=0)
Output: Output:
array([0, 1, 1, 0, 1, 0], dtype=int32)
Enviroment:环境:
Dataset Structure:数据集结构:
/training/ /训练/
---/COVID19/ - -/新冠肺炎/
------/img1.jpg ------/img1.jpg
------/img2.jpg ------/img2.jpg
------/img3.jpg ------/img3.jpg
---/NORMAL/ - -/普通的/
------/img4.jpg ------/img4.jpg
------/img5.jpg ------/img5.jpg
------/img6.jpg ------/img6.jpg
Make Dataset Code:制作数据集代码:
batch_size = 32
img_height = 300
img_width = 300
epochs = 10
input_shape = (img_width, img_height, 3)
AUTOTUNE = tf.data.AUTOTUNE
dataset_url = "https://storage.googleapis.com/fdataset/Dataset.tgz"
data_dir = tf.keras.utils.get_file('training', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="training",
validation_split=0.8,
image_size=(img_width, img_height),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="validation",
validation_split=0.2,
image_size=(img_width, img_height),
batch_size=batch_size)
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
The Model: Model:
model = tf.keras.Sequential()
base_model = tf.keras.applications.DenseNet121(input_shape=input_shape,include_top=False)
base_model.trainable=True
model.add(base_model)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(16,activation='relu'))
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
loss function: "binary_crossentropy"损失 function:“binary_crossentropy”
optimizer: RMSprop优化器:RMSprop
metrics: "accuracy"指标:“准确度”
You can use the following code to get precision and recall along with loss and accuracy:您可以使用以下代码来获得精确度和召回率以及损失和准确度:
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy',
tf.keras.metrics.Recall(),
tf.keras.metrics.Precision()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.