[英]How to compute precision and recall on image classification with MobileNet
[英]How to get Recall and Precision from Tensorflow binary image classification
如何从 Tensorflow 二值图像分类中获得召回率和精度?
我使用此代码来评估我的验证数据集,但我得到了损失和准确性
model.evaluate(validationDataset)
output 像这样
3/3 [==============================] - 1s 262ms/step - loss: 0.1850 - accuracy: 0.9459
[0.18497566878795624, 0.9459459185600281]
如何轻松获得 Recall 和 Precision?
至少我有这个代码的混淆表
tf.math.confusion_matrix(labels=validation_label, predictions=prediction_result).numpy()
Output:
array([[3, 1],
[ 0, 2]], dtype=int32)
预测结果代码:
prediction_result = (model.predict(val_ds) > 0.5).astype("int32")
Output:
array([[0],
[1],
[1],
[0],
[1],
[0]], dtype=int32)
验证 label 代码:
validation_label = np.concatenate([y for x, y in val_ds], axis=0)
Output:
array([0, 1, 1, 0, 1, 0], dtype=int32)
环境:
数据集结构:
/训练/
- -/新冠肺炎/
------/img1.jpg
------/img2.jpg
------/img3.jpg
- -/普通的/
------/img4.jpg
------/img5.jpg
------/img6.jpg
制作数据集代码:
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)
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"))
损失 function:“binary_crossentropy”
优化器:RMSprop
指标:“准确度”
您可以使用以下代码来获得精确度和召回率以及损失和准确度:
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.