简体   繁体   English

如何从 Tensorflow 二值图像分类中获得召回率和精度

[英]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:环境:

  • Google colab谷歌合作实验室
  • Tensorflow 2.7.0 Tensorflow 2.7.0
  • Python 3.7.12 Python 3.7.12

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.

相关问题 如何使用 MobileNet 计算图像分类的精度和召回率 - How to compute precision and recall on image classification with MobileNet 如何从分类报告中计算准确率和召回率 - How to calculate precision and recall from classification report 如何从 Tensorflow 得到 label 预测二值图像分类 - How to get label prediction of binary image classification from Tensorflow 如何从 nltk 分类器中获得准确率和召回率? - How to get the precision and recall from a nltk classifier? 如何告诉scikit-了解给出F-1 /精确/召回得分的标签(二进制分类)? - How to tell scikit-learn for which label the F-1/precision/recall score is given (in binary classification)? Tensorflow Precision,Recall,F1-多标签分类 - Tensorflow Precision, Recall, F1 - multi label classification Tensorflow 中多类分类的分类精度和召回率? - Class wise precision and recall for multi class classification in Tensorflow? 使用Tensorflow CNN分类器获得精度和召回价值 - Get precision and recall value with Tensorflow CNN classifier 张量流计算召回率和精度 - computing recall and precision with tensorflow 如何从Sklearn分类报告中返回精确度,召回率和F1分数的平均分数? - How to return average score for precision, recall and F1-score from Sklearn Classification report?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM