Created
September 11, 2019 17:18
-
-
Save jakirkham/2c66b096d472054ccb1c9556bad9601a to your computer and use it in GitHub Desktop.
Attempt at using CuPy + Chainer to mimic SciPy's convolve
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": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import scipy as sp\n", | |
| "import scipy.ndimage as spimg\n", | |
| "import cupy as cp\n", | |
| "import chainer\n", | |
| "from chainer.functions import convolution_nd" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[0.55021381, 0.60364142, 0.41514555, ..., 0.21014848, 0.06411803,\n", | |
| " 0.00318856],\n", | |
| " [0.565308 , 0.70801313, 0.57825412, ..., 0.77520092, 0.23019033,\n", | |
| " 0.94941756],\n", | |
| " [0.94411044, 0.75518582, 0.3173873 , ..., 0.98212996, 0.45765117,\n", | |
| " 0.46621948],\n", | |
| " ...,\n", | |
| " [0.11534148, 0.09391876, 0.88002047, ..., 0.21290276, 0.96188936,\n", | |
| " 0.75469962],\n", | |
| " [0.69817622, 0.34021495, 0.88724349, ..., 0.96765835, 0.90653356,\n", | |
| " 0.56921228],\n", | |
| " [0.57407528, 0.7842093 , 0.08633729, ..., 0.60257705, 0.96884379,\n", | |
| " 0.37288173]])" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "cp.random.seed(42)\n", | |
| "a = cp.random.random((100, 110))\n", | |
| "a" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[0. , 0. , 0. , ..., 0. , 0. ,\n", | |
| " 0. ],\n", | |
| " [0. , 0.55021381, 0.60364142, ..., 0.06411803, 0.00318856,\n", | |
| " 0. ],\n", | |
| " [0. , 0.565308 , 0.70801313, ..., 0.23019033, 0.94941756,\n", | |
| " 0. ],\n", | |
| " ...,\n", | |
| " [0. , 0.69817622, 0.34021495, ..., 0.90653356, 0.56921228,\n", | |
| " 0. ],\n", | |
| " [0. , 0.57407528, 0.7842093 , ..., 0.96884379, 0.37288173,\n", | |
| " 0. ],\n", | |
| " [0. , 0. , 0. , ..., 0. , 0. ,\n", | |
| " 0. ]])" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "ap = cp.pad(a, 1, \"constant\", constant_values=0)\n", | |
| "ap" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "f = cp.ones(a.ndim * (3,))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[2.42717635, 3.42057602, 3.0604331 , ..., 2.49181719, 2.23226387,\n", | |
| " 1.24691447],\n", | |
| " [4.12647261, 5.43725958, 4.61147945, ..., 4.29905919, 4.13826448,\n", | |
| " 2.17078512],\n", | |
| " [4.47953769, 5.9480567 , 5.47944319, ..., 5.0753601 , 5.58448565,\n", | |
| " 3.27832063],\n", | |
| " ...,\n", | |
| " [2.83098768, 4.73980507, 3.66308175, ..., 5.2031273 , 6.30222493,\n", | |
| " 4.35831288],\n", | |
| " [2.605936 , 4.45953725, 3.90981577, ..., 6.08762965, 6.3171985 ,\n", | |
| " 4.53406034],\n", | |
| " [2.39667576, 3.37025653, 2.85051356, ..., 4.85585691, 4.38770676,\n", | |
| " 2.81747136]])" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "r = convolution_nd(ap[None, None], f[None, None]).array[0, 0]\n", | |
| "r" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[2.42717635, 3.42057602, 3.0604331 , ..., 2.49181719, 2.23226387,\n", | |
| " 1.24691447],\n", | |
| " [4.12647261, 5.43725958, 4.61147945, ..., 4.29905919, 4.13826448,\n", | |
| " 2.17078512],\n", | |
| " [4.47953769, 5.9480567 , 5.47944319, ..., 5.0753601 , 5.58448565,\n", | |
| " 3.27832063],\n", | |
| " ...,\n", | |
| " [2.83098768, 4.73980507, 3.66308175, ..., 5.2031273 , 6.30222493,\n", | |
| " 4.35831288],\n", | |
| " [2.605936 , 4.45953725, 3.90981577, ..., 6.08762965, 6.3171985 ,\n", | |
| " 4.53406034],\n", | |
| " [2.39667576, 3.37025653, 2.85051356, ..., 4.85585691, 4.38770676,\n", | |
| " 2.81747136]])" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "e = spimg.convolve(cp.asnumpy(ap), cp.asnumpy(f))[1:-1, 1:-1]\n", | |
| "e" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "np.allclose(cp.asnumpy(r), e)" | |
| ] | |
| }, | |
| { | |
| "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.7.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment