Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Created June 25, 2020 01:42
Show Gist options
  • Save GenevieveBuckley/977070acc2d169aa356073e103a864cd to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/977070acc2d169aa356073e103a864cd to your computer and use it in GitHub Desktop.
Benchmarking skimage NaN handling
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Benchmarking scikit-image handle nans"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from skimage import io, data, registration\n",
"from skimage.registration import phase_cross_correlation, _masked_phase_cross_correlation\n",
"from skimage.transform import rescale, resize\n",
"from skimage.util import img_as_float"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make some data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 512x512 pixels (A, B)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(512, 512)\n"
]
}
],
"source": [
"shift_a = (20, 35)\n",
"image_A1 = img_as_float(data.camera())\n",
"image_A2 = np.roll(np.roll(img_as_float(data.camera()), shift_a[0], axis=0), shift_a[1], axis=1)\n",
"\n",
"print(image_A1.shape)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(512, 512, 3)\n"
]
}
],
"source": [
"shift_b = (20, 35)\n",
"image_B1 = img_as_float(data.astronaut())\n",
"image_B2 = np.roll(np.roll(img_as_float(data.astronaut()), shift_b[0], axis=0), shift_b[1], axis=1)\n",
"\n",
"print(image_B1.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1000x1000 pixels (C, D)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1000, 1000)\n"
]
}
],
"source": [
"shift_c = (40, 70)\n",
"size_c = (1000, 1000)\n",
"image_C1 = resize(img_as_float(data.camera()), size_c)\n",
"image_C2 = resize(img_as_float(data.camera()), size_c)\n",
"image_C2 = np.roll(np.roll(image_C2, shift_c[0], axis=0), shift_c[1], axis=1)\n",
"\n",
"print(image_C1.shape)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1000, 1000, 3)\n"
]
}
],
"source": [
"shift_d = (40, 70)\n",
"size_d = (1000, 1000)\n",
"image_D1 = resize(img_as_float(data.astronaut()), size_d)\n",
"image_D2 = resize(img_as_float(data.astronaut()), size_d)\n",
"image_D2 = np.roll(np.roll(image_D2, shift_d[0], axis=0), shift_d[1], axis=1)\n",
"\n",
"print(image_D1.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1024x1024 pixels (E, F)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1024, 1024)\n"
]
}
],
"source": [
"shift_e = (40, 70)\n",
"size_e = (1024, 1024)\n",
"image_E1 = resize(img_as_float(data.camera()), size_e)\n",
"image_E2 = resize(img_as_float(data.camera()), size_e)\n",
"image_E2 = np.roll(np.roll(image_E2, shift_e[0], axis=0), shift_e[1], axis=1)\n",
"\n",
"print(image_E1.shape)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1024, 1024, 3)\n"
]
}
],
"source": [
"shift_f = (40, 70)\n",
"size_f = (1024, 1024)\n",
"image_F1 = resize(img_as_float(data.astronaut()), size_f)\n",
"image_F2 = resize(img_as_float(data.astronaut()), size_f)\n",
"image_F2 = np.roll(np.roll(image_F2, shift_f[0], axis=0), shift_f[1], axis=1)\n",
"\n",
"print(image_F1.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2000x2000 pixels (G, H)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2000, 2000)\n"
]
}
],
"source": [
"shift_g = (80, 145)\n",
"size_g = (2000, 2000)\n",
"image_G1 = resize(img_as_float(data.camera()), size_g)\n",
"image_G2 = resize(img_as_float(data.camera()), size_g)\n",
"image_G2 = np.roll(np.roll(image_G2, shift_g[0], axis=0), shift_g[1], axis=1)\n",
"\n",
"print(image_G1.shape)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2000, 2000, 3)\n"
]
}
],
"source": [
"shift_h = (80, 145)\n",
"size_h = (2000, 2000)\n",
"image_H1 = resize(img_as_float(data.astronaut()), size_h)\n",
"image_H2 = resize(img_as_float(data.astronaut()), size_h)\n",
"image_H2 = np.roll(np.roll(image_H2, shift_h[0], axis=0), shift_h[1], axis=1)\n",
"\n",
"print(image_H1.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2048x2048 pixels (I, J)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2000, 2000)\n"
]
}
],
"source": [
"shift_i = (80, 145)\n",
"size_i = (2000, 2000)\n",
"image_I1 = resize(img_as_float(data.camera()), size_i)\n",
"image_I2 = resize(img_as_float(data.camera()), size_i)\n",
"image_I2 = np.roll(np.roll(image_I2, shift_i[0], axis=0), shift_i[1], axis=1)\n",
"\n",
"print(image_I1.shape)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2000, 2000, 3)\n"
]
}
],
"source": [
"shift_j = (80, 145)\n",
"size_j = (2000, 2000)\n",
"image_J1 = resize(img_as_float(data.astronaut()), size_j)\n",
"image_J2 = resize(img_as_float(data.astronaut()), size_j)\n",
"image_J2 = np.roll(np.roll(image_J2, shift_j[0], axis=0), shift_j[1], axis=1)\n",
"\n",
"print(image_J1.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Time correlations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## No NaNs"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"291 ms ± 99.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_A1\n",
"moving_image = image_A2\n",
"expected = shift_a\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(20, 35)\n"
]
},
{
"ename": "NameError",
"evalue": "name 'result' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-16-c473c7728b3d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexpected\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'result' is not defined"
]
}
],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.05 s ± 72.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_B1\n",
"moving_image = image_B2\n",
"expected = shift_b\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(20, 35)\n"
]
},
{
"ename": "NameError",
"evalue": "name 'result' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-18-c473c7728b3d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexpected\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'result' is not defined"
]
}
],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1000x1000 pixels"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"787 ms ± 85.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_C1\n",
"moving_image = image_C2\n",
"expected = shift_c\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.47 s ± 571 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_D1\n",
"moving_image = image_D2\n",
"expected = shift_d\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1024x1024 pixels"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.06 s ± 166 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_E1\n",
"moving_image = image_E2\n",
"expected = shift_e\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.15 s ± 359 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_F1\n",
"moving_image = image_F2\n",
"expected = shift_f\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2000x2000 pixels "
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 s ± 492 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_G1\n",
"moving_image = image_G2\n",
"expected = shift_g\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17.6 s ± 2.49 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_H1\n",
"moving_image = image_H2\n",
"expected = shift_h\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2048x2048 pixels"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.64 s ± 484 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_I1\n",
"moving_image = image_I2\n",
"expected = shift_i\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24.5 s ± 3.39 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"reference_image = image_J1\n",
"moving_image = image_J2\n",
"expected = shift_j\n",
"\n",
"%timeit result = phase_cross_correlation(reference_image, moving_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(expected)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculate benchmarks"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Greyscale: 1.0240549828178693\n",
"RGB: 1.2285714285714286\n"
]
}
],
"source": [
"# scikit-image branch \"master\"\n",
"# 512x512 greyscale - 291 ms ± 99.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 512x512 RGB - 1.05 s ± 72.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"# scikit-image branch \"handle-nans\"\n",
"# 512x512 greyscale - 298 ms ± 27.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 512x512 RGB - 1.29 s ± 198 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"print(\"Greyscale:\", 298 / 291)\n",
"print(\"RGB: \", 1.29 / 1.05)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Breyscale: 1.1994917407878019\n",
"RGB: 1.1454138702460852\n"
]
}
],
"source": [
"# scikit-image branch \"master\"\n",
"# 1000x1000 greyscale - 787 ms ± 85.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 1000x1000 RGB - 4.47 s ± 571 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"# scikit-image branch \"handle-nans\"\n",
"# 1000x1000 greyscale - 944 ms ± 335 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 1000x1000 RGB - 5.12 s ± 472 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"print(\"Breyscale:\", 944 / 787)\n",
"print(\"RGB:\", 5.12 / 4.47)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Breyscale: 1.150943396226415\n",
"RGB: 1.0407766990291263\n"
]
}
],
"source": [
"# scikit-image branch \"master\"\n",
"# 1024x1024 greyscale - 1.06 s ± 166 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 1024x1024 RGB - 5.15 s ± 359 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"# scikit-image branch \"handle-nans\"\n",
"# 1024x1024 greyscale - 1.22 s ± 261 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 1024x1024 RGB - 5.36 s ± 865 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"print(\"Breyscale:\", 1.22 / 1.06)\n",
"print(\"RGB:\", 5.36 / 5.15)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Breyscale: 1.2225\n",
"RGB: 1.0284090909090908\n"
]
}
],
"source": [
"# scikit-image branch \"master\"\n",
"# 2000x2000 greyscale - 4 s ± 492 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 2000x2000 RGB - 17.6 s ± 2.49 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"# scikit-image branch \"handle-nans\"\n",
"# 2000x2000 greyscale - 4.89 s ± 915 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 2000x2000 RGB - 18.1 s ± 2.19 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"print(\"Breyscale:\", 4.89 / 4)\n",
"print(\"RGB:\", 18.1 / 17.6)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Breyscale: 1.2692307692307692\n",
"RGB: 0.8530612244897958\n"
]
}
],
"source": [
"# scikit-image branch \"master\"\n",
"# 2048x2048 greyscale - 3.64 s ± 484 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 2048x2048 RGB - 24.5 s ± 3.39 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"# scikit-image branch \"handle-nans\"\n",
"# 2048x2048 greyscale - 4.62 s ± 657 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"# 2048x2048 RGB - 20.9 s ± 1.5 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"\n",
"print(\"Breyscale:\", 4.62 / 3.64)\n",
"print(\"RGB:\", 20.9 / 24.5)"
]
},
{
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment