如何从 Tensorflow 二值图像分类中获得召回率和精度
How to get Recall and Precision from Tensorflow binary image classification
如何从 Tensorflow 二值图像分类中获取召回率和精度?
我使用这段代码来评估我的验证数据集,但我只得到了损失和准确性
model.evaluate(validationDataset)
这样的输出
3/3 [==============================] - 1s 262ms/step - loss: 0.1850 - accuracy: 0.9459
[0.18497566878795624, 0.9459459185600281]
如何轻松获得Recall和Precision?
至少我对这段代码感到困惑table
tf.math.confusion_matrix(labels=validation_label, predictions=prediction_result).numpy()
输出:
array([[3, 1],
[ 0, 2]], dtype=int32)
预测结果代码:
prediction_result = (model.predict(val_ds) > 0.5).astype("int32")
输出:
array([[0],
[1],
[1],
[0],
[1],
[0]], dtype=int32)
验证标签代码:
validation_label = np.concatenate([y for x, y in val_ds], axis=0)
输出:
array([0, 1, 1, 0, 1, 0], dtype=int32)
环境:
- Google colab
- 张量流 2.7.0
- Python 3.7.12
数据集结构:
/训练/
---/COVID19/
------/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 = 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"))
损失函数:“binary_crossentropy”
优化器:RMSprop
指标:“准确性”
您可以使用以下代码获取精确率和召回率以及损失率和准确率:
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy',
tf.keras.metrics.Recall(),
tf.keras.metrics.Precision()])
如何从 Tensorflow 二值图像分类中获取召回率和精度?
我使用这段代码来评估我的验证数据集,但我只得到了损失和准确性
model.evaluate(validationDataset)
这样的输出
3/3 [==============================] - 1s 262ms/step - loss: 0.1850 - accuracy: 0.9459
[0.18497566878795624, 0.9459459185600281]
如何轻松获得Recall和Precision?
至少我对这段代码感到困惑table
tf.math.confusion_matrix(labels=validation_label, predictions=prediction_result).numpy()
输出:
array([[3, 1],
[ 0, 2]], dtype=int32)
预测结果代码:
prediction_result = (model.predict(val_ds) > 0.5).astype("int32")
输出:
array([[0],
[1],
[1],
[0],
[1],
[0]], dtype=int32)
验证标签代码:
validation_label = np.concatenate([y for x, y in val_ds], axis=0)
输出:
array([0, 1, 1, 0, 1, 0], dtype=int32)
环境:
- Google colab
- 张量流 2.7.0
- Python 3.7.12
数据集结构:
/训练/
---/COVID19/
------/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 = 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"))
损失函数:“binary_crossentropy”
优化器:RMSprop
指标:“准确性”
您可以使用以下代码获取精确率和召回率以及损失率和准确率:
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy',
tf.keras.metrics.Recall(),
tf.keras.metrics.Precision()])