{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inactive Unit Analysis: Example\n",
    "\n",
    "This notebook demonstrates how to run [IUA](https://satishlokkoju.github.io/deepview/api/deepview/introspectors.html#deepview.introspectors.IUA) on a simple dataset. For more general information about how to use DeepView and about each of these steps, it's suggested to start with the How-To guides in the docs (starting with how to [load a model](https://satishlokkoju.github.io/deepview/how_to/connect_model.html)), and then checking out the [Familiarity Notebook for Rare Data and Data Errors](../data_introspection/familiarity_for_rare_data_discovery.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Use DeepView to run inference\n",
    "\n",
    "Let us start by importing everything needed to run on this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from deepview.base import pipeline, ResponseInfo\n",
    "from deepview.introspectors import IUA\n",
    "from deepview.samples import StubImageDataset\n",
    "from deepview.processors import FieldRenamer, Transposer\n",
    "from deepview.exceptions import enable_deprecation_warnings\n",
    "\n",
    "enable_deprecation_warnings(error=True)  # treat DeepView deprecation warnings as errors\n",
    "\n",
    "from deepview_tensorflow import load_tf_model_from_path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download a model, MobileNet, and store it locally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from deepview_tensorflow import TFModelExamples\n",
    "\n",
    "mobilenet = TFModelExamples.MobileNet()\n",
    "model = mobilenet.model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a Producer to generate data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data_producer = StubImageDataset(\n",
    "    dataset_size=32,\n",
    "    image_width=224,\n",
    "    image_height=224,\n",
    "    channel_count=3\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find Convolutional Layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "conv2d_responses = [\n",
    "    info.name\n",
    "    for info in model.response_infos.values()\n",
    "    if info.layer.kind is ResponseInfo.LayerKind.CONV_2D\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set up processing pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "response_producer = pipeline(\n",
    "    data_producer,\n",
    "    FieldRenamer({\"images\": \"input_1:0\"}),\n",
    "    model(conv2d_responses),\n",
    "    Transposer(dim=(0, 3, 1, 2))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Execute IUA introspector\n",
    "\n",
    "Which can be done with just a single line of code!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "iua = IUA.introspect(response_producer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Show table of results\n",
    "\n",
    "`IUA.show(iua)` will show, by default, a table of layers and the discovered inactive units."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "print(IUA.show(iua).head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot results\n",
    "\n",
    "`IUA.show` can also be used to view charts, by setting `vis_type` to `IUA.VisType.CHART`.\n",
    "\n",
    "One layer's chart can be viewed in this manner, e.g. `conv_pw_9`..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "IUA.show(iua, vis_type=IUA.VisType.CHART, response_names=['conv_pw_9'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... or view all responses' charts (omitting the `response_names` parameter):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "IUA.show(iua, vis_type=IUA.VisType.CHART)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "finalized": {
   "timestamp": 1660077213986,
   "trusted": true
  },
  "kernelspec": {
   "display_name": "deepview_dev",
   "language": "python",
   "name": "deepview_dev"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}