Created
December 15, 2015 15:16
-
-
Save d02k01/183fc63ab4eb42f6e786 to your computer and use it in GitHub Desktop.
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": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import chainer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'1.5.1'" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"chainer.__version__" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# numpy vs. cupy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import cupy as cp" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## dot" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def dot_np(a, b):\n", | |
" return a.dot(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def dot_cp(a, b):\n", | |
" return a.dot(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"n = 1000\n", | |
"m = 10000" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"a_np = np.random.randn(n, m).astype('f')\n", | |
"b_np = np.random.randn(m, n).astype('f')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"a_cp = cp.asarray(a_np)\n", | |
"b_cp = cp.asarray(b_np)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 loops, best of 3: 811 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit dot_np(a_np, b_np)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The slowest run took 2175.14 times longer than the fastest. This could mean that an intermediate result is being cached \n", | |
"1 loops, best of 3: 110 µs per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit dot_cp(a_cp, b_cp)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## norm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def norm_np(data):\n", | |
" return np.linalg.norm(data, axis=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "'module' object is not callable", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-12-8377ddd70259>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mcp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma_cp\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;31mTypeError\u001b[0m: 'module' object is not callable" | |
] | |
} | |
], | |
"source": [ | |
"cp.linalg.norm(a_cp, axis=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def norm_cp(data):\n", | |
" return cp.sqrt(cp.sum(data**2, axis=1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"100 loops, best of 3: 11.8 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit norm_np(a_np)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The slowest run took 1863.49 times longer than the fastest. This could mean that an intermediate result is being cached \n", | |
"1 loops, best of 3: 158 µs per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit norm_cp(a_cp)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Compatible codes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from chainer import cuda" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## dot (ufunc)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def dot(a, b):\n", | |
" return a.dot(b)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Total variation (use `chainer.Variable`)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import chainer.functions as F\n", | |
"\n", | |
"def tv(x_data, beta=2):\n", | |
" xp = cuda.get_array_module(x_data)\n", | |
" n, ch, h, w = x_data.shape\n", | |
" \n", | |
" Wh_data = xp.array([[[[1],[-1]]]], dtype='f')\n", | |
" Ww_data = xp.array([[[[1, -1]]]], dtype='f')\n", | |
"\n", | |
" x = chainer.Variable(x_data.astype('f'))\n", | |
" Wh = chainer.Variable(Wh_data)\n", | |
" Ww = chainer.Variable(Ww_data)\n", | |
"\n", | |
" diffh = F.convolution_2d(F.reshape(x, (3, 1, h, w)), W=Wh)\n", | |
" diffw = F.convolution_2d(F.reshape(x, (3, 1, h, w)), W=Ww)\n", | |
"\n", | |
" tv = (F.sum(diffh**2) + F.sum(diffw**2))**(beta / 2.)\n", | |
" return tv" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## im2patch (not use `chainer.Variable`)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def get_patches_idx(image_size, patch_size, stride):\n", | |
" l = image_size - patch_size\n", | |
" return range(l)[::stride] + [l]\n", | |
"\n", | |
"def im2patch(image, patch_size, stride):\n", | |
" xp = cuda.get_array_module(image)\n", | |
" ch, h, w = image.shape\n", | |
" idx_h = get_patches_idx(h, patch_size, stride)\n", | |
" idx_w = get_patches_idx(w, patch_size, stride)\n", | |
"\n", | |
" patches = xp.zeros((len(idx_h) * len(idx_w), ch, patch_size, patch_size),\n", | |
" dtype=image.dtype)\n", | |
" for ih in xrange(len(idx_h)):\n", | |
" hs = idx_h[ih]\n", | |
" he = hs + patch_size\n", | |
" for iw in xrange(len(idx_w)):\n", | |
" ws = idx_w[iw]\n", | |
" we = ws + patch_size\n", | |
" patches[iw + ih * len(idx_h)] += image[:, hs:he, ws:we]\n", | |
" return patches" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"img_np = np.random.randn(3, 256, 256)\n", | |
"img_cp = cp.asarray(img_np)\n", | |
"patch_size = 8\n", | |
"stride = 4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10 loops, best of 3: 13.6 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit im2patch(img_np, patch_size, stride)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 loops, best of 3: 145 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit im2patch(img_cp, patch_size, stride)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"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.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment