I have the following dilemma. I am trying to pickle and then unpickle a numpy array that represents an image.
Executing this code:
a1 = np.zeros((1080, 1920, 3), dtype=np.uint8)
print(sys.getsizeof(a1), a1.shape)
a2 = pickle.dumps(a1)
print(sys.getsizeof(a2), type(a2))
a3 = pickle.loads(a2)
print(sys.getsizeof(a3), a3.shape)
Produces this output:
6220928 (1080, 1920, 3)
6220995 <class 'bytes'>
128 (1080, 1920, 3)
Now, a1 is thus around 6 MB, a2 is the pickle representation of a1 and is a bit longer but still roughly the same. And then I try to unpickle a2 and I get... something obviously not right.
a3 looks fine, i can call methods, I can assign values to it's cells etc.
The result is the same if I replace pickle calls with a1.dumps and np.loads since these just call pickle.
So what exactly is the deal with the weird size?
sys.getsizeofis not the right function to use. Try thenbytesproperty of the array. In your casea1.nbytes. See docs.python.org/3/library/…getsizeofis a tricky tool to use correctly. It is better for numpy arrays than lists, but still you can get unexpected values. Here I suspecta3is aviewof something else. For exampleloadsmight have created a 1d array, and then reshaped it.sys.getsizeof(a3.base)might give an expected size.