{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "import numpy as np\n", "from keras.models import Model\n", "from keras.layers import Input\n", "from keras.layers.convolutional import UpSampling3D\n", "from keras import backend as K" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def format_decimal(arr, places=6):\n", " return [round(x * 10**places) / 10**places for x in arr]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### UpSampling3D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[convolutional.UpSampling3D.0] size 2x2x2 upsampling on 2x2x2x3 input, dim_ordering='tf'**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "in shape: (2, 2, 2, 3)\n", "in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858]\n", "out shape: (4, 4, 4, 3)\n", "out: [-0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, -0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858]\n" ] } ], "source": [ "data_in_shape = (2, 2, 2, 3)\n", "L = UpSampling3D(size=(2, 2, 2), dim_ordering='tf')\n", "\n", "layer_0 = Input(shape=data_in_shape)\n", "layer_1 = L(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\n", "\n", "# set weights to random (use seed for reproducibility)\n", "np.random.seed(260)\n", "data_in = 2 * np.random.random(data_in_shape) - 1\n", "print('')\n", "print('in shape:', data_in_shape)\n", "print('in:', format_decimal(data_in.ravel().tolist()))\n", "result = model.predict(np.array([data_in]))\n", "print('out shape:', result[0].shape)\n", "print('out:', format_decimal(result[0].ravel().tolist()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[convolutional.UpSampling3D.0] size 2x2x2 upsampling on 2x2x2x3 input, dim_ordering='th'**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "in shape: (2, 2, 2, 3)\n", "in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858]\n", "out shape: (2, 4, 4, 6)\n", "out: [-0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, -0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, -0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, -0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858]\n" ] } ], "source": [ "data_in_shape = (2, 2, 2, 3)\n", "L = UpSampling3D(size=(2, 2, 2), dim_ordering='th')\n", "\n", "layer_0 = Input(shape=data_in_shape)\n", "layer_1 = L(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\n", "\n", "# set weights to random (use seed for reproducibility)\n", "np.random.seed(260)\n", "data_in = 2 * np.random.random(data_in_shape) - 1\n", "print('')\n", "print('in shape:', data_in_shape)\n", "print('in:', format_decimal(data_in.ravel().tolist()))\n", "result = model.predict(np.array([data_in]))\n", "print('out shape:', result[0].shape)\n", "print('out:', format_decimal(result[0].ravel().tolist()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[convolutional.UpSampling2D.2] size 1x3x2 upsampling on 2x1x3x2 input, dim_ordering='tf'**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "in shape: (2, 1, 3, 2)\n", "in: [-0.989173, -0.133618, -0.505338, 0.023259, 0.503982, -0.303769, -0.436321, 0.793911, 0.416102, 0.806405, -0.098342, -0.738022]\n", "out shape: (2, 3, 6, 2)\n", "out: [-0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, 0.503982, -0.303769, 0.503982, -0.303769, -0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, 0.503982, -0.303769, 0.503982, -0.303769, -0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, -0.436321, 0.793911, -0.436321, 0.793911, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, -0.436321, 0.793911, -0.436321, 0.793911, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022]\n" ] } ], "source": [ "data_in_shape = (2, 1, 3, 2)\n", "L = UpSampling3D(size=(1, 3, 2), dim_ordering='tf')\n", "\n", "layer_0 = Input(shape=data_in_shape)\n", "layer_1 = L(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\n", "\n", "# set weights to random (use seed for reproducibility)\n", "np.random.seed(252)\n", "data_in = 2 * np.random.random(data_in_shape) - 1\n", "print('')\n", "print('in shape:', data_in_shape)\n", "print('in:', format_decimal(data_in.ravel().tolist()))\n", "result = model.predict(np.array([data_in]))\n", "print('out shape:', result[0].shape)\n", "print('out:', format_decimal(result[0].ravel().tolist()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[convolutional.UpSampling2D.2] size 2x1x2 upsampling on 2x1x3x3 input, dim_ordering='th'**" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "in shape: (2, 1, 3, 3)\n", "in: [-0.47588, 0.366985, 0.040173, 0.015578, -0.906159, 0.241982, -0.771299, -0.443554, -0.56404, -0.17751, 0.541277, -0.233327, 0.024369, 0.858275, 0.496191, 0.980574, -0.59522, 0.480899]\n", "out shape: (2, 2, 3, 6)\n", "out: [-0.47588, -0.47588, 0.366985, 0.366985, 0.040173, 0.040173, 0.015578, 0.015578, -0.906159, -0.906159, 0.241982, 0.241982, -0.771299, -0.771299, -0.443554, -0.443554, -0.56404, -0.56404, -0.47588, -0.47588, 0.366985, 0.366985, 0.040173, 0.040173, 0.015578, 0.015578, -0.906159, -0.906159, 0.241982, 0.241982, -0.771299, -0.771299, -0.443554, -0.443554, -0.56404, -0.56404, -0.17751, -0.17751, 0.541277, 0.541277, -0.233327, -0.233327, 0.024369, 0.024369, 0.858275, 0.858275, 0.496191, 0.496191, 0.980574, 0.980574, -0.59522, -0.59522, 0.480899, 0.480899, -0.17751, -0.17751, 0.541277, 0.541277, -0.233327, -0.233327, 0.024369, 0.024369, 0.858275, 0.858275, 0.496191, 0.496191, 0.980574, 0.980574, -0.59522, -0.59522, 0.480899, 0.480899]\n" ] } ], "source": [ "data_in_shape = (2, 1, 3, 3)\n", "L = UpSampling3D(size=(2, 1, 2), dim_ordering='th')\n", "\n", "layer_0 = Input(shape=data_in_shape)\n", "layer_1 = L(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\n", "\n", "# set weights to random (use seed for reproducibility)\n", "np.random.seed(253)\n", "data_in = 2 * np.random.random(data_in_shape) - 1\n", "print('')\n", "print('in shape:', data_in_shape)\n", "print('in:', format_decimal(data_in.ravel().tolist()))\n", "result = model.predict(np.array([data_in]))\n", "print('out shape:', result[0].shape)\n", "print('out:', format_decimal(result[0].ravel().tolist()))" ] }, { "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.2" } }, "nbformat": 4, "nbformat_minor": 0 }