问题
TL;DR Question
Regarding numpy arrays that are neighter C or F contiguous (array's c_contiguous and f_contiguous flags are False):
- Can an array really be neither C or F contiguous? Or falsy flags just mean numpy can't figure out the correct contigious type?
- What are the performance implications on such arrays? Are there any optimizations we miss when staying in this state?
An array for example:
import numpy as np
arr = np.random.randint(0, 255, (1000, 1000, 3), dtype='uint8')
arr = arr[:, :, ::-1]
assert arr.flags.c_contiguous is False
assert arr.flags.f_contiguous is False
Background
I am trying to optimize a simple code block which is called many times during a program.
This code block is responsible to load PIL Image, convert it to a numpy array, inverse it's channels and return it.
Something like this:
import numpy as np
from PIL import Image
def load_image(path):
arr = np.asarray(Image.open(path).convert('RGB'))
return arr[:, :, ::-1].copy()
The original copy()
call was there in order to enforce the return value to be C-order array, however, I was wondering if there's a way to achieve the same effect without copying the array every time, as it sounds very expensive.
I tried replacing the copy()
call with np.ascontiguousarray()
, but benchmarks showed they're the same, thus I guess it also performs copying behind the scenes.
I decided to apply this solution in the end:
import numpy as np
from PIL import Image
def load_image(path):
arr = np.ascontiguousarray(Image.open(path).convert('RGB'))
return arr[:, :, ::-1]
Here, I convert the image to a C-ordered array, which could incur a copy behind the scenes, but in fact it doesn't, because in the benchmarks this function is X3-X4 faster than the previous one.
However I want to be sure I do not cancel any future optimizations by returning an array which is neither C or F ordered.
来源:https://stackoverflow.com/questions/62075882/numpy-array-neither-c-or-f-contiguous-implications