{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Test models with the xAI evaluatin metrics of th ReVel framework\n", "================================================================" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded the pretrained model.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_93584/3587458112.py:46: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " state_dict = torch.load(\n" ] } ], "source": [ "import torch\n", "\n", "torch.set_num_threads(1)\n", "from torch.utils.data.dataloader import DataLoader\n", "import tqdm\n", "import numpy as np\n", "import json\n", "import argparse\n", "import pandas as pd\n", "import os\n", "\n", "from ReVel.LLEs import get_xai_model\n", "from ReVel.perturbations import get_perturbation\n", "from ReVel.load_data import load_data\n", "from ReVel.revel.revel import ReVel\n", "import TSHIELD.procedures as procedures\n", "device = \"cpu\"\n", "\n", "n_class = 102\n", "ds = \"Flowers\"\n", "iterations = 5\n", "batch_size = 32\n", "max_examples = 500\n", "samples = 10\n", "sigma = 12\n", "xai_model = \"LIME\"\n", "dim = 8\n", "\n", "csv_file = \"./metrics.csv\"\n", "perturbation = get_perturbation(\n", " name=\"square\", dim=dim, num_classes=n_class, final_size=(224, 224)\n", ")\n", "Test = load_data(ds, perturbation=perturbation, train=False, dir=\"./data\")\n", "# Hacer que Test tenga solo las primeras 'samples' de Test\n", "if isinstance(samples, int):\n", " indices = np.random.choice(\n", " [i for i in range(len(Test))], size=samples, replace=False\n", " )\n", " Test = torch.utils.data.Subset(Test, indices)\n", "\n", "TestLoader = iter(DataLoader(Test, batch_size=1, shuffle=False))\n", "classifier = procedures.classifier(\"efficientnet-b2\", n_class)\n", "classifier.to(device)\n", "\n", "\n", "state_dict = torch.load(\n", " f'{os.getcwd()}/model.pth',\n", " map_location=device,\n", ")\n", "\n", "classifier.load_state_dict(state_dict)\n", "classifier.to(device)\n", "classifier.eval()\n", "print(\"Loaded the pretrained model.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "REVEL metrics calculation for a model trained on the Flowers dataset\n", "======================================================================" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "index = 0\n", "for data in tqdm.tqdm(TestLoader, total=len(TestLoader)):#len(TestLoader)):\n", " inputs, labels = data\n", " for k, inp in enumerate(inputs):\n", " inp = inp.to(device)\n", "\n", " # inp dims: (C,H,W) -> (H,W,C)\n", " inp = np.transpose(inp, (1, 2, 0))\n", "\n", " labels = labels[k].to(device)\n", " explainer = get_xai_model(\n", " name=xai_model,\n", " perturbation=perturbation,\n", " max_examples=max_examples,\n", " dim=dim,\n", " sigma=sigma,\n", " )\n", " def classify(image, model=classifier):\n", " \"\"\"\n", " This function takes an image and returns the predicted probabilities.\n", " :param image: A tensor of shape HxWxC\n", " :return: A tensor of shape Cx1\n", " \"\"\"\n", " if isinstance(image, np.ndarray):\n", " image = np.expand_dims(image, 0)\n", "\n", " image = torch.Tensor(image).to(device)\n", "\n", " else:\n", " image = torch.unsqueeze(image, 0)\n", "\n", " # image dims: (N,H,W,C) -> (N,C,H,W)\n", "\n", " image = torch.transpose(image, 3, 2).transpose(2, 1)\n", "\n", " result = model(image)\n", " return result\n", " def model_fordward(\n", " X: np.array, explainator=explainer, model=classify, img=inp\n", " ):\n", " neutral = explainator.perturbation.fn_neutral_image(img)\n", "\n", " avoid = [i for i in range(len(X)) if X[i] == 0]\n", "\n", " segments = explainator.perturbation.segmentation_fn(img.numpy())\n", " perturbation = explainator.perturbation.perturbation(\n", " img, neutral, segments=segments, indexes=avoid\n", " )\n", " return model(perturbation)\n", " segments = explainer.perturbation.segmentation_fn(inp.numpy())\n", "\n", " revel = ReVel(\n", " model_f=classify,\n", " model_g=model_fordward,\n", " instance=inp,\n", " lle=explainer,\n", " n_classes=n_class,\n", " segments=segments,\n", " )\n", " df = revel.evaluate(times=iterations)\n", " df.loc[:, \"dataset\"] = ds\n", " df.loc[:, \"name\"] = \"SHIELD\"\n", " df.loc[:, \"index\"] = index\n", " index+=1\n", " if os.path.exists(csv_file):\n", " bigDF = pd.read_csv(csv_file)\n", " bigDF = pd.concat([bigDF, df])\n", " else:\n", " bigDF = df\n", " bigDF.to_csv(csv_file, index=False)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | conciseness | \n", "local_fidelity | \n", "local_concordance | \n", "prescriptivity | \n", "robustness | \n", "dataset | \n", "name | \n", "index | \n", "
|---|---|---|---|---|---|---|---|---|
| 0 | \n", "0.592374 | \n", "0.968821 | \n", "0.948301 | \n", "0.986014 | \n", "0.900298 | \n", "Flowers | \n", "SHIELD | \n", "0 | \n", "
| 1 | \n", "0.575238 | \n", "0.970056 | \n", "0.950214 | \n", "0.986120 | \n", "0.900298 | \n", "Flowers | \n", "SHIELD | \n", "0 | \n", "
| 2 | \n", "0.621403 | \n", "0.971654 | \n", "0.953586 | \n", "0.982834 | \n", "0.900298 | \n", "Flowers | \n", "SHIELD | \n", "0 | \n", "
| 3 | \n", "0.656200 | \n", "0.968553 | \n", "0.948588 | \n", "0.986866 | \n", "0.900298 | \n", "Flowers | \n", "SHIELD | \n", "0 | \n", "
| 4 | \n", "0.555271 | \n", "0.966748 | \n", "0.944305 | \n", "0.988516 | \n", "0.900298 | \n", "Flowers | \n", "SHIELD | \n", "0 | \n", "
| 5 | \n", "0.718255 | \n", "0.974289 | \n", "0.955635 | \n", "0.983946 | \n", "0.905916 | \n", "Flowers | \n", "SHIELD | \n", "1 | \n", "
| 6 | \n", "0.697398 | \n", "0.970710 | \n", "0.950923 | \n", "0.980930 | \n", "0.905916 | \n", "Flowers | \n", "SHIELD | \n", "1 | \n", "
| 7 | \n", "0.690674 | \n", "0.975697 | \n", "0.957971 | \n", "0.980183 | \n", "0.905916 | \n", "Flowers | \n", "SHIELD | \n", "1 | \n", "
| 8 | \n", "0.687614 | \n", "0.963620 | \n", "0.938403 | \n", "0.978476 | \n", "0.905916 | \n", "Flowers | \n", "SHIELD | \n", "1 | \n", "
| 9 | \n", "0.668301 | \n", "0.974379 | \n", "0.956623 | \n", "0.981830 | \n", "0.905916 | \n", "Flowers | \n", "SHIELD | \n", "1 | \n", "