{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lucid local development notebook\n", "\n", "This notebook allows you to use your development version of the lucid codebase.\n", "It is meant to only be used during development of lucid, not when using it.\n", "\n", "Instead, see \"usage_example.ipynb\" in this folder for an example of how to use lucid in a notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n", "\n", "### Add local package to search path" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os\n", "import sys\n", "\n", "module_path = os.path.abspath(os.path.join('..'))\n", "if module_path not in sys.path:\n", " sys.path.append(module_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we should be able to import it directly, just like if we installed it using `pip`:\n", "\n", "```python\n", "import lucid\n", "```\n", "\n", "However, we will use `autoreload` so we can quickly iterate on local code changes:\n", "\n", "### Enable autoreload" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%load_ext autoreload" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%aimport lucid\n", "%aimport -tensorflow\n", "%aimport -numpy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Modules to reload:\n", "lucid\n", "\n", "Modules to skip:\n", "numpy tensorflow\n" ] } ], "source": [ "%aimport" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check that we're actually seeing the local version, not a package installed in site-packages.\n", "Th next cell should show the path at which you cloned the lucid repo, not a system path:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lucid was loaded from /Users/ludwigschubert/Code/deepviz/lucid.\n" ] } ], "source": [ "module_path = lucid.__path__[0]\n", "print(\"Lucid was loaded from {}.\".format(module_path))\n", "assert os.path.abspath(\"..\") in module_path\n", "del module_path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example usage" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "image = np.random.normal(loc=.5, scale=.1, size=(200,200))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# this is how you set log levels globally:\n", "\n", "import logging\n", "logging.getLogger('lucid').setLevel(logging.DEBUG)\n", "\n", "# or per module:\n", "# logging.getLogger('lucid.misc.io').setLevel(logging.INFO)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DEBUG:lucid.misc.io.loading:Using inferred loader 'img' due to passed file extension '.png'.\n", "float64\n", "DEBUG:lucid.misc.io.showing:Show is assuming rank 2 or 3 tensor to be an image.\n", "DEBUG:lucid.misc.io.serialize_array:Converting inexact array by scaling by 255.00.\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%autoreload\n", "\n", "from lucid.misc.io import load, save, show\n", "\n", "image = load('../tests/fixtures/noise.png')\n", "\n", "print(image.dtype)\n", "show(image)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(dtype('float64'), (5, 10))\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([[ 70., 180., 110., 104., 66., 147., 100., 130., 112., 116.],\n", " [112., 165., 154., 106., 175., 129., 152., 111., 144., 167.],\n", " [135., 139., 73., 133., 139., 89., 120., 132., 98., 115.],\n", " [106., 142., 79., 93., 132., 161., 175., 137., 139., 118.],\n", " [200., 129., 115., 130., 125., 116., 95., 109., 117., 179.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%autoreload\n", "\n", "array = load(\"../tests/fixtures/noise.png\")\n", "print(array.dtype, array.shape)\n", "show(array, domain=None)\n", "array" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(load(\"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optvis Usage" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from lucid.optvis import objectives, param, transform, render\n", "from lucid.modelzoo.vision_models import InceptionV1" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "model = InceptionV1()\n", "model.load_graphdef()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32 416.72797\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "256 713.85126\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "1024 765.27924\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "_ = render.render_vis(model, \"mixed3b_pre_relu:470\", thresholds=(32, 256, 1024))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.14" } }, "nbformat": 4, "nbformat_minor": 2 }