TensorFlow 中调整图片尺寸 tf.image.resize_images 的基本用法及实例代码

本文详细介绍TensorFlow中图片缩放函数resize_images的使用方法,包括输入参数、返回结果及实际应用示例,展示了如何调整图片尺寸并保持长宽比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、环境

TensorFlow API r1.12

CUDA 9.2 V9.2.148

cudnn64_7.dll

Python 3.6.3

Windows 10

二、官方说明

通过指定的方法 method 来调整输入图像 images 为指定的尺寸 size

https://www.tensorflow.org/api_docs/python/tf/image/resize_images

tf.image.resize_images(
    images,
    size,
    method=ResizeMethod.BILINEAR,
    align_corners=False,
    preserve_aspect_ratio=False
)

输入:

(1)images:输入图片,四维张量 [batch, height, width, channels] 或者三维张量 [height, width, channels]

(2)size:要调整输入图片形状的目标尺寸,一维的整型 int32 型张量:new_height, new_width

(3)method:图片形状调整方法

ResizeMethod.BILINEAR:默认方法,双线性插值

ResizeMethod.NEAREST_NEIGHBOR:最近邻插值

ResizeMethod.BICUBIC:双三次插值

ResizeMethod.AREA:区域插值

(4)align_corners:布尔型参数,默认为False,为True时,输入张量和输出张量的四个角的像素点的中心是对齐的,保留四个角的像素值

(5)preserve_aspect_ratio:布尔型参数,默认为False,设置是否保持输入图片的长、宽比,如果设置为True,输入图像 images 的尺寸将调整为输入 size 的大小,同时保持原始输入图片的长宽比。如果输入 size 的比输入图像 images的尺寸大,将会按照比例放大输入图像 images

 

返回结果:

(1)如果输入图像 images 为4维,则返回一个四维的浮点型张量,形状为 [batch_size, new_height, new_width, channels]

(2)如果输入图像 images 为3维,则返回一个三维的浮点型张量,形状为 [new_height, new_width, channels]

 

 

三、实例

>>> import tensorflow as tf
>>> import matplotlib.pyplot as plt
>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets("MNIST_data/")
>>> print(sample_image.shape)
# (1, 784)
>>> sample_image = sample_image.reshape(28,28)
print(sample_image.shape)
# (28, 28)
>>> plt.imshow(sample_image, cmap='Greys')
# <matplotlib.image.AxesImage object at 0x000001E9B8A52780>
>>> plt.show()
# 通过 matplotlib 现实原始图像,如下图
>>> sample_image = sample_image.reshape(1,28,28,1)
>>> print(sample_image.shape)
# (1, 28, 28, 1)

# 调整输入图片的尺度大小
>>> resize_image = tf.image.resize_images(images=sample_image, size=(56,56))
>>> print(resize_image)
# Tensor("resize_images/ResizeBilinear:0", shape=(1, 56, 56, 1), dtype=float32)

>>> with tf.Session() as sess:
...     filewriter = tf.summary.FileWriter(logdir='./summary/', graph=sess.graph)
...     result = sess.run(resize_image)
...     result = result.reshape(56,56)    # 转换为 matplotlib.pyplot 中图像显示方法 imshow 支持的格式,二维 (columns, rows)
...     plt.imshow(result, cmap='Greys')
...     plt.show()
...
# 2018-12-27 11:03:10.573212: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
# 2018-12-27 11:03:10.576503: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
# 2018-12-27 11:03:10.580325: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0
# 2018-12-27 11:03:10.583672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N
# 2018-12-27 11:03:10.586138: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6459 MB memory) -> physical GPU (device: 0, name: Quadro M4000, pci bus id: 0000:03:00.0, compute capability: 5.2)
# <matplotlib.image.AxesImage object at 0x000001E9C9A9F160>

原始的 MNIST 样本图像的尺度 (28, 28)

按照目标尺寸调整后的 MNIST图像的尺度(56, 56)

 

TensorBord 查看

开启一个新的cmd键入命令:tensorboard --logdir="./summary/"

可以看到 tensorboard 在每次 TensorFlow 源程序运行后会自动重载新生成的事件文件“Overwriting the metagraph with the newest event.”,后面每次只需要刷新浏览器即可以查看最新的事件文件

浏览器中查看:

http://localhost:6006

 

在深度学习项目中,有效地进行数据预处理和异步读取是提升Tensorflow模型训练效率的关键。Tensorflowtf.FIFOQueue是一个先进先出的队列,它能够在训练过程中顺序管理数据输入。要使用tf.FIFOQueue进行数据预处理和异步读取,首先需要定义队列的容量和数据类型,然后创建队列对象,并通过队列运行器(如tf.train.QueueRunner)来管理多个线程,这些线程负责将数据预处理后的结果入队。为了优化IO操作,可以使用tf.train.Coordinator来控制线程的生命周期,确保在所有数据被正确处理后能够优雅地结束所有线程,避免程序提前终止。具体操作如下: 参考资源链接:[提升Tensorflow效率:队列与线程优化、文件读取与图片处理详解](https://wenku.csdn.net/doc/446k5rfxse) 1. 定义队列容量和数据类型,并创建tf.FIFOQueue实例。 2. 定义tf.train.QueueRunner来管理多个线程,这些线程负责将预处理后的数据加入队列。 3. 使用tf.train.Coordinator来管理线程的生命周期,确保线程间的同步和协调。 4. 在训练循环中,使用tf.train.start_queue_runners()启动队列运行器,并通过tf.train.Coordinator.request_stop()和tf.train.Coordinator.join()来确保训练结束后线程能够安全停止。 在处理图片数据时,Tensorflow提供了多种API来加载和预处理图像数据,如tf.image.decode_jpeg(), tf.image.resize_images()等。结合tf.FIFOQueue的异步特性,可以高效地加载和处理图片数据,进而加快模型训练速度。使用tf.train.QueueRunner和tf.train.Coordinator可以有效地管理线程和队列,提升数据加载过程的IO效率。 推荐参考《提升Tensorflow效率:队列与线程优化、文件读取与图片处理详解》来深入了解如何实现这些操作,并学习如何在实际项目中应用这些技术,提升Tensorflow应用的性能。 参考资源链接:[提升Tensorflow效率:队列与线程优化、文件读取与图片处理详解](https://wenku.csdn.net/doc/446k5rfxse)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn-WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值