Last active
February 18, 2018 03:45
-
-
Save huseinzol05/c1ef7ed1c7e3afcfe14bcc361689dfba to your computer and use it in GitHub Desktop.
implement atrous2d using numpy
This file contains 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": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"x = np.full((1,7,7,3), 1,dtype=np.float32)\n", | |
"w = np.full((3,3,3,7), 4,dtype=np.float32)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"rf = w.shape[0]\n", | |
"nf = w.shape[3]\n", | |
"s = 2\n", | |
"rate = 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"h_range = int(np.ceil((x.shape[1] - rate * (rf-1)) / s) + 1)\n", | |
"w_range = int(np.ceil((x.shape[2] - rate * (rf-1)) / s) + 1)\n", | |
"i0 = np.repeat(np.arange(rf), rf)\n", | |
"i0 = np.tile(i0, x.shape[3])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1,\n", | |
" 1, 2, 2, 2])" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"i0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"i1 = s * np.repeat(np.arange(h_range), w_range)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 0, 0, 2, 2, 2, 4, 4, 4])" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"i1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"i=i0.reshape(-1, 1) + i1.reshape(1, -1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n", | |
" [2, 2, 2, 4, 4, 4, 6, 6, 6]])" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1,\n", | |
" 2, 0, 1, 2])" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"j0 = np.tile(np.arange(rf), rf * x.shape[3])\n", | |
"j0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 2, 4, 0, 2, 4, 0, 2, 4])" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"j1 = s * np.tile(np.arange(w_range), h_range)\n", | |
"j1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n", | |
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n", | |
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n", | |
" [2, 4, 6, 2, 4, 6, 2, 4, 6]])" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"j = j0.reshape(-1, 1) + j1.reshape(1, -1)\n", | |
"j" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [0],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [1],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2],\n", | |
" [2]])" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"k = np.repeat(np.arange(x.shape[3]), rf * rf).reshape(-1, 1)\n", | |
"k" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(27, 9)" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x[:,i,j,k].reshape((rf * rf * x.shape[3], -1)).shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 108., 108., 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108., 108., 108.]], dtype=float32)" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"output = w.reshape((nf,-1)).dot(x[:,i,j,k].reshape((rf * rf * x.shape[3], -1)))\n", | |
"output" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[[ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.]],\n", | |
"\n", | |
" [[ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.]],\n", | |
"\n", | |
" [[ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.]]]], dtype=float32)" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"output.reshape((-1, h_range, w_range, nf))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 3, 3, 7)" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"output.reshape((-1, h_range, w_range, nf)).shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"x_tf = tf.constant(1, dtype=tf.float32, shape=(1,7,7,3))\n", | |
"w_tf = tf.constant(4, dtype=tf.float32 , shape=(3,3,3,7))\n", | |
"with tf.Session() as sess:\n", | |
" output_tf = sess.run(tf.nn.atrous_conv2d(x_tf, w_tf, 2, 'VALID'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[[ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.]],\n", | |
"\n", | |
" [[ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.]],\n", | |
"\n", | |
" [[ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.],\n", | |
" [ 108., 108., 108., 108., 108., 108., 108.]]]], dtype=float32)" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"output_tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 3, 3, 7)" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"output_tf.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.0" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.mean(output_tf == output.reshape((-1, h_range, w_range, nf)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment