andrewzamp commited on
Commit
a8713e0
·
1 Parent(s): 22ce0b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -51,36 +51,47 @@ def load_and_preprocess_image(image, target_size=(224, 224)):
51
  def make_prediction(image, taxonomic_decision, taxonomic_level):
52
  # Preprocess the image
53
  img_array = load_and_preprocess_image(image)
54
-
55
  # Get the class names from the 'species' column
56
  class_names = sorted(taxo_df['species'].unique())
57
-
58
  # Make a prediction
59
  prediction = model.predict(img_array)
60
-
61
  # Initialize variables for aggregated predictions and level index
62
  aggregated_predictions = None
63
  current_level_index = 0 # Start from the species level
64
-
65
  # Determine the initial taxonomic level based on the user's decision
66
  if taxonomic_decision == "No, I will let the model decide":
67
  current_level_index = 0 # Start at species level if letting the model decide
68
  else:
69
  current_level_index = taxonomic_levels.index(taxonomic_level) # Use specified level
70
 
71
- # Loop through taxonomic levels to check confidence
 
 
 
 
 
 
 
 
 
 
 
 
72
  while current_level_index < len(taxonomic_levels):
73
- # Aggregate predictions based on the current taxonomic level
74
- aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_levels[current_level_index], class_names)
75
-
76
- # Check if the confidence of the top prediction meets the threshold
77
- top_prediction_index = np.argmax(aggregated_predictions)
78
- top_prediction_confidence = aggregated_predictions[0][top_prediction_index]
79
-
80
  if top_prediction_confidence >= 0.80:
81
  break # Confidence threshold met, exit loop
82
-
83
  current_level_index += 1 # Move to the next taxonomic level
 
 
 
 
 
84
 
85
  # Check if a valid prediction was made
86
  if current_level_index == len(taxonomic_levels):
 
51
  def make_prediction(image, taxonomic_decision, taxonomic_level):
52
  # Preprocess the image
53
  img_array = load_and_preprocess_image(image)
54
+
55
  # Get the class names from the 'species' column
56
  class_names = sorted(taxo_df['species'].unique())
57
+
58
  # Make a prediction
59
  prediction = model.predict(img_array)
60
+
61
  # Initialize variables for aggregated predictions and level index
62
  aggregated_predictions = None
63
  current_level_index = 0 # Start from the species level
64
+
65
  # Determine the initial taxonomic level based on the user's decision
66
  if taxonomic_decision == "No, I will let the model decide":
67
  current_level_index = 0 # Start at species level if letting the model decide
68
  else:
69
  current_level_index = taxonomic_levels.index(taxonomic_level) # Use specified level
70
 
71
+ # Aggregate predictions based on the current taxonomic level
72
+ aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_levels[current_level_index], class_names)
73
+
74
+ # Check if the confidence of the top prediction meets the threshold
75
+ top_prediction_index = np.argmax(aggregated_predictions)
76
+ top_prediction_confidence = aggregated_predictions[0][top_prediction_index]
77
+
78
+ # If the user specified a taxonomic level, do not automatically promote to a higher level
79
+ if taxonomic_decision == "Yes, I want to specify the taxonomic level":
80
+ if current_level_index == 0 and top_prediction_confidence < 0.80:
81
+ return "<h1 style='font-weight: bold;'>Confidence too low for specified taxonomic level</h1>"
82
+
83
+ # Proceed to the next level if the confidence threshold is not met and the user allows it
84
  while current_level_index < len(taxonomic_levels):
85
+ # Check if confidence is above the threshold at the current level
 
 
 
 
 
 
86
  if top_prediction_confidence >= 0.80:
87
  break # Confidence threshold met, exit loop
88
+
89
  current_level_index += 1 # Move to the next taxonomic level
90
+ if current_level_index < len(taxonomic_levels):
91
+ # Aggregate predictions for the next level
92
+ aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_levels[current_level_index], class_names)
93
+ top_prediction_index = np.argmax(aggregated_predictions)
94
+ top_prediction_confidence = aggregated_predictions[0][top_prediction_index]
95
 
96
  # Check if a valid prediction was made
97
  if current_level_index == len(taxonomic_levels):