Does ndarray tobytes() create a copy of raw data?

Yes it makes a copy because the bytes type must have the ownership of its raw data (ie. a copy is mandatory). However, you can make a view of the Numpy array without any copy using:

batch_bytes = batch.reshape(-1).view(np.uint8)

Note that the resulting type if different (a 1D Numpy array).


Yes the ndarray.tobytes() creates a copy of the data and stores it in a different place in your computer's memory. This is also described in the NumPy's documentation https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tobytes.html

You can easily test this out by printing the memory address of your objects.

import numpy as np
import time

batch = np.ones([4, 3, 224, 224], dtype="float32")
s = time.time()
batch_bytes = batch.tobytes()
e = time.time()
print(f"{(e-s)*1e3} ms")

print(f"Batch object address:       {hex(id(batch))}")
print(f"batch_bytes object address: {hex(id(batch_bytes))}")

Gives output of:

Batch object address:       0x7f16beab0990
batch_bytes object address: 0x7f16be491010