Last active
November 5, 2017 17:55
-
-
Save JossWhittle/bd0bea0e788ed78bd32d45731a93e76a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2017-11-03T19:47:32.408773Z", | |
| "start_time": "2017-11-03T19:47:32.391263Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Using GPU(s): ['/gpu:0']\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import os\n", | |
| "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n", | |
| "\n", | |
| "import numpy as np\n", | |
| "import matplotlib\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "%matplotlib inline\n", | |
| "\n", | |
| "import tensorflow as tf\n", | |
| "from tensorflow.python.client import device_lib\n", | |
| "\n", | |
| "print('Using GPU(s):', [x.name for x in device_lib.list_local_devices() if x.device_type == 'GPU'])\n", | |
| "\n", | |
| "from datetime import datetime" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2017-11-03T19:47:24.447371Z", | |
| "start_time": "2017-11-03T19:47:24.396Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Extracting tmp/data/train-images-idx3-ubyte.gz\n", | |
| "Extracting tmp/data/train-labels-idx1-ubyte.gz\n", | |
| "Extracting tmp/data/t10k-images-idx3-ubyte.gz\n", | |
| "Extracting tmp/data/t10k-labels-idx1-ubyte.gz\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from tensorflow.examples.tutorials.mnist import input_data\n", | |
| "mnist = input_data.read_data_sets(\"tmp/data/\")\n", | |
| "\n", | |
| "train_data = mnist.train.images # Returns np.array\n", | |
| "train_labels = np.asarray(mnist.train.labels, dtype=np.int32)\n", | |
| "test_data = mnist.test.images # Returns np.array\n", | |
| "test_labels = np.asarray(mnist.test.labels, dtype=np.int32)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 190, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2017-11-03T19:47:24.777435Z", | |
| "start_time": "2017-11-03T19:47:24.770932Z" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def tf_cmap(name,size=256):\n", | |
| " # Make (size, 3 or 4) colour map from matplotlib colour map\n", | |
| " return tf.constant(plt.cm.get_cmap(name=name)(np.arange(size)), dtype=tf.float32)\n", | |
| "\n", | |
| "def tf_imagesc(tensor, max_outputs=None, vmin=None, vmax=None, cmap=None, scale=1):\n", | |
| " with tf.variable_scope('imagesc'):\n", | |
| " # Assert rank 4 tensor with 1 colour channel\n", | |
| " assert len(tensor.get_shape()) == 4, 'ImageSC : Input tensor must have rank 4 (batch, height, width, channel) where (channel == 1)'\n", | |
| " assert tensor.get_shape()[-1] == 1, 'ImageSC : Input tensor must have rank 4 (batch, height, width, channel) where (channel == 1)'\n", | |
| " \n", | |
| " # Only consider the first max_outputs batch elements\n", | |
| " if max_outputs is not None: tensor = tensor[:max_outputs,:,:,:]\n", | |
| " tensor = tf.cast(tensor, tf.float32)\n", | |
| "\n", | |
| " # Default colour map if none is given\n", | |
| " if cmap is None: cmap = tf_cmap('viridis')\n", | |
| " cmap_size = int(cmap.get_shape()[0])\n", | |
| " \n", | |
| " # Min is either given as constant or calculated across whole tensor\n", | |
| " if vmin is None or vmin is 'batch': tmin = tf.reduce_min(tensor)\n", | |
| " elif vmin is 'image': tmin = tf.reduce_min(tensor, axis=[1,2,3], keep_dims=True)\n", | |
| " else: tmin = tf.constant([vmin], dtype=tf.float32)\n", | |
| " \n", | |
| " # Max is either given as constant or calculated across whole tensor\n", | |
| " if vmax is None or vmax is 'batch': tmax = tf.reduce_max(tensor)\n", | |
| " elif vmax is 'image': tmax = tf.reduce_max(tensor, axis=[1,2,3], keep_dims=True)\n", | |
| " else: tmax = tf.constant([vmax], dtype=tf.float32)\n", | |
| " \n", | |
| " # Normalize and clip values to [0,1]\n", | |
| " tensor = tf.clip_by_value((tensor - tmin) / (tmax - tmin), clip_value_min=0., clip_value_max=1.)\n", | |
| " \n", | |
| " # Apply colour map to each pixel\n", | |
| " tensor = tf.cast(tf.gather_nd(cmap, tf.cast(tensor * (cmap_size-1), tf.int32)) * 255., tf.uint8)\n", | |
| " \n", | |
| " # Duplicate pixels if needed\n", | |
| " if (scale > 1):\n", | |
| " height = int(int(tensor.get_shape()[1]) * scale)\n", | |
| " width = int(int(tensor.get_shape()[2]) * scale)\n", | |
| " tensor = tf.image.resize_nearest_neighbor(tensor, [height, width])\n", | |
| " \n", | |
| " return tensor\n", | |
| "\n", | |
| "def tf_arraysc(tensor, grid, pad=1, fill=255, vmin=None, vmax=None, cmap=None, scale=1):\n", | |
| " with tf.variable_scope('arraysc_image'):\n", | |
| " # Assert rank 4 tensor \n", | |
| " assert len(tensor.get_shape()) == 4, 'ArraySC_Image : Input tensor must have rank 4 (batch, height, width, channels)'\n", | |
| " \n", | |
| " grid_h, grid_w = grid\n", | |
| " \n", | |
| " tensor = tf_imagesc(tensor, max_outputs=grid_h*grid_w, vmin=vmin, vmax=vmax, cmap=cmap, scale=scale)\n", | |
| " [_, image_h, image_w, channels] = tensor.get_shape()\n", | |
| " \n", | |
| " # Make padding tensors\n", | |
| " cpadding = None\n", | |
| " rpadding = None\n", | |
| " ipadding = None\n", | |
| " if (pad > 0):\n", | |
| " cfill = np.full([image_h, pad, channels], fill, dtype=np.uint8) \n", | |
| " rfill = np.full([pad, int(image_w * grid_w) + (pad * (int(grid_w) - 1)), channels], fill, dtype=np.uint8)\n", | |
| " ifill = np.full([image_h, image_w, channels], fill, dtype=np.uint8) \n", | |
| " # Fix alpha channel\n", | |
| " if (channels == 4): \n", | |
| " cfill[:,:,3] = 255 \n", | |
| " rfill[:,:,3] = 255\n", | |
| " ifill[:,:,3] = 255\n", | |
| " \n", | |
| " cpadding = tf.constant(cfill, dtype=tf.uint8) \n", | |
| " rpadding = tf.constant(rfill, dtype=tf.uint8)\n", | |
| " ipadding = tf.constant(ifill, dtype=tf.uint8)\n", | |
| " \n", | |
| " batch = grid_h * grid_w\n", | |
| " # Construct grid out of row concenations of image concatenations\n", | |
| " i = 0\n", | |
| " grid = []\n", | |
| " for y in range(grid_h):\n", | |
| " \n", | |
| " row = []\n", | |
| " for x in range(grid_w):\n", | |
| " if (i < batch): image = tensor[i,:,:,:]\n", | |
| " else: image = ipadding\n", | |
| " \n", | |
| " if (x == 0) or (pad <= 0): row = row + [image]\n", | |
| " else: row = row + [cpadding, image]\n", | |
| " i += 1\n", | |
| " \n", | |
| " trow = tf.concat(row, axis=1)\n", | |
| " if (y == 0) or (pad <= 0): grid = grid + [trow]\n", | |
| " else: grid = grid + [rpadding, trow]\n", | |
| " \n", | |
| " tgrid = tf.expand_dims(tf.concat(grid, axis=0), axis=0)\n", | |
| " return tgrid\n", | |
| " \n", | |
| "def tf_arraysc_dense(tensor, vmag=None, cmap=None, scale=1):\n", | |
| " with tf.variable_scope('arraysc_dense'):\n", | |
| " # Assert rank 2 tensor \n", | |
| " assert len(tensor.get_shape()) == 2, 'ArraySC_Dense : Input tensor must have rank 2 (fin, fout)'\n", | |
| " \n", | |
| " tensor = tf.expand_dims(tf.expand_dims(tf.cast(tensor, tf.float32), axis=-1),axis=0)\n", | |
| "\n", | |
| " # Default colour map if none is given\n", | |
| " if cmap is None: cmap = tf_cmap('bwr')\n", | |
| " cmap_size = int(cmap.get_shape()[0])\n", | |
| " \n", | |
| " # Mag is either given as constant or calculated across whole tensor\n", | |
| " if vmag is None: tmax = tf.maximum(tf.abs(tf.reduce_min(tensor)), tf.abs(tf.reduce_max(tensor)))\n", | |
| " else: tmax = tf.constant([abs(vmag)], dtype=tf.float32) \n", | |
| " tmin = -tmax\n", | |
| " \n", | |
| " # Normalize and clip values to [0,1]\n", | |
| " tensor = tf.clip_by_value((tensor - tmin) / (tmax - tmin), clip_value_min=0., clip_value_max=1.)\n", | |
| " \n", | |
| " # Apply colour map to each pixel\n", | |
| " tensor = tf.cast(tf.gather_nd(cmap, tf.cast(tensor * (cmap_size-1), tf.int32)) * 255., tf.uint8)\n", | |
| " \n", | |
| " # Duplicate pixels if needed\n", | |
| " if (scale > 1):\n", | |
| " height = int(int(tensor.get_shape()[1]) * scale)\n", | |
| " width = int(int(tensor.get_shape()[2]) * scale)\n", | |
| " tensor = tf.image.resize_nearest_neighbor(tensor, [height, width])\n", | |
| " \n", | |
| " return tensor\n", | |
| " \n", | |
| "def tf_arraysc_kernel(tensor, pad=1, fill=255, vmag=None, cmap=None, scale=1):\n", | |
| " with tf.variable_scope('arraysc_kernel'):\n", | |
| " # Assert rank 4 tensor \n", | |
| " assert len(tensor.get_shape()) == 4, 'ArraySC_Kernel : Input tensor must have rank 4 (fin, fout, fheight, fwidth)'\n", | |
| " \n", | |
| " tensor = tf.expand_dims(tf.cast(tensor, tf.float32), axis=-1)\n", | |
| "\n", | |
| " # Default colour map if none is given\n", | |
| " if cmap is None: cmap = tf_cmap('bwr')\n", | |
| " cmap_size = int(cmap.get_shape()[0])\n", | |
| " \n", | |
| " # Mag is either given as constant or calculated across whole tensor\n", | |
| " if vmag is None: tmax = tf.maximum(tf.abs(tf.reduce_min(tensor)), tf.abs(tf.reduce_max(tensor)))\n", | |
| " else: tmax = tf.constant([abs(vmag)], dtype=tf.float32) \n", | |
| " tmin = -tmax\n", | |
| " \n", | |
| " # Normalize and clip values to [0,1]\n", | |
| " tensor = tf.clip_by_value((tensor - tmin) / (tmax - tmin), clip_value_min=0., clip_value_max=1.)\n", | |
| " \n", | |
| " # Apply colour map to each pixel\n", | |
| " tensor = tf.cast(tf.gather_nd(cmap, tf.cast(tensor * (cmap_size-1), tf.int32)) * 255., tf.uint8)\n", | |
| " \n", | |
| " [grid_h, grid_w, kernel_h, kernel_w, channels] = tensor.get_shape()\n", | |
| " \n", | |
| " # Make padding tensors\n", | |
| " cpadding = None\n", | |
| " rpadding = None\n", | |
| " if (pad > 0):\n", | |
| " cfill = np.full([1, int(kernel_h * scale), pad, channels], fill, dtype=np.uint8) \n", | |
| " rfill = np.full([1, pad, int(kernel_w * scale * grid_w) + (pad * (int(grid_w) - 1)), channels], fill, dtype=np.uint8)\n", | |
| " \n", | |
| " # Fix alpha channel\n", | |
| " if (channels == 4): \n", | |
| " cfill[:,:,:,3] = 255 \n", | |
| " rfill[:,:,:,3] = 255\n", | |
| " \n", | |
| " cpadding = tf.constant(cfill, dtype=tf.uint8) \n", | |
| " rpadding = tf.constant(rfill, dtype=tf.uint8)\n", | |
| " \n", | |
| " # Construct grid out of row concenations of kernel concatenations\n", | |
| " grid = []\n", | |
| " for y in range(grid_h):\n", | |
| " \n", | |
| " row = []\n", | |
| " for x in range(grid_w):\n", | |
| " kernel = tf.expand_dims(tensor[y,x,:,:,:], axis=0)\n", | |
| " \n", | |
| " # Duplicate pixels if needed\n", | |
| " if (scale > 1):\n", | |
| " height = int(int(kernel.get_shape()[1]) * scale)\n", | |
| " width = int(int(kernel.get_shape()[2]) * scale)\n", | |
| " kernel = tf.image.resize_nearest_neighbor(kernel, [height, width])\n", | |
| " \n", | |
| " if (x == 0) or (pad <= 0): row = row + [kernel]\n", | |
| " else: row = row + [cpadding, kernel]\n", | |
| " \n", | |
| " trow = tf.concat(row, axis=2)\n", | |
| " if (y == 0) or (pad <= 0): grid = grid + [trow]\n", | |
| " else: grid = grid + [rpadding, trow]\n", | |
| " \n", | |
| " tgrid = tf.concat(grid, axis=1)\n", | |
| " return tgrid" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 193, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2017-11-03T19:47:25.581646Z", | |
| "start_time": "2017-11-03T19:47:25.566635Z" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "tf.reset_default_graph()\n", | |
| "with tf.Session() as sess:\n", | |
| " sess.run(tf.global_variables_initializer())\n", | |
| " \n", | |
| " log_dir = 'tmp/imagesc/' + datetime.now().strftime(\"%Y%m%d-%H%M%S\") + '/'\n", | |
| " writer = tf.summary.FileWriter(log_dir)\n", | |
| " \n", | |
| " max_outputs = 4\n", | |
| " \n", | |
| " ph_image = tf.placeholder(dtype=tf.float32, shape=[None, 28, 28, 1]) / 2 # Divide image by 2 to test vmin and vmax\n", | |
| " ph_dense = tf.placeholder(dtype=tf.float32, shape=[64,64])\n", | |
| " ph_kernel = tf.placeholder(dtype=tf.float32, shape=[10,10,5,5])\n", | |
| " \n", | |
| " summary_op = tf.summary.merge([\n", | |
| " tf.summary.image('default_summary', ph_image, max_outputs=max_outputs),\n", | |
| " \n", | |
| " tf.summary.image('imagesc_summary', \n", | |
| " tf_imagesc(ph_image, max_outputs=max_outputs), \n", | |
| " max_outputs=max_outputs),\n", | |
| " \n", | |
| " tf.summary.image('imagesc_summary_image_norm', \n", | |
| " tf_imagesc(ph_image, max_outputs=max_outputs, vmin='image', vmax='image'), \n", | |
| " max_outputs=max_outputs),\n", | |
| " \n", | |
| " tf.summary.image('arraysc_summary', \n", | |
| " tf_arraysc(ph_image, grid=[16,16]), \n", | |
| " max_outputs=max_outputs),\n", | |
| " \n", | |
| " tf.summary.image('arraysc_summary_image_norm', \n", | |
| " tf_arraysc(ph_image, grid=[16,16], vmin='image', vmax='image'), \n", | |
| " max_outputs=max_outputs),\n", | |
| " \n", | |
| " tf.summary.image('arraysc_dense_summary', \n", | |
| " tf_arraysc_dense(ph_dense, scale=10), \n", | |
| " ),\n", | |
| " \n", | |
| " tf.summary.image('arraysc_kernel_summary', \n", | |
| " tf_arraysc_kernel(ph_kernel, scale=10), \n", | |
| " )\n", | |
| " ])\n", | |
| " \n", | |
| " # Random sampling of images from MNIST\n", | |
| " np_images = np.reshape(train_data[np.random.permutation(train_data.shape[0]),:], [-1, 28, 28, 1])\n", | |
| " \n", | |
| " # Brighten each image by its index in the batch to show batchwise vs imagewise normalization\n", | |
| " for b in range(np_images.shape[0]):\n", | |
| " np_images[b,:,:,:] += b\n", | |
| " \n", | |
| " # Run tell dat, homeboi\n", | |
| " [summary] = sess.run([summary_op], feed_dict={\n", | |
| " ph_image : np_images,\n", | |
| " ph_dense : np.random.normal(size=[64,64]),\n", | |
| " ph_kernel : np.random.normal(size=[10,10,5,5])\n", | |
| " })\n", | |
| " \n", | |
| " writer.add_summary(summary)\n", | |
| " writer.flush()\n", | |
| " writer.close()\n", | |
| " " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "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.5.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment