Creating tensor directly from bytes?

The tensor constructor doesn’t accept the ‘bytes’ data type, so when I read raw image data from a file, I wind up going through numpy frombuffer just to get it into an acceptable format.

frameBytes = rgbFile.read(frameSize)
frameTensor = torch.tensor(np.frombuffer(frameBytes, dtype=np.uint8), device=dev)

Is there a better way to do this, or should torch.tensor() get modified to accept bytes as a uint8 data source?

If you use torch.from_numpy, this will be without copy and should be good enough. On the C++ side there is torch::from_blob but I it is not yet mapped to a Python frombuffer, this is ongoing work in PR 59077.

Best regards

Thomas