foxai


Namefoxai JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/softwaremill/FoXAI
SummaryModel Interpretability for PyTorch.
upload_time2023-05-23 12:30:19
maintainerAdam Jan Kaczmarek
docs_urlNone
authorReasonField Lab Team
requires_python>=3.7.2,<3.11
licenseApache-2.0
keywords model interpretability xai explainable ai model understanding feature importance pytorch
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FoXAI

FoXAI simplifies the application of e**X**plainable **AI** algorithms to explain the
performance of neural network models during training. The library acts as an
aggregator of existing libraries with implementations of various XAI algorithms and
seeks to facilitate and popularize their use in machine learning projects.

Currently, only algorithms related to computer vision are supported, but we plan to
add support for text, tabular and multimodal data problems in the future.

## Table of content:
* [Installation](#installation)
    * [GPU acceleration](#gpu-acceleration)
    * [Manual installation](#manual-installation)
* [Getting started](#getting-started)
* [Development](#development)
    * [Requirements](#requirements)
    * [CUDA](#cuda)
    * [pyenv](#pyenv)
    * [Poetry](#poetry)
    * [pre-commit hooks](#pre-commit-hooks-setup)
    * [Note](#note)
        * [Artifacts directory structure](#artifacts-directory-structure)
        * [Examples](#examples)

# Installation

Installation requirements:
* `Python` >=3.7.2,<3.11

**Important**: For any problems regarding installation we advise to refer first to our [FAQ](FAQ.md).

## GPU acceleration

To use the torch library with GPU acceleration, you need to install
a dedicated version of torch with support for the installed version of CUDA
drivers in the version supported by the library, at the moment `torch>=1.12.1,<2.0.0`.
A list of `torch` wheels with CUDA support can be found at
[https://download.pytorch.org/whl/torch/](https://download.pytorch.org/whl/torch/).

## Manual installation

If you would like to install from the source you can build a `wheel` package using `poetry`.
The assumption is that the `poetry` package is installed. You can find how to install
`poetry` [here](#poetry). To build `wheel` package run:

```bash
git clone https://github.com/softwaremill/FoXAI.git
cd FoXAI/
poetry install
poetry build
```

As a result you will get `wheel` file inside `dist/` directory that you can install
via `pip`:
```bash
pip install dist/foxai-x.y.z-py3-none-any.whl
```

# Getting started

To use the FoXAI library in your ML project, simply add an additional object of type
`WandBCallback` to the `Trainer`'s callback list from the `pytorch-lightning` library.
Currently, only the Weights and Biases tool for tracking experiments is supported.

Below is a code snippet from the example (`example/mnist_wandb.py`):

```python
import torch
from pytorch_lightning import Trainer
from pytorch_lightning.loggers import WandbLogger

import wandb
from foxai.callbacks.wandb_callback import WandBCallback
from foxai.context_manager import CVClassificationExplainers, ExplainerWithParams

    ...
    wandb.login()
    wandb_logger = WandbLogger(project=project_name, log_model="all")
    callback = WandBCallback(
        wandb_logger=wandb_logger,
        explainers=[
            ExplainerWithParams(
                explainer_name=CVClassificationExplainers.CV_INTEGRATED_GRADIENTS_EXPLAINER
            ),
            ExplainerWithParams(
                explainer_name=CVClassificationExplainers.CV_GRADIENT_SHAP_EXPLAINER
            ),
        ],
        idx_to_label={index: index for index in range(0, 10)},
    )
    model = LitMNIST()
    trainer = Trainer(
        accelerator="gpu",
        devices=1 if torch.cuda.is_available() else None,
        max_epochs=max_epochs,
        logger=wandb_logger,
        callbacks=[callback],
    )
    trainer.fit(model)
```

## CLI

A CLI tool is available to update the artifacts of an experiment tracked in
Weights and Biases. Allows you to create XAI explanations and send them to
W&B offline. This tool is using `hydra` to handle the configuration of `yaml` files.
To check options type:

```bash
foxai-wandb-updater --help
```

Typical usage with configuration in `config/config.yaml`:
```bash
foxai-wandb-updater --config-dir config/ --config-name config
```

Content of `config.yaml`:
```bash
username: <WANDB_USERANEM>
experiment: <WANDB_EXPERIMENT>
run_id: <WANDB_RUN_ID>
classifier: # model class to explain
  _target_: example.streamlit_app.mnist_model.LitMNIST
  batch_size: 1
  data_dir: "."
explainers: # list of explainers to use
 - explainer_with_params:
    explainer_name: CV_GRADIENT_SHAP_EXPLAINER
    kwargs:
      n_steps: 1000
```

# Development

## Requirements

The project was tested using Python version `3.8`.

## CUDA

The recommended version of CUDA is `10.2` as it is supported since version
`1.5.0` of `torch`. You can check the compatibility of your CUDA version
with the current version of `torch`:
https://pytorch.org/get-started/previous-versions/.

As our starting Docker image we were using the one provided by Nvidia: ``nvidia/cuda:10.2-devel-ubuntu18.04``.

If you wish an easy to use docker image we advise to use our ``Dockerfile``.

## pyenv
Optional step, but probably one of the easiest way to actually get Python version with all the needed aditional tools (e.g. pip).

`pyenv` is a tool used to manage multiple versions of Python. To install
this package follow the instructions on the project repository page:
https://github.com/pyenv/pyenv#installation. After installation You can
install desired Python version, e.g. `3.8.16`:
```bash
pyenv install 3.8.16
```

The next step is required to be able to use a desired version of Python with
`poetry`. To activate a specific version of Python interpreter execute the command:
```bash
pyenv local 3.8.16 # or `pyenv global 3.8.16`
```

Inside the repository with `poetry` You can select a specific version of Python
interpreter with the command:
```bash
poetry env use 3.8.16
```

After changing the interpreter version You have to once again install all
dependencies:
```bash
poetry install
```

## Poetry

To separate runtime environments for different services and repositories, it is
recommended to use a virtual Python environment. You can configure `Poetry` to
create a new virtual environment in the project directory of every repository. To
install `Poetry` follow the instruction at https://python-poetry.org/docs/#installing-with-the-official-installer. We are using `Poetry` in version
`1.2.1`. To install a specific version You have to provide desired package
version:
```bash
curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.1 python3 -
```
Add poetry to PATH:
```bash
export PATH="/home/ubuntu/.local/bin:$PATH"
echo 'export PATH="/home/ubuntu/.local/bin:$PATH"' >> ~/.bashrc
```

After installation, configure the creation of virtual environments in the directory of the project.
```bash
poetry config virtualenvs.create true
poetry config virtualenvs.in-project true
```

The final step is to install all the dependencies defined in the
`pyproject.toml` file.

```bash
poetry install
```

Once all the steps have been completed, the environment is ready to go.
A virtual environment by default will be created with the name `.venv` inside
the project directory.

## Pre-commit hooks setup

To improve the development experience, please make sure to install
our [pre-commit](https://pre-commit.com/) hooks as the very first step after
cloning the repository:

```bash
poetry run pre-commit install
```

## Note
---
At the moment only explainable algorithms for image classification are
implemented. In the future more algorithms and more computer vision tasks will
be introduced. In the end, the module should work with all types of tasks (NLP, etc.).

### Examples

In `example/notebooks/` directory You can find notebooks with example usage of this
framework. Scripts in `example/` directory contain samples of training models using
different callbacks.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/softwaremill/FoXAI",
    "name": "foxai",
    "maintainer": "Adam Jan Kaczmarek",
    "docs_url": null,
    "requires_python": ">=3.7.2,<3.11",
    "maintainer_email": "adam.kaczmarek@reasonfieldlab.com",
    "keywords": "Model Interpretability,XAI,explainable AI,Model Understanding,Feature Importance,PyTorch",
    "author": "ReasonField Lab Team",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/08/4f/bc9c2a1caa5526cb00eacd69aae871d2d427dc1c9303bf763b56dae76113/foxai-0.6.0.tar.gz",
    "platform": null,
    "description": "# FoXAI\n\nFoXAI simplifies the application of e**X**plainable **AI** algorithms to explain the\nperformance of neural network models during training. The library acts as an\naggregator of existing libraries with implementations of various XAI algorithms and\nseeks to facilitate and popularize their use in machine learning projects.\n\nCurrently, only algorithms related to computer vision are supported, but we plan to\nadd support for text, tabular and multimodal data problems in the future.\n\n## Table of content:\n* [Installation](#installation)\n    * [GPU acceleration](#gpu-acceleration)\n    * [Manual installation](#manual-installation)\n* [Getting started](#getting-started)\n* [Development](#development)\n    * [Requirements](#requirements)\n    * [CUDA](#cuda)\n    * [pyenv](#pyenv)\n    * [Poetry](#poetry)\n    * [pre-commit hooks](#pre-commit-hooks-setup)\n    * [Note](#note)\n        * [Artifacts directory structure](#artifacts-directory-structure)\n        * [Examples](#examples)\n\n# Installation\n\nInstallation requirements:\n* `Python` >=3.7.2,<3.11\n\n**Important**: For any problems regarding installation we advise to refer first to our [FAQ](FAQ.md).\n\n## GPU acceleration\n\nTo use the torch library with GPU acceleration, you need to install\na dedicated version of torch with support for the installed version of CUDA\ndrivers in the version supported by the library, at the moment `torch>=1.12.1,<2.0.0`.\nA list of `torch` wheels with CUDA support can be found at\n[https://download.pytorch.org/whl/torch/](https://download.pytorch.org/whl/torch/).\n\n## Manual installation\n\nIf you would like to install from the source you can build a `wheel` package using `poetry`.\nThe assumption is that the `poetry` package is installed. You can find how to install\n`poetry` [here](#poetry). To build `wheel` package run:\n\n```bash\ngit clone https://github.com/softwaremill/FoXAI.git\ncd FoXAI/\npoetry install\npoetry build\n```\n\nAs a result you will get `wheel` file inside `dist/` directory that you can install\nvia `pip`:\n```bash\npip install dist/foxai-x.y.z-py3-none-any.whl\n```\n\n# Getting started\n\nTo use the FoXAI library in your ML project, simply add an additional object of type\n`WandBCallback` to the `Trainer`'s callback list from the `pytorch-lightning` library.\nCurrently, only the Weights and Biases tool for tracking experiments is supported.\n\nBelow is a code snippet from the example (`example/mnist_wandb.py`):\n\n```python\nimport torch\nfrom pytorch_lightning import Trainer\nfrom pytorch_lightning.loggers import WandbLogger\n\nimport wandb\nfrom foxai.callbacks.wandb_callback import WandBCallback\nfrom foxai.context_manager import CVClassificationExplainers, ExplainerWithParams\n\n    ...\n    wandb.login()\n    wandb_logger = WandbLogger(project=project_name, log_model=\"all\")\n    callback = WandBCallback(\n        wandb_logger=wandb_logger,\n        explainers=[\n            ExplainerWithParams(\n                explainer_name=CVClassificationExplainers.CV_INTEGRATED_GRADIENTS_EXPLAINER\n            ),\n            ExplainerWithParams(\n                explainer_name=CVClassificationExplainers.CV_GRADIENT_SHAP_EXPLAINER\n            ),\n        ],\n        idx_to_label={index: index for index in range(0, 10)},\n    )\n    model = LitMNIST()\n    trainer = Trainer(\n        accelerator=\"gpu\",\n        devices=1 if torch.cuda.is_available() else None,\n        max_epochs=max_epochs,\n        logger=wandb_logger,\n        callbacks=[callback],\n    )\n    trainer.fit(model)\n```\n\n## CLI\n\nA CLI tool is available to update the artifacts of an experiment tracked in\nWeights and Biases. Allows you to create XAI explanations and send them to\nW&B offline. This tool is using `hydra` to handle the configuration of `yaml` files.\nTo check options type:\n\n```bash\nfoxai-wandb-updater --help\n```\n\nTypical usage with configuration in `config/config.yaml`:\n```bash\nfoxai-wandb-updater --config-dir config/ --config-name config\n```\n\nContent of `config.yaml`:\n```bash\nusername: <WANDB_USERANEM>\nexperiment: <WANDB_EXPERIMENT>\nrun_id: <WANDB_RUN_ID>\nclassifier: # model class to explain\n  _target_: example.streamlit_app.mnist_model.LitMNIST\n  batch_size: 1\n  data_dir: \".\"\nexplainers: # list of explainers to use\n - explainer_with_params:\n    explainer_name: CV_GRADIENT_SHAP_EXPLAINER\n    kwargs:\n      n_steps: 1000\n```\n\n# Development\n\n## Requirements\n\nThe project was tested using Python version `3.8`.\n\n## CUDA\n\nThe recommended version of CUDA is `10.2` as it is supported since version\n`1.5.0` of `torch`. You can check the compatibility of your CUDA version\nwith the current version of `torch`:\nhttps://pytorch.org/get-started/previous-versions/.\n\nAs our starting Docker image we were using the one provided by Nvidia: ``nvidia/cuda:10.2-devel-ubuntu18.04``.\n\nIf you wish an easy to use docker image we advise to use our ``Dockerfile``.\n\n## pyenv\nOptional step, but probably one of the easiest way to actually get Python version with all the needed aditional tools (e.g. pip).\n\n`pyenv` is a tool used to manage multiple versions of Python. To install\nthis package follow the instructions on the project repository page:\nhttps://github.com/pyenv/pyenv#installation. After installation You can\ninstall desired Python version, e.g. `3.8.16`:\n```bash\npyenv install 3.8.16\n```\n\nThe next step is required to be able to use a desired version of Python with\n`poetry`. To activate a specific version of Python interpreter execute the command:\n```bash\npyenv local 3.8.16 # or `pyenv global 3.8.16`\n```\n\nInside the repository with `poetry` You can select a specific version of Python\ninterpreter with the command:\n```bash\npoetry env use 3.8.16\n```\n\nAfter changing the interpreter version You have to once again install all\ndependencies:\n```bash\npoetry install\n```\n\n## Poetry\n\nTo separate runtime environments for different services and repositories, it is\nrecommended to use a virtual Python environment. You can configure `Poetry` to\ncreate a new virtual environment in the project directory of every repository. To\ninstall `Poetry` follow the instruction at https://python-poetry.org/docs/#installing-with-the-official-installer. We are using `Poetry` in version\n`1.2.1`. To install a specific version You have to provide desired package\nversion:\n```bash\ncurl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.1 python3 -\n```\nAdd poetry to PATH:\n```bash\nexport PATH=\"/home/ubuntu/.local/bin:$PATH\"\necho 'export PATH=\"/home/ubuntu/.local/bin:$PATH\"' >> ~/.bashrc\n```\n\nAfter installation, configure the creation of virtual environments in the directory of the project.\n```bash\npoetry config virtualenvs.create true\npoetry config virtualenvs.in-project true\n```\n\nThe final step is to install all the dependencies defined in the\n`pyproject.toml` file.\n\n```bash\npoetry install\n```\n\nOnce all the steps have been completed, the environment is ready to go.\nA virtual environment by default will be created with the name `.venv` inside\nthe project directory.\n\n## Pre-commit hooks setup\n\nTo improve the development experience, please make sure to install\nour [pre-commit](https://pre-commit.com/) hooks as the very first step after\ncloning the repository:\n\n```bash\npoetry run pre-commit install\n```\n\n## Note\n---\nAt the moment only explainable algorithms for image classification are\nimplemented. In the future more algorithms and more computer vision tasks will\nbe introduced. In the end, the module should work with all types of tasks (NLP, etc.).\n\n### Examples\n\nIn `example/notebooks/` directory You can find notebooks with example usage of this\nframework. Scripts in `example/` directory contain samples of training models using\ndifferent callbacks.\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Model Interpretability for PyTorch.",
    "version": "0.6.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/softwaremill/foxai/issues",
        "Documentation": "https://softwaremill.github.io/FoXAI/",
        "Homepage": "https://github.com/softwaremill/FoXAI",
        "Repository": "https://github.com/softwaremill/FoXAI"
    },
    "split_keywords": [
        "model interpretability",
        "xai",
        "explainable ai",
        "model understanding",
        "feature importance",
        "pytorch"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9d7f145918bdb2983f384298a67c1c7d5b6afd04d59dc1fbd33eadfbc3b40325",
                "md5": "69dc7b97e8eed81905ceaa4680e1c75e",
                "sha256": "bdd92772812632cf32d3d1a5c224fb5eb3c725c98524a0f5e79e32f692343912"
            },
            "downloads": -1,
            "filename": "foxai-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "69dc7b97e8eed81905ceaa4680e1c75e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.2,<3.11",
            "size": 62356,
            "upload_time": "2023-05-23T12:30:16",
            "upload_time_iso_8601": "2023-05-23T12:30:16.802947Z",
            "url": "https://files.pythonhosted.org/packages/9d/7f/145918bdb2983f384298a67c1c7d5b6afd04d59dc1fbd33eadfbc3b40325/foxai-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "084fbc9c2a1caa5526cb00eacd69aae871d2d427dc1c9303bf763b56dae76113",
                "md5": "20fe08dabe621b1f7db1f569f71383d3",
                "sha256": "42b19a951126de23eb07098c56dc4bceef291c13ae371fca26b4dcfb3785465b"
            },
            "downloads": -1,
            "filename": "foxai-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "20fe08dabe621b1f7db1f569f71383d3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.2,<3.11",
            "size": 38458,
            "upload_time": "2023-05-23T12:30:19",
            "upload_time_iso_8601": "2023-05-23T12:30:19.261224Z",
            "url": "https://files.pythonhosted.org/packages/08/4f/bc9c2a1caa5526cb00eacd69aae871d2d427dc1c9303bf763b56dae76113/foxai-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-23 12:30:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "softwaremill",
    "github_project": "FoXAI",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "foxai"
}
        
Elapsed time: 0.06823s