|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Sebastian Raschka 2017-08-19 \n", |
| 13 | + "\n", |
| 14 | + "numpy 1.12.1\n", |
| 15 | + "scipy 0.19.1\n" |
| 16 | + ] |
| 17 | + } |
| 18 | + ], |
| 19 | + "source": [ |
| 20 | + "%load_ext watermark\n", |
| 21 | + "%watermark -a 'Sebastian Raschka' -d -p numpy,scipy" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "## Convolution in 1D" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 2, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stdout", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "Conv1d Implementation: [ 5. 14. 16. 26. 24. 34. 19. 22.]\n", |
| 41 | + "Numpy Results: [ 5 14 16 26 24 34 19 22]\n" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
| 45 | + "source": [ |
| 46 | + "import numpy as np\n", |
| 47 | + "\n", |
| 48 | + "\n", |
| 49 | + "def conv1d(x, w, p=0, s=1):\n", |
| 50 | + " w_rot = np.array(w[::-1])\n", |
| 51 | + " x_padded = np.array(x)\n", |
| 52 | + " if p > 0:\n", |
| 53 | + " zero_pad = np.zeros(shape=p)\n", |
| 54 | + " x_padded = np.concatenate([zero_pad, x_padded, zero_pad])\n", |
| 55 | + " res = []\n", |
| 56 | + " for i in range(0, int(len(x)/s),s):\n", |
| 57 | + " res.append(np.sum(x_padded[i:i+w_rot.shape[0]] * w_rot))\n", |
| 58 | + " return np.array(res)\n", |
| 59 | + "\n", |
| 60 | + "## Testing:\n", |
| 61 | + "x = [1, 3, 2, 4, 5, 6, 1, 3]\n", |
| 62 | + "w = [1, 0, 3, 1, 2]\n", |
| 63 | + "print('Conv1d Implementation: ', \n", |
| 64 | + " conv1d(x, w, p=2, s=1))\n", |
| 65 | + "print('Numpy Results: ', \n", |
| 66 | + " np.convolve(x, w, mode='same'))\n" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "markdown", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "## Convolution in 2D" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 3, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "name": "stdout", |
| 83 | + "output_type": "stream", |
| 84 | + "text": [ |
| 85 | + "Conv2d Implementation: \n", |
| 86 | + " [[ 11. 25. 32. 13.]\n", |
| 87 | + " [ 19. 25. 24. 13.]\n", |
| 88 | + " [ 13. 28. 25. 17.]\n", |
| 89 | + " [ 11. 17. 14. 9.]]\n", |
| 90 | + "Scipy Results: \n", |
| 91 | + " [[11 25 32 13]\n", |
| 92 | + " [19 25 24 13]\n", |
| 93 | + " [13 28 25 17]\n", |
| 94 | + " [11 17 14 9]]\n" |
| 95 | + ] |
| 96 | + } |
| 97 | + ], |
| 98 | + "source": [ |
| 99 | + "import numpy as np\n", |
| 100 | + "import scipy.signal\n", |
| 101 | + "\n", |
| 102 | + "\n", |
| 103 | + "def conv2d(X, W, p=(0,0), s=(1,1)):\n", |
| 104 | + " W_rot = np.array(W)[::-1,::-1]\n", |
| 105 | + " X_orig = np.array(X)\n", |
| 106 | + " n1 = X_orig.shape[0] + 2*p[0]\n", |
| 107 | + " n2 = X_orig.shape[1] + 2*p[1]\n", |
| 108 | + " X_padded = np.zeros(shape=(n1,n2))\n", |
| 109 | + " X_padded[p[0]:p[0]+X_orig.shape[0], \n", |
| 110 | + " p[1]:p[1]+X_orig.shape[1]] = X_orig\n", |
| 111 | + "\n", |
| 112 | + " res = []\n", |
| 113 | + " for i in range(0, int((X_padded.shape[0] - \\\n", |
| 114 | + " W_rot.shape[0])/s[0])+1, s[0]):\n", |
| 115 | + " res.append([])\n", |
| 116 | + " for j in range(0, int((X_padded.shape[1] - \\\n", |
| 117 | + " W_rot.shape[1])/s[1])+1, s[1]):\n", |
| 118 | + " X_sub = X_padded[i:i+W_rot.shape[0],j:j+W_rot.shape[1]]\n", |
| 119 | + " res[-1].append(np.sum(X_sub * W_rot))\n", |
| 120 | + " return(np.array(res))\n", |
| 121 | + " \n", |
| 122 | + "X = [[1, 3, 2, 4], [5, 6, 1, 3], [1 , 2,0, 2], [3, 4, 3, 2]]\n", |
| 123 | + "W = [[1, 0, 3], [1, 2, 1], [0, 1, 1]]\n", |
| 124 | + "print('Conv2d Implementation: \\n', \n", |
| 125 | + " conv2d(X, W, p=(1,1), s=(1,1)))\n", |
| 126 | + "\n", |
| 127 | + "print('Scipy Results: \\n', \n", |
| 128 | + " scipy.signal.convolve2d(X, W, mode='same'))\n" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": { |
| 134 | + "collapsed": true |
| 135 | + }, |
| 136 | + "source": [ |
| 137 | + "## Loading image with SciPy" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": 4, |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "Image shape: (252, 221, 3)\n", |
| 150 | + "Number of channels: 3\n", |
| 151 | + "Image data type: uint8\n", |
| 152 | + "[[[179 134 110]\n", |
| 153 | + " [182 136 112]]\n", |
| 154 | + "\n", |
| 155 | + " [[180 135 111]\n", |
| 156 | + " [182 137 113]]]\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "import scipy.misc\n", |
| 162 | + "\n", |
| 163 | + "\n", |
| 164 | + "img = scipy.misc.imread('./example-image.png', mode='RGB')\n", |
| 165 | + "\n", |
| 166 | + "print('Image shape: ', img.shape)\n", |
| 167 | + "print('Number of channels: ', img.shape[2])\n", |
| 168 | + "print('Image data type: ', img.dtype)\n", |
| 169 | + "\n", |
| 170 | + "print(img[100:102,100:102,:])\n" |
| 171 | + ] |
| 172 | + } |
| 173 | + ], |
| 174 | + "metadata": { |
| 175 | + "kernelspec": { |
| 176 | + "display_name": "Python 3", |
| 177 | + "language": "python", |
| 178 | + "name": "python3" |
| 179 | + }, |
| 180 | + "language_info": { |
| 181 | + "codemirror_mode": { |
| 182 | + "name": "ipython", |
| 183 | + "version": 3 |
| 184 | + }, |
| 185 | + "file_extension": ".py", |
| 186 | + "mimetype": "text/x-python", |
| 187 | + "name": "python", |
| 188 | + "nbconvert_exporter": "python", |
| 189 | + "pygments_lexer": "ipython3", |
| 190 | + "version": "3.6.1" |
| 191 | + } |
| 192 | + }, |
| 193 | + "nbformat": 4, |
| 194 | + "nbformat_minor": 2 |
| 195 | +} |
0 commit comments