Sameh108 commited on
Commit
a4f0bb0
·
verified ·
1 Parent(s): addea42

add threshold for uncertain predictions

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -20,6 +20,7 @@ except Exception as e:
20
  print(f"CRITICAL ERROR: Failed to load model. {e}", file=sys.stderr)
21
 
22
  CLASS_NAMES = ['Mild Demented', 'Moderate Demented', 'Non Demented', 'Very Mild Demented']
 
23
 
24
  def prepare_image(img_bytes):
25
  try:
@@ -61,24 +62,29 @@ def predict():
61
 
62
  try:
63
  file_bytes = file.read()
64
-
65
  processed_img = prepare_image(file_bytes)
66
 
67
  prediction_tensor = model(processed_img, training=False)
68
-
69
  prediction = prediction_tensor.numpy()
70
 
71
  class_index = np.argmax(prediction)
72
  confidence = float(np.max(prediction))
73
 
74
- result_class = CLASS_NAMES[class_index]
75
-
76
- print(f"Prediction: {result_class} ({confidence:.2%})", file=sys.stderr)
 
 
 
 
 
77
 
78
  result = {
79
  'result': result_class,
80
  'confidence': confidence,
81
- 'index': int(class_index)
 
 
82
  }
83
 
84
  return jsonify(result)
 
20
  print(f"CRITICAL ERROR: Failed to load model. {e}", file=sys.stderr)
21
 
22
  CLASS_NAMES = ['Mild Demented', 'Moderate Demented', 'Non Demented', 'Very Mild Demented']
23
+ CONFIDENCE_THRESHOLD = 0.80
24
 
25
  def prepare_image(img_bytes):
26
  try:
 
62
 
63
  try:
64
  file_bytes = file.read()
 
65
  processed_img = prepare_image(file_bytes)
66
 
67
  prediction_tensor = model(processed_img, training=False)
 
68
  prediction = prediction_tensor.numpy()
69
 
70
  class_index = np.argmax(prediction)
71
  confidence = float(np.max(prediction))
72
 
73
+ if confidence < CONFIDENCE_THRESHOLD:
74
+ result_class = "Not Sure"
75
+ message = "Uncertain prediction. Please interpret with caution or upload a clearer image."
76
+ is_uncertain = True
77
+ else:
78
+ result_class = CLASS_NAMES[class_index]
79
+ message = "Prediction success."
80
+ is_uncertain = False
81
 
82
  result = {
83
  'result': result_class,
84
  'confidence': confidence,
85
+ 'message': message,
86
+ 'is_uncertain': is_uncertain,
87
+ 'top_class_suggestion': CLASS_NAMES[class_index]
88
  }
89
 
90
  return jsonify(result)