Created
August 25, 2019 19:31
-
-
Save alexlib/7c41ddfa1487d907336a5df389cf56cd 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Openpiv_Python_Cython_GPU_demo.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"private_outputs": true, | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python2", | |
"display_name": "Python 2" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"[View in Colaboratory](https://colab.research.google.com/github/OpenPIV/openpiv-python-gpu/blob/master/Openpiv_Python_Cython_GPU_demo.ipynb)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "Gw1l_0dyfncc", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"\n", | |
"!/opt/bin/nvidia-smi\n", | |
"!wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64-deb\n", | |
"!dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64-deb 2> /dev/null\n", | |
"!apt-key add /var/cuda-repo-8-0-local-ga2/7fa2af80.pub\n", | |
"!apt-get update\n", | |
"!apt-get install -qq cuda gcc-5 g++-5 -y\n", | |
"!ln -s /usr/bin/gcc-5 /usr/local/cuda/bin/gcc\n", | |
"!ln -s /usr/bin/g++-5 /usr/local/cuda/bin/g++\n", | |
"!/usr/local/cuda/bin/nvcc --version" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "bztUxphUiWAR", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# put nvcc on PATH for PyCUDA install\n", | |
"import os\n", | |
"work_dir = os.getcwd()\n", | |
"if \"/usr/local/cuda/bin\" not in os.environ['PATH'].split(':'):\n", | |
" os.environ['PATH'] += \":/usr/local/cuda/bin\"\n", | |
" \n", | |
"! echo $PATH\n", | |
"! nvcc --version\n", | |
"\n", | |
"! pwd\n", | |
"! ls" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "Ez_JnGBhQaV2", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# install pycuda using pip\n", | |
"! pip install pycuda" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "kWhscug26_Ju", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# check to make sure PyCUDA installed properly\n", | |
"import pycuda.autoinit" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "nhsxCUgqNZzE", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!pip install cython" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "70rcCaKVv0N-", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!# install scikits-cuda from github as the pip version cant find cusolver\n", | |
"!git clone https://github.com/lebedov/scikit-cuda.git\n" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "0TKcC1MEeZWy", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"#!ls\n", | |
"os.chdir(work_dir)\n", | |
"os.chdir('scikit-cuda')\n", | |
"%run setup.py install\n", | |
"%run setup.py test\n", | |
"\n", | |
"# add scikit-cuda to the PYTHONPATH\n", | |
"import sys\n", | |
"sys.path.append(os.getcwd())\n", | |
"os.chdir(work_dir)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "ixK9-lJbgs6a", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"! pip install progressbar" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "Rpdhrze08fXY", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"#!git clone https://github.com/CameronDallas5000/openpiv-python.git\n", | |
"!git clone https://github.com/OpenPIV/openpiv-python-gpu.git\n", | |
"os.chdir('openpiv-python-gpu')\n", | |
"sys.path.append(os.getcwd())\n", | |
"!python setup.py build_ext --inplace\n", | |
"!python setup.py install" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "NU2fc-TFNmxP", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import openpiv" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "X_RvxJFuOZue", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from openpiv import tools, process, validation, filters, scaling, pyprocess\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"%matplotlib inline\n", | |
"from IPython.display import display" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "LCqvHHd-9vB_", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!wget https://raw.githubusercontent.com/CameronDallas5000/openpiv-python/master/openpiv/examples/tutorial-part1/exp1_001_a.bmp\n", | |
"!wget https://raw.githubusercontent.com/CameronDallas5000/openpiv-python/master/openpiv/examples/tutorial-part1/exp1_001_b.bmp\n", | |
"!ls" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "tyghpYPBTc_q", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# from google.colab import files\n", | |
"# files.upload() # choose exp1_001_b.bmp and _c.bmp" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "IR92ibRrTilS", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"frame_a = tools.imread( 'exp1_001_a.bmp' )\n", | |
"frame_b = tools.imread( 'exp1_001_b.bmp' )" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "582KZU21TwGm", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"fig,ax = plt.subplots(1,2,figsize=(10,8))\n", | |
"ax[0].imshow(frame_a,cmap=plt.cm.gray)\n", | |
"ax[1].imshow(frame_b,cmap=plt.cm.gray)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "rpMmy19nIZPA", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "eYd4k2DfIaBw", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# OpenPIV Python version" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "wOC0zYVHT5Mm", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"winsize = 32 # pixels\n", | |
"searchsize = 64 # pixels, search in image B\n", | |
"overlap = 12 # pixels\n", | |
"dt = 0.02 # sec" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "AgZ_Kp3XUAp1", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"u, v, sig2noise = pyprocess.extended_search_area_piv( frame_a.astype(np.int32), frame_b.astype(np.int32), \n", | |
" window_size=winsize, overlap=overlap, dt=dt, \n", | |
" search_area_size=searchsize, sig2noise_method='peak2peak' )" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "y2J7gaXpUEfy", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"x, y = pyprocess.get_coordinates( image_size=frame_a.shape, window_size=searchsize, overlap=overlap )" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "ykcjY9-TUHpa", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tools.save(x, y, u, v, np.zeros_like(u), 'exp1_001.txt' ) # no masking, all values are valid" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "QiTJUjGlISN3", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# OpenPIV Python-GPU version" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "HN6VXtEPmYnp", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from progressbar import *\n" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "VrXv0SR7l4uD", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import openpiv.gpu_process\n", | |
"reload(openpiv.gpu_process)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "uH-F9Ryphvyp", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# gpu code parametes\n", | |
"min_window_size = 32\n", | |
"overlap_ratio = 0.5\n", | |
"coarse_factor = 1\n", | |
"nb_iter_max = 2" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "Dj-oo1BLUrLA", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# First time is slow as the GPU modules need to compile. Once they are compiled, they stay compiled.\n", | |
"#Every time you run this after the first time it will be fast.\n", | |
"x, y, u, v, mask = openpiv.gpu_process.WiDIM( frame_a.astype(np.int32), frame_b.astype(np.int32), np.ones(frame_a.shape, dtype=np.int32),\n", | |
" min_window_size, \n", | |
" overlap_ratio,\n", | |
" coarse_factor,\n", | |
" dt,\n", | |
" nb_iter_max = nb_iter_max)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "o6931RnJehtP", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tools.save(x, y, u, v, np.zeros_like(u), 'exp1_001_gpu.txt' )" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "aITMf5f2Iozd", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# OpenPIV Python-CPU (Cython) version" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "eqzZ_a6xHQOn", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"x, y, u, v, mask = openpiv.process.WiDIM( frame_a.astype(np.int32), frame_b.astype(np.int32), np.ones(frame_a.shape, dtype=np.int32),\n", | |
" min_window_size, \n", | |
" overlap_ratio,\n", | |
" coarse_factor,\n", | |
" dt,\n", | |
" nb_iter_max = nb_iter_max)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "X57HJpHCHxt-", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tools.save(x, y, u, v, np.zeros_like(u), 'exp1_001_cpu.txt' )" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "KlUXKOsRIF-G", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tools.display_vector_field('exp1_001.txt', scale=5000, width=0.0025)\n", | |
"tools.display_vector_field('exp1_001_cpu.txt', scale=5000, width=0.0025)\n", | |
"tools.display_vector_field('exp1_001_gpu.txt', scale=5000, width=0.0025)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "9iI7CFnkIKPy", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment