首页 > 解决方案 > 使用 `tf.image.resize_image_with_crop_or_pad` 来调整 numpy 数组的大小

问题描述

我想tf.image.resize_image_with_crop_or_pad在一个 Numpy 形状数组上使用(100,100,2)它来裁剪或填充到目标形状(h,w,2)

但是,当我这样做时:

img = resize_image_with_crop_or_pad(img, target_height, target_width)
img = np.array(img)

img.shape评估为(),这不是我所期望的。如何将此函数的输出转换为形状正确的 numpy 数组?

标签: pythonnumpytensorflow

解决方案


img = resize_image_with_crop_or_pad(img_tensor, target_height, target_width)
with tf.Session as sess:
    img_output = sess.run(img)

Nowimg_output是一个 numpy 数组,但请注意 img 必须是一个tf.Tensor形状[1, height, width, channels],因此您可以事先执行此操作,这表明您的输入图像已经是一个 numpy 数组:

img_input = np.expand_dims(img_input, 0)
img_tensor = tf.convert_to_tensor(img_input)

推荐阅读