Home > Software design >  Reading numpy array from binary file as float16 instead of float32 reshapes the input
Reading numpy array from binary file as float16 instead of float32 reshapes the input

Time:05-31

I am trying to read .pfm images of shape 804 x 600 for which I have written a function like this. I know that my images are float16 but they are being read as float32.

def read_pfm(file):
    """Method to decode .pfm files and return data as numpy array"""
    f = open(file, "rb")

    # read information on number of channels and shape
    line1, line2, line3 = (f.readline() for _ in range(3))
    width, height = (int(s) for s in line2.split())

    # read data as big endian float
    data = np.fromfile(f,'>f') # TODO: data is read as float32. Why? Should be float16
    print(data.dtype)
    print(data.shape)
    data = np.reshape(data, shape)
    return data

My questions is two-fold:

  1. Why are my images being read as float32 by default when they are float16?
  2. When I do force the images to be read as float16 in this way

data = np.fromfile(f,'>f2')

the shape of input changes from (482400,) to (964800,). Why does this happen?

Edit: I realized that I made a mistake and the images are actually float32. However the answer by Daweo still clarifies the confusion I had about 16-/32-bit.

CodePudding user response:

When I do force the images to be read as float16(...)the shape of input changes from (482400,) to (964800,). Why does this happen?

Observe that 482400 * 2 == 964800 and 32/16 == 2. Consider simple example, let say you have following 8 bits

01101110

when you would be instructed that 8-bit integers are used you would consider that to be single number (01101110), but when instructed that 4-bit integers are used you would consider that to be 2 numbers (0110, 1110) and when instructed that 2-bit integers are used you would consider that to be 4 numbers (01, 10, 11, 10). Likewise if given sequence of bytes when assumed to be holding float32 does contain N numbers, then same sequence of bytes treated as holding float16 does contain N*(32/16) that is N*2 numbers.

  • Related