Convert raw to fp16

# cat rccb_to_fp16.py
import numpy as np
import cv2
import scipy.misc


image_dir='./'

img_height = 1208  #604 # Height of the model input images
img_width = 1920   #960 # Width of the model input images
image_file = "./1.raw"
#resize_dimensions = (32,32)
resize_dimensions = (960,604)  #I got stuck here. Should be (width,height)

# read rccb file
image = np.fromfile(image_file, dtype=np.float16)
print("raw image is:\n{}".format(image))
print("raw image size is:\n{}".format(image.size))
print("raw image shape is:\n{}".format(image.shape[0]))

image = np.reshape(image,[3,img_height, img_width]).astype(np.float32)
print("After reshpe to [3,height,width], image is:\n{}".format(image))
print("After reshpe to [3,height,width], image size is:\n{}".format(image.size))

print("To resize we're using OpenCV which requires (H, W, C) order. RCCB images are stored \
 in (C, H, W) order so we need to transpose before and after the resize.\n")

image = np.transpose(image, (1,2,0))
print("To resize, need to transpose image to:\n{}".format(image))
print("image size is:\n{}".format(image.size))

image = cv2.resize(image,resize_dimensions)
print("After resize, image is:\n{}".format(image))
print("image size is:\n{}".format(image.size))
#scipy.misc.imsave("foo1.png", image)

image = np.transpose(image, (2,0,1))
print("After resize, need to transpose back, image is:\n{}".format(image))
print("image size is:\n{}".format(image.size))
print("image shape is:\n{}".format(image.shape))
image = image.astype(np.float32)
print("image = image.astype(np.float32), then image is:\n{}".format(image))
print("image = image.astype(np.float32), then image size is:\n{}".format(image.size))

with open("./0.fp16", 'w') as f:
     f.write(image.tobytes())
     #image.tofile(f)

# check the fp16's dtype
image_test = np.fromfile("./0.fp16")
print("\nThe fp16 format image is:\n{}".format(image_test))
print("The fp16 format image size is:\n{}".format(image_test.size))

image_test = np.fromfile("./0.fp16", dtype=np.float16)
print("\nThe fp16 format image is:\n{}".format(image_test))
print("The fp16 format image size is:\n{}".format(image_test.size))

image_test = np.fromfile("./0.fp16", dtype=np.float32)
print("\nThe fp16 format image is:\n{}".format(image_test))
print("The fp16 format image size is:\n{}".format(image_test.size))

  

posted on 2019-01-30 17:42  cdekelon  阅读(182)  评论(0)    收藏  举报

导航