Monitor a ImageNet Classifier
Monitor and explain a ML model that classifies natural images to object labels.
In this tutorial, we will guide you step-by-step through using the Obz AI python package to monitor and explain a machine learning model that classifies natural images to object labels.
For anomaly detection, we utilize a data inspection routine based on Gaussian Mixture Model (GMM) fitted on First Order Image Features. For explainable AI, we compute attention maps and class-discriminative attention maps (CDAMs).
What will you learn?
- How to apply a simple image classification model using sample medical images.
- How to log directly to the Obz AI cloud platform from your code.
Setup
First let’s install obzai
package if you haven’t already installed it. Also let’s install gdown
to download pretrained weights.
Import the necessary modules:
Before you get started logging and visualizing data with the ObzClient
from our Python library, you will need your unique API key. This key is required to securely connect your code to your account on our SaaS platform.
Here’s how to find your API key:
-
Log in to your Dashboard:
Open your web browser and go to Obz.AI. Log in using your registered email and password. -
Navigate to the Settings section:
After logging in, look for a menu item called “Settings”. Click on it to access your API settings. Make sure to copy this key now — you will need it in the next steps.
Before you can monitor or visualize the workings of your model using our library, you first need to define the model you want to track.
In this tutorial, we will use a Vision Transformer (ViT) model that has been previously trained and fine-tuned on a subset of the ImageNet data called Imagenette.
Configure the ViT classifer based on DINO backbone
We are adding a binary classification head (see how torch.nn.Linear
) onto a DINO backbone.
Next, let’s prepare the datasets. In general, you will need two separate sets of images:
Reference Dataset: This dataset will be used to extract reference image features and to fit the outlier detection models. In ML, this may be a training dataset.
Inference Dataset: This dataset will be treated as incoming new data on which you want to perform outlier detection. In ML, this may be testing or validation dataset or any new samples in production.
For the tutorial case we will use ImageNet subset called: Imagenette. This dataset is readily available via torchvision package.
Using ObzClient to Explain and Monitor a AI Model
In this section, we’ll show you how to set up and use the ObzClient
to automatically log your data features and model explanation outputs to the Obz AI cloud platform.
Local Usage
You can use the Data Inspector Module and XAI Module locally on your machine. To explore all the core features of these modules, please see the Jupyter notebook tutorials provided:
Cloud Logging with ObzClient
To take full advantage of Obz AI, you can log your data features and model explanation outputs to the Obz AI cloud. This enables you to:
- Track and organize input features and outlier statistics.
- Store and visualize model explanations and results.
- Collaborate and share insights via the Obz dashboard.
Step 1: Instantiate the Modules
Instantiate and fit an Outlier Detector from the Data Inspector
Module for outlier detection:
Instantiate your chosen explainability method from the XAI Tool
Module:
Step 2: Prepare ObzClient
for a cloud logging
Now, when you have your fitted OutlierDetector
instance and XAITool
instances, you are ready to wrap it into your ObzClient
!
It will take care of logging data to your cloud dashboard.
Step 3: Authenticate ObzClient
To use our library and send logs from your project to your online dashboard, you first need to authenticate your client. Authentication links your logs to your specific account and project, ensuring that you can securely access and manage your data.
To authenticate, you will need an API key. This key is a unique identifier for your account and allows the library to send information to your dashboard. You can find your API key by logging into your account and visiting your dashboard. If you haven’t registered yet, please create an account and follow the instructions to generate your API key.
Once you have the API key, pass it to the client’s .login()
method in your Python code.
After you log in successfully, your credentials are automatically stored in a hidden .netrc
file on your computer. This means you do not have to enter your API key again when using the ObzClient
, authentication will happen automatically in future sessions.
Now let’s initialize a project with .init_project()
method. This method accepts following arguments:
-
project_name
(str, required): the name of the project you want to work with.- If the project already exists, you will be connected to it.
- If it is a new name, a new project will be created automatically.
-
ml_task
(str, required): the type of machine learning task you are working on. We will usemulticlass_classification
in this tutorial.- This setting allows ObzClient to tailor the project’s dashboard and logging features to your specific task.
-
index2name
(dict, optional): A dictionary mapping numeric class indices to human-readable class names.- Providing this helps ObzClient visualize your results with actual class names, making your dashboard easier to understand.
Step 4: Log data features and model explanations to the Obz AI cloud platform
Now you can log your reference data. Simply call .log_reference()
method on your client object. It will automatically send all reference data from your Outlier Detector into your Dashboard.
With ObzClient
initiated with an outlier detector and an XAI tool, we are ready to .run_and_log
, processing batches of samples in the inference data.