Fian Julio
Fian Julio

Reputation: 39

How to get Recall and Precision from Tensorflow binary image classification

How to get Recall and Precision from Tensorflow binary image classification ?

I use this code to evaluate my validation dataset, but I just got loss and accuracy

model.evaluate(validationDataset)

The output like this

3/3 [==============================] - 1s 262ms/step - loss: 0.1850 - accuracy: 0.9459
[0.18497566878795624, 0.9459459185600281]

how to get Recall and Precision easily?

at least I've got the confusion table with this code

tf.math.confusion_matrix(labels=validation_label, predictions=prediction_result).numpy()

Output:

array([[3,  1],
       [ 0, 2]], dtype=int32)

prediction result code:

prediction_result = (model.predict(val_ds) > 0.5).astype("int32")

Output:

array([[0],
       [1],
       [1],
       [0],
       [1],
       [0]], dtype=int32)

validation label code:

validation_label = np.concatenate([y for x, y in val_ds], axis=0)

Output:

array([0, 1, 1, 0, 1, 0], dtype=int32)

Enviroment:

Dataset Structure:
/training/
---/COVID19/
------/img1.jpg
------/img2.jpg
------/img3.jpg
---/NORMAL/
------/img4.jpg
------/img5.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 = 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"
optimizer : RMSprop
metrics : "accuracy"

Upvotes: 0

Views: 1189

Answers (1)

user11530462
user11530462

Reputation:

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()])

Upvotes: 1

Related Questions