{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Otsu's threshold in scikit-image and pyclesperanto\n", "This notebook compares different implementations of Otsu's thresholding method. pyclesperanto's `thresholding_otsu` uses scikit-image under the hood. It just determines the histogram differently, on the GPU, and also applies the threshold on the GPU.\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 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])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "skimage thresholding duration: 0.3397221565246582\n", "skimage thresholding duration: 0.3506922721862793\n", "skimage thresholding duration: 0.35404443740844727\n", "skimage thresholding duration: 0.3540225028991699\n", "skimage thresholding duration: 0.3640248775482178\n", "skimage thresholding duration: 0.3759934902191162\n", "skimage thresholding duration: 0.390505313873291\n", "skimage thresholding duration: 0.42533421516418457\n", "skimage thresholding duration: 0.3800032138824463\n", "skimage thresholding duration: 0.3799574375152588\n" ] } ], "source": [ "# thresholding with scikit-image\n", "result_image = None\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " threshold = filters.threshold_otsu(test_image)\n", " result_image = test_image >= threshold\n", " print(\"skimage thresholding duration: \" + str(time.time() - start_time))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pyclesperanto thresholding duration: 0.08271050453186035\n", "pyclesperanto thresholding duration: 0.04976248741149902\n", "pyclesperanto thresholding duration: 0.03821372985839844\n", "pyclesperanto thresholding duration: 0.03829836845397949\n", "pyclesperanto thresholding duration: 0.03852105140686035\n", "pyclesperanto thresholding duration: 0.03886008262634277\n", "pyclesperanto thresholding duration: 0.03933429718017578\n", "pyclesperanto thresholding duration: 0.03995966911315918\n", "pyclesperanto thresholding duration: 0.039511919021606445\n", "pyclesperanto thresholding duration: 0.037897348403930664\n" ] } ], "source": [ "# thresholding with pyclesperanto\n", "result_image = None\n", "\n", "test_image_gpu = cle.push_zyx(test_image)\n", "\n", "for i in range(0, 10):\n", " start_time = time.time()\n", " result_image = cle.threshold_otsu(test_image_gpu, result_image)\n", " print(\"pyclesperanto thresholding duration: \" + str(time.time() - start_time))" ] }, { "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }