{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Diversity - Feature Visualization", "version": "0.3.2", "views": {}, "default_view": {}, "provenance": [] }, "kernelspec": { "name": "python2", "display_name": "Python 2" } }, "cells": [ { "metadata": { "id": "JndnmDMp66FL", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### Copyright 2018 Google LLC.\n", "\n", "Licensed under the Apache License, Version 2.0 (the \"License\");" ] }, { "metadata": { "id": "hMqWDc_m6rUC", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } }, "cellView": "both" }, "cell_type": "code", "source": [ "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "_vAVmphMywZR", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Negative Neurons - Feature Visualization\n", "\n", "This notebook uses [**Lucid**](https://github.com/tensorflow/lucid) to reproduce some of the results in [Feature Visualization](https://distill.pub/2017/feature-visualization/).\n", "\n", "This notebook doesn't introduce the abstractions behind lucid; you may wish to also read the [Lucid tutorial](https://colab.research.google.com/github/tensorflow/lucid/blob/master/notebooks/tutorial.ipynb).\n", "\n", "**Note**: The easiest way to use this tutorial is as a colab notebook, which allows you to dive in with no setup. We recommend you enable a free GPU by going:\n", "\n", "> **Runtime**   →   **Change runtime type**   →   **Hardware Accelerator: GPU**" ] }, { "metadata": { "id": "FsFc1mE51tCd", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## Install, Import, Load Model" ] }, { "metadata": { "id": "RBr8QbboRAdU", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "!pip install --quiet lucid==0.0.5\n", "\n", "import numpy as np\n", "import scipy.ndimage as nd\n", "import tensorflow as tf\n", "\n", "import lucid.modelzoo.vision_models as models\n", "from lucid.misc.io import show\n", "import lucid.optvis.objectives as objectives\n", "import lucid.optvis.param as param\n", "import lucid.optvis.render as render\n", "import lucid.optvis.transform as transform" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "yNALaA0QRJVT", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Let's import a model from the Lucid modelzoo!\n", "\n", "model = models.InceptionV1()\n", "model.load_graphdef()" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "ujM5RP7LGGnt", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Diversity" ] }, { "metadata": { "id": "aXPtRHK0GGnv", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n", "" ] }, { "metadata": { "id": "VeRQbcWXZ6cE", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 288 }, "outputId": "dc7831bb-e60a-46f2-c040-fc47d48e4a6a", "executionInfo": { "status": "ok", "timestamp": 1526678594901, "user_tz": 420, "elapsed": 422578, "user": { "displayName": "Christopher Olah", "photoUrl": "//lh5.googleusercontent.com/-GhJP0RTFLEs/AAAAAAAAAAI/AAAAAAAAEZ8/wDVK-lwJYfA/s50-c-k-no/photo.jpg", "userId": "104171973056281402320" } } }, "cell_type": "code", "source": [ "param_f = lambda: param.image(128, batch=4)\n", "obj = objectives.channel(\"mixed5a_pre_relu\", 9) - 1e2*objectives.diversity(\"mixed5a\")\n", "_ = render.render_vis(model, obj, param_f)" ], "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ "WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/lucid/optvis/objectives.py:399: calling l2_normalize (from tensorflow.python.ops.nn_impl) with dim is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "dim is deprecated, use axis instead\n" ], "name": "stdout" }, { "output_type": "stream", "text": [ "WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/lucid/optvis/objectives.py:399: calling l2_normalize (from tensorflow.python.ops.nn_impl) with dim is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "dim is deprecated, use axis instead\n" ], "name": "stderr" }, { "output_type": "stream", "text": [ "512 745.20917\n" ], "name": "stdout" }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "" ] }, "metadata": { "tags": [] } } ] }, { "metadata": { "id": "tA-tuupQGd-f", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] } ] }