简体   繁体   中英

How to save CNN prediction as image with float32 data type?

I am trying to output the CNN prediction result as float32 format. When I am using the following code, the image outputs are scaled between 0-255 with 8 bit format. How can I keep the float32?

pred= model.predict(X_test)

-> dtype('float32')

for i, image in enumerate(pred, 1):
    tf.keras.preprocessing.image.save_img(f'output_test/{i}.tif', image,scale=False)

In order to save the predictions to images with float32 data type, we can use tifffile . The edited code is:

import numpy as np
from tifffile import imsave

pred= model.predict(X_test)

for i, image in enumerate(pred, 1):
    imsave(f'output_test/{i}.tif', image)

Try this:

from keras.preprocessing.image import array_to_img
from PIL import Image

pred = model.predict(X_test)
for i, im in enumerate(pred, 1):
    img = array_to_img(im, scale=False)
    img.save('saved_image_{}.png'.format(i), format="PNG")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM