Skip to content

Instantly share code, notes, and snippets.

@zaccharieramzi
Last active October 1, 2019 07:43
Show Gist options
  • Save zaccharieramzi/f05930697b40b74b412e14b420316972 to your computer and use it in GitHub Desktop.
Save zaccharieramzi/f05930697b40b74b412e14b420316972 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"batch_size = 1\n",
"x_first = np.random.rand(batch_size, 256, 256)\n",
"x_last = np.random.rand(256, 256, batch_size)\n",
"\n",
"def iterate_first_fft(arr):\n",
" k = np.empty_like(arr, dtype=np.complex64)\n",
" for i, a in enumerate(arr):\n",
" k[i] = np.fft.fft2(a)\n",
" return k\n",
"\n",
"def iterate_last_fft(arr):\n",
" k = np.empty_like(arr, dtype=np.complex64)\n",
" for i in range(arr.shape[-1]):\n",
" k[..., i] = np.fft.fft2(arr[..., i])\n",
" return k\n",
"\n",
"k_it_first = iterate_first_fft(x_first)\n",
"k_np_first = np.fft.fftn(x_first, axes=(1, 2))\n",
"np.testing.assert_allclose(k_it_first.real, k_np_first.real)\n",
"np.testing.assert_allclose(k_it_first.imag, k_np_first.imag)\n",
"\n",
"k_it_last = iterate_last_fft(x_last)\n",
"k_np_last = np.fft.fftn(x_last, axes=(0, 1))\n",
"np.testing.assert_allclose(k_it_last.real, k_np_last.real)\n",
"np.testing.assert_allclose(k_it_last.imag, k_np_last.imag)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.29 ms ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k_it_first = iterate_first_fft(x_first)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.23 ms ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k_np_first = np.fft.fft2(x_first, axes=(1, 2))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.23 ms ± 14.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k_np_first = np.fft.fftn(x_first, axes=(1, 2))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.17 ms ± 25.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k_it_last = iterate_last_fft(x_last)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.24 ms ± 8.86 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k_np_last = np.fft.fft2(x_last, axes=(0, 1))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.25 ms ± 13.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k_np_last = np.fft.fftn(x_last, axes=(0, 1))"
]
}
],
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment