Created
July 16, 2014 16:13
-
-
Save patricksnape/7b31c14c7103c2995956 to your computer and use it in GitHub Desktop.
Test Channel Ordering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:77c388ed0a793d1acdf154c79323133d9ec5afcbe50fa114d023cdd29f6d82c6" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"\n", | |
"channels_last = np.random.random((150, 150, 40))\n", | |
"channels_first = np.random.random((40, 150, 150))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def gradient_channels_first(image_data):\n", | |
" g = np.empty((image_data.shape[0] * 2,) + image_data.shape[1:])\n", | |
" for i in xrange(image_data.shape[0]):\n", | |
" gx, gy = np.gradient(image_data[i])\n", | |
" g[2 * i] = gx\n", | |
" g[2 * i + 1] = gy\n", | |
" return g" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def gradient_channels_last(image_data):\n", | |
" g = np.empty(image_data.shape[:2] + (image_data.shape[2] * 2,))\n", | |
" for i in xrange(image_data.shape[2]):\n", | |
" gx, gy = np.gradient(image_data[..., i])\n", | |
" g[..., 2 * i] = gx\n", | |
" g[..., 2 * i + 1] = gy\n", | |
" return g" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit gradient_channels_first(channels_first)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"100 loops, best of 3: 13 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit gradient_channels_last(channels_last)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10 loops, best of 3: 22.7 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment