Tuesday 15 April 2014

python - Fastest way to convert ubyte [0, 255] array to float array [-0.5, +0.5] with NumPy -



python - Fastest way to convert ubyte [0, 255] array to float array [-0.5, +0.5] with NumPy -

the question in title , pretty straightforward.

i have file f reading ubyte array:

arr = numpy.fromfile(f, '>u1', size * rows * cols).reshape((size, rows, cols)) max_value = 0xff # max value of ubyte

currently i'm renormalizing info in 3 passes, follows:

arr = images.astype(float) arr -= max_value / 2.0 arr /= max_value

since array large, takes noticeable fraction of second. great if in 1 or 2 passes through data, think faster.

is there way me perform "composite" vector operation decrease number of passes? or, there other way me speed up?

i did:

ar = ar - 255/2. ar *= 1./255

seems faster :)

no timed it, it's twice fast on system. seems ar = ar - 255/2. subtraction , type conversion on fly. also, seems partition scalar not optimized: it's faster partition 1 time , bunch of multiplications on array. though additional floating point operation may increment round-off error.

as noted in comments, numexpr might fast yet simple way accomplish this. on scheme it's factor 2 quicker, due numexpr using multiple cores , not much fact single pass on array. code:

import numexpr ar = numexpr.evaluate('(ar - 255.0/2.0) / 255.0')

python arrays numpy normalization vectorization

No comments:

Post a Comment