andrewzamp commited on
Commit
2759d0e
·
1 Parent(s): 3927ff5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -21,8 +21,8 @@ def get_class_name(predicted_class, taxonomic_level):
21
  unique_labels = sorted(taxo_df[taxonomic_level].unique())
22
  return unique_labels[predicted_class]
23
 
24
- # Function to aggregate predictions to the specified taxonomic level
25
- def aggregate_predictions(predicted_probs, taxonomic_level):
26
  unique_labels = sorted(taxo_df[taxonomic_level].unique())
27
  aggregated_predictions = np.zeros((predicted_probs.shape[0], len(unique_labels)))
28
 
@@ -31,7 +31,7 @@ def aggregate_predictions(predicted_probs, taxonomic_level):
31
  higher_level = row[taxonomic_level]
32
 
33
  species_index = class_names.index(species) # Index of the species in the prediction array
34
- higher_level_index = unique_labels.index(higher_level) # Index of the higher taxonomic level
35
 
36
  aggregated_predictions[:, higher_level_index] += predicted_probs[:, species_index]
37
 
@@ -47,24 +47,22 @@ def load_and_preprocess_image(image, target_size=(224, 224)):
47
  img_array = preprocess_input(img_array)
48
  return img_array
49
 
50
- # Function to make predictions at the selected taxonomic level
51
  def make_prediction(image, taxonomic_level):
52
  # Preprocess the image
53
  img_array = load_and_preprocess_image(image)
54
- # Make a prediction at the species level
55
  prediction = model.predict(img_array)
56
 
57
- # Aggregate predictions to the selected taxonomic level
58
- aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_level)
59
 
60
  # Get the top 5 predictions
61
- top_indices = np.argsort(aggregated_predictions[0])[-5:][::-1] # Indices of top 5 predictions
62
 
63
- # Get the predicted class and common name for the top prediction
64
  predicted_class_index = np.argmax(aggregated_predictions)
65
  predicted_class_name = aggregated_class_labels[predicted_class_index]
66
-
67
- # Get common name for the top predicted class
68
  predicted_common_name = taxo_df[taxo_df[taxonomic_level] == predicted_class_name]['common_name'].values[0]
69
  confidence = aggregated_predictions[0][predicted_class_index] * 100 # Confidence of the predicted class
70
 
@@ -76,10 +74,11 @@ def make_prediction(image, taxonomic_level):
76
  class_name = aggregated_class_labels[i]
77
  common_name = taxo_df[taxo_df[taxonomic_level] == class_name]['common_name'].values[0]
78
  confidence_percentage = aggregated_predictions[0][i] * 100
 
79
  output_text += f"<div style='display: flex; justify-content: space-between;'>" \
80
  f"<span style='font-style: italic;'>{class_name}</span>&nbsp;(<span>{common_name}</span>)" \
81
  f"<span style='margin-left: auto;'>{confidence_percentage:.2f}%</span></div>"
82
-
83
  return output_text
84
 
85
  # Define the Gradio interface
@@ -88,7 +87,7 @@ interface = gr.Interface(
88
  inputs=[gr.Image(type="pil"), # Input type: Image (PIL format)
89
  gr.Dropdown(choices=taxonomic_levels, label="Taxonomic level", value="species")], # Use 'value' instead of 'default'
90
  outputs="html", # Output type: HTML for formatting
91
- title="Amazon Arboreal Species Classification",
92
  description="Upload an image and select the taxonomic level to classify the species."
93
  )
94
 
 
21
  unique_labels = sorted(taxo_df[taxonomic_level].unique())
22
  return unique_labels[predicted_class]
23
 
24
+ # Function to aggregate predictions to a higher taxonomic level
25
+ def aggregate_predictions(predicted_probs, taxonomic_level, class_names):
26
  unique_labels = sorted(taxo_df[taxonomic_level].unique())
27
  aggregated_predictions = np.zeros((predicted_probs.shape[0], len(unique_labels)))
28
 
 
31
  higher_level = row[taxonomic_level]
32
 
33
  species_index = class_names.index(species) # Index of the species in the prediction array
34
+ higher_level_index = unique_labels.index(higher_level)
35
 
36
  aggregated_predictions[:, higher_level_index] += predicted_probs[:, species_index]
37
 
 
47
  img_array = preprocess_input(img_array)
48
  return img_array
49
 
50
+ # Function to make predictions
51
  def make_prediction(image, taxonomic_level):
52
  # Preprocess the image
53
  img_array = load_and_preprocess_image(image)
54
+ # Make a prediction
55
  prediction = model.predict(img_array)
56
 
57
+ # Aggregate predictions based on the selected taxonomic level
58
+ aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_level, class_names)
59
 
60
  # Get the top 5 predictions
61
+ top_indices = np.argsort(aggregated_predictions[0])[-5:][::-1]
62
 
63
+ # Get predicted class and common name for the top prediction
64
  predicted_class_index = np.argmax(aggregated_predictions)
65
  predicted_class_name = aggregated_class_labels[predicted_class_index]
 
 
66
  predicted_common_name = taxo_df[taxo_df[taxonomic_level] == predicted_class_name]['common_name'].values[0]
67
  confidence = aggregated_predictions[0][predicted_class_index] * 100 # Confidence of the predicted class
68
 
 
74
  class_name = aggregated_class_labels[i]
75
  common_name = taxo_df[taxo_df[taxonomic_level] == class_name]['common_name'].values[0]
76
  confidence_percentage = aggregated_predictions[0][i] * 100
77
+
78
  output_text += f"<div style='display: flex; justify-content: space-between;'>" \
79
  f"<span style='font-style: italic;'>{class_name}</span>&nbsp;(<span>{common_name}</span>)" \
80
  f"<span style='margin-left: auto;'>{confidence_percentage:.2f}%</span></div>"
81
+
82
  return output_text
83
 
84
  # Define the Gradio interface
 
87
  inputs=[gr.Image(type="pil"), # Input type: Image (PIL format)
88
  gr.Dropdown(choices=taxonomic_levels, label="Taxonomic level", value="species")], # Use 'value' instead of 'default'
89
  outputs="html", # Output type: HTML for formatting
90
+ title="Amazon arboreal species classification",
91
  description="Upload an image and select the taxonomic level to classify the species."
92
  )
93