起因:我想将对图片的预处理步骤放到GPU里来做,这样可以一定程度上节省GPU从内存读数据的时间。
但是在查阅了一些资料后发现,截止至2018年10月,依然有人反映 tf.image.resize 对图片下采样的结果会有大约几个像素的位移。(无论align_corners=True 还是False)
对此我自己做了小实验,发现PIL的resize和tf的resize结果不同。(插值方法是BICUBIC)代码如下:
import Tensorflow as tf
from PIL import Image
import numpy as np
def pil_to_np(img_pil):
ar = np.array(img_pil)
return ar.astype(np.float32) / 255.
path = 'zebra_GT.png'
###PIL_resize_1/2###
img_pil = Image.open(path)
resized_pil = img_pil.resize((292,194),Image.BICUBIC)
resized_np = pil_to_np(resized_pil)
###tf_resize_1/2###
img_raw = tf.read_file(path)
img_tensor = tf.image.decode_png(img_raw)
img_tensor_4d = tf.expand_dims(img_tensor,0)
resized_tf = tf.squeeze(tf.image.resize_bicubic(img_tensor_4d,[194,292], align_coners=True))
with tf.Session() as sess:
init_op = tf.global_v