{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pixel-wise comparison\n", "clEsperanto brings some pixel-by-pixel comparison functions which are also available with numpy. Let's see how numpy performs in comparison with our OpenCL stuff. When doing similar comparisons with ImageJ, we saw more performance benefits when GPU-accelerating 3D operations compared to 2D operations. https://clij.github.io/clij-benchmarking/benchmarking_operations_jmh\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", "import numpy as np\n", "import cupy as cp\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", "test_image1 = np.random.random([100, 512, 512])\n", "test_image2 = np.random.random([100, 512, 512])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## clEsperanto" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "clEsperanto greater_or_equal duration: 0.031006336212158203\n", "clEsperanto greater_or_equal duration: 0.003000974655151367\n", "clEsperanto greater_or_equal duration: 0.0020003318786621094\n", "clEsperanto greater_or_equal duration: 0.003000497817993164\n", "clEsperanto greater_or_equal duration: 0.002000570297241211\n", "clEsperanto greater_or_equal duration: 0.003000497817993164\n", "clEsperanto greater_or_equal duration: 0.0020008087158203125\n", "clEsperanto greater_or_equal duration: 0.003000497817993164\n", "clEsperanto greater_or_equal duration: 0.002000570297241211\n", "clEsperanto greater_or_equal duration: 0.002000570297241211\n" ] } ], "source": [ "# multiply with pyclesperanto\n", "result_image = None\n", "\n", "cl_test_image1 = cle.push_zyx(test_image1)\n", "cl_test_image2 = cle.push_zyx(test_image2)\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = cle.greater_or_equal(cl_test_image1, cl_test_image2, result_image)\n", " print(\"clEsperanto greater_or_equal duration: \" + str(time.time() - start_time))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## cupy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# comparie with numpy\n", "result_image = None\n", "cp_test_image1 = cp.asarray(test_image1)\n", "cp_test_image2 = cp.asarray(test_image2)\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = cp.greater_equal(cp_test_image1, cp_test_image2)\n", " cp.cuda.stream.get_current_stream().synchronize() # we need to wait here to measure time properly\n", " print(\"cupy greater_equal duration: \" + str(time.time() - start_time))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## numpy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# comparie with numpy\n", "result_image = None\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = np.greater_equal(test_image1, test_image2)\n", " print(\"Numpy greater_equal 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 }