From b607f7fe8239b87e47274a85f59558e1607fc25f Mon Sep 17 00:00:00 2001 From: Parikshit Date: Mon, 6 Jul 2026 18:27:37 +0530 Subject: [PATCH 1/2] Fix CNN prediction threshold for binary classification --- computer_vision/cnn_classification.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/computer_vision/cnn_classification.py b/computer_vision/cnn_classification.py index 115333eba0d1..08f824dd46b8 100644 --- a/computer_vision/cnn_classification.py +++ b/computer_vision/cnn_classification.py @@ -94,7 +94,9 @@ test_image = np.expand_dims(test_image, axis=0) result = classifier.predict(test_image) # training_set.class_indices - if result[0][0] == 0: + # The sigmoid output is a probability in the range [0, 1]. + # Use a threshold of 0.5 to convert the probability into a binary prediction. + if result[0][0] < 0.5: prediction = "Normal" - if result[0][0] == 1: + else: prediction = "Abnormality detected" From 4188336db3bee4109fbeb26f36c9a1022afce127 Mon Sep 17 00:00:00 2001 From: Parikshit Date: Mon, 6 Jul 2026 18:38:26 +0530 Subject: [PATCH 2/2] Use ternary operator for binary prediction --- computer_vision/cnn_classification.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/computer_vision/cnn_classification.py b/computer_vision/cnn_classification.py index 08f824dd46b8..2cf2de00f982 100644 --- a/computer_vision/cnn_classification.py +++ b/computer_vision/cnn_classification.py @@ -96,7 +96,4 @@ # training_set.class_indices # The sigmoid output is a probability in the range [0, 1]. # Use a threshold of 0.5 to convert the probability into a binary prediction. - if result[0][0] < 0.5: - prediction = "Normal" - else: - prediction = "Abnormality detected" + prediction = "Normal" if result[0][0] < 0.5 else "Abnormality detected"