{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Gaussian blur benchmarking\n", "This notebook compares different implementations of the Gaussian blur filter.\n", "\n", "**Note:** benchmarking results vary heavily depending on image size, kernel size, used operations, parameters and used hardware. Use this notebook to adapt it to your use-case scenario and benchmark on your target hardware. If you have different scenarios or use-cases, you are very welcome to submit your notebook as pull-request!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyclesperanto_prototype as cle\n", "from skimage import filters\n", "import cupy\n", "import cupyx.scipy.ndimage as ndi\n", "import time\n", "\n", "# to measure kernel execution duration properly, we need to set this flag. It will slow down exection of workflows a bit though\n", "cle.set_wait_for_kernel_finish(True)\n", "\n", "# selet a GPU with the following in the name. This will fallback to any other GPU if none with this name is found\n", "cle.select_device('RTX')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# test data\n", "import numpy as np\n", "\n", "test_image = np.random.random([100, 512, 512])\n", "\n", "sigma = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## clEsperanto" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pyclesperanto Gaussian duration: 0.22815942764282227\n", "pyclesperanto Gaussian duration: 0.2140495777130127\n", "pyclesperanto Gaussian duration: 0.2140507698059082\n", "pyclesperanto Gaussian duration: 0.21404790878295898\n", "pyclesperanto Gaussian duration: 0.21464228630065918\n", "pyclesperanto Gaussian duration: 0.21403980255126953\n", "pyclesperanto Gaussian duration: 0.2160477638244629\n", "pyclesperanto Gaussian duration: 0.21304845809936523\n", "pyclesperanto Gaussian duration: 0.2190532684326172\n", "pyclesperanto Gaussian duration: 0.21404457092285156\n" ] } ], "source": [ "# convolve with pyclesperanto\n", "result_image = None\n", "\n", "cl_test_image = cle.push_zyx(test_image)\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = cle.gaussian_blur(cl_test_image, result_image, sigma_x=sigma, sigma_y=sigma, sigma_z=sigma)\n", " print(\"pyclesperanto Gaussian duration: \" + str(time.time() - start_time))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## cupy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cupy Gaussian duration: 0.6600561141967773\n", "cupy Gaussian duration: 0.5146195888519287\n", "cupy Gaussian duration: 0.5141170024871826\n", "cupy Gaussian duration: 0.5151159763336182\n", "cupy Gaussian duration: 0.5141191482543945\n", "cupy Gaussian duration: 0.5151159763336182\n", "cupy Gaussian duration: 0.5151159763336182\n", "cupy Gaussian duration: 0.5141153335571289\n", "cupy Gaussian duration: 0.514115571975708\n", "cupy Gaussian duration: 0.513115406036377\n" ] } ], "source": [ "# convolve with cupy\n", "result_image = None\n", "cu_test_image = cupy.asarray(test_image)\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = ndi.gaussian_filter(cu_test_image, output=result_image, sigma=sigma)\n", " cupy.cuda.stream.get_current_stream().synchronize() # we need to wait here to measure time properly\n", " print(\"cupy Gaussian duration: \" + str(time.time() - start_time))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## scikit-image" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "skimage Gaussian duration: 2.6776022911071777\n", "skimage Gaussian duration: 2.6525967121124268\n", "skimage Gaussian duration: 2.6535966396331787\n", "skimage Gaussian duration: 2.653596878051758\n", "skimage Gaussian duration: 2.6505963802337646\n", "skimage Gaussian duration: 2.6475954055786133\n", "skimage Gaussian duration: 2.653596878051758\n", "skimage Gaussian duration: 2.6495962142944336\n", "skimage Gaussian duration: 2.645595073699951\n", "skimage Gaussian duration: 2.511046886444092\n" ] } ], "source": [ "# convolve with scikit-image\n", "result_image = None\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = filters.gaussian(test_image, output=result_image, sigma=sigma)\n", " print(\"skimage Gaussian duration: \" + str(time.time() - start_time)) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }