<jupyter_start><jupyter_text>Introduction to CetaceXplainThe idea behind CetaceXplain is to provide a module which can easily lend insight into what machine learning models for dolphin whistle classification consider important for their decisions. This module will give provide some useful tools to get statistical reports about a model's performance, and will use the SHAP library (https://github.com/slundberg/shap) to visualize the pixels of importance on a spectrogram for whistle contour classification.The link to SHAP provided contains some other interesting features (see shap.GradientExplainer to explain specific layers within a model) which can possible be explored in future.<jupyter_code># For library requirements, run the commented line below in a directory with the requirements.txt file for cetacexplain
# ! pip install -r requirements.txt 


import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator 
# If using Google Drive, use the commented lines below to mount Google Drive so data can be accessed:

# from google.colab import drive
# drive.mount('/content/drive') # '/content' is the current working directory<jupyter_output><empty_output><jupyter_text>Importing CetaceXplainWe access `cetacexplain` by copying it to the current working directory (if not there already), then using the regular `import` command.<jupyter_code># Copy cetacexplain.py to current working  directory (the file path can be changed as needed to access it).
# Example: ! cp /content/drive/MyDrive/'Colab Notebooks'/cetacexplain.py .
# This can be ignored if cetacexplain is already in the current working directory.

import cetacexplain<jupyter_output>WARNING:tensorflow:From C:\Users\tifar\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\compat\v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term<jupyter_text>Load the Classifier Model and Image DataHere we load one of the deep learning classifiers which was trained separately, and load some test image data.<jupyter_code>IMAGE_SHAPE = (202, 413)
directory_name = "../../model_test_train/"
model = tf.keras.models.load_model(directory_name + "transfer_learning_classifier_fine_tuned_improved.h5")


TEST_DATA_DIR = directory_name + "test"
test_generator = ImageDataGenerator(
                    rescale=1./255
                ).flow_from_directory(
                      TEST_DATA_DIR,
                      shuffle=True,
                      batch_size = 50,
                      target_size=IMAGE_SHAPE
                      )<jupyter_output>WARNING:tensorflow:From C:\Users\tifar\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\layers\normalization\batch_normalization.py:532: _colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Found 600 images belonging to 3 classes.<jupyter_text>Create ImageClassifierModelNow we use `cetacexplain` to create an `ImageClassifierModel` using the tensorflow deep learning model, and show an example of the `explain` function. Pixels which the model considers as positive for the image's class are highlighted red, while those which it considers negative for a classification are coloured blue. See https://github.com/slundberg/shapdeep-learning-example-with-deepexplainer-tensorflowkeras-models for a very clear example using the MNIST dataset.<jupyter_code>image_data, image_labels = test_generator.next()

# create random sample of background images for explainer to gauge expected model output
# and create random test images for explanation
background_images, test_images, test_image_labels = image_data[0:10], image_data[30:34], image_labels[30:34]

new_model = cetacexplain.ImageClassifierModel(model, background_images, image_labels.argmax(axis = 1), 
            class_names = list(test_generator.class_indices.keys()))

# show example of explain function
new_model.explain(test_images, test_image_labels)<jupyter_output>keras is no longer supported, please use tf.keras instead.
Your TensorFlow version is newer than 2.4.0 and so graph support has been removed in eager mode and some static graphs may not be supported. See PR #1483 for discussion.<jupyter_text>Show Statistical MetricsWe show how the ImageClassifierModel's other methods can be used to get an idea of the classifier model's statistical performance as well.<jupyter_code># get statistical metrics for batch tested
print(new_model.report_stats(image_data, image_labels))
new_model.show_confusion_matrix(image_data, image_labels)<jupyter_output>precision    recall  f1-score   support

  bottlenose       0.65      0.79      0.71        14
      common       0.44      0.57      0.50        14
melon-headed       0.80      0.55      0.65        22

    accuracy                           0.62        50
   macro avg       0.63      0.63      0.62        50
weighted avg       0.66      0.62      0.62        50
{'fraction_non_alphanumeric': 0.06065288777284141, 'fraction_numerical': 0.020861502800849915, 'mean_word_length': 4.096456692913386, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '593630', 'license': 'MIT', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1365, 'n_words': 514, 'path': 'CetaceXplain/CetaceXplain_Demo.ipynb', 'repo_name': 'dolphin-acoustics-vip/CetaceXplain', 'size': '5177'}