{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Inactive Unit Analysis: Example\n", "\n", "This notebook demonstrates how to run [IUA](https://apple.github.io/dnikit/api/dnikit/introspectors.html#dnikit.introspectors.IUA) on a simple dataset. For more general information about how to use DNIKit 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://apple.github.io/dnikit/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 DNIKit to run inference\n", "\n", "Let us start by importing everything needed to run on this notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dnikit.base import pipeline, ResponseInfo\n", "from dnikit.introspectors import IUA\n", "from dnikit.samples import StubImageDataset\n", "from dnikit.processors import FieldRenamer, Transposer\n", "from dnikit.exceptions import enable_deprecation_warnings\n", "\n", "enable_deprecation_warnings(error=True) # treat DNIKit deprecation warnings as errors\n", "\n", "from dnikit_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": {}, "outputs": [], "source": [ "from dnikit_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": {}, "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": {}, "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": {}, "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": {}, "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": {}, "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": {}, "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": { "scrolled": false }, "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": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.9.8" } }, "nbformat": 4, "nbformat_minor": 1 }